Lighting_DR.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef FLATKIT_LIGHTING_DR_INCLUDED
  2. #define FLATKIT_LIGHTING_DR_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. inline half NdotLTransition(half3 normal, half3 lightDir, half selfShadingSize, half shadowEdgeSize, half flatness) {
  5. const half NdotL = dot(normal, lightDir);
  6. const half angleDiff = saturate((NdotL * 0.5 + 0.5) - selfShadingSize);
  7. const half angleDiffTransition = smoothstep(0, shadowEdgeSize, angleDiff);
  8. return lerp(angleDiff, angleDiffTransition, flatness);
  9. }
  10. inline half NdotLTransitionPrimary(half3 normal, half3 lightDir) {
  11. return NdotLTransition(normal, lightDir, _SelfShadingSize, _ShadowEdgeSize, _Flatness);
  12. }
  13. #if defined(DR_CEL_EXTRA_ON)
  14. inline half NdotLTransitionExtra(half3 normal, half3 lightDir) {
  15. return NdotLTransition(normal, lightDir, _SelfShadingSizeExtra, _ShadowEdgeSizeExtra, _FlatnessExtra);
  16. }
  17. #endif
  18. inline half NdotLTransitionTexture(half3 normal, half3 lightDir, sampler2D stepTex) {
  19. const half NdotL = dot(normal, lightDir);
  20. const half angleDiff = saturate((NdotL * 0.5 + 0.5) - _SelfShadingSize * 0.0);
  21. const half4 rampColor = tex2D(stepTex, half2(angleDiff, 0.5));
  22. // NOTE: The color channel here corresponds to the texture format in the shader editor script.
  23. const half angleDiffTransition = rampColor.r;
  24. return angleDiffTransition;
  25. }
  26. inline void ApplyLightToColor(Light light, inout half3 c) {
  27. #if defined(_UNITYSHADOWMODE_MULTIPLY)
  28. c *= lerp(1, light.shadowAttenuation, _UnityShadowPower);
  29. #endif
  30. #if defined(_UNITYSHADOWMODE_COLOR)
  31. c = lerp(lerp(c, _UnityShadowColor.rgb, _UnityShadowColor.a), c, light.shadowAttenuation);
  32. #endif
  33. c.rgb *= light.color * light.distanceAttenuation;
  34. }
  35. half3 LightingPhysicallyBased_DSTRM(Light light, InputData inputData)
  36. {
  37. // If all light in the scene is baked, we use custom light direction for the cel shading.
  38. #if defined(LIGHTMAP_ON)
  39. light.direction = _LightmapDirection;
  40. #else
  41. light.direction = lerp(light.direction, _LightmapDirection, _OverrideLightmapDir);
  42. #endif
  43. half4 c = _BaseColor;
  44. #if defined(_CELPRIMARYMODE_SINGLE)
  45. const half NdotLTPrimary = NdotLTransitionPrimary(inputData.normalWS, light.direction);
  46. c = lerp(_ColorDim, c, NdotLTPrimary);
  47. #endif // _CELPRIMARYMODE_SINGLE
  48. #if defined(_CELPRIMARYMODE_STEPS)
  49. const half NdotLTSteps = NdotLTransitionTexture(inputData.normalWS, light.direction, _CelStepTexture);
  50. c = lerp(_ColorDimSteps, c, NdotLTSteps);
  51. #endif // _CELPRIMARYMODE_STEPS
  52. #if defined(_CELPRIMARYMODE_CURVE)
  53. const half NdotLTCurve = NdotLTransitionTexture(inputData.normalWS, light.direction, _CelCurveTexture);
  54. c = lerp(_ColorDimCurve, c, NdotLTCurve);
  55. #endif // _CELPRIMARYMODE_CURVE
  56. #if defined(DR_CEL_EXTRA_ON)
  57. const half NdotLTExtra = NdotLTransitionExtra(inputData.normalWS, light.direction);
  58. c = lerp(_ColorDimExtra, c, NdotLTExtra);
  59. #endif // DR_CEL_EXTRA_ON
  60. #if defined(DR_GRADIENT_ON)
  61. const float angleRadians = _GradientAngle / 180.0 * PI;
  62. #if defined(_GRADIENTSPACE_WORLD)
  63. const float2 position = inputData.positionWS.xy;
  64. #else
  65. const float2 position = TransformWorldToObject(inputData.positionWS).xy;
  66. #endif
  67. const float posGradRotated = (position.x - _GradientCenterX) * sin(angleRadians) +
  68. (position.y - _GradientCenterY) * cos(angleRadians);
  69. const half gradientFactor = saturate((_GradientSize * 0.5 - posGradRotated) / _GradientSize);
  70. c = lerp(c, _ColorGradient, gradientFactor);
  71. #endif // DR_GRADIENT_ON
  72. const half NdotL = dot(inputData.normalWS, light.direction);
  73. #if defined(DR_RIM_ON)
  74. const float rim = 1.0 - dot(inputData.viewDirectionWS, inputData.normalWS);
  75. const float rimSpread = 1.0 - _FlatRimSize - NdotL * _FlatRimLightAlign;
  76. const float rimEdgeSmooth = _FlatRimEdgeSmoothness;
  77. const float rimTransition = smoothstep(rimSpread - rimEdgeSmooth * 0.5, rimSpread + rimEdgeSmooth * 0.5, rim);
  78. c.rgb = lerp(c.rgb, _FlatRimColor.rgb, rimTransition * _FlatRimColor.a);
  79. #endif // DR_RIM_ON
  80. #if defined(DR_SPECULAR_ON)
  81. // Halfway between lighting direction and view vector.
  82. const float3 halfVector = normalize(light.direction + inputData.viewDirectionWS);
  83. const float NdotH = dot(inputData.normalWS, halfVector) * 0.5 + 0.5;
  84. const float specular = saturate(pow(abs(NdotH), 100.0 * (1.0 - _FlatSpecularSize) * (1.0 - _FlatSpecularSize)));
  85. const float specularTransition = smoothstep(0.5 - _FlatSpecularEdgeSmoothness * 0.1,
  86. 0.5 + _FlatSpecularEdgeSmoothness * 0.1, specular);
  87. c = lerp(c, _FlatSpecularColor, specularTransition);
  88. #endif // DR_SPECULAR_ON
  89. #if defined(_UNITYSHADOW_OCCLUSION)
  90. const float occludedAttenuation = smoothstep(0.25, 0.0, -min(NdotL, 0));
  91. light.shadowAttenuation *= occludedAttenuation;
  92. light.distanceAttenuation *= occludedAttenuation;
  93. #endif
  94. ApplyLightToColor(light, c.rgb);
  95. return c.rgb;
  96. }
  97. void StylizeLight(inout Light light)
  98. {
  99. const half shadowAttenuation = saturate(light.shadowAttenuation * _UnityShadowSharpness);
  100. light.shadowAttenuation = shadowAttenuation;
  101. const float distanceAttenuation = smoothstep(0, _LightFalloffSize + 0.001, light.distanceAttenuation);
  102. light.distanceAttenuation = distanceAttenuation;
  103. /*
  104. #if LIGHTMAP_ON
  105. const half3 lightColor = 0;
  106. #else
  107. */
  108. const half3 lightColor = lerp(half3(1, 1, 1), light.color, _LightContribution);
  109. /*
  110. #endif
  111. */
  112. light.color = lightColor;
  113. }
  114. half4 UniversalFragment_DSTRM(InputData inputData, SurfaceData surfaceData, float2 uv)
  115. {
  116. const half4 shadowMask = CalculateShadowMask(inputData);
  117. #if VERSION_GREATER_EQUAL(10, 0)
  118. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, shadowMask);
  119. #else
  120. Light mainLight = GetMainLight(inputData.shadowCoord);
  121. #endif
  122. #if UNITY_VERSION >= 202220
  123. uint meshRenderingLayers = GetMeshRenderingLayer();
  124. #elif VERSION_GREATER_EQUAL(12, 0)
  125. uint meshRenderingLayers = GetMeshRenderingLightLayer();
  126. #endif
  127. #if LIGHTMAP_ON
  128. mainLight.distanceAttenuation = 1.0;
  129. #endif
  130. StylizeLight(mainLight);
  131. #if defined(_SCREEN_SPACE_OCCLUSION)
  132. AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(inputData.normalizedScreenSpaceUV);
  133. mainLight.color *= aoFactor.directAmbientOcclusion;
  134. inputData.bakedGI *= aoFactor.indirectAmbientOcclusion;
  135. #endif
  136. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, shadowMask);
  137. // Apply Flat Kit stylizing to `inputData.bakedGI` (which is half3).
  138. #if LIGHTMAP_ON
  139. // Apply cel shading. Can also separate modes by `#if defined(_CELPRIMARYMODE_SINGLE)` etc.
  140. // length(inputData.bakedGI) can be replaced with inputData.bakedGI to use light map color more directly.
  141. /*
  142. float lighmapEdgeSize = saturate(_ShadowEdgeSize * 10.0);
  143. inputData.bakedGI = lerp(_ColorDim.rgb, _BaseColor.rgb,
  144. smoothstep(_SelfShadingSize - lighmapEdgeSize, _SelfShadingSize + lighmapEdgeSize, length(inputData.bakedGI)));
  145. // Apply shadow modes
  146. #if defined(_UNITYSHADOWMODE_MULTIPLY)
  147. inputData.bakedGI = lerp(1, inputData.bakedGI, (1 - inputData.bakedGI) * _UnityShadowPower);
  148. #endif
  149. #if defined(_UNITYSHADOWMODE_COLOR)
  150. inputData.bakedGI = lerp(inputData.bakedGI, _UnityShadowColor.rgb, _UnityShadowColor.a * inputData.bakedGI);
  151. #endif
  152. */
  153. #endif
  154. const half4 albedo = half4(surfaceData.albedo + surfaceData.emission, surfaceData.alpha);
  155. const float2 detailUV = TRANSFORM_TEX(uv, _DetailMap);
  156. const half4 detail = SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, detailUV);
  157. #if defined(_BASEMAP_PREMULTIPLY)
  158. const half3 brdf = albedo.rgb;
  159. #else
  160. const half3 brdf = _BaseColor.rgb;
  161. #endif
  162. BRDFData brdfData;
  163. InitializeBRDFData(brdf, 1.0 - 1.0 / kDielectricSpec.a, 0, 0, surfaceData.alpha, brdfData);
  164. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, 1.0, inputData.normalWS, inputData.viewDirectionWS);
  165. #if VERSION_GREATER_EQUAL(12, 0)
  166. #ifdef _LIGHT_LAYERS
  167. if (IsMatchingLightLayer(mainLight.layerMask, meshRenderingLayers))
  168. #endif
  169. #endif
  170. color += LightingPhysicallyBased_DSTRM(mainLight, inputData);
  171. #if defined(_ADDITIONAL_LIGHTS)
  172. uint pixelLightCount = GetAdditionalLightsCount();
  173. #if USE_FORWARD_PLUS
  174. for (uint lightIndex = 0; lightIndex < min(URP_FP_DIRECTIONAL_LIGHTS_COUNT, MAX_VISIBLE_LIGHTS); lightIndex++)
  175. {
  176. FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
  177. Light light = GetAdditionalLight(lightIndex, inputData.positionWS, shadowMask);//, aoFactor);
  178. StylizeLight(light);
  179. #ifdef _LIGHT_LAYERS
  180. if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
  181. #endif
  182. {
  183. color += LightingPhysicallyBased_DSTRM(light, inputData);
  184. }
  185. }
  186. #endif
  187. LIGHT_LOOP_BEGIN(pixelLightCount)
  188. Light light = GetAdditionalLight(lightIndex, inputData.positionWS, shadowMask);//, aoFactor);
  189. StylizeLight(light);
  190. #ifdef _LIGHT_LAYERS
  191. if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
  192. #endif
  193. {
  194. color += LightingPhysicallyBased_DSTRM(light, inputData);
  195. }
  196. LIGHT_LOOP_END
  197. #endif
  198. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  199. color += inputData.vertexLighting * brdfData.diffuse;
  200. #endif
  201. // Base map.
  202. {
  203. #if defined(_TEXTUREBLENDINGMODE_ADD)
  204. color += lerp(half3(0.0f, 0.0f, 0.0f), albedo.rgb, _TextureImpact);
  205. #else // _TEXTUREBLENDINGMODE_MULTIPLY
  206. color *= lerp(half3(1.0f, 1.0f, 1.0f), albedo.rgb, _TextureImpact);
  207. #endif
  208. }
  209. // Detail map.
  210. {
  211. #if defined(_DETAILMAPBLENDINGMODE_ADD)
  212. color += lerp(0, _DetailMapColor.rgb, detail.rgb * _DetailMapImpact).rgb;
  213. #endif
  214. #if defined(_DETAILMAPBLENDINGMODE_MULTIPLY)
  215. // color *= lerp(1, _DetailMapColor.rgb, detail.rgb * _DetailMapImpact).rgb;
  216. color *= lerp(1, detail.rgb * _DetailMapColor.rgb, _DetailMapImpact).rgb;
  217. #endif
  218. #if defined(_DETAILMAPBLENDINGMODE_INTERPOLATE)
  219. color = lerp(color, detail.rgb, _DetailMapImpact * _DetailMapColor.rgb * detail.a).rgb;
  220. #endif
  221. }
  222. color += surfaceData.emission;
  223. #ifdef _DBUFFER
  224. // Modified `DBuffer.hlsl` function `ApplyDecalToBaseColor` to use light attenuation.
  225. FETCH_DBUFFER(DBuffer, _DBufferTexture, int2(inputData.positionCS.xy));
  226. DecalSurfaceData decalSurfaceData;
  227. DECODE_FROM_DBUFFER(DBuffer, decalSurfaceData);
  228. half3 decalColor = decalSurfaceData.baseColor.xyz;
  229. ApplyLightToColor(mainLight, decalColor);
  230. color.xyz = color.xyz * decalSurfaceData.baseColor.w + decalColor;
  231. #endif
  232. return half4(color, surfaceData.alpha);
  233. }
  234. #endif // FLATKIT_LIGHTING_DR_INCLUDED