LightShadow.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  7. const _lightPositionWorld = /*@__PURE__*/ new Vector3();
  8. const _lookTarget = /*@__PURE__*/ new Vector3();
  9. class LightShadow {
  10. constructor( camera ) {
  11. this.camera = camera;
  12. this.bias = 0;
  13. this.normalBias = 0;
  14. this.radius = 1;
  15. this.blurSamples = 8;
  16. this.mapSize = new Vector2( 512, 512 );
  17. this.map = null;
  18. this.mapPass = null;
  19. this.matrix = new Matrix4();
  20. this.autoUpdate = true;
  21. this.needsUpdate = false;
  22. this._frustum = new Frustum();
  23. this._frameExtents = new Vector2( 1, 1 );
  24. this._viewportCount = 1;
  25. this._viewports = [
  26. new Vector4( 0, 0, 1, 1 )
  27. ];
  28. }
  29. getViewportCount() {
  30. return this._viewportCount;
  31. }
  32. getFrustum() {
  33. return this._frustum;
  34. }
  35. updateMatrices( light ) {
  36. const shadowCamera = this.camera;
  37. const shadowMatrix = this.matrix;
  38. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  39. shadowCamera.position.copy( _lightPositionWorld );
  40. _lookTarget.setFromMatrixPosition( light.target.matrixWorld );
  41. shadowCamera.lookAt( _lookTarget );
  42. shadowCamera.updateMatrixWorld();
  43. _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  44. this._frustum.setFromProjectionMatrix( _projScreenMatrix );
  45. shadowMatrix.set(
  46. 0.5, 0.0, 0.0, 0.5,
  47. 0.0, 0.5, 0.0, 0.5,
  48. 0.0, 0.0, 0.5, 0.5,
  49. 0.0, 0.0, 0.0, 1.0
  50. );
  51. shadowMatrix.multiply( _projScreenMatrix );
  52. }
  53. getViewport( viewportIndex ) {
  54. return this._viewports[ viewportIndex ];
  55. }
  56. getFrameExtents() {
  57. return this._frameExtents;
  58. }
  59. dispose() {
  60. if ( this.map ) {
  61. this.map.dispose();
  62. }
  63. if ( this.mapPass ) {
  64. this.mapPass.dispose();
  65. }
  66. }
  67. copy( source ) {
  68. this.camera = source.camera.clone();
  69. this.bias = source.bias;
  70. this.radius = source.radius;
  71. this.mapSize.copy( source.mapSize );
  72. return this;
  73. }
  74. clone() {
  75. return new this.constructor().copy( this );
  76. }
  77. toJSON() {
  78. const object = {};
  79. if ( this.bias !== 0 ) object.bias = this.bias;
  80. if ( this.normalBias !== 0 ) object.normalBias = this.normalBias;
  81. if ( this.radius !== 1 ) object.radius = this.radius;
  82. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  83. object.camera = this.camera.toJSON( false ).object;
  84. delete object.camera.matrix;
  85. return object;
  86. }
  87. }
  88. export { LightShadow };