lights_fragment_begin.glsl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. export default /* glsl */`
  2. /**
  3. * This is a template that can be used to light a material, it uses pluggable
  4. * RenderEquations (RE)for specific lighting scenarios.
  5. *
  6. * Instructions for use:
  7. * - Ensure that both RE_Direct, RE_IndirectDiffuse and RE_IndirectSpecular are defined
  8. * - If you have defined an RE_IndirectSpecular, you need to also provide a Material_LightProbeLOD. <---- ???
  9. * - Create a material parameter that is to be passed as the third parameter to your lighting functions.
  10. *
  11. * TODO:
  12. * - Add area light support.
  13. * - Add sphere light support.
  14. * - Add diffuse light probe (irradiance cubemap) support.
  15. */
  16. GeometricContext geometry;
  17. geometry.position = - vViewPosition;
  18. geometry.normal = normal;
  19. geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
  20. #ifdef USE_CLEARCOAT
  21. geometry.clearcoatNormal = clearcoatNormal;
  22. #endif
  23. IncidentLight directLight;
  24. #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
  25. PointLight pointLight;
  26. #if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
  27. PointLightShadow pointLightShadow;
  28. #endif
  29. #pragma unroll_loop_start
  30. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  31. pointLight = pointLights[ i ];
  32. getPointLightInfo( pointLight, geometry, directLight );
  33. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
  34. pointLightShadow = pointLightShadows[ i ];
  35. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
  36. #endif
  37. RE_Direct( directLight, geometry, material, reflectedLight );
  38. }
  39. #pragma unroll_loop_end
  40. #endif
  41. #if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
  42. SpotLight spotLight;
  43. #if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
  44. SpotLightShadow spotLightShadow;
  45. #endif
  46. #pragma unroll_loop_start
  47. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  48. spotLight = spotLights[ i ];
  49. getSpotLightInfo( spotLight, geometry, directLight );
  50. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
  51. spotLightShadow = spotLightShadows[ i ];
  52. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
  53. #endif
  54. RE_Direct( directLight, geometry, material, reflectedLight );
  55. }
  56. #pragma unroll_loop_end
  57. #endif
  58. #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )
  59. DirectionalLight directionalLight;
  60. #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
  61. DirectionalLightShadow directionalLightShadow;
  62. #endif
  63. #pragma unroll_loop_start
  64. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  65. directionalLight = directionalLights[ i ];
  66. getDirectionalLightInfo( directionalLight, geometry, directLight );
  67. #if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
  68. directionalLightShadow = directionalLightShadows[ i ];
  69. directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
  70. #endif
  71. RE_Direct( directLight, geometry, material, reflectedLight );
  72. }
  73. #pragma unroll_loop_end
  74. #endif
  75. #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
  76. RectAreaLight rectAreaLight;
  77. #pragma unroll_loop_start
  78. for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
  79. rectAreaLight = rectAreaLights[ i ];
  80. RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
  81. }
  82. #pragma unroll_loop_end
  83. #endif
  84. #if defined( RE_IndirectDiffuse )
  85. vec3 iblIrradiance = vec3( 0.0 );
  86. vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
  87. irradiance += getLightProbeIrradiance( lightProbe, geometry );
  88. #if ( NUM_HEMI_LIGHTS > 0 )
  89. #pragma unroll_loop_start
  90. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  91. irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
  92. }
  93. #pragma unroll_loop_end
  94. #endif
  95. #endif
  96. #if defined( RE_IndirectSpecular )
  97. vec3 radiance = vec3( 0.0 );
  98. vec3 clearcoatRadiance = vec3( 0.0 );
  99. #endif
  100. `;