ParticlesUnlitForwardPass.hlsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef UNIVERSAL_PARTICLES_UNLIT_FORWARD_PASS_INCLUDED
  2. #define UNIVERSAL_PARTICLES_UNLIT_FORWARD_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Unlit.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Particles.hlsl"
  5. void InitializeInputData(VaryingsParticle input, SurfaceData surfaceData, 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(surfaceData.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. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS.xyz, 1.0), input.positionWS.w);
  23. inputData.vertexLighting = 0;
  24. inputData.bakedGI = SampleSHPixel(input.vertexSH, inputData.normalWS);
  25. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  26. inputData.shadowMask = 1;
  27. inputData.shadowCoord = 0;
  28. #if defined(DEBUG_DISPLAY) && !defined(PARTICLES_EDITOR_META_PASS)
  29. inputData.vertexSH = input.vertexSH;
  30. #endif
  31. }
  32. void InitializeSurfaceData(ParticleParams particleParams, out SurfaceData surfaceData)
  33. {
  34. surfaceData = (SurfaceData)0;
  35. half4 albedo = SampleAlbedo(TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap), particleParams);
  36. half3 normalTS = SampleNormalTS(particleParams.uv, particleParams.blendUv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  37. #if defined (_DISTORTION_ON)
  38. albedo.rgb = Distortion(albedo, normalTS, _DistortionStrengthScaled, _DistortionBlend, particleParams.projectedPosition);
  39. #endif
  40. #if defined(_EMISSION)
  41. half3 emission = BlendTexture(TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap), particleParams.uv, particleParams.blendUv).rgb * _EmissionColor.rgb;
  42. #else
  43. const half3 emission = 0;
  44. #endif
  45. surfaceData.albedo = albedo.rgb; // NOTE: Pre-multiplied and modulated in SampleAlbedo().
  46. surfaceData.specular = 0;
  47. surfaceData.normalTS = normalTS;
  48. surfaceData.emission = emission;
  49. surfaceData.metallic = 0;
  50. surfaceData.smoothness = 1;
  51. surfaceData.occlusion = 1;
  52. surfaceData.alpha = albedo.a;
  53. surfaceData.clearCoatMask = 0;
  54. surfaceData.clearCoatSmoothness = 1;
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // Vertex and Fragment functions //
  58. ///////////////////////////////////////////////////////////////////////////////
  59. VaryingsParticle vertParticleUnlit(AttributesParticle input)
  60. {
  61. VaryingsParticle output = (VaryingsParticle)0;
  62. UNITY_SETUP_INSTANCE_ID(input);
  63. UNITY_TRANSFER_INSTANCE_ID(input, output);
  64. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  65. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  66. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  67. half fogFactor = 0.0;
  68. #if !defined(_FOG_FRAGMENT)
  69. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  70. #endif
  71. // position ws is used to compute eye depth in vertFading
  72. output.positionWS.xyz = vertexInput.positionWS;
  73. output.positionWS.w = fogFactor;
  74. output.clipPos = vertexInput.positionCS;
  75. output.color = GetParticleColor(input.color);
  76. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  77. #ifdef _NORMALMAP
  78. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  79. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  80. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  81. #else
  82. output.normalWS = half3(normalInput.normalWS);
  83. output.viewDirWS = viewDirWS;
  84. #endif
  85. #if defined(_FLIPBOOKBLENDING_ON)
  86. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  87. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
  88. #else
  89. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
  90. #endif
  91. #else
  92. GetParticleTexcoords(output.texcoord, input.texcoords.xy);
  93. #endif
  94. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  95. output.projectedPosition = vertexInput.positionNDC;
  96. #endif
  97. return output;
  98. }
  99. half4 fragParticleUnlit(VaryingsParticle input) : SV_Target
  100. {
  101. UNITY_SETUP_INSTANCE_ID(input);
  102. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  103. ParticleParams particleParams;
  104. InitParticleParams(input, particleParams);
  105. SurfaceData surfaceData;
  106. InitializeSurfaceData(particleParams, surfaceData);
  107. InputData inputData;
  108. InitializeInputData(input, surfaceData, inputData);
  109. SETUP_DEBUG_TEXTURE_DATA(inputData, input.texcoord, _BaseMap);
  110. half4 finalColor = UniversalFragmentUnlit(inputData, surfaceData);
  111. #if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
  112. float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  113. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
  114. finalColor.rgb *= aoFactor.directAmbientOcclusion;
  115. #endif
  116. finalColor.rgb = MixFog(finalColor.rgb, inputData.fogCoord);
  117. finalColor.a = OutputAlpha(finalColor.a, _Surface);
  118. return finalColor;
  119. }
  120. #endif // UNIVERSAL_PARTICLES_UNLIT_FORWARD_PASS_INCLUDED