ParticlesSimpleLitForwardPass.hlsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef UNIVERSAL_PARTICLES_FORWARD_SIMPLE_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_PARTICLES_FORWARD_SIMPLE_LIT_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Particles.hlsl"
  5. void InitializeInputData(VaryingsParticle input, half3 normalTS, out InputData inputData)
  6. {
  7. inputData = (InputData)0;
  8. inputData.positionWS = input.positionWS.xyz;
  9. #ifdef _NORMALMAP
  10. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  11. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  12. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  13. #else
  14. half3 viewDirWS = input.viewDirWS;
  15. inputData.normalWS = input.normalWS;
  16. #endif
  17. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  18. #if SHADER_HINT_NICE_QUALITY
  19. viewDirWS = SafeNormalize(viewDirWS);
  20. #endif
  21. inputData.viewDirectionWS = viewDirWS;
  22. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  23. inputData.shadowCoord = input.shadowCoord;
  24. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  25. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  26. #else
  27. inputData.shadowCoord = float4(0, 0, 0, 0);
  28. #endif
  29. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS.xyz, 1.0), input.positionWS.w);
  30. inputData.vertexLighting = half3(0.0h, 0.0h, 0.0h);
  31. inputData.bakedGI = SampleSHPixel(input.vertexSH, inputData.normalWS);
  32. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  33. inputData.shadowMask = half4(1, 1, 1, 1);
  34. #if defined(DEBUG_DISPLAY) && !defined(PARTICLES_EDITOR_META_PASS)
  35. inputData.vertexSH = input.vertexSH;
  36. #endif
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // Vertex and Fragment functions //
  40. ///////////////////////////////////////////////////////////////////////////////
  41. VaryingsParticle ParticlesLitVertex(AttributesParticle input)
  42. {
  43. VaryingsParticle output;
  44. UNITY_SETUP_INSTANCE_ID(input);
  45. UNITY_TRANSFER_INSTANCE_ID(input, output);
  46. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  47. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  48. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  49. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  50. #ifdef _NORMALMAP
  51. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  52. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  53. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  54. #else
  55. output.normalWS = half3(normalInput.normalWS);
  56. output.viewDirWS = viewDirWS;
  57. #endif
  58. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  59. half fogFactor = 0.0;
  60. #if !defined(_FOG_FRAGMENT)
  61. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  62. #endif
  63. output.positionWS.xyz = vertexInput.positionWS.xyz;
  64. output.positionWS.w = fogFactor;
  65. output.clipPos = vertexInput.positionCS;
  66. output.color = GetParticleColor(input.color);
  67. #if defined(_FLIPBOOKBLENDING_ON)
  68. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  69. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
  70. #else
  71. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
  72. #endif
  73. #else
  74. GetParticleTexcoords(output.texcoord, input.texcoords.xy);
  75. #endif
  76. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  77. output.projectedPosition = vertexInput.positionNDC;
  78. #endif
  79. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  80. output.shadowCoord = GetShadowCoord(vertexInput);
  81. #endif
  82. return output;
  83. }
  84. half4 ParticlesLitFragment(VaryingsParticle input) : SV_Target
  85. {
  86. UNITY_SETUP_INSTANCE_ID(input);
  87. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  88. ParticleParams particleParams;
  89. InitParticleParams(input, particleParams);
  90. half3 normalTS = SampleNormalTS(particleParams.uv, particleParams.blendUv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  91. half4 albedo = SampleAlbedo(TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap), particleParams);
  92. half3 diffuse = AlphaModulate(albedo.rgb, albedo.a);
  93. half alpha = albedo.a;
  94. #if defined(_EMISSION)
  95. half3 emission = BlendTexture(TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap), particleParams.uv, particleParams.blendUv) * _EmissionColor.rgb;
  96. #else
  97. half3 emission = half3(0, 0, 0);
  98. #endif
  99. half4 specularGloss = SampleSpecularSmoothness(particleParams.uv, particleParams.blendUv, albedo.a, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap));
  100. #if defined(_DISTORTION_ON)
  101. diffuse = Distortion(half4(diffuse, alpha), normalTS, _DistortionStrengthScaled, _DistortionBlend, particleParams.projectedPosition);
  102. #endif
  103. InputData inputData;
  104. InitializeInputData(input, normalTS, inputData);
  105. half4 color = UniversalFragmentBlinnPhong(inputData, diffuse, specularGloss, specularGloss.a, emission, alpha, normalTS);
  106. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  107. color.a = OutputAlpha(color.a, _Surface);
  108. return color;
  109. }
  110. #endif // UNIVERSAL_PARTICLES_FORWARD_SIMPLE_LIT_PASS_INCLUDED