CSMShader.js 11 KB

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