CTI URP Lighting.hlsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef URP_TRANSLUCENTLIGHTING_INCLUDED
  2. #define URP_TRANSLUCENTLIGHTING_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. half3 CTI_GlobalIllumination(BRDFData brdfData, half3 bakedGI, half occlusion, half3 normalWS, half3 viewDirectionWS,
  5. half specOccluison)
  6. {
  7. half3 reflectVector = reflect(-viewDirectionWS, normalWS);
  8. half NoV = saturate(dot(normalWS, viewDirectionWS));
  9. half fresnelTerm = Pow4(1.0 - NoV);
  10. half3 indirectDiffuse = bakedGI * occlusion;
  11. half3 indirectSpecular = GlossyEnvironmentReflection(reflectVector, brdfData.perceptualRoughness, occlusion) * specOccluison;
  12. return EnvironmentBRDF(brdfData, indirectDiffuse, indirectSpecular, fresnelTerm);
  13. }
  14. half3 LightingPhysicallyBasedWrapped(BRDFData brdfData, half3 lightColor, half3 lightDirectionWS, half lightAttenuation, half3 normalWS, half3 viewDirectionWS, half NdotL)
  15. {
  16. //NdotL is wrapped... not correct for specular
  17. half3 radiance = lightColor * (lightAttenuation * NdotL);
  18. return DirectBDRF(brdfData, normalWS, lightDirectionWS, viewDirectionWS) * radiance;
  19. }
  20. half3 LightingPhysicallyBasedWrapped(BRDFData brdfData, Light light, half3 normalWS, half3 viewDirectionWS, half NdotL)
  21. {
  22. return LightingPhysicallyBasedWrapped(brdfData, light.color, light.direction, light.distanceAttenuation * light.shadowAttenuation, normalWS, viewDirectionWS, NdotL);
  23. }
  24. half4 CTIURPFragmentPBR(InputData inputData, SurfaceData surfaceData,
  25. half4 translucency, half AmbientReflection, half Wrap)
  26. {
  27. BRDFData brdfData;
  28. InitializeBRDFData(surfaceData, brdfData);
  29. // Debugging
  30. #if defined(DEBUG_DISPLAY)
  31. half4 debugColor;
  32. if (CanDebugOverrideOutputColor(inputData, surfaceData, brdfData, debugColor))
  33. {
  34. return debugColor;
  35. }
  36. #endif
  37. half4 shadowMask = CalculateShadowMask(inputData);
  38. AmbientOcclusionFactor aoFactor = CreateAmbientOcclusionFactor(inputData, surfaceData);
  39. uint meshRenderingLayers = GetMeshRenderingLightLayer();
  40. Light mainLight = GetMainLight(inputData, shadowMask, aoFactor);
  41. half3 mainLightColor = mainLight.color;
  42. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI);
  43. LightingData lightingData = CreateLightingData(inputData, surfaceData);
  44. //half3 color = CTI_GlobalIllumination(brdfData, inputData.bakedGI, occlusion, inputData.normalWS, inputData.viewDirectionWS, AmbientReflection);
  45. // In order to use probe blending and proper AO we have to use the new GlobalIllumination function
  46. lightingData.giColor = GlobalIllumination(
  47. brdfData,
  48. brdfData, // brdfDataClearCoat,
  49. 0, // surfaceData.clearCoatMask
  50. inputData.bakedGI,
  51. aoFactor.indirectAmbientOcclusion,
  52. inputData.positionWS,
  53. inputData.normalWS,
  54. inputData.viewDirectionWS
  55. );
  56. half w = Wrap;
  57. // TODO: Move to inspector
  58. half WrappedNormalization = rcp((1.0h + w) * (1.0h + w));
  59. half NdotL;
  60. #if defined(_LIGHT_LAYERS)
  61. UNITY_BRANCH if (IsMatchingLightLayer(mainLight.layerMask, meshRenderingLayers))
  62. {
  63. #endif
  64. // Wrapped Diffuse
  65. // NdotL = saturate((dot(inputData.normalWS, mainLight.direction) + w) / ((1.0h + w) * (1.0h + w)) );
  66. NdotL = saturate((dot(inputData.normalWS, mainLight.direction) + w) * WrappedNormalization );
  67. lightingData.mainLightColor = LightingPhysicallyBasedWrapped(brdfData, mainLight, inputData.normalWS, inputData.viewDirectionWS, NdotL);
  68. // Translucency
  69. half transPower = translucency.y;
  70. half3 transLightDir = mainLight.direction + inputData.normalWS * translucency.w;
  71. half transDot = dot( transLightDir, -inputData.viewDirectionWS );
  72. transDot = exp2(saturate(transDot) * transPower - transPower);
  73. lightingData.mainLightColor += transDot * (1.0 - NdotL) * mainLight.color * lerp(1.0h, mainLight.shadowAttenuation, translucency.z) * brdfData.diffuse * translucency.x;
  74. #if defined(_LIGHT_LAYERS)
  75. }
  76. #endif
  77. #ifdef _ADDITIONAL_LIGHTS
  78. uint pixelLightCount = GetAdditionalLightsCount();
  79. // Clustered Lighting
  80. LIGHT_LOOP_BEGIN(pixelLightCount)
  81. // URP 12
  82. Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
  83. #if defined(_LIGHT_LAYERS)
  84. UNITY_BRANCH if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
  85. {
  86. #endif
  87. // Wrapped Diffuse
  88. NdotL = saturate((dot(inputData.normalWS, light.direction) + w) * WrappedNormalization );
  89. lightingData.additionalLightsColor += LightingPhysicallyBasedWrapped(
  90. brdfData, light, inputData.normalWS, inputData.viewDirectionWS, NdotL);
  91. // Translucency
  92. half transPower = translucency.y;
  93. half3 transLightDir = light.direction + inputData.normalWS * translucency.w;
  94. half transDot = dot( transLightDir, -inputData.viewDirectionWS );
  95. transDot = exp2(saturate(transDot) * transPower - transPower);
  96. lightingData.additionalLightsColor += brdfData.diffuse * transDot * (1.0h - NdotL) * light.color * lerp(1.0h, light.shadowAttenuation, translucency.z) * light.distanceAttenuation * translucency.x;
  97. #if defined(_LIGHT_LAYERS)
  98. }
  99. #endif
  100. LIGHT_LOOP_END
  101. #endif
  102. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  103. lightingData.vertexLightingColor += inputData.vertexLighting * brdfData.diffuse;
  104. #endif
  105. return CalculateFinalColor(lightingData, surfaceData.alpha);
  106. }
  107. #endif