Skip to content

Commit a83c567

Browse files
committed
WebGLRenderer: Fixed castShadow.
1 parent a8e787b commit a83c567

File tree

2 files changed

+141
-137
lines changed

2 files changed

+141
-137
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,34 +1988,36 @@ THREE.WebGLRenderer = function ( parameters ) {
19881988

19891989
var light = lights[ i ];
19901990

1991-
if ( light.castShadow === false ) continue;
1991+
if ( light.castShadow === true ) {
19921992

1993-
if ( light instanceof THREE.PointLight || light instanceof THREE.SpotLight || light instanceof THREE.DirectionalLight ) {
1993+
if ( light instanceof THREE.PointLight || light instanceof THREE.SpotLight || light instanceof THREE.DirectionalLight ) {
19941994

1995-
var shadow = light.shadow;
1995+
var shadow = light.shadow;
19961996

1997-
if ( light instanceof THREE.PointLight ) {
1997+
if ( light instanceof THREE.PointLight ) {
19981998

1999-
// for point lights we set the shadow matrix to be a translation-only matrix
2000-
// equal to inverse of the light's position
2001-
_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
2002-
shadow.matrix.identity().setPosition( _vector3 );
1999+
// for point lights we set the shadow matrix to be a translation-only matrix
2000+
// equal to inverse of the light's position
2001+
_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
2002+
shadow.matrix.identity().setPosition( _vector3 );
20032003

2004-
// for point lights we set the sign of the shadowDarkness uniform to be negative
2005-
uniforms.shadowDarkness.value[ j ] = - shadow.darkness;
2004+
// for point lights we set the sign of the shadowDarkness uniform to be negative
2005+
uniforms.shadowDarkness.value[ j ] = - shadow.darkness;
20062006

2007-
} else {
2007+
} else {
20082008

2009-
uniforms.shadowDarkness.value[ j ] = shadow.darkness;
2009+
uniforms.shadowDarkness.value[ j ] = shadow.darkness;
20102010

2011-
}
2011+
}
2012+
2013+
uniforms.shadowMatrix.value[ j ] = shadow.matrix;
2014+
uniforms.shadowMap.value[ j ] = shadow.map;
2015+
uniforms.shadowMapSize.value[ j ] = shadow.mapSize;
2016+
uniforms.shadowBias.value[ j ] = shadow.bias;
20122017

2013-
uniforms.shadowMatrix.value[ j ] = shadow.matrix;
2014-
uniforms.shadowMap.value[ j ] = shadow.map;
2015-
uniforms.shadowMapSize.value[ j ] = shadow.mapSize;
2016-
uniforms.shadowBias.value[ j ] = shadow.bias;
2018+
j ++;
20172019

2018-
j ++;
2020+
}
20192021

20202022
}
20212023

src/renderers/webgl/WebGLShadowMap.js

Lines changed: 121 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -118,178 +118,180 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
118118

119119
var light = _lights[ i ];
120120

121-
if ( light.castShadow === false ) continue;
122-
123-
var shadow = light.shadow;
124-
var shadowCamera = shadow.camera;
125-
var shadowMapSize = shadow.mapSize;
126-
127-
if ( light instanceof THREE.PointLight ) {
128-
129-
faceCount = 6;
130-
isPointLight = true;
131-
132-
var vpWidth = shadowMapSize.x / 4.0;
133-
var vpHeight = shadowMapSize.y / 2.0;
134-
135-
// These viewports map a cube-map onto a 2D texture with the
136-
// following orientation:
137-
//
138-
// xzXZ
139-
// y Y
140-
//
141-
// X - Positive x direction
142-
// x - Negative x direction
143-
// Y - Positive y direction
144-
// y - Negative y direction
145-
// Z - Positive z direction
146-
// z - Negative z direction
147-
148-
// positive X
149-
cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
150-
// negative X
151-
cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
152-
// positive Z
153-
cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
154-
// negative Z
155-
cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
156-
// positive Y
157-
cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
158-
// negative Y
159-
cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
160-
161-
} else {
162-
163-
faceCount = 1;
164-
isPointLight = false;
121+
if ( light.castShadow === true ) {
122+
123+
var shadow = light.shadow;
124+
var shadowCamera = shadow.camera;
125+
var shadowMapSize = shadow.mapSize;
126+
127+
if ( light instanceof THREE.PointLight ) {
128+
129+
faceCount = 6;
130+
isPointLight = true;
131+
132+
var vpWidth = shadowMapSize.x / 4.0;
133+
var vpHeight = shadowMapSize.y / 2.0;
134+
135+
// These viewports map a cube-map onto a 2D texture with the
136+
// following orientation:
137+
//
138+
// xzXZ
139+
// y Y
140+
//
141+
// X - Positive x direction
142+
// x - Negative x direction
143+
// Y - Positive y direction
144+
// y - Negative y direction
145+
// Z - Positive z direction
146+
// z - Negative z direction
147+
148+
// positive X
149+
cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
150+
// negative X
151+
cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
152+
// positive Z
153+
cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
154+
// negative Z
155+
cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
156+
// positive Y
157+
cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
158+
// negative Y
159+
cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
165160

166-
}
161+
} else {
167162

168-
if ( shadow.map === null ) {
163+
faceCount = 1;
164+
isPointLight = false;
169165

170-
var shadowFilter = THREE.LinearFilter;
166+
}
171167

172-
if ( scope.type === THREE.PCFSoftShadowMap ) {
168+
if ( shadow.map === null ) {
173169

174-
shadowFilter = THREE.NearestFilter;
170+
var shadowFilter = THREE.LinearFilter;
175171

176-
}
172+
if ( scope.type === THREE.PCFSoftShadowMap ) {
177173

178-
var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
174+
shadowFilter = THREE.NearestFilter;
179175

180-
shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
181-
shadow.matrix = new THREE.Matrix4();
176+
}
182177

183-
//
178+
var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
184179

185-
if ( light instanceof THREE.SpotLight ) {
180+
shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
181+
shadow.matrix = new THREE.Matrix4();
186182

187-
shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
183+
//
188184

189-
}
185+
if ( light instanceof THREE.SpotLight ) {
190186

191-
shadowCamera.updateProjectionMatrix();
187+
shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
192188

193-
}
189+
}
194190

195-
var shadowMap = shadow.map;
196-
var shadowMatrix = shadow.matrix;
191+
shadowCamera.updateProjectionMatrix();
197192

198-
_lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
199-
shadowCamera.position.copy( _lightPositionWorld );
193+
}
200194

201-
_renderer.setRenderTarget( shadowMap );
202-
_renderer.clear();
195+
var shadowMap = shadow.map;
196+
var shadowMatrix = shadow.matrix;
203197

204-
// render shadow map for each cube face (if omni-directional) or
205-
// run a single pass if not
198+
_lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
199+
shadowCamera.position.copy( _lightPositionWorld );
206200

207-
for ( var face = 0; face < faceCount; face ++ ) {
201+
_renderer.setRenderTarget( shadowMap );
202+
_renderer.clear();
208203

209-
if ( isPointLight ) {
204+
// render shadow map for each cube face (if omni-directional) or
205+
// run a single pass if not
210206

211-
_lookTarget.copy( shadowCamera.position );
212-
_lookTarget.add( cubeDirections[ face ] );
213-
shadowCamera.up.copy( cubeUps[ face ] );
214-
shadowCamera.lookAt( _lookTarget );
215-
var vpDimensions = cube2DViewPorts[ face ];
216-
_renderer.setViewport( vpDimensions.x, vpDimensions.y, vpDimensions.z, vpDimensions.w );
207+
for ( var face = 0; face < faceCount; face ++ ) {
217208

218-
} else {
209+
if ( isPointLight ) {
219210

220-
_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
221-
shadowCamera.lookAt( _lookTarget );
211+
_lookTarget.copy( shadowCamera.position );
212+
_lookTarget.add( cubeDirections[ face ] );
213+
shadowCamera.up.copy( cubeUps[ face ] );
214+
shadowCamera.lookAt( _lookTarget );
215+
var vpDimensions = cube2DViewPorts[ face ];
216+
_renderer.setViewport( vpDimensions.x, vpDimensions.y, vpDimensions.z, vpDimensions.w );
222217

223-
}
218+
} else {
219+
220+
_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
221+
shadowCamera.lookAt( _lookTarget );
222+
223+
}
224+
225+
shadowCamera.updateMatrixWorld();
226+
shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
224227

225-
shadowCamera.updateMatrixWorld();
226-
shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
228+
// compute shadow matrix
227229

228-
// compute shadow matrix
230+
shadowMatrix.set(
231+
0.5, 0.0, 0.0, 0.5,
232+
0.0, 0.5, 0.0, 0.5,
233+
0.0, 0.0, 0.5, 0.5,
234+
0.0, 0.0, 0.0, 1.0
235+
);
229236

230-
shadowMatrix.set(
231-
0.5, 0.0, 0.0, 0.5,
232-
0.0, 0.5, 0.0, 0.5,
233-
0.0, 0.0, 0.5, 0.5,
234-
0.0, 0.0, 0.0, 1.0
235-
);
237+
shadowMatrix.multiply( shadowCamera.projectionMatrix );
238+
shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
236239

237-
shadowMatrix.multiply( shadowCamera.projectionMatrix );
238-
shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
240+
// update camera matrices and frustum
239241

240-
// update camera matrices and frustum
242+
_projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
243+
_frustum.setFromMatrix( _projScreenMatrix );
241244

242-
_projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
243-
_frustum.setFromMatrix( _projScreenMatrix );
245+
// set object matrices & frustum culling
244246

245-
// set object matrices & frustum culling
247+
_renderList.length = 0;
246248

247-
_renderList.length = 0;
249+
projectObject( scene, shadowCamera );
248250

249-
projectObject( scene, shadowCamera );
251+
// render shadow map
252+
// render regular objects
250253

251-
// render shadow map
252-
// render regular objects
254+
for ( var j = 0, jl = _renderList.length; j < jl; j ++ ) {
253255

254-
for ( var j = 0, jl = _renderList.length; j < jl; j ++ ) {
256+
var object = _renderList[ j ];
257+
var geometry = _objects.update( object );
258+
var material = object.material;
255259

256-
var object = _renderList[ j ];
257-
var geometry = _objects.update( object );
258-
var material = object.material;
260+
if ( material instanceof THREE.MeshFaceMaterial ) {
259261

260-
if ( material instanceof THREE.MeshFaceMaterial ) {
262+
var groups = geometry.groups;
263+
var materials = material.materials;
261264

262-
var groups = geometry.groups;
263-
var materials = material.materials;
265+
for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
264266

265-
for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
267+
var group = groups[ k ];
268+
var groupMaterial = materials[ group.materialIndex ];
266269

267-
var group = groups[ k ];
268-
var groupMaterial = materials[ group.materialIndex ];
270+
if ( groupMaterial.visible === true ) {
269271

270-
if ( groupMaterial.visible === true ) {
272+
var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld );
273+
_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, group );
271274

272-
var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld );
273-
_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, group );
275+
}
274276

275277
}
276278

277-
}
279+
} else {
278280

279-
} else {
281+
var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld );
282+
_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, null );
280283

281-
var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld );
282-
_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, null );
284+
}
283285

284286
}
285287

286288
}
287289

288-
}
290+
// We must call _renderer.resetGLState() at the end of each iteration of
291+
// the light loop in order to force material updates for each light.
292+
_renderer.resetGLState();
289293

290-
//We must call _renderer.resetGLState() at the end of each iteration of
291-
// the light loop in order to force material updates for each light.
292-
_renderer.resetGLState();
294+
}
293295

294296
}
295297

0 commit comments

Comments
 (0)