Lux_Lighting_Toon.hlsl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #if !defined(SHADERGRAPH_PREVIEW) || ( defined(LIGHTWEIGHT_LIGHTING_INCLUDED) || defined(UNIVERSAL_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. #if defined(LIGHTWEIGHT_LIGHTING_INCLUDED) || defined(UNIVERSAL_LIGHTING_INCLUDED)
  8. half3 LightingSpecular_Toon (Light light, half lightingRemap, half3 normalWS, half3 viewDirectionWS, half3 specular, half specularSmoothness, half smoothness, half specularStep, half specularUpper){
  9. half3 halfVec = SafeNormalize(light.direction + viewDirectionWS);
  10. half NdotH = saturate(dot(normalWS, halfVec));
  11. half modifier = pow(NdotH /* lightingRemap*/, specularSmoothness);
  12. // Normalization? Na, we just multiply by smoothness in the return statement.
  13. // #define ONEOVERTWOPI 0.159155h
  14. // half normalization = (specularSmoothness + 1) * ONEOVERTWOPI;
  15. // Sharpen / CHECK: This deforms the highlight?!
  16. half modifierSharpened = smoothstep(specularStep, specularUpper, modifier);
  17. return light.color * specular * modifierSharpened * smoothness;
  18. }
  19. #endif
  20. #endif
  21. void Lighting_half(
  22. // Base inputs
  23. float3 positionWS,
  24. half3 viewDirectionWS,
  25. // Normal inputs
  26. half3 normalWS,
  27. half3 tangentWS,
  28. half3 bitangentWS,
  29. bool enableNormalMapping,
  30. half3 normalTS,
  31. // Surface description
  32. half3 albedo,
  33. half3 shadedAlbedo,
  34. bool enableSpecular,
  35. half3 specular,
  36. half smoothness,
  37. half occlusion,
  38. // Smoothsteps
  39. half diffuseStep,
  40. half diffuseFalloff,
  41. half specularStep,
  42. half specularFalloff,
  43. half shadowFalloff,
  44. half shadowBiasDirectional,
  45. half shadowBiasAdditional,
  46. // Colorize shaded parts
  47. bool colorizeMainLight,
  48. bool colorizeAddLights,
  49. // Rim Lighting
  50. bool enableRimLighting,
  51. half rimPower,
  52. half rimFalloff,
  53. half4 rimColor,
  54. half rimAttenuation,
  55. // Lightmapping
  56. float2 lightMapUV,
  57. bool receiveSSAO,
  58. // Final lit color
  59. out half3 Lighting,
  60. out half3 MetaAlbedo,
  61. out half3 MetaSpecular
  62. )
  63. {
  64. #if defined(SHADERGRAPH_PREVIEW) || ( !defined(LIGHTWEIGHT_LIGHTING_INCLUDED) && !defined(UNIVERSAL_LIGHTING_INCLUDED) )
  65. Lighting = albedo;
  66. MetaAlbedo = half3(0,0,0);
  67. MetaSpecular = half3(0,0,0);
  68. #else
  69. // Real Lighting ----------
  70. half3 tnormal = normalWS;
  71. if (enableNormalMapping) {
  72. tnormal = TransformTangentToWorld(normalTS, half3x3(tangentWS.xyz, bitangentWS.xyz, normalWS.xyz));
  73. }
  74. normalWS = NormalizeNormalPerPixel(tnormal);
  75. viewDirectionWS = SafeNormalize(viewDirectionWS);
  76. // Remap values
  77. half diffuseUpper = saturate(diffuseStep + diffuseFalloff);
  78. //rimFalloff *= 20.0h;
  79. float4 clipPos = TransformWorldToHClip(positionWS);
  80. // Get Shadow Sampling Coords / Unfortunately per pixel...
  81. #if SHADOWS_SCREEN
  82. float4 shadowCoord = ComputeScreenPos(clipPos);
  83. #else
  84. float4 shadowCoord = TransformWorldToShadowCoord(positionWS);
  85. #endif
  86. // Shadow mask
  87. #if defined(SHADOWS_SHADOWMASK) && defined(LIGHTMAP_ON)
  88. half4 shadowMask = SAMPLE_SHADOWMASK(lightMapUV);
  89. #elif !defined (LIGHTMAP_ON)
  90. half4 shadowMask = unity_ProbesOcclusion;
  91. #else
  92. half4 shadowMask = half4(1, 1, 1, 1);
  93. #endif
  94. //Light mainLight = GetMainLight(shadowCoord);
  95. Light mainLight = GetMainLight(shadowCoord, positionWS, shadowMask);
  96. // SSAO
  97. #if defined(_SCREEN_SPACE_OCCLUSION)
  98. AmbientOcclusionFactor aoFactor;
  99. aoFactor.indirectAmbientOcclusion = 1;
  100. aoFactor.directAmbientOcclusion = 1;
  101. if(receiveSSAO) {
  102. float4 ndc = clipPos * 0.5f;
  103. float2 normalized = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  104. normalized /= clipPos.w;
  105. normalized *= _ScreenParams.xy;
  106. // We could also use IN.Screenpos(default) --> ( IN.Screenpos.xy * _ScreenParams.xy)
  107. // HDRP 10.1
  108. normalized = GetNormalizedScreenSpaceUV(normalized);
  109. aoFactor = GetScreenSpaceAmbientOcclusion(normalized);
  110. mainLight.color *= aoFactor.directAmbientOcclusion;
  111. occlusion = min(occlusion, aoFactor.indirectAmbientOcclusion);
  112. occlusion = smoothstep(diffuseStep, diffuseUpper, occlusion);
  113. }
  114. #endif
  115. // GI Lighting
  116. half3 bakedGI;
  117. #ifdef LIGHTMAP_ON
  118. lightMapUV = lightMapUV * unity_LightmapST.xy + unity_LightmapST.zw;
  119. bakedGI = SAMPLE_GI(lightMapUV, half3(0,0,0), normalWS);
  120. #else
  121. // CHECK: Do we have3 to multiply SH with occlusion here?
  122. bakedGI = SampleSH(normalWS) * occlusion;
  123. #endif
  124. mainLight.shadowAttenuation = smoothstep(0.0h, shadowFalloff, mainLight.shadowAttenuation);
  125. MixRealtimeAndBakedGI(mainLight, normalWS, bakedGI, half4(0, 0, 0, 0));
  126. // Set up Lighting
  127. half lightIntensity = 0;
  128. half3 specularLighting = 0;
  129. half3 rimLighting = 0;
  130. half3 lightColor = 0;
  131. half luminance;
  132. // Main Light
  133. half NdotL = saturate(dot(normalWS, mainLight.direction));
  134. NdotL = smoothstep(diffuseStep, diffuseUpper, NdotL);
  135. half atten = NdotL * mainLight.distanceAttenuation * saturate(shadowBiasDirectional + mainLight.shadowAttenuation);
  136. if (colorizeMainLight) {
  137. lightColor = mainLight.color * mainLight.distanceAttenuation;
  138. }
  139. else {
  140. lightColor = mainLight.color * atten;
  141. }
  142. luminance = Luminance(mainLight.color);
  143. lightIntensity += luminance * atten;
  144. // Specular
  145. half specularSmoothness;
  146. half3 spec;
  147. half specularUpper;
  148. if (enableSpecular) {
  149. specularSmoothness = exp2(10 * smoothness + 1);
  150. specularUpper = saturate(specularStep + specularFalloff * (1.0h + smoothness));
  151. spec = LightingSpecular_Toon(mainLight, NdotL, normalWS, viewDirectionWS, specular, specularSmoothness, smoothness, specularStep, specularUpper);
  152. specularLighting = spec * atten;
  153. }
  154. // Rim Lighting
  155. if (enableRimLighting) {
  156. half rim = saturate(1.0h - saturate( dot(normalWS, viewDirectionWS)) );
  157. //rimLighting = smoothstep(rimPower, rimPower + rimFalloff, rim) * rimColor.rgb;
  158. // Stabilize rim
  159. float delta = fwidth(rim);
  160. rimLighting = smoothstep(rimPower - delta, rimPower + rimFalloff + delta, rim) * rimColor.rgb;
  161. }
  162. // Handle additional lights
  163. #ifdef _ADDITIONAL_LIGHTS
  164. uint pixelLightCount = GetAdditionalLightsCount();
  165. for (uint i = 0u; i < pixelLightCount; ++i) {
  166. // Light light = GetAdditionalPerObjectLight(index, positionWS); // here; shadowAttenuation = 1.0;
  167. // URP 10: We have to use the new GetAdditionalLight function
  168. Light light = GetAdditionalLight(i, positionWS, shadowMask);
  169. #if defined(_SCREEN_SPACE_OCCLUSION)
  170. if(receiveSSAO) {
  171. light.color *= aoFactor.directAmbientOcclusion;
  172. }
  173. #endif
  174. light.shadowAttenuation = smoothstep(0.0h, shadowFalloff, light.shadowAttenuation);
  175. NdotL = saturate(dot(normalWS, light.direction));
  176. NdotL = smoothstep(diffuseStep, diffuseUpper, NdotL);
  177. atten = NdotL * light.distanceAttenuation * saturate(shadowBiasAdditional + light.shadowAttenuation);
  178. if(colorizeAddLights) {
  179. lightColor += light.color * light.distanceAttenuation;
  180. }
  181. else {
  182. lightColor += light.color * atten;
  183. }
  184. luminance = Luminance(light.color); //dot(light.color, half3(0.2126729h, 0.7151522h, 0.0721750h) );
  185. lightIntensity += luminance * atten;
  186. if (enableSpecular) {
  187. spec = LightingSpecular_Toon(light, NdotL, normalWS, viewDirectionWS, specular, specularSmoothness, smoothness, specularStep, specularUpper);
  188. specularLighting += spec * atten;
  189. }
  190. }
  191. #endif
  192. // Combine Lighting
  193. half3 litAlbedo = lerp(shadedAlbedo, albedo, saturate(lightIntensity.xxx) );
  194. Lighting =
  195. // ambient diffuse lighting
  196. //bakedGI * litAlbedo // <---------- was wrong!
  197. bakedGI * albedo
  198. // direct diffuse lighting
  199. + litAlbedo * lightColor
  200. + (specularLighting * lightIntensity
  201. + rimLighting * lerp(1.0h, lightIntensity, rimAttenuation) )
  202. * lightColor
  203. ;
  204. // Set Albedo for meta pass
  205. #if defined(LIGHTWEIGHT_META_PASS_INCLUDED) || defined(UNIVERSAL_META_PASS_INCLUDED)
  206. Lighting = half3(0,0,0);
  207. MetaAlbedo = albedo;
  208. MetaSpecular = half3(0.02,0.02,0.02);
  209. #else
  210. MetaAlbedo = half3(0,0,0);
  211. MetaSpecular = half3(0,0,0);
  212. #endif
  213. // End Real Lighting ----------
  214. #endif
  215. }
  216. // Unity 2019.1. needs a float version
  217. void Lighting_float(
  218. // Base inputs
  219. float3 positionWS,
  220. half3 viewDirectionWS,
  221. // Normal inputs
  222. half3 normalWS,
  223. half3 tangentWS,
  224. half3 bitangentWS,
  225. bool enableNormalMapping,
  226. half3 normalTS,
  227. // Surface description
  228. half3 albedo,
  229. half3 shadedAlbedo,
  230. bool enableSpecular,
  231. half3 specular,
  232. half smoothness,
  233. half occlusion,
  234. // Smoothsteps
  235. half diffuseStep,
  236. half diffuseFalloff,
  237. half specularStep,
  238. half specularFalloff,
  239. half shadowFalloff,
  240. half shadowBiasDirectional,
  241. half shadowBiasAdditional,
  242. // Colorize shaded parts
  243. bool colorizeMainLight,
  244. bool colorizeAddLights,
  245. // Rim Lighting
  246. bool enableRimLighting,
  247. half rimPower,
  248. half rimFalloff,
  249. half4 rimColor,
  250. half rimAttenuation,
  251. // Lightmapping
  252. float2 lightMapUV,
  253. bool receiveSSAO,
  254. // Final lit color
  255. out half3 Lighting,
  256. out half3 MetaAlbedo,
  257. out half3 MetaSpecular
  258. )
  259. {
  260. Lighting_half(
  261. positionWS, viewDirectionWS, normalWS, tangentWS, bitangentWS, enableNormalMapping, normalTS,
  262. albedo, shadedAlbedo, enableSpecular, specular, smoothness, occlusion,
  263. diffuseStep, diffuseFalloff, specularStep, specularFalloff, shadowFalloff, shadowBiasDirectional, shadowBiasAdditional, colorizeMainLight, colorizeAddLights,
  264. enableRimLighting, rimPower, rimFalloff, rimColor, rimAttenuation,
  265. lightMapUV, receiveSSAO,
  266. Lighting, MetaAlbedo, MetaSpecular
  267. );
  268. }