CSMShader.js 8.1 KB

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