CSMShader.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { ShaderChunk } from '../../../build/three.module.js';
  2. const CSMShader = {
  3. lights_fragment_begin: /* glsl */`
  4. GeometricContext geometry;
  5. geometry.position = - vViewPosition;
  6. geometry.normal = normal;
  7. geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
  8. #ifdef CLEARCOAT
  9. geometry.clearcoatNormal = clearcoatNormal;
  10. #endif
  11. IncidentLight directLight;
  12. #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
  13. PointLight pointLight;
  14. #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
  15. PointLightShadow pointLightShadow;
  16. #endif
  17. #pragma unroll_loop_start
  18. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  19. pointLight = pointLights[ i ];
  20. getPointLightInfo( pointLight, geometry, directLight );
  21. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
  22. pointLightShadow = pointLightShadows[ i ];
  23. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
  24. #endif
  25. RE_Direct( directLight, geometry, material, reflectedLight );
  26. }
  27. #pragma unroll_loop_end
  28. #endif
  29. #if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
  30. SpotLight spotLight;
  31. #if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
  32. SpotLightShadow spotLightShadow;
  33. #endif
  34. #pragma unroll_loop_start
  35. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  36. spotLight = spotLights[ i ];
  37. getSpotLightInfo( spotLight, geometry, directLight );
  38. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
  39. spotLightShadow = spotLightShadows[ i ];
  40. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
  41. #endif
  42. RE_Direct( directLight, geometry, material, reflectedLight );
  43. }
  44. #pragma unroll_loop_end
  45. #endif
  46. #if ( NUM_DIR_LIGHTS > 0) && defined( RE_Direct ) && defined( USE_CSM ) && defined( CSM_CASCADES )
  47. DirectionalLight directionalLight;
  48. float linearDepth = (vViewPosition.z) / (shadowFar - cameraNear);
  49. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  50. DirectionalLightShadow directionalLightShadow;
  51. #endif
  52. #if defined( USE_SHADOWMAP ) && defined( CSM_FADE )
  53. vec2 cascade;
  54. float cascadeCenter;
  55. float closestEdge;
  56. float margin;
  57. float csmx;
  58. float csmy;
  59. #pragma unroll_loop_start
  60. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  61. directionalLight = directionalLights[ i ];
  62. getDirectionalLightInfo( directionalLight, geometry, directLight );
  63. // NOTE: Depth gets larger away from the camera.
  64. // cascade.x is closer, cascade.y is further
  65. cascade = CSM_cascades[ i ];
  66. cascadeCenter = ( cascade.x + cascade.y ) / 2.0;
  67. closestEdge = linearDepth < cascadeCenter ? cascade.x : cascade.y;
  68. margin = 0.25 * pow( closestEdge, 2.0 );
  69. csmx = cascade.x - margin / 2.0;
  70. csmy = cascade.y + margin / 2.0;
  71. if( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS && linearDepth >= csmx && ( linearDepth < csmy || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 ) ) {
  72. float dist = min( linearDepth - csmx, csmy - linearDepth );
  73. float ratio = clamp( dist / margin, 0.0, 1.0 );
  74. if( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS ) {
  75. vec3 prevColor = directLight.color;
  76. directionalLightShadow = directionalLightShadows[ i ];
  77. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  78. bool shouldFadeLastCascade = UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth > cascadeCenter;
  79. directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );
  80. }
  81. ReflectedLight prevLight = reflectedLight;
  82. RE_Direct( directLight, geometry, material, reflectedLight );
  83. bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
  84. float blendRatio = shouldBlend ? ratio : 1.0;
  85. reflectedLight.directDiffuse = mix( prevLight.directDiffuse, reflectedLight.directDiffuse, blendRatio );
  86. reflectedLight.directSpecular = mix( prevLight.directSpecular, reflectedLight.directSpecular, blendRatio );
  87. reflectedLight.indirectDiffuse = mix( prevLight.indirectDiffuse, reflectedLight.indirectDiffuse, blendRatio );
  88. reflectedLight.indirectSpecular = mix( prevLight.indirectSpecular, reflectedLight.indirectSpecular, blendRatio );
  89. }
  90. }
  91. #pragma unroll_loop_end
  92. #else
  93. #pragma unroll_loop_start
  94. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  95. directionalLight = directionalLights[ i ];
  96. getDirectionalLightInfo( directionalLight, geometry, directLight );
  97. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  98. directionalLightShadow = directionalLightShadows[ i ];
  99. if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y) directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  100. #endif
  101. if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometry, material, reflectedLight );
  102. }
  103. #pragma unroll_loop_end
  104. #endif
  105. #endif
  106. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && !defined( USE_CSM ) && !defined( CSM_CASCADES )
  107. DirectionalLight directionalLight;
  108. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  109. DirectionalLightShadow directionalLightShadow;
  110. #endif
  111. #pragma unroll_loop_start
  112. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  113. directionalLight = directionalLights[ i ];
  114. getDirectionalLightInfo( directionalLight, geometry, directLight );
  115. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  116. directionalLightShadow = directionalLightShadows[ i ];
  117. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  118. #endif
  119. RE_Direct( directLight, geometry, material, reflectedLight );
  120. }
  121. #pragma unroll_loop_end
  122. #endif
  123. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  124. RectAreaLight rectAreaLight;
  125. #pragma unroll_loop_start
  126. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  127. rectAreaLight = rectAreaLights[ i ];
  128. RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
  129. }
  130. #pragma unroll_loop_end
  131. #endif
  132. #if defined( RE_IndirectDiffuse )
  133. vec3 iblIrradiance = vec3( 0.0 );
  134. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  135. irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
  136. #if ( NUM_HEMI_LIGHTS > 0 )
  137. #pragma unroll_loop_start
  138. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  139. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
  140. }
  141. #pragma unroll_loop_end
  142. #endif
  143. #endif
  144. #if defined( RE_IndirectSpecular )
  145. vec3 radiance = vec3( 0.0 );
  146. vec3 clearcoatRadiance = vec3( 0.0 );
  147. #endif
  148. `,
  149. lights_pars_begin: /* glsl */`
  150. #if defined( USE_CSM ) && defined( CSM_CASCADES )
  151. uniform vec2 CSM_cascades[CSM_CASCADES];
  152. uniform float cameraNear;
  153. uniform float shadowFar;
  154. #endif
  155. ` + ShaderChunk.lights_pars_begin
  156. };
  157. export { CSMShader };