Lux URP Tree Creator Lighting.hlsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef LIGHTWEIGHT_TREE_INCLUDED
  2. #define LIGHTWEIGHT_TREE_INCLUDED
  3. // Bark lighting
  4. inline half3 LightingTreeBark (Light light, half3 albedo, half3 specular, half gloss, half squashAmount, half3 normal, half3 viewDir)
  5. {
  6. float3 halfDir = SafeNormalize(light.direction + viewDir);
  7. half NoL = saturate( dot (normal, light.direction) );
  8. float NoH = saturate( dot (normal, halfDir) );
  9. float spec = pow (NoH, specular.r * 128.0f) * gloss;
  10. half3 c;
  11. half3 lighting = light.color * light.distanceAttenuation * light.shadowAttenuation * squashAmount;
  12. // c = albedo * lighting * NoL + lighting * specular * spec;
  13. c = (albedo + specular * spec) * NoL * lighting;
  14. return c;
  15. }
  16. half4 LuxURPTreeBarkFragment (InputData inputData, half3 albedo, half3 specular,
  17. half smoothness, half occlusion, half alpha, half squashAmount
  18. )
  19. {
  20. // ShadowMask: To ensure backward compatibility we have to avoid using shadowMask input, as it is not present in older shaders
  21. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  22. half4 shadowMask = inputData.shadowMask;
  23. #elif !defined (LIGHTMAP_ON)
  24. half4 shadowMask = unity_ProbesOcclusion;
  25. #else
  26. half4 shadowMask = half4(1, 1, 1, 1);
  27. #endif
  28. //Light mainLight = GetMainLight(inputData.shadowCoord);
  29. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, shadowMask);
  30. // SSAO
  31. #if defined(_SCREEN_SPACE_OCCLUSION)
  32. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(inputData.normalizedScreenSpaceUV);
  33. mainLight.color *= aoFactor.directAmbientOcclusion;
  34. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  35. #endif
  36. //MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, half4(0, 0, 0, 0));
  37. half3 color = albedo * inputData.bakedGI * occlusion;
  38. color += LightingTreeBark(mainLight, albedo, specular, smoothness, 1.0h, inputData.normalWS, inputData.viewDirectionWS);
  39. #ifdef _ADDITIONAL_LIGHTS
  40. uint pixelLightCount = GetAdditionalLightsCount();
  41. for (uint i = 0u; i < pixelLightCount; ++i)
  42. {
  43. // Light light = GetAdditionalLight(i, inputData.positionWS);
  44. // URP 10: We have to use the new GetAdditionalLight function
  45. Light light = GetAdditionalLight(i, inputData.positionWS, shadowMask);
  46. #if defined(_SCREEN_SPACE_OCCLUSION)
  47. light.color *= aoFactor.directAmbientOcclusion;
  48. #endif
  49. color += LightingTreeBark(light, albedo, specular, smoothness, squashAmount, inputData.normalWS, inputData.viewDirectionWS);
  50. }
  51. #endif
  52. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  53. color += inputData.vertexLighting * albedo;
  54. #endif
  55. return half4(color, alpha);
  56. }
  57. // Leaf lighting
  58. inline half3 LightingTreeLeaf(Light light, half3 albedo, half3 specular, half gloss, half2 translucency, half3 translucencyColor, half squashAmount, half3 normal, half3 viewDir)
  59. {
  60. float3 halfDir = SafeNormalize(light.direction + viewDir);
  61. half NoL = dot(normal, light.direction);
  62. float NoH = saturate( dot (normal, halfDir) );
  63. float spec = pow(NoH, specular.r * 128.0f) * gloss;
  64. // view dependent back contribution for translucency
  65. half backContrib = saturate(dot(viewDir, -light.direction));
  66. // normally translucency is more like -nl, but looks better when it's view dependent
  67. backContrib = lerp(saturate(-NoL), backContrib, translucency.y);
  68. translucencyColor *= backContrib * translucency.x;
  69. // wrap-around diffuse
  70. NoL = saturate (NoL * 0.6h + 0.4h);
  71. half3 c;
  72. /////@TODO: what is is this multiply 2x here???
  73. c = albedo * (translucencyColor * 2 + NoL);
  74. // No lighting on spec?!
  75. // c = c * light.color * light.distanceAttenuation * light.shadowAttenuation * squashAmount + spec;
  76. half3 lighting = light.color * light.distanceAttenuation * light.shadowAttenuation * squashAmount;
  77. c = (c + spec) * lighting;
  78. return c;
  79. }
  80. half4 LuxURPTreeLeafFragmentPBR(InputData inputData, half3 albedo, half3 specular,
  81. half smoothness, half occlusion, half alpha, half2 translucency, half3 translucencyColor, half squashAmount, half shadowStrength
  82. )
  83. {
  84. // ShadowMask: To ensure backward compatibility we have to avoid using shadowMask input, as it is not present in older shaders
  85. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  86. half4 shadowMask = inputData.shadowMask;
  87. #elif !defined (LIGHTMAP_ON)
  88. half4 shadowMask = unity_ProbesOcclusion;
  89. #else
  90. half4 shadowMask = half4(1, 1, 1, 1);
  91. #endif
  92. //Light mainLight = GetMainLight(inputData.shadowCoord);
  93. //MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, half4(0, 0, 0, 0));
  94. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, shadowMask);
  95. // SSAO
  96. #if defined(_SCREEN_SPACE_OCCLUSION)
  97. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(inputData.normalizedScreenSpaceUV);
  98. mainLight.color *= aoFactor.directAmbientOcclusion;
  99. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  100. #endif
  101. mainLight.shadowAttenuation = lerp(1.0h, mainLight.shadowAttenuation, shadowStrength * squashAmount /* fade out */);
  102. half3 color = albedo * inputData.bakedGI * occlusion;
  103. color += LightingTreeLeaf(mainLight, albedo, specular, smoothness, translucency, translucencyColor, 1, inputData.normalWS, inputData.viewDirectionWS);
  104. #ifdef _ADDITIONAL_LIGHTS
  105. uint pixelLightCount = GetAdditionalLightsCount();
  106. for (uint i = 0u; i < pixelLightCount; ++i)
  107. {
  108. Light light = GetAdditionalLight(i, inputData.positionWS);
  109. color += LightingTreeLeaf(light, albedo, specular, smoothness, translucency, translucencyColor, squashAmount, inputData.normalWS, inputData.viewDirectionWS);
  110. }
  111. #endif
  112. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  113. color += inputData.vertexLighting * albedo;
  114. #endif
  115. return half4(color, alpha);
  116. }
  117. #endif