PointLightShadow.js 2.4 KB

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