LightShadow.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Matrix4 } from '../math/Matrix4.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { Vector3 } from '../math/Vector3.js';
  4. import { Vector4 } from '../math/Vector4.js';
  5. import { Frustum } from '../math/Frustum.js';
  6. /**
  7. * @author mrdoob / http://mrdoob.com/
  8. */
  9. function LightShadow( camera ) {
  10. this.camera = camera;
  11. this.bias = 0;
  12. this.radius = 1;
  13. this.mapSize = new Vector2( 512, 512 );
  14. this.map = null;
  15. this.matrix = new Matrix4();
  16. this._frustum = new Frustum();
  17. this._frameExtents = new Vector2( 1, 1 );
  18. this._viewportCount = 1;
  19. this._viewports = [
  20. new Vector4( 0, 0, 1, 1 )
  21. ];
  22. }
  23. Object.assign( LightShadow.prototype, {
  24. _projScreenMatrix: new Matrix4(),
  25. _lightPositionWorld: new Vector3(),
  26. _lookTarget: new Vector3(),
  27. getViewportCount: function () {
  28. return this._viewportCount;
  29. },
  30. getFrustum: function () {
  31. return this._frustum;
  32. },
  33. updateMatrices: function ( light ) {
  34. var shadowCamera = this.camera,
  35. shadowMatrix = this.matrix,
  36. projScreenMatrix = this._projScreenMatrix,
  37. lookTarget = this._lookTarget,
  38. lightPositionWorld = this._lightPositionWorld;
  39. lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  40. shadowCamera.position.copy( lightPositionWorld );
  41. lookTarget.setFromMatrixPosition( light.target.matrixWorld );
  42. shadowCamera.lookAt( lookTarget );
  43. shadowCamera.updateMatrixWorld();
  44. projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  45. this._frustum.setFromMatrix( projScreenMatrix );
  46. shadowMatrix.set(
  47. 0.5, 0.0, 0.0, 0.5,
  48. 0.0, 0.5, 0.0, 0.5,
  49. 0.0, 0.0, 0.5, 0.5,
  50. 0.0, 0.0, 0.0, 1.0
  51. );
  52. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  53. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  54. },
  55. getViewport: function ( viewportIndex ) {
  56. return this._viewports[ viewportIndex ];
  57. },
  58. getFrameExtents: function () {
  59. return this._frameExtents;
  60. },
  61. copy: function ( source ) {
  62. this.camera = source.camera.clone();
  63. this.bias = source.bias;
  64. this.radius = source.radius;
  65. this.mapSize.copy( source.mapSize );
  66. return this;
  67. },
  68. clone: function () {
  69. return new this.constructor().copy( this );
  70. },
  71. toJSON: function () {
  72. var object = {};
  73. if ( this.bias !== 0 ) object.bias = this.bias;
  74. if ( this.radius !== 1 ) object.radius = this.radius;
  75. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  76. object.camera = this.camera.toJSON( false ).object;
  77. delete object.camera.matrix;
  78. return object;
  79. }
  80. } );
  81. export { LightShadow };
粤ICP备19079148号