Skip to content

Commit 9350a77

Browse files
committed
fix moving light with moving target
1 parent 03d5a45 commit 9350a77

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

examples/webgl_shadowmap_viewer.html

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@
7474
spotLight.name = 'Spot Light';
7575
spotLight.angle = Math.PI / 5;
7676
spotLight.penumbra = 0.3;
77-
spotLight.position.set( 10, 10, 5 );
77+
spotLight.position.set( 2, 2, 2 );
7878
spotLight.lookAt( 0, 0, 0 );
7979
spotLight.castShadow = true;
80-
spotLight.shadow.camera.near = 8;
81-
spotLight.shadow.camera.far = 30;
80+
spotLight.shadow.camera.near = 0.5;
81+
spotLight.shadow.camera.far = 40;
8282
spotLight.shadow.mapSize.width = 1024;
8383
spotLight.shadow.mapSize.height = 1024;
84-
scene.add( spotLight );
8584

8685
scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
8786

@@ -117,13 +116,25 @@
117116
torusKnot.receiveShadow = true;
118117
scene.add( torusKnot );
119118

119+
120120
var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
121121
cube = new THREE.Mesh( geometry, material );
122122
cube.position.set( 8, 3, 8 );
123123
cube.castShadow = true;
124124
cube.receiveShadow = true;
125125
scene.add( cube );
126126

127+
var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
128+
target = new THREE.Mesh( geometry, material );
129+
target.scale.multiplyScalar( 18 );
130+
target.position.set( 30, 30, 30 );
131+
target.castShadow = true;
132+
target.receiveShadow = true;
133+
torusKnot.add( target );
134+
135+
spotLight.target = target;
136+
cube.add( spotLight );
137+
127138
var geometry = new THREE.BoxBufferGeometry( 10, 0.15, 10 );
128139
var material = new THREE.MeshPhongMaterial( {
129140
color: 0xa0adaf,
@@ -217,8 +228,8 @@
217228
renderShadowMapViewers();
218229

219230
torusKnot.rotation.x += 0.25 * delta;
220-
torusKnot.rotation.y += 2 * delta;
221-
torusKnot.rotation.z += 1 * delta;
231+
torusKnot.rotation.y += 1 * delta;
232+
torusKnot.rotation.z += 2 * delta;
222233

223234
cube.rotation.x += 0.25 * delta;
224235
cube.rotation.y += 2 * delta;

src/renderers/webgl/WebGLShadowMap.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Vector3 } from '../../math/Vector3.js';
1212
import { Vector2 } from '../../math/Vector2.js';
1313
import { Matrix4 } from '../../math/Matrix4.js';
1414
import { Frustum } from '../../math/Frustum.js';
15+
import { Object3D } from '../../core/Object3D.js';
1516

1617
function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
1718

@@ -31,7 +32,9 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
3132
_depthMaterials = new Array( _NumberOfMaterialVariants ),
3233
_distanceMaterials = new Array( _NumberOfMaterialVariants ),
3334

34-
_materialCache = {};
35+
_materialCache = {},
36+
37+
_lightWorld = new Object3D();
3538

3639
var shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide };
3740

@@ -203,14 +206,26 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {
203206

204207
faceCount = 1;
205208

206-
if ( light.target ) {
209+
if ( shadowCamera.matrixAutoUpdate ) {
207210

208-
_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
209-
light.lookAt( _lookTarget );
211+
if ( light.target ) {
210212

211-
}
213+
_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
214+
if ( light.parent ) {
212215

213-
if ( shadowCamera.matrixAutoUpdate ) {
216+
_lightWorld.position.setFromMatrixPosition( light.matrixWorld );
217+
_lightWorld.lookAt( _lookTarget );
218+
light.quaternion.copy( light.parent.quaternion ).inverse();
219+
light.quaternion.multiply( _lightWorld.quaternion );
220+
light.updateMatrixWorld();
221+
222+
} else {
223+
224+
light.lookAt( _lookTarget );
225+
226+
}
227+
228+
}
214229

215230
shadowCamera.matrixWorld.copy( light.matrixWorld );
216231
shadowCamera.matrixWorld.scale( cameraConvention );

0 commit comments

Comments
 (0)