Lux URP Lit Extended Lighting.hlsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. real GI_Luminance(real3 linearRgb)
  2. {
  3. return dot(linearRgb, real3(0.2126729, 0.7151522, 0.0721750));
  4. }
  5. // Horizon Occlusion for Normal Mapped Reflections: http://marmosetco.tumblr.com/post/81245981087
  6. half LuxGetHorizonOcclusion(half3 R, half3 normalWS, half3 vertexNormal, half horizonFade)
  7. {
  8. //half3 R = reflect(-V, normalWS);
  9. half specularOcclusion = saturate(1.0 + horizonFade * dot(R, vertexNormal));
  10. // smooth it
  11. return specularOcclusion * specularOcclusion;
  12. }
  13. half3 LuxExtended_GlobalIllumination(BRDFData brdfData, half3 bakedGI, half occlusion, half3 normalWS, half3 viewDirectionWS, half GItoAO, half GItoAOBias, half3 bentNormal, half3 geoNormalWS, half horizonOcllusion)
  14. {
  15. half3 reflectVector = reflect(-viewDirectionWS, normalWS);
  16. half fresnelTerm = Pow4(1.0 - saturate(dot(normalWS, viewDirectionWS)));
  17. half3 indirectDiffuse = bakedGI * occlusion;
  18. half reflOcclusion = 1;
  19. #if defined(_BENTNORMAL)
  20. reflOcclusion = saturate(dot(normalWS, bentNormal));
  21. /*
  22. occlusion = sqrt(1.0 - saturate(occlusion/reflOcclusion));
  23. occlusion = TWO_PI * (1.0 - occlusion);
  24. occlusion = saturate(occlusion * INV_FOUR_PI);
  25. reflOcclusion = 1;
  26. */
  27. #endif
  28. // Horizon Occlusion
  29. #if defined (_SAMPLENORMAL) && defined(_UBER)
  30. reflOcclusion *= LuxGetHorizonOcclusion( reflectVector, normalWS, geoNormalWS, horizonOcllusion);
  31. #endif
  32. // AO from lightmap
  33. #if defined(LIGHTMAP_ON) && defined(_ENABLE_AO_FROM_GI)
  34. half specOcclusion = saturate( GI_Luminance(bakedGI) * GItoAO + GItoAOBias);
  35. half3 indirectSpecular = GlossyEnvironmentReflection(reflectVector, brdfData.perceptualRoughness, reflOcclusion * occlusion * specOcclusion );
  36. #else
  37. half3 indirectSpecular = GlossyEnvironmentReflection(reflectVector, brdfData.perceptualRoughness, reflOcclusion * occlusion);
  38. #endif
  39. return EnvironmentBRDF(brdfData, indirectDiffuse, indirectSpecular, fresnelTerm);
  40. }
  41. half4 LuxExtended_UniversalFragmentPBR(InputData inputData, half3 albedo, half metallic, half3 specular,
  42. half smoothness, half occlusion, half3 emission, half alpha,
  43. half GItoAO, half GItoAOBias, half3 bentNormal, half3 geoNormalWS, half horizonOcllusion
  44. )
  45. {
  46. BRDFData brdfData;
  47. InitializeBRDFData(albedo, metallic, specular, smoothness, alpha, brdfData);
  48. // ShadowMask: To ensure backward compatibility we have to avoid using shadowMask input, as it is not present in older shaders
  49. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  50. half4 shadowMask = inputData.shadowMask;
  51. #elif !defined (LIGHTMAP_ON)
  52. half4 shadowMask = unity_ProbesOcclusion;
  53. #else
  54. half4 shadowMask = half4(1, 1, 1, 1);
  55. #endif
  56. //Light mainLight = GetMainLight(inputData.shadowCoord);
  57. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, shadowMask);
  58. #if defined(_SCREEN_SPACE_OCCLUSION)
  59. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(inputData.normalizedScreenSpaceUV);
  60. mainLight.color *= aoFactor.directAmbientOcclusion;
  61. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  62. #endif
  63. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, half4(0, 0, 0, 0));
  64. half3 color = LuxExtended_GlobalIllumination(brdfData, inputData.bakedGI, occlusion, inputData.normalWS, inputData.viewDirectionWS, GItoAO, GItoAOBias, bentNormal, geoNormalWS, horizonOcllusion);
  65. color += LightingPhysicallyBased(brdfData, mainLight, inputData.normalWS, inputData.viewDirectionWS);
  66. #ifdef _ADDITIONAL_LIGHTS
  67. uint pixelLightCount = GetAdditionalLightsCount();
  68. for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex)
  69. {
  70. // Light light = GetAdditionalLight(lightIndex, inputData.positionWS);
  71. // URP 10: We have to use the new GetAdditionalLight function
  72. Light light = GetAdditionalLight(lightIndex, inputData.positionWS, shadowMask);
  73. color += LightingPhysicallyBased(brdfData, light, inputData.normalWS, inputData.viewDirectionWS);
  74. }
  75. #endif
  76. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  77. color += inputData.vertexLighting * brdfData.diffuse;
  78. #endif
  79. color += emission;
  80. return half4(color, alpha);
  81. }