Shader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import * as THREE from '../../../build/three.module.js';
  2. export default {
  3. lights_fragment_begin: /* glsl */`
  4. GeometricContext geometry;
  5. geometry.position = - vViewPosition;
  6. geometry.normal = normal;
  7. geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
  8. #ifdef CLEARCOAT
  9. geometry.clearcoatNormal = clearcoatNormal;
  10. #endif
  11. IncidentLight directLight;
  12. #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
  13. PointLight pointLight;
  14. #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
  15. PointLightShadow pointLightShadow;
  16. #endif
  17. #pragma unroll_loop
  18. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  19. pointLight = pointLights[ i ];
  20. getPointDirectLightIrradiance( pointLight, geometry, directLight );
  21. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
  22. pointLightShadow = pointLightShadows[ i ];
  23. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
  24. #endif
  25. RE_Direct( directLight, geometry, material, reflectedLight );
  26. }
  27. #endif
  28. #if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
  29. SpotLight spotLight;
  30. #if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
  31. SpotLightShadow spotLightShadow;
  32. #endif
  33. #pragma unroll_loop
  34. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  35. spotLight = spotLights[ i ];
  36. getSpotDirectLightIrradiance( spotLight, geometry, directLight );
  37. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
  38. spotLightShadow = spotLightShadows[ i ];
  39. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
  40. #endif
  41. RE_Direct( directLight, geometry, material, reflectedLight );
  42. }
  43. #endif
  44. #if ( NUM_DIR_LIGHTS > 0) && defined( RE_Direct ) && defined( USE_CSM ) && defined( CSM_CASCADES )
  45. DirectionalLight directionalLight;
  46. float linearDepth = (vViewPosition.z) / (shadowFar - cameraNear);
  47. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  48. DirectionalLightShadow directionalLightShadow;
  49. #endif
  50. #pragma unroll_loop
  51. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  52. directionalLight = directionalLights[ i ];
  53. getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
  54. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  55. directionalLightShadow = directionalLightShadows[ i ];
  56. 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;
  57. #endif
  58. 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 );
  59. }
  60. #endif
  61. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct ) && !defined( USE_CSM ) && !defined( CSM_CASCADES )
  62. DirectionalLight directionalLight;
  63. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  64. DirectionalLightShadow directionalLightShadow;
  65. #endif
  66. #pragma unroll_loop
  67. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  68. directionalLight = directionalLights[ i ];
  69. getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
  70. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  71. directionalLightShadow = directionalLightShadows[ i ];
  72. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  73. #endif
  74. RE_Direct( directLight, geometry, material, reflectedLight );
  75. }
  76. #endif
  77. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  78. RectAreaLight rectAreaLight;
  79. #pragma unroll_loop
  80. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  81. rectAreaLight = rectAreaLights[ i ];
  82. RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
  83. }
  84. #endif
  85. #if defined( RE_IndirectDiffuse )
  86. vec3 iblIrradiance = vec3( 0.0 );
  87. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  88. irradiance += getLightProbeIrradiance( lightProbe, geometry );
  89. #if ( NUM_HEMI_LIGHTS > 0 )
  90. #pragma unroll_loop
  91. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  92. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
  93. }
  94. #endif
  95. #endif
  96. #if defined( RE_IndirectSpecular )
  97. vec3 radiance = vec3( 0.0 );
  98. vec3 clearcoatRadiance = vec3( 0.0 );
  99. #endif
  100. `,
  101. lights_pars_begin: /* glsl */`
  102. #if defined( USE_CSM ) && defined( CSM_CASCADES )
  103. uniform vec2 CSM_cascades[CSM_CASCADES];
  104. uniform float cameraNear;
  105. uniform float shadowFar;
  106. #endif
  107. ` + THREE.ShaderChunk.lights_pars_begin
  108. };