CSMShader.js 10 KB

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