CSMShader.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { ShaderChunk } from 'three';
  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. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  64. // NOTE: Depth gets larger away from the camera.
  65. // cascade.x is closer, cascade.y is further
  66. cascade = CSM_cascades[ i ];
  67. cascadeCenter = ( cascade.x + cascade.y ) / 2.0;
  68. closestEdge = linearDepth < cascadeCenter ? cascade.x : cascade.y;
  69. margin = 0.25 * pow( closestEdge, 2.0 );
  70. csmx = cascade.x - margin / 2.0;
  71. csmy = cascade.y + margin / 2.0;
  72. if( linearDepth >= csmx && ( linearDepth < csmy || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 ) ) {
  73. float dist = min( linearDepth - csmx, csmy - linearDepth );
  74. float ratio = clamp( dist / margin, 0.0, 1.0 );
  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. ReflectedLight prevLight = reflectedLight;
  81. RE_Direct( directLight, geometry, material, reflectedLight );
  82. bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
  83. float blendRatio = shouldBlend ? ratio : 1.0;
  84. reflectedLight.directDiffuse = mix( prevLight.directDiffuse, reflectedLight.directDiffuse, blendRatio );
  85. reflectedLight.directSpecular = mix( prevLight.directSpecular, reflectedLight.directSpecular, blendRatio );
  86. reflectedLight.indirectDiffuse = mix( prevLight.indirectDiffuse, reflectedLight.indirectDiffuse, blendRatio );
  87. reflectedLight.indirectSpecular = mix( prevLight.indirectSpecular, reflectedLight.indirectSpecular, blendRatio );
  88. }
  89. #endif
  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. 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 );
  101. #endif
  102. }
  103. #pragma unroll_loop_end
  104. #endif
  105. #if ( NUM_DIR_LIGHTS > NUM_DIR_LIGHT_SHADOWS)
  106. // compute the lights not casting shadows (if any)
  107. #pragma unroll_loop_start
  108. for ( int i = NUM_DIR_LIGHT_SHADOWS; i < NUM_DIR_LIGHTS; i ++ ) {
  109. directionalLight = directionalLights[ i ];
  110. getDirectionalLightInfo( directionalLight, geometry, directLight );
  111. RE_Direct( directLight, geometry, material, reflectedLight );
  112. }
  113. #pragma unroll_loop_end
  114. #endif
  115. #endif
  116. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && !defined( USE_CSM ) && !defined( CSM_CASCADES )
  117. DirectionalLight directionalLight;
  118. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  119. DirectionalLightShadow directionalLightShadow;
  120. #endif
  121. #pragma unroll_loop_start
  122. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  123. directionalLight = directionalLights[ i ];
  124. getDirectionalLightInfo( directionalLight, geometry, directLight );
  125. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  126. directionalLightShadow = directionalLightShadows[ i ];
  127. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  128. #endif
  129. RE_Direct( directLight, geometry, material, reflectedLight );
  130. }
  131. #pragma unroll_loop_end
  132. #endif
  133. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  134. RectAreaLight rectAreaLight;
  135. #pragma unroll_loop_start
  136. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  137. rectAreaLight = rectAreaLights[ i ];
  138. RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
  139. }
  140. #pragma unroll_loop_end
  141. #endif
  142. #if defined( RE_IndirectDiffuse )
  143. vec3 iblIrradiance = vec3( 0.0 );
  144. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  145. irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
  146. #if ( NUM_HEMI_LIGHTS > 0 )
  147. #pragma unroll_loop_start
  148. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  149. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
  150. }
  151. #pragma unroll_loop_end
  152. #endif
  153. #endif
  154. #if defined( RE_IndirectSpecular )
  155. vec3 radiance = vec3( 0.0 );
  156. vec3 clearcoatRadiance = vec3( 0.0 );
  157. #endif
  158. `,
  159. lights_pars_begin: /* glsl */`
  160. #if defined( USE_CSM ) && defined( CSM_CASCADES )
  161. uniform vec2 CSM_cascades[CSM_CASCADES];
  162. uniform float cameraNear;
  163. uniform float shadowFar;
  164. #endif
  165. ` + ShaderChunk.lights_pars_begin
  166. };
  167. export { CSMShader };