From b1f94dacc3e741adffe906dd9ba9df969f04e617 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Mon, 8 Feb 2016 11:58:00 +0900 Subject: [PATCH] SpotLight: Implemented @sgrif's penumbra with @WestLangley suggestions. See #5080. --- src/lights/Light.js | 2 +- src/lights/SpotLight.js | 6 +++--- src/loaders/ObjectLoader.js | 2 +- src/renderers/WebGLRenderer.js | 2 +- src/renderers/shaders/ShaderChunk/lights_pars.glsl | 4 ++-- src/renderers/shaders/UniformsLib.js | 2 +- src/renderers/webgl/WebGLLights.js | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lights/Light.js b/src/lights/Light.js index b7d9809d7d725b..452c767220ee50 100644 --- a/src/lights/Light.js +++ b/src/lights/Light.js @@ -42,7 +42,7 @@ THREE.Light.prototype.toJSON = function ( meta ) { if ( this.distance !== undefined ) data.object.distance = this.distance; if ( this.angle !== undefined ) data.object.angle = this.angle; if ( this.decay !== undefined ) data.object.decay = this.decay; - if ( this.exponent !== undefined ) data.object.exponent = this.exponent; + if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra; return data; diff --git a/src/lights/SpotLight.js b/src/lights/SpotLight.js index 77ae89394ffaff..4cb30172a5383a 100644 --- a/src/lights/SpotLight.js +++ b/src/lights/SpotLight.js @@ -2,7 +2,7 @@ * @author alteredq / http://alteredqualia.com/ */ -THREE.SpotLight = function ( color, intensity, distance, angle, exponent, decay ) { +THREE.SpotLight = function ( color, intensity, distance, angle, penumbra, decay ) { THREE.Light.call( this, color, intensity ); @@ -15,7 +15,7 @@ THREE.SpotLight = function ( color, intensity, distance, angle, exponent, decay this.distance = ( distance !== undefined ) ? distance : 0; this.angle = ( angle !== undefined ) ? angle : Math.PI / 3; - this.exponent = ( exponent !== undefined ) ? exponent : 10; + 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 ) ); @@ -31,7 +31,7 @@ THREE.SpotLight.prototype.copy = function ( source ) { this.distance = source.distance; this.angle = source.angle; - this.exponent = source.exponent; + this.penumbra = source.penumbra; this.decay = source.decay; this.target = source.target.clone(); diff --git a/src/loaders/ObjectLoader.js b/src/loaders/ObjectLoader.js index 2f205144a1e6a2..fb5b8c6b2296eb 100755 --- a/src/loaders/ObjectLoader.js +++ b/src/loaders/ObjectLoader.js @@ -514,7 +514,7 @@ THREE.ObjectLoader.prototype = { case 'SpotLight': - object = new THREE.SpotLight( data.color, data.intensity, data.distance, data.angle, data.exponent, data.decay ); + object = new THREE.SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay ); break; diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 0bae8a4fac8a27..2da2de0a3901c2 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2702,7 +2702,7 @@ THREE.WebGLRenderer = function ( parameters ) { uniforms.direction.transformDirection( viewMatrix ); uniforms.angleCos = Math.cos( light.angle ); - uniforms.exponent = light.exponent; + uniforms.penumbra = Math.cos( light.angle ) * light.penumbra; uniforms.decay = ( light.distance === 0 ) ? 0.0 : light.decay; uniforms.shadow = light.castShadow; diff --git a/src/renderers/shaders/ShaderChunk/lights_pars.glsl b/src/renderers/shaders/ShaderChunk/lights_pars.glsl index af8764c8d93a55..a28ce8f251353d 100644 --- a/src/renderers/shaders/ShaderChunk/lights_pars.glsl +++ b/src/renderers/shaders/ShaderChunk/lights_pars.glsl @@ -68,7 +68,7 @@ float distance; float decay; float angleCos; - float exponent; + float penumbra; int shadow; float shadowBias; @@ -90,7 +90,7 @@ if ( spotEffect > spotLight.angleCos ) { float spotEffect = dot( spotLight.direction, directLight.direction ); - spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) ); + spotEffect *= clamp( ( spotEffect - spotLight.angleCos ) / spotLight.penumbra, 0.0, 1.0 ); directLight.color = spotLight.color; directLight.color *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) ); diff --git a/src/renderers/shaders/UniformsLib.js b/src/renderers/shaders/UniformsLib.js index 60e201c001397a..e7ab4baeb5a6fc 100644 --- a/src/renderers/shaders/UniformsLib.js +++ b/src/renderers/shaders/UniformsLib.js @@ -112,7 +112,7 @@ THREE.UniformsLib = { "direction": { type: "v3" }, "distance": { type: "f" }, "angleCos": { type: "f" }, - "exponent": { type: "f" }, + "penumbra": { type: "f" }, "decay": { type: "f" }, "shadow": { type: "i" }, diff --git a/src/renderers/webgl/WebGLLights.js b/src/renderers/webgl/WebGLLights.js index a4239044a607c0..c382d11bf21ee3 100644 --- a/src/renderers/webgl/WebGLLights.js +++ b/src/renderers/webgl/WebGLLights.js @@ -37,7 +37,7 @@ THREE.WebGLLights = function () { color: new THREE.Color(), distance: 0, angleCos: 0, - exponent: 0, + penumbra: 0, decay: 0, shadow: false,