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.isPointLightShadow = true;
  14. this._frameExtents = new Vector2( 4, 2 );
  15. this._viewportCount = 6;
  16. this._viewports = [
  17. // These viewports map a cube-map onto a 2D texture with the
  18. // following orientation:
  19. //
  20. // xzXZ
  21. // y Y
  22. //
  23. // X - Positive x direction
  24. // x - Negative x direction
  25. // Y - Positive y direction
  26. // y - Negative y direction
  27. // Z - Positive z direction
  28. // z - Negative z direction
  29. // positive X
  30. new Vector4( 2, 1, 1, 1 ),
  31. // negative X
  32. new Vector4( 0, 1, 1, 1 ),
  33. // positive Z
  34. new Vector4( 3, 1, 1, 1 ),
  35. // negative Z
  36. new Vector4( 1, 1, 1, 1 ),
  37. // positive Y
  38. new Vector4( 3, 0, 1, 1 ),
  39. // negative Y
  40. new Vector4( 1, 0, 1, 1 )
  41. ];
  42. this._cubeDirections = [
  43. new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
  44. new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
  45. ];
  46. this._cubeUps = [
  47. new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
  48. new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
  49. ];
  50. }
  51. updateMatrices( light, viewportIndex = 0 ) {
  52. const camera = this.camera;
  53. const shadowMatrix = this.matrix;
  54. const far = light.distance || camera.far;
  55. if ( far !== camera.far ) {
  56. camera.far = far;
  57. camera.updateProjectionMatrix();
  58. }
  59. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  60. camera.position.copy( _lightPositionWorld );
  61. _lookTarget.copy( camera.position );
  62. _lookTarget.add( this._cubeDirections[ viewportIndex ] );
  63. camera.up.copy( this._cubeUps[ viewportIndex ] );
  64. camera.lookAt( _lookTarget );
  65. camera.updateMatrixWorld();
  66. shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
  67. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  68. this._frustum.setFromProjectionMatrix( _projScreenMatrix );
  69. }
  70. }
  71. export { PointLightShadow };
粤ICP备19079148号