CTI_TranslucentLighting.cginc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef CTI_TRANSLUCENT_LIGHTING_INCLUDED
  2. #define CTI_TRANSLUCENT_LIGHTING_INCLUDED
  3. #include "UnityShaderVariables.cginc"
  4. #include "UnityStandardConfig.cginc"
  5. #include "UnityLightingCommon.cginc"
  6. #include "UnityGlobalIllumination.cginc"
  7. //-------------------------------------------------------------------------------------
  8. // Compatibilty settings
  9. // Uncomment either "#define USEALLOY" or "#define USEUBER" to enable deferred lighting support for the given shader package.
  10. // Leave them commented in case you are using the Lux Foliage deferred lighting shader.
  11. // More infos in the docs.
  12. // #define USEALLOY
  13. // #define USEUBER
  14. //-------------------------------------------------------------------------------------
  15. // Default BRDF to use:
  16. #if !defined (UNITY_BRDF_PBS) // allow to explicitly override BRDF in custom shader
  17. // still add safe net for low shader models, otherwise we might end up with shaders failing to compile
  18. // the only exception is WebGL in 5.3 - it will be built with shader target 2.0 but we want it to get rid of constraints, as it is effectively desktop
  19. #if SHADER_TARGET < 30 && !UNITY_53_SPECIFIC_TARGET_WEBGL
  20. #define UNITY_BRDF_PBS BRDF3_Unity_PBS
  21. #elif UNITY_PBS_USE_BRDF3
  22. #define UNITY_BRDF_PBS BRDF3_Unity_PBS
  23. #elif UNITY_PBS_USE_BRDF2
  24. #define UNITY_BRDF_PBS BRDF2_Unity_PBS
  25. #elif UNITY_PBS_USE_BRDF1
  26. #define UNITY_BRDF_PBS BRDF1_Unity_PBS
  27. #elif defined(SHADER_TARGET_SURFACE_ANALYSIS)
  28. // we do preprocess pass during shader analysis and we dont actually care about brdf as we need only inputs/outputs
  29. #define UNITY_BRDF_PBS BRDF1_Unity_PBS
  30. #else
  31. #error something broke in auto-choosing BRDF
  32. #endif
  33. #endif
  34. //-------------------------------------------------------------------------------------
  35. // BRDF for lights extracted from *indirect* directional lightmaps (baked and realtime).
  36. // Baked directional lightmap with *direct* light uses UNITY_BRDF_PBS.
  37. // For better quality change to BRDF1_Unity_PBS.
  38. // No directional lightmaps in SM2.0.
  39. #if !defined(UNITY_BRDF_PBS_LIGHTMAP_INDIRECT)
  40. #define UNITY_BRDF_PBS_LIGHTMAP_INDIRECT BRDF2_Unity_PBS
  41. #endif
  42. #if !defined (UNITY_BRDF_GI)
  43. #define UNITY_BRDF_GI BRDF_Unity_Indirect
  44. #endif
  45. //-------------------------------------------------------------------------------------
  46. inline half3 BRDF_Unity_Indirect (half3 baseColor, half3 specColor, half oneMinusReflectivity, half oneMinusRoughness, half3 normal, half3 viewDir, half occlusion, UnityGI gi)
  47. {
  48. half3 c = 0;
  49. #if defined(DIRLIGHTMAP_SEPARATE)
  50. gi.indirect.diffuse = 0;
  51. gi.indirect.specular = 0;
  52. #ifdef LIGHTMAP_ON
  53. c += UNITY_BRDF_PBS_LIGHTMAP_INDIRECT (baseColor, specColor, oneMinusReflectivity, oneMinusRoughness, normal, viewDir, gi.light2, gi.indirect).rgb * occlusion;
  54. #endif
  55. #ifdef DYNAMICLIGHTMAP_ON
  56. c += UNITY_BRDF_PBS_LIGHTMAP_INDIRECT (baseColor, specColor, oneMinusReflectivity, oneMinusRoughness, normal, viewDir, gi.light3, gi.indirect).rgb * occlusion;
  57. #endif
  58. #endif
  59. return c;
  60. }
  61. //-------------------------------------------------------------------------------------
  62. // little helpers for GI calculation
  63. #define UNITY_GLOSSY_ENV_FROM_SURFACE(x, s, data) \
  64. Unity_GlossyEnvironmentData g; \
  65. g.roughness = 1 - s.Smoothness; \
  66. g.reflUVW = reflect(-data.worldViewDir, s.Normal); \
  67. #if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS
  68. #define UNITY_GI(x, s, data) x = UnityGlobalIllumination (data, s.Occlusion, s.Normal);
  69. #else
  70. #define UNITY_GI(x, s, data) \
  71. UNITY_GLOSSY_ENV_FROM_SURFACE(g, s, data); \
  72. x = UnityGlobalIllumination (data, s.Occlusion, s.Normal, g);
  73. #endif
  74. //-------------------------------------------------------------------------------------
  75. // Surface shader output structure to be used with physically
  76. // based shading model.
  77. struct SurfaceOutputStandardTranslucent {
  78. fixed3 Albedo;
  79. fixed3 Normal;
  80. half3 Emission;
  81. half3 Specular;
  82. half Translucency;
  83. half ScatteringPower;
  84. half Smoothness;
  85. half Occlusion;
  86. fixed Alpha;
  87. };
  88. inline half4 LightingStandardTranslucent (SurfaceOutputStandardTranslucent s, half3 viewDir, UnityGI gi)
  89. {
  90. s.Normal = normalize(s.Normal);
  91. // energy conservation
  92. half oneMinusReflectivity;
  93. s.Albedo = EnergyConservationBetweenDiffuseAndSpecular (s.Albedo, s.Specular, /*out*/ oneMinusReflectivity);
  94. // shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha)
  95. // this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha
  96. half outputAlpha;
  97. s.Albedo = PreMultiplyAlpha (s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha);
  98. half4 c = UNITY_BRDF_PBS (s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
  99. c.rgb += UNITY_BRDF_GI (s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi);
  100. c.a = outputAlpha;
  101. // Decrecated
  102. // Thin Layer Translucency
  103. /* // Only using dotNL gives us more lively lighting beyond the shadow distance.
  104. half backlight = saturate( dot(-s.Normal, gi.light.dir) + 0.2);
  105. half fresnel = (1.0 - backlight) * (1.0 - backlight);
  106. fresnel *= fresnel;
  107. //#if defined (DIRECTIONAL) || defined (DIRECTIONAL_COOKIE)
  108. c.rgb += s.Albedo * backlight * (1.0 - fresnel) * 4.0 * s.Translucency * gi.light.color;
  109. */
  110. half transPower = s.ScatteringPower * 10.0f;
  111. half3 transLightDir = gi.light.dir + s.Normal * 0.01;
  112. half transDot = dot( transLightDir, -viewDir );
  113. transDot = exp2(saturate(transDot) * transPower - transPower);
  114. half3 lightScattering = transDot * gi.light.color * (1.0 - saturate(dot(s.Normal, gi.light.dir)) );
  115. c.rgb += s.Albedo * 4.0 * s.Translucency * lightScattering;
  116. return c;
  117. }
  118. inline half4 LightingStandardTranslucent_Deferred (SurfaceOutputStandardTranslucent s, half3 viewDir, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal)
  119. {
  120. // energy conservation
  121. half oneMinusReflectivity;
  122. s.Albedo = EnergyConservationBetweenDiffuseAndSpecular (s.Albedo, s.Specular, /*out*/ oneMinusReflectivity);
  123. // For indirect lighting we simply use the built in BRDF
  124. half4 c = UNITY_BRDF_PBS (s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
  125. c.rgb += UNITY_BRDF_GI (s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi);
  126. outDiffuseOcclusion = half4(s.Albedo, s.Occlusion);
  127. half4 emission;
  128. // Alloy Support
  129. #if defined (USEALLOY)
  130. outSpecSmoothness = half4(s.Specular, s.Smoothness);
  131. outNormal = half4(s.Normal * 0.5 + 0.5, 1);
  132. emission = half4(s.Emission + c.rgb, 1.0 - s.Translucency);
  133. // UBER Support
  134. #elif defined (USEUBER)
  135. //outDiffuseOcclusion = half4(half3(1,0,0), s.Occlusion);
  136. outSpecSmoothness = half4(s.Specular, s.Smoothness);
  137. float translucency = floor(saturate(s.Translucency) * 15) * (-128);
  138. outNormal = half4(s.Normal * 0.5 + 0.5, 1);
  139. emission = half4(s.Emission + c.rgb, translucency);
  140. // Lux Support
  141. #else
  142. outSpecSmoothness = half4(s.Specular.r, s.ScatteringPower, s.Translucency, s.Smoothness);
  143. s.Normal = normalize(s.Normal);
  144. // Mark as translucent
  145. outNormal = half4(s.Normal * 0.5 + 0.5, 0.66);
  146. emission = half4(s.Emission + c.rgb, 1);
  147. #endif
  148. return emission;
  149. }
  150. inline void LightingStandardTranslucent_GI (
  151. SurfaceOutputStandardTranslucent s,
  152. UnityGIInput data,
  153. inout UnityGI gi)
  154. {
  155. UNITY_GI(gi, s, data);
  156. }
  157. #endif