UNPKG

2.59 kBJavaScriptView Raw
1import { LightShadow } from './LightShadow.js';
2import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
3import { Matrix4 } from '../math/Matrix4.js';
4import { Vector2 } from '../math/Vector2.js';
5import { Vector3 } from '../math/Vector3.js';
6import { Vector4 } from '../math/Vector4.js';
7
8const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
9const _lightPositionWorld = /*@__PURE__*/ new Vector3();
10const _lookTarget = /*@__PURE__*/ new Vector3();
11
12class PointLightShadow extends LightShadow {
13
14 constructor() {
15
16 super( new PerspectiveCamera( 90, 1, 0.5, 500 ) );
17
18 this._frameExtents = new Vector2( 4, 2 );
19
20 this._viewportCount = 6;
21
22 this._viewports = [
23 // These viewports map a cube-map onto a 2D texture with the
24 // following orientation:
25 //
26 // xzXZ
27 // y Y
28 //
29 // X - Positive x direction
30 // x - Negative x direction
31 // Y - Positive y direction
32 // y - Negative y direction
33 // Z - Positive z direction
34 // z - Negative z direction
35
36 // positive X
37 new Vector4( 2, 1, 1, 1 ),
38 // negative X
39 new Vector4( 0, 1, 1, 1 ),
40 // positive Z
41 new Vector4( 3, 1, 1, 1 ),
42 // negative Z
43 new Vector4( 1, 1, 1, 1 ),
44 // positive Y
45 new Vector4( 3, 0, 1, 1 ),
46 // negative Y
47 new Vector4( 1, 0, 1, 1 )
48 ];
49
50 this._cubeDirections = [
51 new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
52 new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
53 ];
54
55 this._cubeUps = [
56 new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
57 new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
58 ];
59
60 }
61
62 updateMatrices( light, viewportIndex = 0 ) {
63
64 const camera = this.camera;
65 const shadowMatrix = this.matrix;
66
67 const far = light.distance || camera.far;
68
69 if ( far !== camera.far ) {
70
71 camera.far = far;
72 camera.updateProjectionMatrix();
73
74 }
75
76 _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
77 camera.position.copy( _lightPositionWorld );
78
79 _lookTarget.copy( camera.position );
80 _lookTarget.add( this._cubeDirections[ viewportIndex ] );
81 camera.up.copy( this._cubeUps[ viewportIndex ] );
82 camera.lookAt( _lookTarget );
83 camera.updateMatrixWorld();
84
85 shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
86
87 _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
88 this._frustum.setFromProjectionMatrix( _projScreenMatrix );
89
90 }
91
92}
93
94PointLightShadow.prototype.isPointLightShadow = true;
95
96export { PointLightShadow };