PointLightShadow.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { LightShadow } from './LightShadow.js';
  2. import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Vector2 } from '../math/Vector2.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { Vector4 } from '../math/Vector4.js';
  7. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  8. const _lightPositionWorld = /*@__PURE__*/ new Vector3();
  9. const _lookTarget = /*@__PURE__*/ new Vector3();
  10. class PointLightShadow extends LightShadow {
  11. constructor() {
  12. super( new PerspectiveCamera( 90, 1, 0.5, 500 ) );
  13. this._frameExtents = new Vector2( 4, 2 );
  14. this._viewportCount = 6;
  15. this._viewports = [
  16. // These viewports map a cube-map onto a 2D texture with the
  17. // following orientation:
  18. //
  19. // xzXZ
  20. // y Y
  21. //
  22. // X - Positive x direction
  23. // x - Negative x direction
  24. // Y - Positive y direction
  25. // y - Negative y direction
  26. // Z - Positive z direction
  27. // z - Negative z direction
  28. // positive X
  29. new Vector4( 2, 1, 1, 1 ),
  30. // negative X
  31. new Vector4( 0, 1, 1, 1 ),
  32. // positive Z
  33. new Vector4( 3, 1, 1, 1 ),
  34. // negative Z
  35. new Vector4( 1, 1, 1, 1 ),
  36. // positive Y
  37. new Vector4( 3, 0, 1, 1 ),
  38. // negative Y
  39. new Vector4( 1, 0, 1, 1 )
  40. ];
  41. this._cubeDirections = [
  42. new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
  43. new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
  44. ];
  45. this._cubeUps = [
  46. new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
  47. new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
  48. ];
  49. }
  50. updateMatrices( light, viewportIndex = 0 ) {
  51. const camera = this.camera;
  52. const shadowMatrix = this.matrix;
  53. const far = light.distance || camera.far;
  54. if ( far !== camera.far ) {
  55. camera.far = far;
  56. camera.updateProjectionMatrix();
  57. }
  58. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  59. camera.position.copy( _lightPositionWorld );
  60. _lookTarget.copy( camera.position );
  61. _lookTarget.add( this._cubeDirections[ viewportIndex ] );
  62. camera.up.copy( this._cubeUps[ viewportIndex ] );
  63. camera.lookAt( _lookTarget );
  64. camera.updateMatrixWorld();
  65. shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
  66. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  67. this._frustum.setFromProjectionMatrix( _projScreenMatrix );
  68. }
  69. }
  70. PointLightShadow.prototype.isPointLightShadow = true;
  71. export { PointLightShadow };