Lux_Lighting_Transparent.hlsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #if !defined(SHADERGRAPH_PREVIEW) || defined(LIGHTWEIGHT_LIGHTING_INCLUDED)
  2. // As we do not have access to the vertex lights we will make the shader always sample add lights per pixel
  3. #if defined(_ADDITIONAL_LIGHTS_VERTEX)
  4. #undef _ADDITIONAL_LIGHTS_VERTEX
  5. #define _ADDITIONAL_LIGHTS
  6. #endif
  7. #endif
  8. void Lighting_half(
  9. // Base inputs
  10. float3 positionWS,
  11. half3 viewDirectionWS,
  12. // Normal inputs
  13. half3 normalWS,
  14. half3 tangentWS,
  15. half3 bitangentWS,
  16. bool enableNormalMapping,
  17. half3 normalTS,
  18. // Surface description
  19. half3 albedo,
  20. half metallic,
  21. half3 specular,
  22. half smoothness,
  23. half occlusion,
  24. half alpha,
  25. // Lightmapping
  26. float2 lightMapUV,
  27. // Final lit color
  28. out half3 MetaAlbedo,
  29. out half3 FinalLighting,
  30. out half3 MetaSpecular
  31. )
  32. {
  33. //#ifdef SHADERGRAPH_PREVIEW
  34. #if defined(SHADERGRAPH_PREVIEW) || ( !defined(LIGHTWEIGHT_LIGHTING_INCLUDED) && !defined(UNIVERSAL_LIGHTING_INCLUDED) )
  35. FinalLighting = albedo;
  36. MetaAlbedo = half3(0,0,0);
  37. MetaSpecular = half3(0,0,0);
  38. #else
  39. // This fixes the fog issue
  40. #if defined (_ALPHAPREMULTIPLY_ON)
  41. unity_FogColor *= alpha;
  42. // Two Materials Setup: We would have to tweak alpha according to fog intensity? which we do not have.
  43. #endif
  44. // Real Lighting ----------
  45. if (enableNormalMapping) {
  46. normalWS = TransformTangentToWorld(normalTS, half3x3(tangentWS.xyz, bitangentWS.xyz, normalWS.xyz));
  47. }
  48. normalWS = NormalizeNormalPerPixel(normalWS);
  49. viewDirectionWS = SafeNormalize(viewDirectionWS);
  50. // GI Lighting
  51. half3 bakedGI;
  52. #ifdef LIGHTMAP_ON
  53. lightMapUV = lightMapUV * unity_LightmapST.xy + unity_LightmapST.zw;
  54. bakedGI = SAMPLE_GI(lightMapUV, half3(0,0,0), normalWS);
  55. #else
  56. bakedGI = SampleSH(normalWS);
  57. #endif
  58. BRDFData brdfData;
  59. InitializeBRDFData(albedo, metallic, specular, smoothness, alpha, brdfData);
  60. float4 clipPos = TransformWorldToHClip(positionWS);
  61. // Get Shadow Sampling Coords / Unfortunately per pixel...
  62. #if SHADOWS_SCREEN
  63. float4 shadowCoord = ComputeScreenPos(clipPos);
  64. #else
  65. float4 shadowCoord = TransformWorldToShadowCoord(positionWS);
  66. #endif
  67. // Shadow mask
  68. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  69. half4 shadowMask = SAMPLE_SHADOWMASK(lightMapUV);
  70. #elif !defined (LIGHTMAP_ON)
  71. half4 shadowMask = unity_ProbesOcclusion;
  72. #else
  73. half4 shadowMask = half4(1, 1, 1, 1);
  74. #endif
  75. //Light mainLight = GetMainLight(shadowCoord);
  76. Light mainLight = GetMainLight(shadowCoord, positionWS, shadowMask);
  77. // SSAO - no ssao here as it does not write into the depth/depthnormal buffer
  78. FinalLighting = GlobalIllumination(brdfData, bakedGI, occlusion, normalWS, viewDirectionWS);
  79. MixRealtimeAndBakedGI(mainLight, normalWS, bakedGI, half4(0, 0, 0, 0));
  80. // GetMainLight will return screen space shadows.
  81. #if defined(_MAIN_LIGHT_SHADOWS)
  82. float4 shadowCoordWS = TransformWorldToShadowCoord(positionWS);
  83. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
  84. half shadowStrength = GetMainLightShadowStrength();
  85. mainLight.shadowAttenuation = SampleShadowmap(shadowCoordWS, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
  86. #endif
  87. // Main Light
  88. FinalLighting += LightingPhysicallyBased(brdfData, mainLight, normalWS, viewDirectionWS);
  89. // Handle additional lights
  90. #ifdef _ADDITIONAL_LIGHTS
  91. uint pixelLightCount = GetAdditionalLightsCount();
  92. for (uint i = 0u; i < pixelLightCount; ++i) {
  93. // Light light = GetAdditionalPerObjectLight(index, positionWS); // here; shadowAttenuation = 1.0;
  94. // URP 10: We have to use the new GetAdditionalLight function
  95. Light light = GetAdditionalLight(i, positionWS, shadowMask);
  96. FinalLighting += LightingPhysicallyBased(brdfData, light, normalWS, viewDirectionWS);
  97. }
  98. #endif
  99. // Set Albedo for meta pass
  100. #if defined(LIGHTWEIGHT_META_PASS_INCLUDED) || defined(UNIVERSAL_META_PASS_INCLUDED)
  101. FinalLighting = half3(0,0,0);
  102. MetaAlbedo = albedo;
  103. MetaSpecular = specular;
  104. #else
  105. MetaAlbedo = half3(0,0,0);
  106. MetaSpecular = half3(0,0,0);
  107. #endif
  108. // End Real Lighting ----------
  109. #endif
  110. }
  111. // Unity 2019.1. needs a float version
  112. void Lighting_float(
  113. // Base inputs
  114. float3 positionWS,
  115. half3 viewDirectionWS,
  116. // Normal inputs
  117. half3 normalWS,
  118. half3 tangentWS,
  119. half3 bitangentWS,
  120. bool enableNormalMapping,
  121. half3 normalTS,
  122. // Surface description
  123. half3 albedo,
  124. half metallic,
  125. half3 specular,
  126. half smoothness,
  127. half occlusion,
  128. half alpha,
  129. // Lightmapping
  130. float2 lightMapUV,
  131. // Final lit color
  132. out half3 MetaAlbedo,
  133. out half3 FinalLighting,
  134. out half3 MetaSpecular
  135. )
  136. {
  137. Lighting_half(
  138. positionWS, viewDirectionWS, normalWS, tangentWS, bitangentWS, enableNormalMapping, normalTS,
  139. albedo, metallic, specular, smoothness, occlusion, alpha,
  140. lightMapUV, MetaAlbedo, FinalLighting, MetaSpecular);
  141. }