ソースを参照

LightShadow API clean up.

Mr.doob 9 年 前
コミット
0e045c7c4b

+ 3 - 3
examples/js/utils/ShadowMapViewer.js

@@ -35,7 +35,7 @@ THREE.ShadowMapViewer = function ( light ) {
 	var userAutoClearSetting;
 
 	//Holds the initial position and dimension of the HUD
-	var frame = { 
+	var frame = {
 		x: 10,
 		y: 10,
 		width: 256,
@@ -103,7 +103,7 @@ THREE.ShadowMapViewer = function ( light ) {
 
 	//- API
 	// Set to false to disable displaying this shadow map
-	this.enabled = true; 
+	this.enabled = true;
 
 	// Set the size of the displayed shadow map on the HUD
 	this.size = {
@@ -150,7 +150,7 @@ THREE.ShadowMapViewer = function ( light ) {
 			//always end up with the scene's first added shadow casting light's shadowMap
 			//in the shader
 			//See: https://github.com/mrdoob/three.js/issues/5932
-			uniforms.tDiffuse.value = light.shadowMap;
+			uniforms.tDiffuse.value = light.shadow.map;
 
 			userAutoClearSetting = renderer.autoClear;
 			renderer.autoClear = false; // To allow render overlay

+ 9 - 8
examples/webgl_shadowmap_viewer.html

@@ -36,11 +36,11 @@
 		<script src="js/utils/ShadowMapViewer.js"></script>
 
 		<script src="js/Detector.js"></script>
-		
+
 		<script src="js/libs/stats.min.js"></script>
 
 		<script>
-		
+
 			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
 
 			var camera, scene, renderer, clock, stats;
@@ -71,22 +71,24 @@
 				scene = new THREE.Scene();
 
 				// Lights
-				var ambient = new THREE.AmbientLight( 0x404040 );
-				scene.add( ambient );
+
+				scene.add( new THREE.AmbientLight( 0x404040 ) );
 
 				spotLight = new THREE.SpotLight( 0xffffff );
+				spotLight.name = 'Spot Light';
 				spotLight.position.set( 10, 10, 5 );
 				spotLight.castShadow = true;
 				spotLight.shadowCameraNear = 8;
 				spotLight.shadowCameraFar = 30;
 				spotLight.shadowDarkness = 0.5;
-				spotLight.shadowCameraVisible = true;
 				spotLight.shadowMapWidth = 1024;
 				spotLight.shadowMapHeight = 1024;
-				spotLight.name = 'Spot Light';
 				scene.add( spotLight );
 
+				scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
+
 				dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
+				dirLight.name = 'Dir. Light';
 				dirLight.position.set( 0, 10, 0 );
 				dirLight.castShadow = true;
 				dirLight.shadowCameraNear = 1;
@@ -96,12 +98,11 @@
 				dirLight.shadowCameraTop	= 15;
 				dirLight.shadowCameraBottom = -15;
 				dirLight.shadowDarkness = 0.5;
-				dirLight.shadowCameraVisible = true;
 				dirLight.shadowMapWidth = 1024;
 				dirLight.shadowMapHeight = 1024;
-				dirLight.name = 'Dir. Light';
 				scene.add( dirLight );
 
+				scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
 
 				// Geometry
 				var geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );

+ 2 - 0
src/extras/helpers/CameraHelper.js

@@ -94,6 +94,8 @@ THREE.CameraHelper = function ( camera ) {
 	THREE.LineSegments.call( this, geometry, material );
 
 	this.camera = camera;
+	this.camera.updateProjectionMatrix();
+
 	this.matrix = camera.matrixWorld;
 	this.matrixAutoUpdate = false;
 

+ 0 - 54
src/lights/Light.js

@@ -24,63 +24,37 @@ Object.defineProperties( THREE.Light.prototype, {
 			console.warn( 'THREE.Light: .onlyShadow has been removed.' );
 		}
 	},
-	shadowCamera: {
-		get: function () {
-			return this.shadow.camera;
-		}
-	},
 	shadowCameraFov: {
-		get: function () {
-			return this.shadow.camera.fov;
-		},
 		set: function ( value ) {
 			this.shadow.camera.fov = value;
 		}
 	},
 	shadowCameraLeft: {
-		get: function () {
-			return this.shadow.camera.left;
-		},
 		set: function ( value ) {
 			this.shadow.camera.left = value;
 		}
 	},
 	shadowCameraRight: {
-		get: function () {
-			return this.shadow.camera.right;
-		},
 		set: function ( value ) {
 			this.shadow.camera.right = value;
 		}
 	},
 	shadowCameraTop: {
-		get: function () {
-			return this.shadow.camera.top;
-		},
 		set: function ( value ) {
 			this.shadow.camera.top = value;
 		}
 	},
 	shadowCameraBottom: {
-		get: function () {
-			return this.shadow.camera.bottom;
-		},
 		set: function ( value ) {
 			this.shadow.camera.bottom = value;
 		}
 	},
 	shadowCameraNear: {
-		get: function () {
-			return this.shadow.camera.near;
-		},
 		set: function ( value ) {
 			this.shadow.camera.near = value;
 		}
 	},
 	shadowCameraFar: {
-		get: function () {
-			return this.shadow.camera.far;
-		},
 		set: function ( value ) {
 			this.shadow.camera.far = value;
 		}
@@ -91,52 +65,24 @@ Object.defineProperties( THREE.Light.prototype, {
 		}
 	},
 	shadowBias: {
-		get: function () {
-			return this.shadow.bias;
-		},
 		set: function ( value ) {
 			this.shadow.bias = value;
 		}
 	},
 	shadowDarkness: {
-		get: function () {
-			return this.shadow.darkness;
-		},
 		set: function ( value ) {
 			this.shadow.darkness = value;
 		}
 	},
-	shadowMap: {
-		get: function () {
-			return this.shadow.map;
-		},
-		set: function ( value ) {
-			this.shadow.map = value;
-		}
-	},
 	shadowMapWidth: {
-		get: function () {
-			return this.shadow.mapSize.width;
-		},
 		set: function ( value ) {
 			this.shadow.mapSize.width = value;
 		}
 	},
 	shadowMapHeight: {
-		get: function () {
-			return this.shadow.mapSize.height;
-		},
 		set: function ( value ) {
 			this.shadow.mapSize.height = value;
 		}
-	},
-	shadowMatrix: {
-		get: function () {
-			return this.shadow.matrix;
-		},
-		set: function ( value ) {
-			this.shadow.matrix = value;
-		}
 	}
 } );
 

+ 10 - 8
src/renderers/WebGLRenderer.js

@@ -1988,30 +1988,32 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 				var light = lights[ i ];
 
-				if ( ! light.castShadow ) continue;
+				if ( light.castShadow === false ) continue;
 
 				if ( light instanceof THREE.PointLight || light instanceof THREE.SpotLight || light instanceof THREE.DirectionalLight ) {
 
+					var shadow = light.shadow;
+
 					if ( light instanceof THREE.PointLight ) {
 
 						// for point lights we set the shadow matrix to be a translation-only matrix
 						// equal to inverse of the light's position
 						_vector3.setFromMatrixPosition( light.matrixWorld ).negate();
-						light.shadowMatrix.identity().setPosition( _vector3 );
+						shadow.matrix.identity().setPosition( _vector3 );
 
 						// for point lights we set the sign of the shadowDarkness uniform to be negative
-						uniforms.shadowDarkness.value[ j ] = - light.shadowDarkness;
+						uniforms.shadowDarkness.value[ j ] = - shadow.darkness;
 
 					} else {
 
-						uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
+						uniforms.shadowDarkness.value[ j ] = shadow.darkness;
 
 					}
 
-					uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
-					uniforms.shadowMap.value[ j ] = light.shadowMap;
-					uniforms.shadowMapSize.value[ j ] = light.shadow.mapSize;
-					uniforms.shadowBias.value[ j ] = light.shadowBias;
+					uniforms.shadowMatrix.value[ j ] = shadow.matrix;
+					uniforms.shadowMap.value[ j ] = shadow.map;
+					uniforms.shadowMapSize.value[ j ] = shadow.mapSize;
+					uniforms.shadowBias.value[ j ] = shadow.bias;
 
 					j ++;
 

+ 13 - 12
src/renderers/webgl/WebGLShadowMap.js

@@ -56,7 +56,6 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 		var useMorphing = ( i & _MorphingFlag ) !== 0;
 		var useSkinning = ( i & _SkinningFlag ) !== 0;
 
-
 		var depthMaterial = new THREE.ShaderMaterial( {
 			uniforms: depthUniforms,
 			vertexShader: depthShader.vertexShader,
@@ -121,13 +120,17 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 			if ( light.castShadow === false ) continue;
 
+			var shadow = light.shadow;
+			var shadowCamera = shadow.camera;
+			var shadowMapSize = shadow.mapSize;
+
 			if ( light instanceof THREE.PointLight ) {
 
 				faceCount = 6;
 				isPointLight = true;
 
-				var vpWidth = light.shadowMapWidth / 4.0;
-				var vpHeight = light.shadowMapHeight / 2.0;
+				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:
@@ -162,7 +165,7 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 			}
 
-			if ( light.shadowMap === null ) {
+			if ( shadow.map === null ) {
 
 				var shadowFilter = THREE.LinearFilter;
 
@@ -174,25 +177,23 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 				var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
 
-				light.shadowMap = new THREE.WebGLRenderTarget( light.shadow.mapSize.width, light.shadow.mapSize.height, pars );
-
-				light.shadowMatrix = new THREE.Matrix4();
+				shadow.map = new THREE.WebGLRenderTarget( shadowMapSize.x, shadowMapSize.y, pars );
+				shadow.matrix = new THREE.Matrix4();
 
 				//
 
 				if ( light instanceof THREE.SpotLight ) {
 
-					light.shadowCamera.aspect = light.shadow.mapSize.x / light.shadow.mapSize.y;
+					shadowCamera.aspect = shadowMapSize.x / shadowMapSize.y;
 
 				}
 
-				light.shadowCamera.updateProjectionMatrix();
+				shadowCamera.updateProjectionMatrix();
 
 			}
 
-			var shadowMap = light.shadowMap;
-			var shadowMatrix = light.shadowMatrix;
-			var shadowCamera = light.shadowCamera;
+			var shadowMap = shadow.map;
+			var shadowMatrix = shadow.matrix;
 
 			_lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
 			shadowCamera.position.copy( _lightPositionWorld );