Browse Source

WebGLShadowMap: Use parsed lights array.

Mr.doob 9 years ago
parent
commit
a103cd56b1
2 changed files with 124 additions and 126 deletions
  1. 1 1
      src/renderers/WebGLRenderer.js
  2. 123 125
      src/renderers/webgl/WebGLShadowMap.js

+ 1 - 1
src/renderers/WebGLRenderer.js

@@ -259,7 +259,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	// shadow map
 
-	var shadowMap = new THREE.WebGLShadowMap( this, lights, objects );
+	var shadowMap = new THREE.WebGLShadowMap( this, _lights, objects );
 
 	this.shadowMap = shadowMap;
 

+ 123 - 125
src/renderers/webgl/WebGLShadowMap.js

@@ -114,185 +114,183 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 		// render depth map
 
-		for ( var i = 0, il = _lights.length; i < il; i ++ ) {
-
-			var light = _lights[ i ];
-
-			if ( light.castShadow === true ) {
-
-				var shadow = light.shadow;
-				var shadowCamera = shadow.camera;
-				var shadowMapSize = shadow.mapSize;
-
-				if ( light instanceof THREE.PointLight ) {
-
-					faceCount = 6;
-					isPointLight = true;
-
-					var vpWidth = shadowMapSize.x / 4.0;
-					var vpHeight = shadowMapSize.y / 2.0;
-
-					// These viewports map a cube-map onto a 2D texture with the
-					// following orientation:
-					//
-					//  xzXZ
-					//   y Y
-					//
-					// X - Positive x direction
-					// x - Negative x direction
-					// Y - Positive y direction
-					// y - Negative y direction
-					// Z - Positive z direction
-					// z - Negative z direction
-
-					// positive X
-					cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
-					// negative X
-					cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
-					// positive Z
-					cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
-					// negative Z
-					cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
-					// positive Y
-					cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
-					// negative Y
-					cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
+		var shadows = _lights.shadows;
+
+		for ( var i = 0, il = shadows.length; i < il; i ++ ) {
+
+			var light = shadows[ i ][ 0 ];
+
+			var shadow = light.shadow;
+			var shadowCamera = shadow.camera;
+			var shadowMapSize = shadow.mapSize;
+
+			if ( light instanceof THREE.PointLight ) {
+
+				faceCount = 6;
+				isPointLight = true;
+
+				var vpWidth = shadowMapSize.x / 4.0;
+				var vpHeight = shadowMapSize.y / 2.0;
+
+				// These viewports map a cube-map onto a 2D texture with the
+				// following orientation:
+				//
+				//  xzXZ
+				//   y Y
+				//
+				// X - Positive x direction
+				// x - Negative x direction
+				// Y - Positive y direction
+				// y - Negative y direction
+				// Z - Positive z direction
+				// z - Negative z direction
+
+				// positive X
+				cube2DViewPorts[ 0 ].set( vpWidth * 2, vpHeight, vpWidth, vpHeight );
+				// negative X
+				cube2DViewPorts[ 1 ].set( 0, vpHeight, vpWidth, vpHeight );
+				// positive Z
+				cube2DViewPorts[ 2 ].set( vpWidth * 3, vpHeight, vpWidth, vpHeight );
+				// negative Z
+				cube2DViewPorts[ 3 ].set( vpWidth, vpHeight, vpWidth, vpHeight );
+				// positive Y
+				cube2DViewPorts[ 4 ].set( vpWidth * 3, 0, vpWidth, vpHeight );
+				// negative Y
+				cube2DViewPorts[ 5 ].set( vpWidth, 0, vpWidth, vpHeight );
+
+			} else {
+
+				faceCount = 1;
+				isPointLight = false;
 
-				} else {
-
-					faceCount = 1;
-					isPointLight = false;
-
-				}
-
-				if ( shadow.map === null ) {
-
-					var shadowFilter = THREE.LinearFilter;
+			}
 
-					if ( scope.type === THREE.PCFSoftShadowMap ) {
+			if ( shadow.map === null ) {
 
-						shadowFilter = THREE.NearestFilter;
+				var shadowFilter = THREE.LinearFilter;
 
-					}
+				if ( scope.type === THREE.PCFSoftShadowMap ) {
 
-					var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
+					shadowFilter = THREE.NearestFilter;
 
-					shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
-					shadow.matrix = new THREE.Matrix4();
+				}
 
-					//
+				var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
 
-					if ( light instanceof THREE.SpotLight ) {
+				shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
+				shadow.matrix = new THREE.Matrix4();
 
-						shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
+				//
 
-					}
+				if ( light instanceof THREE.SpotLight ) {
 
-					shadowCamera.updateProjectionMatrix();
+					shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
 
 				}
 
-				var shadowMap = shadow.map;
-				var shadowMatrix = shadow.matrix;
+				shadowCamera.updateProjectionMatrix();
 
-				_lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
-				shadowCamera.position.copy( _lightPositionWorld );
+			}
 
-				_renderer.setRenderTarget( shadowMap );
-				_renderer.clear();
+			var shadowMap = shadow.map;
+			var shadowMatrix = shadow.matrix;
 
-				// render shadow map for each cube face (if omni-directional) or
-				// run a single pass if not
+			_lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
+			shadowCamera.position.copy( _lightPositionWorld );
 
-				for ( var face = 0; face < faceCount; face ++ ) {
+			_renderer.setRenderTarget( shadowMap );
+			_renderer.clear();
 
-					if ( isPointLight ) {
+			// render shadow map for each cube face (if omni-directional) or
+			// run a single pass if not
 
-						_lookTarget.copy( shadowCamera.position );
-						_lookTarget.add( cubeDirections[ face ] );
-						shadowCamera.up.copy( cubeUps[ face ] );
-						shadowCamera.lookAt( _lookTarget );
-						var vpDimensions = cube2DViewPorts[ face ];
-						_renderer.setViewport( vpDimensions.x, vpDimensions.y, vpDimensions.z, vpDimensions.w );
+			for ( var face = 0; face < faceCount; face ++ ) {
 
-					} else {
+				if ( isPointLight ) {
 
-						_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
-						shadowCamera.lookAt( _lookTarget );
+					_lookTarget.copy( shadowCamera.position );
+					_lookTarget.add( cubeDirections[ face ] );
+					shadowCamera.up.copy( cubeUps[ face ] );
+					shadowCamera.lookAt( _lookTarget );
+					var vpDimensions = cube2DViewPorts[ face ];
+					_renderer.setViewport( vpDimensions.x, vpDimensions.y, vpDimensions.z, vpDimensions.w );
 
-					}
+				} else {
 
-					shadowCamera.updateMatrixWorld();
-					shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
+					_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
+					shadowCamera.lookAt( _lookTarget );
+
+				}
 
-					// compute shadow matrix
+				shadowCamera.updateMatrixWorld();
+				shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
 
-					shadowMatrix.set(
-						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
-					);
+				// compute shadow matrix
 
-					shadowMatrix.multiply( shadowCamera.projectionMatrix );
-					shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
+				shadowMatrix.set(
+					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
+				);
 
-					// update camera matrices and frustum
+				shadowMatrix.multiply( shadowCamera.projectionMatrix );
+				shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
 
-					_projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
-					_frustum.setFromMatrix( _projScreenMatrix );
+				// update camera matrices and frustum
 
-					// set object matrices & frustum culling
+				_projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
+				_frustum.setFromMatrix( _projScreenMatrix );
 
-					_renderList.length = 0;
+				// set object matrices & frustum culling
 
-					projectObject( scene, camera, shadowCamera );
+				_renderList.length = 0;
 
-					// render shadow map
-					// render regular objects
+				projectObject( scene, camera, shadowCamera );
 
-					for ( var j = 0, jl = _renderList.length; j < jl; j ++ ) {
+				// render shadow map
+				// render regular objects
 
-						var object = _renderList[ j ];
-						var geometry = _objects.update( object );
-						var material = object.material;
+				for ( var j = 0, jl = _renderList.length; j < jl; j ++ ) {
 
-						if ( material instanceof THREE.MeshFaceMaterial ) {
+					var object = _renderList[ j ];
+					var geometry = _objects.update( object );
+					var material = object.material;
 
-							var groups = geometry.groups;
-							var materials = material.materials;
+					if ( material instanceof THREE.MeshFaceMaterial ) {
 
-							for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
+						var groups = geometry.groups;
+						var materials = material.materials;
 
-								var group = groups[ k ];
-								var groupMaterial = materials[ group.materialIndex ];
+						for ( var k = 0, kl = groups.length; k < kl; k ++ ) {
 
-								if ( groupMaterial.visible === true ) {
+							var group = groups[ k ];
+							var groupMaterial = materials[ group.materialIndex ];
 
-									var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld );
-									_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, group );
+							if ( groupMaterial.visible === true ) {
 
-								}
+								var depthMaterial = getDepthMaterial( object, groupMaterial, isPointLight, _lightPositionWorld );
+								_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, group );
 
 							}
 
-						} else {
+						}
 
-							var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld );
-							_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, null );
+					} else {
 
-						}
+						var depthMaterial = getDepthMaterial( object, material, isPointLight, _lightPositionWorld );
+						_renderer.renderBufferDirect( shadowCamera, _lights, null, geometry, depthMaterial, object, null );
 
 					}
 
 				}
 
-				// We must call _renderer.resetGLState() at the end of each iteration of
-				// the light loop in order to force material updates for each light.
-				_renderer.resetGLState();
-
 			}
 
+			// We must call _renderer.resetGLState() at the end of each iteration of
+			// the light loop in order to force material updates for each light.
+			_renderer.resetGLState();
+
 		}
 
 		_renderer.setViewport( _vector4.x, _vector4.y, _vector4.z, _vector4.w );