Particles.hlsl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. TEXTURE2D_X(_CameraDepthTexture); SAMPLER(sampler_CameraDepthTexture);
  7. TEXTURE2D_X(_CameraOpaqueTexture); SAMPLER(sampler_CameraOpaqueTexture);
  8. // Pre-multiplied alpha helper
  9. #if defined(_ALPHAPREMULTIPLY_ON)
  10. #define ALBEDO_MUL albedo
  11. #else
  12. #define ALBEDO_MUL albedo.a
  13. #endif
  14. #if defined(_ALPHAPREMULTIPLY_ON)
  15. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) albedo * val
  16. #elif defined(_ALPHAMODULATE_ON)
  17. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) half4(lerp(half3(1.0h, 1.0h, 1.0h), albedo.rgb, albedo.a * val), albedo.a * val)
  18. #else
  19. #define SOFT_PARTICLE_MUL_ALBEDO(albedo, val) albedo * half4(1.0h, 1.0h, 1.0h, val)
  20. #endif
  21. // Color blending fragment function
  22. float4 MixParticleColor(float4 baseColor, float4 particleColor, float4 colorAddSubDiff)
  23. {
  24. #if defined(_COLOROVERLAY_ON) // Overlay blend
  25. float4 output = baseColor;
  26. output.rgb = lerp(1 - 2 * (1 - baseColor.rgb) * (1 - particleColor.rgb), 2 * baseColor.rgb * particleColor.rgb, step(baseColor.rgb, 0.5));
  27. output.a *= particleColor.a;
  28. return output;
  29. #elif defined(_COLORCOLOR_ON) // Color blend
  30. half3 aHSL = RgbToHsv(baseColor.rgb);
  31. half3 bHSL = RgbToHsv(particleColor.rgb);
  32. half3 rHSL = half3(bHSL.x, bHSL.y, aHSL.z);
  33. return half4(HsvToRgb(rHSL), baseColor.a * particleColor.a);
  34. #elif defined(_COLORADDSUBDIFF_ON) // Additive, Subtractive and Difference blends based on 'colorAddSubDiff'
  35. float4 output = baseColor;
  36. output.rgb = baseColor.rgb + particleColor.rgb * colorAddSubDiff.x;
  37. output.rgb = lerp(output.rgb, abs(output.rgb), colorAddSubDiff.y);
  38. output.a *= particleColor.a;
  39. return output;
  40. #else // Default to Multiply blend
  41. return baseColor * particleColor;
  42. #endif
  43. }
  44. // Soft particles - returns alpha value for fading particles based on the depth to the background pixel
  45. float SoftParticles(float near, float far, float4 projection)
  46. {
  47. float fade = 1;
  48. if (near > 0.0 || far > 0.0)
  49. {
  50. float sceneZ = LinearEyeDepth(SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(projection.xy / projection.w)).r, _ZBufferParams);
  51. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  52. fade = saturate (far * ((sceneZ - near) - thisZ));
  53. }
  54. return fade;
  55. }
  56. // Camera fade - returns alpha value for fading particles based on camera distance
  57. half CameraFade(float near, float far, float4 projection)
  58. {
  59. float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
  60. return saturate((thisZ - near) * far);
  61. }
  62. half3 AlphaModulate(half3 albedo, half alpha)
  63. {
  64. #if defined(_ALPHAMODULATE_ON)
  65. return lerp(half3(1.0h, 1.0h, 1.0h), albedo, alpha);
  66. #elif defined(_ALPHAPREMULTIPLY_ON)
  67. return albedo * alpha;
  68. #endif
  69. return albedo;
  70. }
  71. half3 Distortion(float4 baseColor, float3 normal, half strength, half blend, float4 projection)
  72. {
  73. float2 screenUV = (projection.xy / projection.w) + normal.xy * strength * baseColor.a;
  74. screenUV = UnityStereoTransformScreenSpaceTex(screenUV);
  75. float4 Distortion = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenUV);
  76. return lerp(Distortion.rgb, baseColor.rgb, saturate(baseColor.a - blend));
  77. }
  78. // Sample a texture and do blending for texture sheet animation if needed
  79. half4 BlendTexture(TEXTURE2D_PARAM(_Texture, sampler_Texture), float2 uv, float3 blendUv)
  80. {
  81. half4 color = SAMPLE_TEXTURE2D(_Texture, sampler_Texture, uv);
  82. #ifdef _FLIPBOOKBLENDING_ON
  83. half4 color2 = SAMPLE_TEXTURE2D(_Texture, sampler_Texture, blendUv.xy);
  84. color = lerp(color, color2, blendUv.z);
  85. #endif
  86. return color;
  87. }
  88. // Sample a normal map in tangent space
  89. half3 SampleNormalTS(float2 uv, float3 blendUv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = 1.0h)
  90. {
  91. #if defined(_NORMALMAP)
  92. half4 n = BlendTexture(TEXTURE2D_ARGS(bumpMap, sampler_bumpMap), uv, blendUv);
  93. #if BUMP_SCALE_NOT_SUPPORTED
  94. return UnpackNormal(n);
  95. #else
  96. return UnpackNormalScale(n, scale);
  97. #endif
  98. #else
  99. return half3(0.0h, 0.0h, 1.0h);
  100. #endif
  101. }
  102. #endif // UNIVERSAL_PARTICLES_INCLUDED