Shader.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @author vHawk / https://github.com/vHawk/
  3. */
  4. import { ShaderChunk } from '../../../build/three.module.js';
  5. export default {
  6. lights_fragment_begin: /* glsl */`
  7. GeometricContext geometry;
  8. geometry.position = - vViewPosition;
  9. geometry.normal = normal;
  10. geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
  11. #ifdef CLEARCOAT
  12. geometry.clearcoatNormal = clearcoatNormal;
  13. #endif
  14. IncidentLight directLight;
  15. #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
  16. PointLight pointLight;
  17. #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
  18. PointLightShadow pointLightShadow;
  19. #endif
  20. #pragma unroll_loop
  21. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  22. pointLight = pointLights[ i ];
  23. getPointDirectLightIrradiance( pointLight, geometry, directLight );
  24. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
  25. pointLightShadow = pointLightShadows[ i ];
  26. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
  27. #endif
  28. RE_Direct( directLight, geometry, material, reflectedLight );
  29. }
  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
  37. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  38. spotLight = spotLights[ i ];
  39. getSpotDirectLightIrradiance( 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. #endif
  47. #if ( NUM_DIR_LIGHTS > 0) && defined( RE_Direct ) && defined( USE_CSM ) && defined( CSM_CASCADES )
  48. DirectionalLight directionalLight;
  49. float linearDepth = (vViewPosition.z) / (shadowFar - cameraNear);
  50. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  51. DirectionalLightShadow directionalLightShadow;
  52. #endif
  53. #if defined( CSM_FADE )
  54. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  55. directionalLight = directionalLights[ i ];
  56. getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
  57. // NOTE: Depth gets larger away from the camera.
  58. // cascade.x is closer, cascade.y is further
  59. vec2 cascade = CSM_cascades[ i ];
  60. float cascadeCenter = ( cascade.x + cascade.y ) / 2.0;
  61. float closestEdge = linearDepth < cascadeCenter ? cascade.x : cascade.y;
  62. float margin = 0.25 * pow( closestEdge, 2.0 );
  63. float csmx = cascade.x - margin / 2.0;
  64. float csmy = cascade.y + margin / 2.0;
  65. if( i < NUM_DIR_LIGHT_SHADOWS && linearDepth >= csmx && ( linearDepth < csmy || i == CSM_CASCADES - 1 ) ) {
  66. float dist = min( linearDepth - csmx, csmy - linearDepth );
  67. float ratio = clamp( dist / margin, 0.0, 1.0 );
  68. if( i < NUM_DIR_LIGHT_SHADOWS ) {
  69. vec3 prevColor = directLight.color;
  70. directionalLightShadow = directionalLightShadows[ i ];
  71. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  72. bool shouldFadeLastCascade = i == CSM_CASCADES - 1 && linearDepth > ( csmx + csmy ) / 2.0;
  73. directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );
  74. }
  75. ReflectedLight prevLight = reflectedLight;
  76. RE_Direct( directLight, geometry, material, reflectedLight );
  77. bool shouldBlend = i != CSM_CASCADES - 1 || i == CSM_CASCADES - 1 && linearDepth < ( csmx + csmy ) / 2.0;
  78. float blendRatio = shouldBlend ? ratio : 1.0;
  79. reflectedLight.directDiffuse = mix( prevLight.directDiffuse, reflectedLight.directDiffuse, blendRatio );
  80. reflectedLight.directSpecular = mix( prevLight.directSpecular, reflectedLight.directSpecular, blendRatio );
  81. reflectedLight.indirectDiffuse = mix( prevLight.indirectDiffuse, reflectedLight.indirectDiffuse, blendRatio );
  82. reflectedLight.indirectSpecular = mix( prevLight.indirectSpecular, reflectedLight.indirectSpecular, blendRatio );
  83. }
  84. }
  85. #else
  86. #pragma unroll_loop
  87. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  88. directionalLight = directionalLights[ i ];
  89. getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
  90. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  91. directionalLightShadow = directionalLightShadows[ i ];
  92. 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;
  93. #endif
  94. 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 );
  95. }
  96. #endif
  97. #endif
  98. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && !defined( USE_CSM ) && !defined( CSM_CASCADES )
  99. DirectionalLight directionalLight;
  100. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  101. DirectionalLightShadow directionalLightShadow;
  102. #endif
  103. #pragma unroll_loop
  104. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  105. directionalLight = directionalLights[ i ];
  106. getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
  107. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  108. directionalLightShadow = directionalLightShadows[ i ];
  109. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  110. #endif
  111. RE_Direct( directLight, geometry, material, reflectedLight );
  112. }
  113. #endif
  114. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  115. RectAreaLight rectAreaLight;
  116. #pragma unroll_loop
  117. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  118. rectAreaLight = rectAreaLights[ i ];
  119. RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
  120. }
  121. #endif
  122. #if defined( RE_IndirectDiffuse )
  123. vec3 iblIrradiance = vec3( 0.0 );
  124. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  125. irradiance += getLightProbeIrradiance( lightProbe, geometry );
  126. #if ( NUM_HEMI_LIGHTS > 0 )
  127. #pragma unroll_loop
  128. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  129. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
  130. }
  131. #endif
  132. #endif
  133. #if defined( RE_IndirectSpecular )
  134. vec3 radiance = vec3( 0.0 );
  135. vec3 clearcoatRadiance = vec3( 0.0 );
  136. #endif
  137. `,
  138. lights_pars_begin: /* glsl */`
  139. #if defined( USE_CSM ) && defined( CSM_CASCADES )
  140. uniform vec2 CSM_cascades[CSM_CASCADES];
  141. uniform float cameraNear;
  142. uniform float shadowFar;
  143. #endif
  144. ` + ShaderChunk.lights_pars_begin
  145. };