ParticlesLitForwardPass.hlsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef UNIVERSAL_PARTICLES_FORWARD_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_PARTICLES_FORWARD_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 = (VaryingsParticle)0;
  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. half3 vertexLight = VertexLighting(vertexInput.positionWS, half3(normalInput.normalWS));
  51. half fogFactor = 0.0;
  52. #if !defined(_FOG_FRAGMENT)
  53. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  54. #endif
  55. #ifdef _NORMALMAP
  56. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  57. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  58. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  59. #else
  60. output.normalWS = half3(normalInput.normalWS);
  61. output.viewDirWS = viewDirWS;
  62. #endif
  63. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  64. output.positionWS.xyz = vertexInput.positionWS;
  65. output.positionWS.w = fogFactor;
  66. output.clipPos = vertexInput.positionCS;
  67. output.color = GetParticleColor(input.color);
  68. #if defined(_FLIPBOOKBLENDING_ON)
  69. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  70. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
  71. #else
  72. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
  73. #endif
  74. #else
  75. GetParticleTexcoords(output.texcoord, input.texcoords.xy);
  76. #endif
  77. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  78. output.projectedPosition = vertexInput.positionNDC;
  79. #endif
  80. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  81. output.shadowCoord = GetShadowCoord(vertexInput);
  82. #endif
  83. return output;
  84. }
  85. half4 ParticlesLitFragment(VaryingsParticle input) : SV_Target
  86. {
  87. UNITY_SETUP_INSTANCE_ID(input);
  88. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  89. ParticleParams particleParams;
  90. InitParticleParams(input, particleParams);
  91. SurfaceData surfaceData;
  92. InitializeParticleLitSurfaceData(particleParams, surfaceData);
  93. InputData inputData;
  94. InitializeInputData(input, surfaceData.normalTS, inputData);
  95. SETUP_DEBUG_TEXTURE_DATA(inputData, input.texcoord, _BaseMap);
  96. half4 color = UniversalFragmentPBR(inputData, surfaceData);
  97. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  98. color.a = OutputAlpha(color.a, _Surface);
  99. return color;
  100. }
  101. #endif // UNIVERSAL_PARTICLES_FORWARD_LIT_PASS_INCLUDED