ParticlesLitGbufferPass.hlsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef UNIVERSAL_PARTICLES_GBUFFER_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_PARTICLES_GBUFFER_LIT_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  5. void InitializeInputData(VaryingsParticle input, half3 normalTS, out InputData inputData)
  6. {
  7. inputData = (InputData)0;
  8. inputData.positionWS = input.positionWS.xyz;
  9. inputData.positionCS = input.clipPos;
  10. #ifdef _NORMALMAP
  11. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  12. inputData.normalWS = TransformTangentToWorld(normalTS,
  13. half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  14. #else
  15. half3 viewDirWS = input.viewDirWS;
  16. inputData.normalWS = input.normalWS;
  17. #endif
  18. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  19. #if SHADER_HINT_NICE_QUALITY
  20. viewDirWS = SafeNormalize(viewDirWS);
  21. #endif
  22. inputData.viewDirectionWS = viewDirWS;
  23. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  24. inputData.shadowCoord = input.shadowCoord;
  25. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  26. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  27. #else
  28. inputData.shadowCoord = float4(0, 0, 0, 0);
  29. #endif
  30. inputData.fogCoord = 0.0; // not used for deferred shading
  31. inputData.vertexLighting = half3(0.0h, 0.0h, 0.0h);
  32. inputData.bakedGI = SampleSHPixel(input.vertexSH, inputData.normalWS);
  33. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  34. inputData.shadowMask = half4(1, 1, 1, 1);
  35. #if defined(DEBUG_DISPLAY) && !defined(PARTICLES_EDITOR_META_PASS)
  36. inputData.vertexSH = input.vertexSH;
  37. #endif
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // Vertex and Fragment functions //
  41. ///////////////////////////////////////////////////////////////////////////////
  42. VaryingsParticle ParticlesGBufferVertex(AttributesParticle input)
  43. {
  44. VaryingsParticle output = (VaryingsParticle)0;
  45. UNITY_SETUP_INSTANCE_ID(input);
  46. UNITY_TRANSFER_INSTANCE_ID(input, output);
  47. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  48. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  49. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  50. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  51. half3 vertexLight = VertexLighting(vertexInput.positionWS, half3(normalInput.normalWS));
  52. #ifdef _NORMALMAP
  53. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  54. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  55. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  56. #else
  57. output.normalWS = half3(normalInput.normalWS);
  58. output.viewDirWS = viewDirWS;
  59. #endif
  60. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  61. output.positionWS.xyz = vertexInput.positionWS;
  62. output.clipPos = vertexInput.positionCS;
  63. output.color = input.color;
  64. #if defined(_FLIPBOOKBLENDING_ON)
  65. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  66. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
  67. #else
  68. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
  69. #endif
  70. #else
  71. GetParticleTexcoords(output.texcoord, input.texcoords.xy);
  72. #endif
  73. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  74. output.shadowCoord = GetShadowCoord(vertexInput);
  75. #endif
  76. return output;
  77. }
  78. FragmentOutput ParticlesGBufferFragment(VaryingsParticle input)
  79. {
  80. UNITY_SETUP_INSTANCE_ID(input);
  81. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  82. float3 blendUv = float3(0, 0, 0);
  83. #if defined(_FLIPBOOKBLENDING_ON)
  84. blendUv = input.texcoord2AndBlend;
  85. #endif
  86. float4 projectedPosition = float4(0,0,0,0);
  87. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  88. projectedPosition = input.projectedPosition;
  89. #endif
  90. SurfaceData surfaceData;
  91. InitializeParticleLitSurfaceData(input.texcoord, blendUv, input.color, projectedPosition, surfaceData);
  92. InputData inputData;
  93. InitializeInputData(input, surfaceData.normalTS, inputData);
  94. SETUP_DEBUG_TEXTURE_DATA(inputData, input.texcoord, _BaseMap);
  95. // Stripped down version of UniversalFragmentPBR().
  96. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  97. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  98. BRDFData brdfData;
  99. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  100. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  101. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  102. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  103. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  104. }
  105. #endif // UNIVERSAL_PARTICLES_GBUFFER_LIT_PASS_INCLUDED