Particles.hlsl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef UNIVERSAL_PARTICLES_INCLUDED
  2. #define UNIVERSAL_PARTICLES_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  7. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ParticlesInstancing.hlsl"
  9. struct ParticleParams
  10. {
  11. float4 positionWS;
  12. float4 vertexColor;
  13. float4 projectedPosition;
  14. half4 baseColor;
  15. float3 blendUv;
  16. float2 uv;
  17. };
  18. void InitParticleParams(VaryingsParticle input, out ParticleParams output)
  19. {
  20. output = (ParticleParams) 0;
  21. output.uv = input.texcoord;
  22. output.vertexColor = input.color;
  23. #if defined(_FLIPBOOKBLENDING_ON)
  24. output.blendUv = input.texcoord2AndBlend;
  25. #else
  26. output.blendUv = float3(0,0,0);
  27. #endif
  28. #if !defined(PARTICLES_EDITOR_META_PASS)
  29. output.positionWS = input.positionWS;
  30. output.baseColor = _BaseColor;
  31. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  32. output.projectedPosition = input.projectedPosition;
  33. #else
  34. output.projectedPosition = float4(0,0,0,0);
  35. #endif
  36. #endif
  37. }
  38. // Pre-multiplied alpha helper
  39. #if defined(_ALPHAPREMULTIPLY_ON)
  40. #define ALBEDO_MUL albedo
  41. #else
  42. #define ALBEDO_MUL albedo.a
  43. #endif
  44. #if defined(_ALPHAPREMULTIPLY_ON)
  45. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) albedo * val
  46. #elif defined(_ALPHAMODULATE_ON)
  47. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) half4(lerp(half3(1.0h, 1.0h, 1.0h), albedo.rgb, albedo.a * val), albedo.a * val)
  48. #else
  49. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) albedo * half4(1.0h, 1.0h, 1.0h, val)
  50. #endif
  51. // Color blending fragment function
  52. float4 MixParticleColor(float4 baseColor, float4 particleColor, float4 colorAddSubDiff)
  53. {
  54. #if defined(_COLOROVERLAY_ON) // Overlay blend
  55. float4 output = baseColor;
  56. output.rgb = lerp(1 - 2 * (1 - baseColor.rgb) * (1 - particleColor.rgb), 2 * baseColor.rgb * particleColor.rgb, step(baseColor.rgb, 0.5));
  57. output.a *= particleColor.a;
  58. return output;
  59. #elif defined(_COLORCOLOR_ON) // Color blend
  60. half3 aHSL = RgbToHsv(baseColor.rgb);
  61. half3 bHSL = RgbToHsv(particleColor.rgb);
  62. half3 rHSL = half3(bHSL.x, bHSL.y, aHSL.z);
  63. return half4(HsvToRgb(rHSL), baseColor.a * particleColor.a);
  64. #elif defined(_COLORADDSUBDIFF_ON) // Additive, Subtractive and Difference blends based on 'colorAddSubDiff'
  65. float4 output = baseColor;
  66. output.rgb = baseColor.rgb + particleColor.rgb * colorAddSubDiff.x;
  67. output.rgb = lerp(output.rgb, abs(output.rgb), colorAddSubDiff.y);
  68. output.a *= particleColor.a;
  69. return output;
  70. #else // Default to Multiply blend
  71. return baseColor * particleColor;
  72. #endif
  73. }
  74. // Soft particles - returns alpha value for fading particles based on the depth to the background pixel
  75. float SoftParticles(float near, float far, float4 projection)
  76. {
  77. float fade = 1;
  78. if (near > 0.0 || far > 0.0)
  79. {
  80. float sceneZ = LinearEyeDepth(SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(projection.xy / projection.w)).r, _ZBufferParams);
  81. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  82. fade = saturate(far * ((sceneZ - near) - thisZ));
  83. }
  84. return fade;
  85. }
  86. // Soft particles - returns alpha value for fading particles based on the depth to the background pixel
  87. float SoftParticles(float near, float far, ParticleParams params)
  88. {
  89. float fade = 1;
  90. if (near > 0.0 || far > 0.0)
  91. {
  92. float rawDepth = SampleSceneDepth(params.projectedPosition.xy / params.projectedPosition.w);
  93. float sceneZ = LinearEyeDepth(rawDepth, _ZBufferParams);
  94. float thisZ = LinearEyeDepth(params.positionWS.xyz, GetWorldToViewMatrix());
  95. fade = saturate(far * ((sceneZ - near) - thisZ));
  96. }
  97. return fade;
  98. }
  99. // Camera fade - returns alpha value for fading particles based on camera distance
  100. half CameraFade(float near, float far, float4 projection)
  101. {
  102. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  103. return saturate((thisZ - near) * far);
  104. }
  105. half3 AlphaModulate(half3 albedo, half alpha)
  106. {
  107. #if defined(_ALPHAMODULATE_ON)
  108. return lerp(half3(1.0h, 1.0h, 1.0h), albedo, alpha);
  109. #elif defined(_ALPHAPREMULTIPLY_ON)
  110. return albedo * alpha;
  111. #endif
  112. return albedo;
  113. }
  114. half3 Distortion(float4 baseColor, float3 normal, half strength, half blend, float4 projection)
  115. {
  116. float2 screenUV = (projection.xy / projection.w) + normal.xy * strength * baseColor.a;
  117. screenUV = UnityStereoTransformScreenSpaceTex(screenUV);
  118. float4 Distortion = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenUV);
  119. return lerp(Distortion.rgb, baseColor.rgb, saturate(baseColor.a - blend));
  120. }
  121. // Sample a texture and do blending for texture sheet animation if needed
  122. half4 BlendTexture(TEXTURE2D_PARAM(_Texture, sampler_Texture), float2 uv, float3 blendUv)
  123. {
  124. half4 color = SAMPLE_TEXTURE2D(_Texture, sampler_Texture, uv);
  125. #ifdef _FLIPBOOKBLENDING_ON
  126. half4 color2 = SAMPLE_TEXTURE2D(_Texture, sampler_Texture, blendUv.xy);
  127. color = lerp(color, color2, blendUv.z);
  128. #endif
  129. return color;
  130. }
  131. // Sample a normal map in tangent space
  132. half3 SampleNormalTS(float2 uv, float3 blendUv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = 1.0h)
  133. {
  134. #if defined(_NORMALMAP)
  135. half4 n = BlendTexture(TEXTURE2D_ARGS(bumpMap, sampler_bumpMap), uv, blendUv);
  136. #if BUMP_SCALE_NOT_SUPPORTED
  137. return UnpackNormal(n);
  138. #else
  139. return UnpackNormalScale(n, scale);
  140. #endif
  141. #else
  142. return half3(0.0h, 0.0h, 1.0h);
  143. #endif
  144. }
  145. half4 GetParticleColor(half4 color)
  146. {
  147. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  148. #if !defined(UNITY_PARTICLE_INSTANCE_DATA_NO_COLOR)
  149. UNITY_PARTICLE_INSTANCE_DATA data = unity_ParticleInstanceData[unity_InstanceID];
  150. color = lerp(half4(1.0, 1.0, 1.0, 1.0), color, unity_ParticleUseMeshColors);
  151. color *= UnpackFromR8G8B8A8(data.color);
  152. #endif
  153. #endif
  154. return color;
  155. }
  156. void GetParticleTexcoords(out float2 outputTexcoord, out float3 outputTexcoord2AndBlend, in float4 inputTexcoords, in float inputBlend)
  157. {
  158. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  159. if (unity_ParticleUVShiftData.x != 0.0)
  160. {
  161. UNITY_PARTICLE_INSTANCE_DATA data = unity_ParticleInstanceData[unity_InstanceID];
  162. float numTilesX = unity_ParticleUVShiftData.y;
  163. float2 animScale = unity_ParticleUVShiftData.zw;
  164. #ifdef UNITY_PARTICLE_INSTANCE_DATA_NO_ANIM_FRAME
  165. float sheetIndex = 0.0;
  166. #else
  167. float sheetIndex = data.animFrame;
  168. #endif
  169. float index0 = floor(sheetIndex);
  170. float vIdx0 = floor(index0 / numTilesX);
  171. float uIdx0 = floor(index0 - vIdx0 * numTilesX);
  172. float2 offset0 = float2(uIdx0 * animScale.x, (1.0 - animScale.y) - vIdx0 * animScale.y); // Copied from built-in as is and it looks like upside-down flip
  173. outputTexcoord = inputTexcoords.xy * animScale.xy + offset0.xy;
  174. #ifdef _FLIPBOOKBLENDING_ON
  175. float index1 = floor(sheetIndex + 1.0);
  176. float vIdx1 = floor(index1 / numTilesX);
  177. float uIdx1 = floor(index1 - vIdx1 * numTilesX);
  178. float2 offset1 = float2(uIdx1 * animScale.x, (1.0 - animScale.y) - vIdx1 * animScale.y);
  179. outputTexcoord2AndBlend.xy = inputTexcoords.xy * animScale.xy + offset1.xy;
  180. outputTexcoord2AndBlend.z = frac(sheetIndex);
  181. #endif
  182. }
  183. else
  184. #endif
  185. {
  186. outputTexcoord = inputTexcoords.xy;
  187. #ifdef _FLIPBOOKBLENDING_ON
  188. outputTexcoord2AndBlend.xy = inputTexcoords.zw;
  189. outputTexcoord2AndBlend.z = inputBlend;
  190. #endif
  191. }
  192. #ifndef _FLIPBOOKBLENDING_ON
  193. outputTexcoord2AndBlend.xy = inputTexcoords.xy;
  194. outputTexcoord2AndBlend.z = 0.5;
  195. #endif
  196. }
  197. void GetParticleTexcoords(out float2 outputTexcoord, in float2 inputTexcoord)
  198. {
  199. float3 dummyTexcoord2AndBlend = 0.0;
  200. GetParticleTexcoords(outputTexcoord, dummyTexcoord2AndBlend, inputTexcoord.xyxy, 0.0);
  201. }
  202. #endif // UNIVERSAL_PARTICLES_INCLUDED