lightFragment.fx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #ifdef LIGHT{X}
  2. #if defined(SHADOWONLY) || defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X}) && defined(LIGHTMAPNOSPECULAR{X})
  3. //No light calculation
  4. #else
  5. #ifdef PBR
  6. // Compute Pre Lighting infos
  7. #ifdef SPOTLIGHT{X}
  8. preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
  9. #elif defined(POINTLIGHT{X})
  10. preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
  11. #elif defined(HEMILIGHT{X})
  12. preInfo = computeHemisphericPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
  13. #elif defined(DIRLIGHT{X})
  14. preInfo = computeDirectionalPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
  15. #endif
  16. preInfo.NdotV = NdotV;
  17. // Compute Attenuation infos
  18. #ifdef SPOTLIGHT{X}
  19. #ifdef LIGHT_FALLOFF_GLTF{X}
  20. preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
  21. preInfo.attenuation *= computeDirectionalLightFalloff_GLTF(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
  22. #elif defined(LIGHT_FALLOFF_PHYSICAL{X})
  23. preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
  24. preInfo.attenuation *= computeDirectionalLightFalloff_Physical(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w);
  25. #elif defined(LIGHT_FALLOFF_STANDARD{X})
  26. preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
  27. preInfo.attenuation *= computeDirectionalLightFalloff_Standard(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w);
  28. #else
  29. preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
  30. preInfo.attenuation *= computeDirectionalLightFalloff(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
  31. #endif
  32. #elif defined(POINTLIGHT{X})
  33. #ifdef LIGHT_FALLOFF_GLTF{X}
  34. preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
  35. #elif defined(LIGHT_FALLOFF_PHYSICAL{X})
  36. preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
  37. #elif defined(LIGHT_FALLOFF_STANDARD{X})
  38. preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
  39. #else
  40. preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
  41. #endif
  42. #else
  43. preInfo.attenuation = 1.0;
  44. #endif
  45. // Simulates Light radius for diffuse and spec term
  46. // clear coat is using a dedicated roughness
  47. #ifdef HEMILIGHT{X}
  48. preInfo.roughness = roughness;
  49. #else
  50. preInfo.roughness = adjustRoughnessFromLightProperties(roughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
  51. #endif
  52. // Diffuse contribution
  53. #ifdef HEMILIGHT{X}
  54. info.diffuse = computeHemisphericDiffuseLighting(preInfo, light{X}.vLightDiffuse.rgb, light{X}.vLightGround);
  55. #elif defined(SS_TRANSLUCENCY)
  56. info.diffuse = computeDiffuseAndTransmittedLighting(preInfo, light{X}.vLightDiffuse.rgb, transmittance);
  57. #else
  58. info.diffuse = computeDiffuseLighting(preInfo, light{X}.vLightDiffuse.rgb);
  59. #endif
  60. // Specular contribution
  61. #ifdef SPECULARTERM
  62. #ifdef ANISOTROPIC
  63. info.specular = computeAnisotropicSpecularLighting(preInfo, viewDirectionW, normalW, anisotropicTangent, anisotropicBitangent, anisotropy, specularEnvironmentR0, specularEnvironmentR90, AARoughnessFactors.x, light{X}.vLightDiffuse.rgb);
  64. #else
  65. info.specular = computeSpecularLighting(preInfo, normalW, specularEnvironmentR0, specularEnvironmentR90, AARoughnessFactors.x, light{X}.vLightDiffuse.rgb);
  66. #endif
  67. #endif
  68. // Sheen contribution
  69. #ifdef SHEEN
  70. #ifdef SHEEN_LINKWITHALBEDO
  71. // BE Carefull: Sheen intensity is replacing the roughness value.
  72. preInfo.roughness = sheenIntensity;
  73. #endif
  74. info.sheen = computeSheenLighting(preInfo, normalW, sheenColor, specularEnvironmentR90, AARoughnessFactors.x, light{X}.vLightDiffuse.rgb);
  75. #endif
  76. // Clear Coat contribution
  77. #ifdef CLEARCOAT
  78. // Simulates Light radius
  79. #ifdef HEMILIGHT{X}
  80. preInfo.roughness = clearCoatRoughness;
  81. #else
  82. preInfo.roughness = adjustRoughnessFromLightProperties(clearCoatRoughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
  83. #endif
  84. info.clearCoat = computeClearCoatLighting(preInfo, clearCoatNormalW, clearCoatAARoughnessFactors.x, clearCoatIntensity, light{X}.vLightDiffuse.rgb);
  85. #ifdef CLEARCOAT_TINT
  86. // Absorption
  87. absorption = computeClearCoatLightingAbsorption(clearCoatNdotVRefract, preInfo.L, clearCoatNormalW, clearCoatColor, clearCoatThickness, clearCoatIntensity);
  88. info.diffuse *= absorption;
  89. #ifdef SPECULARTERM
  90. info.specular *= absorption;
  91. #endif
  92. #endif
  93. // Apply energy conservation on diffuse and specular term.
  94. info.diffuse *= info.clearCoat.w;
  95. #ifdef SPECULARTERM
  96. info.specular *= info.clearCoat.w;
  97. #endif
  98. #ifdef SHEEN
  99. info.sheen *= info.clearCoat.w;
  100. #endif
  101. #endif
  102. #else
  103. #ifdef SPOTLIGHT{X}
  104. info = computeSpotLighting(viewDirectionW, normalW, light{X}.vLightData, light{X}.vLightDirection, light{X}.vLightDiffuse.rgb, light{X}.vLightSpecular.rgb, light{X}.vLightDiffuse.a, glossiness);
  105. #elif defined(HEMILIGHT{X})
  106. info = computeHemisphericLighting(viewDirectionW, normalW, light{X}.vLightData, light{X}.vLightDiffuse.rgb, light{X}.vLightSpecular.rgb, light{X}.vLightGround, glossiness);
  107. #elif defined(POINTLIGHT{X}) || defined(DIRLIGHT{X})
  108. info = computeLighting(viewDirectionW, normalW, light{X}.vLightData, light{X}.vLightDiffuse.rgb, light{X}.vLightSpecular.rgb, light{X}.vLightDiffuse.a, glossiness);
  109. #endif
  110. #endif
  111. #ifdef PROJECTEDLIGHTTEXTURE{X}
  112. info.diffuse *= computeProjectionTextureDiffuseLighting(projectionLightSampler{X}, textureProjectionMatrix{X});
  113. #endif
  114. #endif
  115. #ifdef SHADOW{X}
  116. #ifdef SHADOWCSM{X}
  117. for (int i = 0; i < SHADOWCSMNUM_CASCADES{X}; i++)
  118. {
  119. #ifdef SHADOWCSM_RIGHTHANDED{X}
  120. diff{X} = viewFrustumZ{X}[i] + vPositionFromCamera{X}.z;
  121. #else
  122. diff{X} = viewFrustumZ{X}[i] - vPositionFromCamera{X}.z;
  123. #endif
  124. if (diff{X} >= 0.) {
  125. index{X} = i;
  126. break;
  127. }
  128. }
  129. #ifdef SHADOWCSMUSESHADOWMAXZ{X}
  130. if (index{X} >= 0)
  131. #endif
  132. {
  133. #if defined(SHADOWPCF{X})
  134. #if defined(SHADOWLOWQUALITY{X})
  135. shadow = computeShadowWithCSMPCF1(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  136. #elif defined(SHADOWMEDIUMQUALITY{X})
  137. shadow = computeShadowWithCSMPCF3(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  138. #else
  139. shadow = computeShadowWithCSMPCF5(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  140. #endif
  141. #elif defined(SHADOWPCSS{X})
  142. #if defined(SHADOWLOWQUALITY{X})
  143. shadow = computeShadowWithCSMPCSS16(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  144. #elif defined(SHADOWMEDIUMQUALITY{X})
  145. shadow = computeShadowWithCSMPCSS32(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  146. #else
  147. shadow = computeShadowWithCSMPCSS64(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  148. #endif
  149. #else
  150. shadow = computeShadowCSM(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  151. #endif
  152. #ifdef SHADOWCSMDEBUG{X}
  153. shadowDebug{X} = vec3(shadow) * vCascadeColorsMultiplier{X}[index{X}];
  154. #endif
  155. #ifndef SHADOWCSMNOBLEND{X}
  156. float frustumLength = frustumLengths{X}[index{X}];
  157. float diffRatio = clamp(diff{X} / frustumLength, 0., 1.) * cascadeBlendFactor{X};
  158. if (index{X} < (SHADOWCSMNUM_CASCADES{X} - 1) && diffRatio < 1.)
  159. {
  160. index{X} += 1;
  161. float nextShadow = 0.;
  162. #if defined(SHADOWPCF{X})
  163. #if defined(SHADOWLOWQUALITY{X})
  164. nextShadow = computeShadowWithCSMPCF1(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  165. #elif defined(SHADOWMEDIUMQUALITY{X})
  166. nextShadow = computeShadowWithCSMPCF3(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  167. #else
  168. nextShadow = computeShadowWithCSMPCF5(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  169. #endif
  170. #elif defined(SHADOWPCSS{X})
  171. #if defined(SHADOWLOWQUALITY{X})
  172. nextShadow = computeShadowWithCSMPCSS16(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  173. #elif defined(SHADOWMEDIUMQUALITY{X})
  174. nextShadow = computeShadowWithCSMPCSS32(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  175. #else
  176. nextShadow = computeShadowWithCSMPCSS64(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w, lightSizeUVCorrection{X}[index{X}], depthCorrection{X}[index{X}], penumbraDarkness{X});
  177. #endif
  178. #else
  179. nextShadow = computeShadowCSM(float(index{X}), vPositionFromLight{X}[index{X}], vDepthMetric{X}[index{X}], shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  180. #endif
  181. shadow = mix(nextShadow, shadow, diffRatio);
  182. #ifdef SHADOWCSMDEBUG{X}
  183. shadowDebug{X} = mix(vec3(nextShadow) * vCascadeColorsMultiplier{X}[index{X}], shadowDebug{X}, diffRatio);
  184. #endif
  185. }
  186. #endif
  187. }
  188. #elif defined(SHADOWCLOSEESM{X})
  189. #if defined(SHADOWCUBE{X})
  190. shadow = computeShadowWithCloseESMCube(light{X}.vLightData.xyz, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.z, light{X}.depthValues);
  191. #else
  192. shadow = computeShadowWithCloseESM(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.z, light{X}.shadowsInfo.w);
  193. #endif
  194. #elif defined(SHADOWESM{X})
  195. #if defined(SHADOWCUBE{X})
  196. shadow = computeShadowWithESMCube(light{X}.vLightData.xyz, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.z, light{X}.depthValues);
  197. #else
  198. shadow = computeShadowWithESM(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.z, light{X}.shadowsInfo.w);
  199. #endif
  200. #elif defined(SHADOWPOISSON{X})
  201. #if defined(SHADOWCUBE{X})
  202. shadow = computeShadowWithPoissonSamplingCube(light{X}.vLightData.xyz, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.x, light{X}.depthValues);
  203. #else
  204. shadow = computeShadowWithPoissonSampling(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  205. #endif
  206. #elif defined(SHADOWPCF{X})
  207. #if defined(SHADOWLOWQUALITY{X})
  208. shadow = computeShadowWithPCF1(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  209. #elif defined(SHADOWMEDIUMQUALITY{X})
  210. shadow = computeShadowWithPCF3(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  211. #else
  212. shadow = computeShadowWithPCF5(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.yz, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  213. #endif
  214. #elif defined(SHADOWPCSS{X})
  215. #if defined(SHADOWLOWQUALITY{X})
  216. shadow = computeShadowWithPCSS16(vPositionFromLight{X}, vDepthMetric{X}, depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  217. #elif defined(SHADOWMEDIUMQUALITY{X})
  218. shadow = computeShadowWithPCSS32(vPositionFromLight{X}, vDepthMetric{X}, depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  219. #else
  220. shadow = computeShadowWithPCSS64(vPositionFromLight{X}, vDepthMetric{X}, depthSampler{X}, shadowSampler{X}, light{X}.shadowsInfo.y, light{X}.shadowsInfo.z, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  221. #endif
  222. #else
  223. #if defined(SHADOWCUBE{X})
  224. shadow = computeShadowCube(light{X}.vLightData.xyz, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.depthValues);
  225. #else
  226. shadow = computeShadow(vPositionFromLight{X}, vDepthMetric{X}, shadowSampler{X}, light{X}.shadowsInfo.x, light{X}.shadowsInfo.w);
  227. #endif
  228. #endif
  229. #ifdef SHADOWONLY
  230. #ifndef SHADOWINUSE
  231. #define SHADOWINUSE
  232. #endif
  233. globalShadow += shadow;
  234. shadowLightCount += 1.0;
  235. #endif
  236. #else
  237. shadow = 1.;
  238. #endif
  239. #ifndef SHADOWONLY
  240. #ifdef CUSTOMUSERLIGHTING
  241. diffuseBase += computeCustomDiffuseLighting(info, diffuseBase, shadow);
  242. #ifdef SPECULARTERM
  243. specularBase += computeCustomSpecularLighting(info, specularBase, shadow);
  244. #endif
  245. #elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X})
  246. diffuseBase += lightmapColor.rgb * shadow;
  247. #ifdef SPECULARTERM
  248. #ifndef LIGHTMAPNOSPECULAR{X}
  249. specularBase += info.specular * shadow * lightmapColor.rgb;
  250. #endif
  251. #endif
  252. #ifdef CLEARCOAT
  253. #ifndef LIGHTMAPNOSPECULAR{X}
  254. clearCoatBase += info.clearCoat.rgb * shadow * lightmapColor.rgb;
  255. #endif
  256. #endif
  257. #ifdef SHEEN
  258. #ifndef LIGHTMAPNOSPECULAR{X}
  259. sheenBase += info.sheen.rgb * shadow;
  260. #endif
  261. #endif
  262. #else
  263. #ifdef SHADOWCSMDEBUG{X}
  264. diffuseBase += info.diffuse * shadowDebug{X};
  265. #else
  266. diffuseBase += info.diffuse * shadow;
  267. #endif
  268. #ifdef SPECULARTERM
  269. specularBase += info.specular * shadow;
  270. #endif
  271. #ifdef CLEARCOAT
  272. clearCoatBase += info.clearCoat.rgb * shadow;
  273. #endif
  274. #ifdef SHEEN
  275. sheenBase += info.sheen.rgb * shadow;
  276. #endif
  277. #endif
  278. #endif
  279. #endif