Lux_Lighting_Flat.hlsl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //#if !defined(SHADERGRAPH_PREVIEW)
  2. #if !defined(SHADERGRAPH_PREVIEW) || defined(LIGHTWEIGHT_LIGHTING_INCLUDED)
  3. // As we do not have access to the vertex lights we will make the shder always sample add lights per pixel
  4. #if defined(_ADDITIONAL_LIGHTS_VERTEX)
  5. #undef _ADDITIONAL_LIGHTS_VERTEX
  6. #define _ADDITIONAL_LIGHTS
  7. #endif
  8. #endif
  9. void Lighting_half(
  10. // Base inputs
  11. float3 positionWS,
  12. float3 viewDirectionWS, // must be float?! for mobile
  13. // Surface description
  14. half3 albedo,
  15. half3 specular,
  16. half smoothness,
  17. half occlusion,
  18. half alpha,
  19. float2 lightMapUV,
  20. bool receiveSSAO,
  21. // Final lit color
  22. out half3 Lighting,
  23. out half3 MetaAlbedo,
  24. out half3 MetaSpecular
  25. )
  26. {
  27. //#if defined(SHADERGRAPH_PREVIEW)
  28. #if defined(SHADERGRAPH_PREVIEW) || ( !defined(LIGHTWEIGHT_LIGHTING_INCLUDED) && !defined(UNIVERSAL_LIGHTING_INCLUDED) )
  29. Lighting = albedo;
  30. MetaAlbedo = half3(0,0,0);
  31. MetaSpecular = half3(0,0,0);
  32. #else
  33. // Real Lighting ----------
  34. half metallic = 0;
  35. half3 tnormal = normalize( cross(ddy(positionWS), ddx(positionWS)) );
  36. // TODO: Vulkan on Android here shows inverted normals?
  37. #if defined(SHADER_API_VULKAN)
  38. tnormal *= -1;
  39. #endif
  40. half3 normalWS = tnormal;
  41. viewDirectionWS = SafeNormalize(viewDirectionWS);
  42. // GI Lighting
  43. half3 bakedGI;
  44. #ifdef LIGHTMAP_ON
  45. lightMapUV = lightMapUV * unity_LightmapST.xy + unity_LightmapST.zw;
  46. bakedGI = SAMPLE_GI(lightMapUV, half3(0,0,0), normalWS);
  47. #else
  48. // CHECK: Do we have3 to multiply SH with occlusion here?
  49. bakedGI = SampleSH(normalWS) * occlusion;
  50. #endif
  51. BRDFData brdfData;
  52. InitializeBRDFData(albedo, metallic, specular, smoothness, alpha, brdfData);
  53. float4 clipPos = TransformWorldToHClip(positionWS);
  54. // Get Shadow Sampling Coords / Unfortunately per pixel...
  55. #if SHADOWS_SCREEN
  56. float4 shadowCoord = ComputeScreenPos(clipPos);
  57. #else
  58. float4 shadowCoord = TransformWorldToShadowCoord(positionWS);
  59. #endif
  60. // Shadow mask
  61. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  62. half4 shadowMask = SAMPLE_SHADOWMASK(lightMapUV);
  63. #elif !defined (LIGHTMAP_ON)
  64. half4 shadowMask = unity_ProbesOcclusion;
  65. #else
  66. half4 shadowMask = half4(1, 1, 1, 1);
  67. #endif
  68. //Light mainLight = GetMainLight(shadowCoord);
  69. Light mainLight = GetMainLight(shadowCoord, positionWS, shadowMask);
  70. // SSAO
  71. #if defined(_SCREEN_SPACE_OCCLUSION)
  72. AmbientOcclusionFactor aoFactor;
  73. aoFactor.indirectAmbientOcclusion = 1;
  74. aoFactor.directAmbientOcclusion = 1;
  75. if(receiveSSAO) {
  76. float4 ndc = clipPos * 0.5f;
  77. float2 normalized = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  78. normalized /= clipPos.w;
  79. normalized *= _ScreenParams.xy;
  80. // We could also use IN.Screenpos(default) --> ( IN.Screenpos.xy * _ScreenParams.xy)
  81. // HDRP 10.1
  82. normalized = GetNormalizedScreenSpaceUV(normalized);
  83. aoFactor = GetScreenSpaceAmbientOcclusion(normalized);
  84. mainLight.color *= aoFactor.directAmbientOcclusion;
  85. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  86. }
  87. #endif
  88. MixRealtimeAndBakedGI(mainLight, normalWS, bakedGI, half4(0, 0, 0, 0));
  89. Lighting = GlobalIllumination(brdfData, bakedGI, occlusion, normalWS, viewDirectionWS);
  90. Lighting += LightingPhysicallyBased(brdfData, mainLight, normalWS, viewDirectionWS);
  91. #ifdef _ADDITIONAL_LIGHTS
  92. uint pixelLightCount = GetAdditionalLightsCount();
  93. for (uint i = 0u; i < pixelLightCount; ++i)
  94. {
  95. // Light light = GetAdditionalPerObjectLight(index, positionWS); // here; shadowAttenuation = 1.0;
  96. // URP 10: We have to use the new GetAdditionalLight function
  97. Light light = GetAdditionalLight(i, positionWS, shadowMask);
  98. #if defined(_SCREEN_SPACE_OCCLUSION)
  99. if(receiveSSAO) {
  100. light.color *= aoFactor.directAmbientOcclusion;
  101. }
  102. #endif
  103. #if defined(_SCREEN_SPACE_OCCLUSION)
  104. if(receiveSSAO) {
  105. light.color *= aoFactor.directAmbientOcclusion;
  106. }
  107. #endif
  108. Lighting += LightingPhysicallyBased(brdfData, light, normalWS, viewDirectionWS);
  109. }
  110. #endif
  111. //#ifdef _ADDITIONAL_LIGHTS_VERTEX
  112. // Lighting += inputData.vertexLighting * brdfData.diffuse;
  113. //#endif
  114. // Set Albedo for meta pass
  115. #if defined(LIGHTWEIGHT_META_PASS_INCLUDED)
  116. Lighting = half3(0,0,0);
  117. MetaAlbedo = albedo;
  118. MetaSpecular = half3(0.02,0.02,0.02);
  119. #else
  120. MetaAlbedo = half3(0,0,0);
  121. MetaSpecular = half3(0,0,0);
  122. #endif
  123. // End Real Lighting ----------
  124. #endif
  125. }
  126. // Unity 2019.1. needs a float version
  127. void Lighting_float(
  128. // Base inputs
  129. float3 positionWS,
  130. half3 viewDirectionWS,
  131. // Surface description
  132. half3 albedo,
  133. half3 specular,
  134. half smoothness,
  135. half occlusion,
  136. half alpha,
  137. float2 lightMapUV,
  138. bool receiveSSAO,
  139. // Final lit color
  140. out half3 Lighting,
  141. out half3 MetaAlbedo,
  142. out half3 MetaSpecular
  143. )
  144. {
  145. Lighting_half(
  146. positionWS, viewDirectionWS,
  147. albedo, specular, smoothness, occlusion, alpha,
  148. lightMapUV, receiveSSAO,
  149. Lighting, MetaAlbedo, MetaSpecular
  150. );
  151. }