瀏覽代碼

Added DirectionalLightShadow and SpotLightShadow. Automatically adjust SpotLightShadow. See #7690.

Mr.doob 9 年之前
父節點
當前提交
b23a98f82b

+ 1 - 1
src/lights/DirectionalLight.js

@@ -14,7 +14,7 @@ THREE.DirectionalLight = function ( color, intensity ) {
 
 	this.target = new THREE.Object3D();
 
-	this.shadow = new THREE.LightShadow( new THREE.OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
+	this.shadow = new THREE.DirectionalLightShadow();
 
 };
 

+ 9 - 0
src/lights/DirectionalLightShadow.js

@@ -0,0 +1,9 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.DirectionalLightShadow = function ( light ) {
+
+	THREE.LightShadow.call( this, new THREE.OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
+
+};

+ 1 - 1
src/lights/SpotLight.js

@@ -18,7 +18,7 @@ THREE.SpotLight = function ( color, intensity, distance, angle, penumbra, decay
 	this.penumbra = ( penumbra !== undefined ) ? penumbra : 0;
 	this.decay = ( decay !== undefined ) ? decay : 1;	// for physically correct lights, should be 2.
 
-	this.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 0.5, 500 ) );
+	this.shadow = new THREE.SpotLightShadow();
 
 };
 

+ 34 - 0
src/lights/SpotLightShadow.js

@@ -0,0 +1,34 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.SpotLightShadow = function () {
+
+	THREE.LightShadow.call( this, new THREE.PerspectiveCamera( 50, 1, 0.5, 500 ) );
+
+};
+
+THREE.SpotLightShadow.prototype = {
+
+	constructor: THREE.SpotLightShadow,
+
+	update: function ( light ) {
+
+		var fov = THREE.Math.radToDeg( 2 * light.angle );
+		var aspect = this.mapSize.width / this.mapSize.height;
+		var far = light.distance || 500;
+
+		var camera = this.camera;
+
+		if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) {
+
+			camera.fov = fov;
+			camera.aspect = aspect;
+			camera.far = far;
+			camera.updateProjectionMatrix();
+
+		}
+
+	}
+
+};

+ 3 - 7
src/renderers/webgl/WebGLShadowMap.js

@@ -171,15 +171,11 @@ THREE.WebGLShadowMap = function ( _renderer, _lights, _objects ) {
 
 				shadow.map = new THREE.WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars );
 
-				//
-
-				if ( light instanceof THREE.SpotLight ) {
-
-					shadowCamera.aspect = _shadowMapSize.x / _shadowMapSize.y;
+			}
 
-				}
+			if ( shadow instanceof THREE.SpotLightShadow ) {
 
-				shadowCamera.updateProjectionMatrix();
+				shadow.update( light );
 
 			}
 

+ 2 - 0
utils/build/includes/common.json

@@ -67,9 +67,11 @@
 	"src/lights/LightShadow.js",
 	"src/lights/AmbientLight.js",
 	"src/lights/DirectionalLight.js",
+	"src/lights/DirectionalLightShadow.js",
 	"src/lights/HemisphereLight.js",
 	"src/lights/PointLight.js",
 	"src/lights/SpotLight.js",
+	"src/lights/SpotLightShadow.js",
 	"src/loaders/AudioLoader.js",
 	"src/loaders/Cache.js",
 	"src/loaders/Loader.js",