LightShadow.js 2.6 KB
Newer Older
B
bentok 已提交
1 2
import { Matrix4 } from '../math/Matrix4.js';
import { Vector2 } from '../math/Vector2.js';
3 4 5
import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js';
import { Frustum } from '../math/Frustum.js';
R
Rich Harris 已提交
6

M
Mr.doob 已提交
7 8 9 10
/**
 * @author mrdoob / http://mrdoob.com/
 */

M
Mr.doob 已提交
11
function LightShadow( camera ) {
M
Mr.doob 已提交
12 13 14 15

	this.camera = camera;

	this.bias = 0;
M
Mr.doob 已提交
16
	this.radius = 1;
M
Mr.doob 已提交
17

R
Rich Harris 已提交
18
	this.mapSize = new Vector2( 512, 512 );
M
Mr.doob 已提交
19 20

	this.map = null;
S
supereggbert 已提交
21
	this.mapPass = null;
R
Rich Harris 已提交
22
	this.matrix = new Matrix4();
M
Mr.doob 已提交
23

S
lint  
supereggbert 已提交
24 25
	this._frustum = new Frustum();
	this._frameExtents = new Vector2( 1, 1 );
26

S
lint  
supereggbert 已提交
27
	this._viewportCount = 1;
28

S
lint  
supereggbert 已提交
29
	this._viewports = [
30

S
lint  
supereggbert 已提交
31
		new Vector4( 0, 0, 1, 1 )
32

S
lint  
supereggbert 已提交
33
	];
34

M
Mr.doob 已提交
35
}
M
Mr.doob 已提交
36

R
Rich Harris 已提交
37
Object.assign( LightShadow.prototype, {
M
Mr.doob 已提交
38

S
lint  
supereggbert 已提交
39
	_projScreenMatrix: new Matrix4(),
40

S
lint  
supereggbert 已提交
41
	_lightPositionWorld: new Vector3(),
42

S
lint  
supereggbert 已提交
43
	_lookTarget: new Vector3(),
44

S
lint  
supereggbert 已提交
45
	getViewportCount: function () {
46

S
lint  
supereggbert 已提交
47
		return this._viewportCount;
48

S
lint  
supereggbert 已提交
49
	},
50

S
lint  
supereggbert 已提交
51
	getFrustum: function () {
52

S
lint  
supereggbert 已提交
53
		return this._frustum;
54

S
lint  
supereggbert 已提交
55
	},
56

S
supereggbrt 已提交
57
	updateMatrices: function ( light ) {
58

S
lint  
supereggbert 已提交
59 60
		var shadowCamera = this.camera,
			shadowMatrix = this.matrix,
S
supereggbrt 已提交
61 62 63 64 65 66 67 68 69 70
			projScreenMatrix = this._projScreenMatrix,
			lookTarget = this._lookTarget,
			lightPositionWorld = this._lightPositionWorld;

		lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
		shadowCamera.position.copy( lightPositionWorld );

		lookTarget.setFromMatrixPosition( light.target.matrixWorld );
		shadowCamera.lookAt( lookTarget );
		shadowCamera.updateMatrixWorld();
71

S
lint  
supereggbert 已提交
72
		projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
73 74
		this._frustum.setFromMatrix( projScreenMatrix );

S
lint  
supereggbert 已提交
75
		shadowMatrix.set(
76 77 78 79 80 81 82 83 84
			0.5, 0.0, 0.0, 0.5,
			0.0, 0.5, 0.0, 0.5,
			0.0, 0.0, 0.5, 0.5,
			0.0, 0.0, 0.0, 1.0
		);

		shadowMatrix.multiply( shadowCamera.projectionMatrix );
		shadowMatrix.multiply( shadowCamera.matrixWorldInverse );

S
lint  
supereggbert 已提交
85
	},
86

S
lint  
supereggbert 已提交
87
	getViewport: function ( viewportIndex ) {
88

S
lint  
supereggbert 已提交
89
		return this._viewports[ viewportIndex ];
90

S
lint  
supereggbert 已提交
91
	},
92

S
lint  
supereggbert 已提交
93
	getFrameExtents: function () {
94

S
lint  
supereggbert 已提交
95
		return this._frameExtents;
96

S
lint  
supereggbert 已提交
97
	},
98

M
Mr.doob 已提交
99 100 101 102 103
	copy: function ( source ) {

		this.camera = source.camera.clone();

		this.bias = source.bias;
M
Mr.doob 已提交
104
		this.radius = source.radius;
M
Mr.doob 已提交
105 106 107

		this.mapSize.copy( source.mapSize );

A
Al McElrath 已提交
108 109
		return this;

M
Mr.doob 已提交
110 111 112 113 114 115
	},

	clone: function () {

		return new this.constructor().copy( this );

116 117 118 119 120 121 122 123 124 125
	},

	toJSON: function () {

		var object = {};

		if ( this.bias !== 0 ) object.bias = this.bias;
		if ( this.radius !== 1 ) object.radius = this.radius;
		if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();

M
Mr.doob 已提交
126 127
		object.camera = this.camera.toJSON( false ).object;
		delete object.camera.matrix;
128 129 130

		return object;

M
Mr.doob 已提交
131 132
	}

M
Mr.doob 已提交
133
} );
R
Rich Harris 已提交
134 135


136
export { LightShadow };