Lux_Lighting_Transmission.hlsl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // Lighting specific inputs
  26. half transmissionStrength,
  27. half transmissionPower,
  28. half transmissionDistortion,
  29. half transmissionShadowstrength,
  30. half transmissionMaskByShadowstrength,
  31. // Lightmapping
  32. float2 lightMapUV,
  33. // Final lit color
  34. out half3 MetaAlbedo,
  35. out half3 FinalLighting,
  36. out half3 MetaSpecular
  37. )
  38. {
  39. //#ifdef SHADERGRAPH_PREVIEW
  40. #if defined(SHADERGRAPH_PREVIEW) || ( !defined(LIGHTWEIGHT_LIGHTING_INCLUDED) && !defined(UNIVERSAL_LIGHTING_INCLUDED) )
  41. FinalLighting = albedo;
  42. MetaAlbedo = half3(0,0,0);
  43. MetaSpecular = half3(0,0,0);
  44. #else
  45. // Real Lighting ----------
  46. if (enableNormalMapping) {
  47. normalWS = TransformTangentToWorld(normalTS, half3x3(tangentWS.xyz, bitangentWS.xyz, normalWS.xyz));
  48. }
  49. normalWS = NormalizeNormalPerPixel(normalWS);
  50. viewDirectionWS = SafeNormalize(viewDirectionWS);
  51. // GI Lighting
  52. half3 bakedGI;
  53. #ifdef LIGHTMAP_ON
  54. lightMapUV = lightMapUV * unity_LightmapST.xy + unity_LightmapST.zw;
  55. bakedGI = SAMPLE_GI(lightMapUV, half3(0,0,0), normalWS);
  56. #else
  57. bakedGI = SampleSH(normalWS);
  58. #endif
  59. BRDFData brdfData;
  60. InitializeBRDFData(albedo, metallic, specular, smoothness, alpha, brdfData);
  61. float4 clipPos = TransformWorldToHClip(positionWS);
  62. // Get Shadow Sampling Coords / Unfortunately per pixel...
  63. #if SHADOWS_SCREEN
  64. float4 shadowCoord = ComputeScreenPos(clipPos);
  65. #else
  66. float4 shadowCoord = TransformWorldToShadowCoord(positionWS);
  67. #endif
  68. // Shadow mask
  69. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  70. half4 shadowMask = SAMPLE_SHADOWMASK(lightMapUV);
  71. #elif !defined (LIGHTMAP_ON)
  72. half4 shadowMask = unity_ProbesOcclusion;
  73. #else
  74. half4 shadowMask = half4(1, 1, 1, 1);
  75. #endif
  76. //Light mainLight = GetMainLight(shadowCoord);
  77. Light mainLight = GetMainLight(shadowCoord, positionWS, shadowMask);
  78. half3 mainLightColor = mainLight.color;
  79. // SSAO
  80. #if defined(_SCREEN_SPACE_OCCLUSION)
  81. float4 ndc = clipPos * 0.5f;
  82. float2 normalized = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  83. normalized /= clipPos.w;
  84. normalized *= _ScreenParams.xy;
  85. // We could also use IN.Screenpos(default) --> ( IN.Screenpos.xy * _ScreenParams.xy)
  86. // HDRP 10.1
  87. normalized = GetNormalizedScreenSpaceUV(normalized);
  88. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalized);
  89. mainLight.color *= aoFactor.directAmbientOcclusion;
  90. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  91. #endif
  92. FinalLighting = GlobalIllumination(brdfData, bakedGI, occlusion, normalWS, viewDirectionWS);
  93. MixRealtimeAndBakedGI(mainLight, normalWS, bakedGI, half4(0, 0, 0, 0));
  94. // Main Light
  95. FinalLighting += LightingPhysicallyBased(brdfData, mainLight, normalWS, viewDirectionWS);
  96. // translucency
  97. half3 transLightDir = mainLight.direction + normalWS * transmissionDistortion;
  98. half transDot = dot( transLightDir, -viewDirectionWS );
  99. transDot = exp2(saturate(transDot) * transmissionPower - transmissionPower);
  100. half NdotL = saturate(dot(normalWS, mainLight.direction));
  101. FinalLighting += brdfData.diffuse * transDot * (1.0h - NdotL) * mainLightColor * lerp(1.0h, mainLight.shadowAttenuation, transmissionShadowstrength) * transmissionStrength * 4;
  102. // Handle additional lights
  103. #ifdef _ADDITIONAL_LIGHTS
  104. uint pixelLightCount = GetAdditionalLightsCount();
  105. for (uint i = 0u; i < pixelLightCount; ++i) {
  106. // Get index upfront as we need it for GetAdditionalLightShadowParams();
  107. int index = GetPerObjectLightIndex(i);
  108. // Light light = GetAdditionalPerObjectLight(index, positionWS); // here; shadowAttenuation = 1.0;
  109. // URP 10: We have to use the new GetAdditionalLight function
  110. Light light = GetAdditionalLight(i, positionWS, shadowMask);
  111. half3 lightColor = light.color;
  112. #if defined(_SCREEN_SPACE_OCCLUSION)
  113. light.color *= aoFactor.directAmbientOcclusion;
  114. #endif
  115. FinalLighting += LightingPhysicallyBased(brdfData, light, normalWS, viewDirectionWS);
  116. // Transmission
  117. half4 shadowParams = GetAdditionalLightShadowParams(index);
  118. lightColor *= lerp(1, shadowParams.x, transmissionMaskByShadowstrength); // shadowParams.x == shadow strength, which is 0 for point lights
  119. transLightDir = light.direction + normalWS * transmissionDistortion;
  120. transDot = dot( transLightDir, -viewDirectionWS );
  121. transDot = exp2(saturate(transDot) * transmissionPower - transmissionPower);
  122. NdotL = saturate(dot(normalWS, light.direction));
  123. FinalLighting += brdfData.diffuse * transDot * (1.0h - NdotL) * lightColor * lerp(1.0h, light.shadowAttenuation, transmissionShadowstrength) * light.distanceAttenuation * transmissionStrength * 4;
  124. }
  125. #endif
  126. // Set Albedo for meta pass
  127. #if defined(LIGHTWEIGHT_META_PASS_INCLUDED) || defined(UNIVERSAL_META_PASS_INCLUDED)
  128. FinalLighting = half3(0,0,0);
  129. MetaAlbedo = albedo;
  130. MetaSpecular = specular;
  131. #else
  132. MetaAlbedo = half3(0,0,0);
  133. MetaSpecular = half3(0,0,0);
  134. #endif
  135. // End Real Lighting ----------
  136. #endif
  137. }
  138. // Unity 2019.1. needs a float version
  139. void Lighting_float(
  140. // Base inputs
  141. float3 positionWS,
  142. half3 viewDirectionWS,
  143. // Normal inputs
  144. half3 normalWS,
  145. half3 tangentWS,
  146. half3 bitangentWS,
  147. bool enableNormalMapping,
  148. half3 normalTS,
  149. // Surface description
  150. half3 albedo,
  151. half metallic,
  152. half3 specular,
  153. half smoothness,
  154. half occlusion,
  155. half alpha,
  156. // Lighting specific inputs
  157. half transmissionStrength,
  158. half transmissionPower,
  159. half transmissionDistortion,
  160. half transmissionShadowstrength,
  161. half transmissionMaskByShadowstrength,
  162. // Lightmapping
  163. float2 lightMapUV,
  164. // Final lit color
  165. out half3 MetaAlbedo,
  166. out half3 FinalLighting,
  167. out half3 MetaSpecular
  168. )
  169. {
  170. Lighting_half(
  171. positionWS, viewDirectionWS, normalWS, tangentWS, bitangentWS, enableNormalMapping, normalTS,
  172. albedo, metallic, specular, smoothness, occlusion, alpha,
  173. transmissionStrength, transmissionPower, transmissionDistortion, transmissionShadowstrength, transmissionMaskByShadowstrength,
  174. lightMapUV, MetaAlbedo, FinalLighting, MetaSpecular);
  175. }