LightShadow.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.intensity = 1;
  13. this.bias = 0;
  14. this.normalBias = 0;
  15. this.radius = 1;
  16. this.blurSamples = 8;
  17. this.mapSize = new Vector2( 512, 512 );
  18. this.map = null;
  19. this.mapPass = null;
  20. this.matrix = new Matrix4();
  21. this.autoUpdate = true;
  22. this.needsUpdate = false;
  23. this._frustum = new Frustum();
  24. this._frameExtents = new Vector2( 1, 1 );
  25. this._viewportCount = 1;
  26. this._viewports = [
  27. new Vector4( 0, 0, 1, 1 )
  28. ];
  29. }
  30. getViewportCount() {
  31. return this._viewportCount;
  32. }
  33. getFrustum() {
  34. return this._frustum;
  35. }
  36. updateMatrices( light ) {
  37. const shadowCamera = this.camera;
  38. const shadowMatrix = this.matrix;
  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.setFromProjectionMatrix( _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( _projScreenMatrix );
  53. }
  54. getViewport( viewportIndex ) {
  55. return this._viewports[ viewportIndex ];
  56. }
  57. getFrameExtents() {
  58. return this._frameExtents;
  59. }
  60. dispose() {
  61. if ( this.map ) {
  62. this.map.dispose();
  63. }
  64. if ( this.mapPass ) {
  65. this.mapPass.dispose();
  66. }
  67. }
  68. copy( source ) {
  69. this.camera = source.camera.clone();
  70. this.intensity = source.intensity;
  71. this.bias = source.bias;
  72. this.radius = source.radius;
  73. this.mapSize.copy( source.mapSize );
  74. return this;
  75. }
  76. clone() {
  77. return new this.constructor().copy( this );
  78. }
  79. toJSON() {
  80. const object = {};
  81. if ( this.intensity !== 1 ) object.intensity = this.intensity;
  82. if ( this.bias !== 0 ) object.bias = this.bias;
  83. if ( this.normalBias !== 0 ) object.normalBias = this.normalBias;
  84. if ( this.radius !== 1 ) object.radius = this.radius;
  85. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  86. object.camera = this.camera.toJSON( false ).object;
  87. delete object.camera.matrix;
  88. return object;
  89. }
  90. }
  91. export { LightShadow };