Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParticlesLitGbufferPass.hlsl 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. viewDirWS = SafeNormalize(viewDirWS);
  20. inputData.viewDirectionWS = viewDirWS;
  21. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  22. inputData.shadowCoord = input.shadowCoord;
  23. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  24. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  25. #else
  26. inputData.shadowCoord = float4(0, 0, 0, 0);
  27. #endif
  28. inputData.fogCoord = 0.0; // not used for deferred shading
  29. inputData.vertexLighting = half3(0.0h, 0.0h, 0.0h);
  30. #if !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  31. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  32. GetAbsolutePositionWS(inputData.positionWS),
  33. inputData.normalWS,
  34. inputData.viewDirectionWS,
  35. inputData.positionCS.xy,
  36. input.probeOcclusion,
  37. inputData.shadowMask);
  38. #else
  39. inputData.bakedGI = SampleSHPixel(input.vertexSH, inputData.normalWS);
  40. #endif
  41. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  42. inputData.shadowMask = half4(1, 1, 1, 1);
  43. #if defined(DEBUG_DISPLAY) && !defined(PARTICLES_EDITOR_META_PASS)
  44. inputData.vertexSH = input.vertexSH;
  45. #endif
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // Vertex and Fragment functions //
  49. ///////////////////////////////////////////////////////////////////////////////
  50. VaryingsParticle ParticlesGBufferVertex(AttributesParticle input)
  51. {
  52. VaryingsParticle output = (VaryingsParticle)0;
  53. UNITY_SETUP_INSTANCE_ID(input);
  54. UNITY_TRANSFER_INSTANCE_ID(input, output);
  55. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  56. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  57. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  58. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  59. half3 vertexLight = VertexLighting(vertexInput.positionWS, half3(normalInput.normalWS));
  60. #ifdef _NORMALMAP
  61. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  62. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  63. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  64. #else
  65. output.normalWS = half3(normalInput.normalWS);
  66. output.viewDirWS = viewDirWS;
  67. #endif
  68. OUTPUT_SH4(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH, output.probeOcclusion);
  69. output.positionWS.xyz = vertexInput.positionWS;
  70. output.clipPos = vertexInput.positionCS;
  71. output.color = input.color;
  72. #if defined(_FLIPBOOKBLENDING_ON)
  73. #if defined(UNITY_PARTICLE_INSTANCING_ENABLED)
  74. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords.xyxy, 0.0);
  75. #else
  76. GetParticleTexcoords(output.texcoord, output.texcoord2AndBlend, input.texcoords, input.texcoordBlend);
  77. #endif
  78. #else
  79. GetParticleTexcoords(output.texcoord, input.texcoords.xy);
  80. #endif
  81. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  82. output.shadowCoord = GetShadowCoord(vertexInput);
  83. #endif
  84. return output;
  85. }
  86. FragmentOutput ParticlesGBufferFragment(VaryingsParticle input)
  87. {
  88. UNITY_SETUP_INSTANCE_ID(input);
  89. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  90. float3 blendUv = float3(0, 0, 0);
  91. #if defined(_FLIPBOOKBLENDING_ON)
  92. blendUv = input.texcoord2AndBlend;
  93. #endif
  94. float4 projectedPosition = float4(0,0,0,0);
  95. #if defined(_SOFTPARTICLES_ON) || defined(_FADING_ON) || defined(_DISTORTION_ON)
  96. projectedPosition = input.projectedPosition;
  97. #endif
  98. SurfaceData surfaceData;
  99. InitializeParticleLitSurfaceData(input.texcoord, blendUv, input.color, projectedPosition, surfaceData);
  100. InputData inputData;
  101. InitializeInputData(input, surfaceData.normalTS, inputData);
  102. SETUP_DEBUG_TEXTURE_DATA_FOR_TEX(inputData, input.texcoord, _BaseMap);
  103. // Stripped down version of UniversalFragmentPBR().
  104. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  105. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  106. BRDFData brdfData;
  107. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  108. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  109. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  110. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  111. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  112. }
  113. #endif // UNIVERSAL_PARTICLES_GBUFFER_LIT_PASS_INCLUDED