LightShadow.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.mapPass = null;
  16. this.matrix = new Matrix4();
  17. this._frustum = new Frustum();
  18. this._frameExtents = new Vector2( 1, 1 );
  19. this._viewportCount = 1;
  20. this._viewports = [
  21. new Vector4( 0, 0, 1, 1 )
  22. ];
  23. }
  24. Object.assign( LightShadow.prototype, {
  25. _projScreenMatrix: new Matrix4(),
  26. _lightPositionWorld: new Vector3(),
  27. _lookTarget: new Vector3(),
  28. getViewportCount: function () {
  29. return this._viewportCount;
  30. },
  31. getFrustum: function () {
  32. return this._frustum;
  33. },
  34. updateMatrices: function ( light ) {
  35. const shadowCamera = this.camera,
  36. shadowMatrix = this.matrix,
  37. projScreenMatrix = this._projScreenMatrix,
  38. lookTarget = this._lookTarget,
  39. lightPositionWorld = this._lightPositionWorld;
  40. lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  41. shadowCamera.position.copy( lightPositionWorld );
  42. lookTarget.setFromMatrixPosition( light.target.matrixWorld );
  43. shadowCamera.lookAt( lookTarget );
  44. shadowCamera.updateMatrixWorld();
  45. projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  46. this._frustum.setFromProjectionMatrix( projScreenMatrix );
  47. shadowMatrix.set(
  48. 0.5, 0.0, 0.0, 0.5,
  49. 0.0, 0.5, 0.0, 0.5,
  50. 0.0, 0.0, 0.5, 0.5,
  51. 0.0, 0.0, 0.0, 1.0
  52. );
  53. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  54. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  55. },
  56. getViewport: function ( viewportIndex ) {
  57. return this._viewports[ viewportIndex ];
  58. },
  59. getFrameExtents: function () {
  60. return this._frameExtents;
  61. },
  62. copy: function ( source ) {
  63. this.camera = source.camera.clone();
  64. this.bias = source.bias;
  65. this.radius = source.radius;
  66. this.mapSize.copy( source.mapSize );
  67. return this;
  68. },
  69. clone: function () {
  70. return new this.constructor().copy( this );
  71. },
  72. toJSON: function () {
  73. const object = {};
  74. if ( this.bias !== 0 ) object.bias = this.bias;
  75. if ( this.radius !== 1 ) object.radius = this.radius;
  76. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  77. object.camera = this.camera.toJSON( false ).object;
  78. delete object.camera.matrix;
  79. return object;
  80. }
  81. } );
  82. export { LightShadow };
粤ICP备19079148号