CSMShader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { ShaderChunk } from 'three';
  2. /**
  3. * @module CSMShader
  4. * @three_import import { CSMShader } from 'three/addons/csm/CSMShader.js';
  5. */
  6. /**
  7. * The object that holds the GLSL enhancements to enable CSM. This
  8. * code is injected into the built-in material shaders by {@link CSM}.
  9. *
  10. * @type {Object}
  11. */
  12. const CSMShader = {
  13. lights_fragment_begin: /* glsl */`
  14. vec3 geometryPosition = - vViewPosition;
  15. vec3 geometryNormal = normal;
  16. vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
  17. vec3 geometryClearcoatNormal = vec3( 0.0 );
  18. #ifdef USE_CLEARCOAT
  19. geometryClearcoatNormal = clearcoatNormal;
  20. #endif
  21. #ifdef USE_IRIDESCENCE
  22. float dotNVi = saturate( dot( normal, geometryViewDir ) );
  23. if ( material.iridescenceThickness == 0.0 ) {
  24. material.iridescence = 0.0;
  25. } else {
  26. material.iridescence = saturate( material.iridescence );
  27. }
  28. if ( material.iridescence > 0.0 ) {
  29. material.iridescenceFresnel = evalIridescence( 1.0, material.iridescenceIOR, dotNVi, material.iridescenceThickness, material.specularColor );
  30. // Iridescence F0 approximation
  31. material.iridescenceF0 = Schlick_to_F0( material.iridescenceFresnel, 1.0, dotNVi );
  32. }
  33. #endif
  34. IncidentLight directLight;
  35. #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
  36. PointLight pointLight;
  37. #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
  38. PointLightShadow pointLightShadow;
  39. #endif
  40. #pragma unroll_loop_start
  41. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  42. pointLight = pointLights[ i ];
  43. getPointLightInfo( pointLight, geometryPosition, directLight );
  44. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
  45. pointLightShadow = pointLightShadows[ i ];
  46. directLight.color *= ( directLight.visible && receiveShadow ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowIntensity, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
  47. #endif
  48. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  49. }
  50. #pragma unroll_loop_end
  51. #endif
  52. #if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
  53. SpotLight spotLight;
  54. vec4 spotColor;
  55. vec3 spotLightCoord;
  56. bool inSpotLightMap;
  57. #if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
  58. SpotLightShadow spotLightShadow;
  59. #endif
  60. #pragma unroll_loop_start
  61. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  62. spotLight = spotLights[ i ];
  63. getSpotLightInfo( spotLight, geometryPosition, directLight );
  64. // spot lights are ordered [shadows with maps, shadows without maps, maps without shadows, none]
  65. #if ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS )
  66. #define SPOT_LIGHT_MAP_INDEX UNROLLED_LOOP_INDEX
  67. #elif ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
  68. #define SPOT_LIGHT_MAP_INDEX NUM_SPOT_LIGHT_MAPS
  69. #else
  70. #define SPOT_LIGHT_MAP_INDEX ( UNROLLED_LOOP_INDEX - NUM_SPOT_LIGHT_SHADOWS + NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS )
  71. #endif
  72. #if ( SPOT_LIGHT_MAP_INDEX < NUM_SPOT_LIGHT_MAPS )
  73. spotLightCoord = vSpotLightCoord[ i ].xyz / vSpotLightCoord[ i ].w;
  74. inSpotLightMap = all( lessThan( abs( spotLightCoord * 2. - 1. ), vec3( 1.0 ) ) );
  75. spotColor = texture2D( spotLightMap[ SPOT_LIGHT_MAP_INDEX ], spotLightCoord.xy );
  76. directLight.color = inSpotLightMap ? directLight.color * spotColor.rgb : directLight.color;
  77. #endif
  78. #undef SPOT_LIGHT_MAP_INDEX
  79. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
  80. spotLightShadow = spotLightShadows[ i ];
  81. directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowIntensity, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotLightCoord[ i ] ) : 1.0;
  82. #endif
  83. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  84. }
  85. #pragma unroll_loop_end
  86. #endif
  87. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && defined( USE_CSM ) && defined( CSM_CASCADES )
  88. DirectionalLight directionalLight;
  89. float linearDepth = (vViewPosition.z) / (shadowFar - cameraNear);
  90. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  91. DirectionalLightShadow directionalLightShadow;
  92. #endif
  93. #if defined( USE_SHADOWMAP ) && defined( CSM_FADE )
  94. vec2 cascade;
  95. float cascadeCenter;
  96. float closestEdge;
  97. float margin;
  98. float csmx;
  99. float csmy;
  100. #pragma unroll_loop_start
  101. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  102. directionalLight = directionalLights[ i ];
  103. getDirectionalLightInfo( directionalLight, directLight );
  104. #if ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  105. // NOTE: Depth gets larger away from the camera.
  106. // cascade.x is closer, cascade.y is further
  107. cascade = CSM_cascades[ i ];
  108. cascadeCenter = ( cascade.x + cascade.y ) / 2.0;
  109. closestEdge = linearDepth < cascadeCenter ? cascade.x : cascade.y;
  110. margin = 0.25 * pow( closestEdge, 2.0 );
  111. csmx = cascade.x - margin / 2.0;
  112. csmy = cascade.y + margin / 2.0;
  113. if( linearDepth >= csmx && ( linearDepth < csmy || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 ) ) {
  114. float dist = min( linearDepth - csmx, csmy - linearDepth );
  115. float ratio = clamp( dist / margin, 0.0, 1.0 );
  116. vec3 prevColor = directLight.color;
  117. directionalLightShadow = directionalLightShadows[ i ];
  118. directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  119. bool shouldFadeLastCascade = UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth > cascadeCenter;
  120. directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );
  121. ReflectedLight prevLight = reflectedLight;
  122. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  123. bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
  124. float blendRatio = shouldBlend ? ratio : 1.0;
  125. reflectedLight.directDiffuse = mix( prevLight.directDiffuse, reflectedLight.directDiffuse, blendRatio );
  126. reflectedLight.directSpecular = mix( prevLight.directSpecular, reflectedLight.directSpecular, blendRatio );
  127. reflectedLight.indirectDiffuse = mix( prevLight.indirectDiffuse, reflectedLight.indirectDiffuse, blendRatio );
  128. reflectedLight.indirectSpecular = mix( prevLight.indirectSpecular, reflectedLight.indirectSpecular, blendRatio );
  129. }
  130. #endif
  131. }
  132. #pragma unroll_loop_end
  133. #elif defined (USE_SHADOWMAP)
  134. #pragma unroll_loop_start
  135. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  136. directionalLight = directionalLights[ i ];
  137. getDirectionalLightInfo( directionalLight, directLight );
  138. #if ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  139. directionalLightShadow = directionalLightShadows[ i ];
  140. if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y) directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  141. if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  142. #endif
  143. }
  144. #pragma unroll_loop_end
  145. #elif ( NUM_DIR_LIGHT_SHADOWS > 0 )
  146. // note: no loop here - all CSM lights are in fact one light only
  147. getDirectionalLightInfo( directionalLights[0], directLight );
  148. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  149. #endif
  150. #if ( NUM_DIR_LIGHTS > NUM_DIR_LIGHT_SHADOWS)
  151. // compute the lights not casting shadows (if any)
  152. #pragma unroll_loop_start
  153. for ( int i = NUM_DIR_LIGHT_SHADOWS; i < NUM_DIR_LIGHTS; i ++ ) {
  154. directionalLight = directionalLights[ i ];
  155. getDirectionalLightInfo( directionalLight, directLight );
  156. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  157. }
  158. #pragma unroll_loop_end
  159. #endif
  160. #endif
  161. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && !defined( USE_CSM ) && !defined( CSM_CASCADES )
  162. DirectionalLight directionalLight;
  163. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  164. DirectionalLightShadow directionalLightShadow;
  165. #endif
  166. #pragma unroll_loop_start
  167. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  168. directionalLight = directionalLights[ i ];
  169. getDirectionalLightInfo( directionalLight, directLight );
  170. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  171. directionalLightShadow = directionalLightShadows[ i ];
  172. directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  173. #endif
  174. RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  175. }
  176. #pragma unroll_loop_end
  177. #endif
  178. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  179. RectAreaLight rectAreaLight;
  180. #pragma unroll_loop_start
  181. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  182. rectAreaLight = rectAreaLights[ i ];
  183. RE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
  184. }
  185. #pragma unroll_loop_end
  186. #endif
  187. #if defined( RE_IndirectDiffuse )
  188. vec3 iblIrradiance = vec3( 0.0 );
  189. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  190. #if defined( USE_LIGHT_PROBES )
  191. irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );
  192. #endif
  193. #if ( NUM_HEMI_LIGHTS > 0 )
  194. #pragma unroll_loop_start
  195. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  196. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );
  197. }
  198. #pragma unroll_loop_end
  199. #endif
  200. #endif
  201. #if defined( RE_IndirectSpecular )
  202. vec3 radiance = vec3( 0.0 );
  203. vec3 clearcoatRadiance = vec3( 0.0 );
  204. #endif
  205. `,
  206. lights_pars_begin: /* glsl */`
  207. #if defined( USE_CSM ) && defined( CSM_CASCADES )
  208. uniform vec2 CSM_cascades[CSM_CASCADES];
  209. uniform float cameraNear;
  210. uniform float shadowFar;
  211. #endif
  212. ` + ShaderChunk.lights_pars_begin
  213. };
  214. export { CSMShader };