Particles.hlsl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.0, 1.0, 1.0), albedo.rgb, albedo.a * val), albedo.a * val)
  48. #else
  49. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) albedo * half4(1.0, 1.0, 1.0, val)
  50. #endif
  51. // Color blending fragment function
  52. half4 MixParticleColor(half4 baseColor, half4 particleColor, half4 colorAddSubDiff)
  53. {
  54. #if defined(_COLOROVERLAY_ON) // Overlay blend
  55. half4 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. half4 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 rawDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(projection.xy / projection.w)).r;
  81. float sceneZ = (unity_OrthoParams.w == 0) ? LinearEyeDepth(rawDepth, _ZBufferParams) : LinearDepthToEyeDepth(rawDepth);
  82. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  83. fade = saturate(far * ((sceneZ - near) - thisZ));
  84. }
  85. return fade;
  86. }
  87. // Soft particles - returns alpha value for fading particles based on the depth to the background pixel
  88. float SoftParticles(float near, float far, ParticleParams params)
  89. {
  90. float fade = 1;
  91. if (near > 0.0 || far > 0.0)
  92. {
  93. float rawDepth = SampleSceneDepth(params.projectedPosition.xy / params.projectedPosition.w);
  94. float sceneZ = (unity_OrthoParams.w == 0) ? LinearEyeDepth(rawDepth, _ZBufferParams) : LinearDepthToEyeDepth(rawDepth);
  95. float thisZ = LinearEyeDepth(params.positionWS.xyz, GetWorldToViewMatrix());
  96. fade = saturate(far * ((sceneZ - near) - thisZ));
  97. }
  98. return fade;
  99. }
  100. // Camera fade - returns alpha value for fading particles based on camera distance
  101. half CameraFade(float near, float far, float4 projection)
  102. {
  103. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  104. return half(saturate((thisZ - near) * far));
  105. }
  106. half3 AlphaModulate(half3 albedo, half alpha)
  107. {
  108. #if defined(_ALPHAMODULATE_ON)
  109. return lerp(half3(1.0h, 1.0h, 1.0h), albedo, alpha);
  110. #elif defined(_ALPHAPREMULTIPLY_ON)
  111. return albedo * alpha;
  112. #endif
  113. return albedo;
  114. }
  115. half3 Distortion(float4 baseColor, float3 normal, half strength, half blend, float4 projection)
  116. {
  117. float2 screenUV = (projection.xy / projection.w) + normal.xy * strength * baseColor.a;
  118. screenUV = UnityStereoTransformScreenSpaceTex(screenUV);
  119. float4 Distortion = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenUV);
  120. return half3(lerp(Distortion.rgb, baseColor.rgb, saturate(baseColor.a - blend)));
  121. }
  122. // Sample a texture and do blending for texture sheet animation if needed
  123. half4 BlendTexture(TEXTURE2D_PARAM(_Texture, sampler_Texture), float2 uv, float3 blendUv)
  124. {
  125. half4 color = half4(SAMPLE_TEXTURE2D(_Texture, sampler_Texture, uv));
  126. #ifdef _FLIPBOOKBLENDING_ON
  127. half4 color2 = half4(SAMPLE_TEXTURE2D(_Texture, sampler_Texture, blendUv.xy));
  128. color = lerp(color, color2, half(blendUv.z));
  129. #endif
  130. return color;
  131. }
  132. // Sample a normal map in tangent space
  133. half3 SampleNormalTS(float2 uv, float3 blendUv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = half(1.0))
  134. {
  135. #if defined(_NORMALMAP)
  136. half4 n = BlendTexture(TEXTURE2D_ARGS(bumpMap, sampler_bumpMap), uv, blendUv);
  137. #if BUMP_SCALE_NOT_SUPPORTED
  138. return UnpackNormal(n);
  139. #else
  140. return UnpackNormalScale(n, scale);
  141. #endif
  142. #else
  143. return half3(0.0, 0.0, 1.0);
  144. #endif
  145. }
  146. half4 GetParticleColor(half4 color)
  147. {
  148. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  149. #if !defined(UNITY_PARTICLE_INSTANCE_DATA_NO_COLOR)
  150. UNITY_PARTICLE_INSTANCE_DATA data = unity_ParticleInstanceData[unity_InstanceID];
  151. color = lerp(half4(1.0, 1.0, 1.0, 1.0), color, unity_ParticleUseMeshColors);
  152. color *= half4(UnpackFromR8G8B8A8(data.color));
  153. #endif
  154. #endif
  155. return color;
  156. }
  157. void GetParticleTexcoords(out float2 outputTexcoord, out float3 outputTexcoord2AndBlend, in float4 inputTexcoords, in float inputBlend)
  158. {
  159. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  160. if (unity_ParticleUVShiftData.x != 0.0)
  161. {
  162. UNITY_PARTICLE_INSTANCE_DATA data = unity_ParticleInstanceData[unity_InstanceID];
  163. float numTilesX = unity_ParticleUVShiftData.y;
  164. float2 animScale = unity_ParticleUVShiftData.zw;
  165. #ifdef UNITY_PARTICLE_INSTANCE_DATA_NO_ANIM_FRAME
  166. float sheetIndex = 0.0;
  167. #else
  168. float sheetIndex = data.animFrame;
  169. #endif
  170. float index0 = floor(sheetIndex);
  171. float vIdx0 = floor(index0 / numTilesX);
  172. float uIdx0 = floor(index0 - vIdx0 * numTilesX);
  173. 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
  174. outputTexcoord = inputTexcoords.xy * animScale.xy + offset0.xy;
  175. #ifdef _FLIPBOOKBLENDING_ON
  176. float index1 = floor(sheetIndex + 1.0);
  177. float vIdx1 = floor(index1 / numTilesX);
  178. float uIdx1 = floor(index1 - vIdx1 * numTilesX);
  179. float2 offset1 = float2(uIdx1 * animScale.x, (1.0 - animScale.y) - vIdx1 * animScale.y);
  180. outputTexcoord2AndBlend.xy = inputTexcoords.xy * animScale.xy + offset1.xy;
  181. outputTexcoord2AndBlend.z = frac(sheetIndex);
  182. #endif
  183. }
  184. else
  185. #endif
  186. {
  187. outputTexcoord = inputTexcoords.xy;
  188. #ifdef _FLIPBOOKBLENDING_ON
  189. outputTexcoord2AndBlend.xy = inputTexcoords.zw;
  190. outputTexcoord2AndBlend.z = inputBlend;
  191. #endif
  192. }
  193. #ifndef _FLIPBOOKBLENDING_ON
  194. outputTexcoord2AndBlend.xy = inputTexcoords.xy;
  195. outputTexcoord2AndBlend.z = 0.5;
  196. #endif
  197. }
  198. void GetParticleTexcoords(out float2 outputTexcoord, in float2 inputTexcoord)
  199. {
  200. float3 dummyTexcoord2AndBlend = 0.0;
  201. GetParticleTexcoords(outputTexcoord, dummyTexcoord2AndBlend, inputTexcoord.xyxy, 0.0);
  202. }
  203. #endif // UNIVERSAL_PARTICLES_INCLUDED