CSMShader.js 7.7 KB

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