@@ -118,178 +118,180 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
118
118
119
119
var light = _lights [ i ] ;
120
120
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 ) ;
165
160
166
- }
161
+ } else {
167
162
168
- if ( shadow . map === null ) {
163
+ faceCount = 1 ;
164
+ isPointLight = false ;
169
165
170
- var shadowFilter = THREE . LinearFilter ;
166
+ }
171
167
172
- if ( scope . type === THREE . PCFSoftShadowMap ) {
168
+ if ( shadow . map === null ) {
173
169
174
- shadowFilter = THREE . NearestFilter ;
170
+ var shadowFilter = THREE . LinearFilter ;
175
171
176
- }
172
+ if ( scope . type === THREE . PCFSoftShadowMap ) {
177
173
178
- var pars = { minFilter : shadowFilter , magFilter : shadowFilter , format : THREE . RGBAFormat } ;
174
+ shadowFilter = THREE . NearestFilter ;
179
175
180
- shadow . map = new THREE . WebGLRenderTarget ( shadowMapSize . x , shadowMapSize . y , pars ) ;
181
- shadow . matrix = new THREE . Matrix4 ( ) ;
176
+ }
182
177
183
- //
178
+ var pars = { minFilter : shadowFilter , magFilter : shadowFilter , format : THREE . RGBAFormat } ;
184
179
185
- if ( light instanceof THREE . SpotLight ) {
180
+ shadow . map = new THREE . WebGLRenderTarget ( shadowMapSize . x , shadowMapSize . y , pars ) ;
181
+ shadow . matrix = new THREE . Matrix4 ( ) ;
186
182
187
- shadowCamera . aspect = shadowMapSize . x / shadowMapSize . y ;
183
+ //
188
184
189
- }
185
+ if ( light instanceof THREE . SpotLight ) {
190
186
191
- shadowCamera . updateProjectionMatrix ( ) ;
187
+ shadowCamera . aspect = shadowMapSize . x / shadowMapSize . y ;
192
188
193
- }
189
+ }
194
190
195
- var shadowMap = shadow . map ;
196
- var shadowMatrix = shadow . matrix ;
191
+ shadowCamera . updateProjectionMatrix ( ) ;
197
192
198
- _lightPositionWorld . setFromMatrixPosition ( light . matrixWorld ) ;
199
- shadowCamera . position . copy ( _lightPositionWorld ) ;
193
+ }
200
194
201
- _renderer . setRenderTarget ( shadowMap ) ;
202
- _renderer . clear ( ) ;
195
+ var shadowMap = shadow . map ;
196
+ var shadowMatrix = shadow . matrix ;
203
197
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 ) ;
206
200
207
- for ( var face = 0 ; face < faceCount ; face ++ ) {
201
+ _renderer . setRenderTarget ( shadowMap ) ;
202
+ _renderer . clear ( ) ;
208
203
209
- if ( isPointLight ) {
204
+ // render shadow map for each cube face (if omni-directional) or
205
+ // run a single pass if not
210
206
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 ++ ) {
217
208
218
- } else {
209
+ if ( isPointLight ) {
219
210
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 ) ;
222
217
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 ) ;
224
227
225
- shadowCamera . updateMatrixWorld ( ) ;
226
- shadowCamera . matrixWorldInverse . getInverse ( shadowCamera . matrixWorld ) ;
228
+ // compute shadow matrix
227
229
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
+ ) ;
229
236
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 ) ;
236
239
237
- shadowMatrix . multiply ( shadowCamera . projectionMatrix ) ;
238
- shadowMatrix . multiply ( shadowCamera . matrixWorldInverse ) ;
240
+ // update camera matrices and frustum
239
241
240
- // update camera matrices and frustum
242
+ _projScreenMatrix . multiplyMatrices ( shadowCamera . projectionMatrix , shadowCamera . matrixWorldInverse ) ;
243
+ _frustum . setFromMatrix ( _projScreenMatrix ) ;
241
244
242
- _projScreenMatrix . multiplyMatrices ( shadowCamera . projectionMatrix , shadowCamera . matrixWorldInverse ) ;
243
- _frustum . setFromMatrix ( _projScreenMatrix ) ;
245
+ // set object matrices & frustum culling
244
246
245
- // set object matrices & frustum culling
247
+ _renderList . length = 0 ;
246
248
247
- _renderList . length = 0 ;
249
+ projectObject ( scene , shadowCamera ) ;
248
250
249
- projectObject ( scene , shadowCamera ) ;
251
+ // render shadow map
252
+ // render regular objects
250
253
251
- // render shadow map
252
- // render regular objects
254
+ for ( var j = 0 , jl = _renderList . length ; j < jl ; j ++ ) {
253
255
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 ;
255
259
256
- var object = _renderList [ j ] ;
257
- var geometry = _objects . update ( object ) ;
258
- var material = object . material ;
260
+ if ( material instanceof THREE . MeshFaceMaterial ) {
259
261
260
- if ( material instanceof THREE . MeshFaceMaterial ) {
262
+ var groups = geometry . groups ;
263
+ var materials = material . materials ;
261
264
262
- var groups = geometry . groups ;
263
- var materials = material . materials ;
265
+ for ( var k = 0 , kl = groups . length ; k < kl ; k ++ ) {
264
266
265
- for ( var k = 0 , kl = groups . length ; k < kl ; k ++ ) {
267
+ var group = groups [ k ] ;
268
+ var groupMaterial = materials [ group . materialIndex ] ;
266
269
267
- var group = groups [ k ] ;
268
- var groupMaterial = materials [ group . materialIndex ] ;
270
+ if ( groupMaterial . visible === true ) {
269
271
270
- if ( groupMaterial . visible === true ) {
272
+ var depthMaterial = getDepthMaterial ( object , groupMaterial , isPointLight , _lightPositionWorld ) ;
273
+ _renderer . renderBufferDirect ( shadowCamera , _lights , null , geometry , depthMaterial , object , group ) ;
271
274
272
- var depthMaterial = getDepthMaterial ( object , groupMaterial , isPointLight , _lightPositionWorld ) ;
273
- _renderer . renderBufferDirect ( shadowCamera , _lights , null , geometry , depthMaterial , object , group ) ;
275
+ }
274
276
275
277
}
276
278
277
- }
279
+ } else {
278
280
279
- } else {
281
+ var depthMaterial = getDepthMaterial ( object , material , isPointLight , _lightPositionWorld ) ;
282
+ _renderer . renderBufferDirect ( shadowCamera , _lights , null , geometry , depthMaterial , object , null ) ;
280
283
281
- var depthMaterial = getDepthMaterial ( object , material , isPointLight , _lightPositionWorld ) ;
282
- _renderer . renderBufferDirect ( shadowCamera , _lights , null , geometry , depthMaterial , object , null ) ;
284
+ }
283
285
284
286
}
285
287
286
288
}
287
289
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 ( ) ;
289
293
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
+ }
293
295
294
296
}
295
297
0 commit comments