ParticlesLitForwardPass.hlsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct AttributesParticle
  5. {
  6. float4 vertex : POSITION;
  7. float3 normal : NORMAL;
  8. half4 color : COLOR;
  9. #if defined(_FLIPBOOKBLENDING_ON) && !defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  10. float4 texcoords : TEXCOORD0;
  11. float texcoordBlend : TEXCOORD1;
  12. #else
  13. float2 texcoords : TEXCOORD0;
  14. #endif
  15. float4 tangent : TANGENT;
  16. UNITY_VERTEX_INPUT_INSTANCE_ID
  17. };
  18. struct VaryingsParticle
  19. {
  20. half4 color : COLOR;
  21. float2 texcoord : TEXCOORD0;
  22. float4 positionWS : TEXCOORD1;
  23. #ifdef _NORMALMAP
  24. float4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  25. float4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  26. float4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  27. #else
  28. float3 normalWS : TEXCOORD2;
  29. float3 viewDirWS : TEXCOORD3;
  30. #endif
  31. #if defined(_FLIPBOOKBLENDING_ON)
  32. float3 texcoord2AndBlend : TEXCOORD5;
  33. #endif
  34. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  35. float4 projectedPosition : TEXCOORD6;
  36. #endif
  37. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  38. float4 shadowCoord : TEXCOORD7;
  39. #endif
  40. float3 vertexSH : TEXCOORD8; // SH
  41. float4 clipPos : SV_POSITION;
  42. UNITY_VERTEX_INPUT_INSTANCE_ID
  43. UNITY_VERTEX_OUTPUT_STEREO
  44. };
  45. void InitializeInputData(VaryingsParticle input, half3 normalTS, out InputData output)
  46. {
  47. output = (InputData)0;
  48. output.positionWS = input.positionWS.xyz;
  49. #ifdef _NORMALMAP
  50. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  51. output.normalWS = TransformTangentToWorld(normalTS,
  52. half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  53. #else
  54. half3 viewDirWS = input.viewDirWS;
  55. output.normalWS = input.normalWS;
  56. #endif
  57. output.normalWS = NormalizeNormalPerPixel(output.normalWS);
  58. #if SHADER_HINT_NICE_QUALITY
  59. viewDirWS = SafeNormalize(viewDirWS);
  60. #endif
  61. output.viewDirectionWS = viewDirWS;
  62. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  63. output.shadowCoord = input.shadowCoord;
  64. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  65. output.shadowCoord = TransformWorldToShadowCoord(output.positionWS);
  66. #else
  67. output.shadowCoord = float4(0, 0, 0, 0);
  68. #endif
  69. output.fogCoord = (half)input.positionWS.w;
  70. output.vertexLighting = half3(0.0h, 0.0h, 0.0h);
  71. output.bakedGI = SampleSHPixel(input.vertexSH, output.normalWS);
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Vertex and Fragment functions //
  75. ///////////////////////////////////////////////////////////////////////////////
  76. VaryingsParticle ParticlesLitVertex(AttributesParticle input)
  77. {
  78. VaryingsParticle output = (VaryingsParticle)0;
  79. UNITY_SETUP_INSTANCE_ID(input);
  80. UNITY_TRANSFER_INSTANCE_ID(input, output);
  81. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  82. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.vertex.xyz);
  83. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, input.tangent);
  84. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  85. #if !SHADER_HINT_NICE_QUALITY
  86. viewDirWS = SafeNormalize(viewDirWS);
  87. #endif
  88. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  89. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  90. #ifdef _NORMALMAP
  91. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  92. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  93. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  94. #else
  95. output.normalWS = normalInput.normalWS;
  96. output.viewDirWS = viewDirWS;
  97. #endif
  98. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  99. output.positionWS.xyz = vertexInput.positionWS;
  100. output.positionWS.w = fogFactor;
  101. output.clipPos = vertexInput.positionCS;
  102. output.color = input.color;
  103. output.texcoord = input.texcoords.xy;
  104. #ifdef _FLIPBOOKBLENDING_ON
  105. output.texcoord2AndBlend.xy = input.texcoords.zw;
  106. output.texcoord2AndBlend.z = input.texcoordBlend;
  107. #endif
  108. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  109. output.projectedPosition = vertexInput.positionNDC;
  110. #endif
  111. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  112. output.shadowCoord = GetShadowCoord(vertexInput);
  113. #endif
  114. return output;
  115. }
  116. half4 ParticlesLitFragment(VaryingsParticle input) : SV_Target
  117. {
  118. UNITY_SETUP_INSTANCE_ID(input);
  119. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  120. float3 blendUv = float3(0, 0, 0);
  121. #if defined(_FLIPBOOKBLENDING_ON)
  122. blendUv = input.texcoord2AndBlend;
  123. #endif
  124. float4 projectedPosition = float4(0,0,0,0);
  125. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  126. projectedPosition = input.projectedPosition;
  127. #endif
  128. SurfaceData surfaceData;
  129. InitializeParticleLitSurfaceData(input.texcoord, blendUv, input.color, projectedPosition, surfaceData);
  130. InputData inputData = (InputData)0;
  131. InitializeInputData(input, surfaceData.normalTS, inputData);
  132. half4 color = UniversalFragmentPBR(inputData, surfaceData.albedo,
  133. surfaceData.metallic, half3(0, 0, 0), surfaceData.smoothness, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha);
  134. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  135. return color;
  136. }
  137. #endif // UNIVERSAL_PARTICLES_FORWARD_LIT_PASS_INCLUDED