Skip to content

Commit

Permalink
light.target optional + optional camera construction param
Browse files Browse the repository at this point in the history
  • Loading branch information
mbredif committed Aug 8, 2018
1 parent 6201c9a commit 7065b86
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 94 deletions.
2 changes: 2 additions & 0 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ THREE.GLTFLoader = ( function () {

case 'directional':
lightNode = new THREE.DirectionalLight( color );
lightNode.target = new THREE.Object3D();
lightNode.target.position.set( 0, 0, 1 );
lightNode.add( lightNode.target );
break;
Expand All @@ -309,6 +310,7 @@ THREE.GLTFLoader = ( function () {
lightDef.spot.outerConeAngle = lightDef.spot.outerConeAngle !== undefined ? lightDef.spot.outerConeAngle : Math.PI / 4.0;
lightNode.angle = lightDef.spot.outerConeAngle;
lightNode.penumbra = 1.0 - lightDef.spot.innerConeAngle / lightDef.spot.outerConeAngle;
lightNode.target = new THREE.Object3D();
lightNode.target.position.set( 0, 0, 1 );
lightNode.add( lightNode.target );
break;
Expand Down
19 changes: 6 additions & 13 deletions examples/js/renderers/WebGLDeferredRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,14 +890,11 @@ THREE.WebGLDeferredRenderer = function ( parameters ) {
uniforms.lightAngle.value = originalLight.angle;
uniforms.lightColor.value.copy( originalLight.color );
uniforms.lightIntensity.value = originalLight.intensity;
uniforms.lightPositionVS.value.setFromMatrixPosition( originalLight.matrixWorld ).applyMatrix4( _currentCamera.matrixWorldInverse );
uniforms.lightPositionVS.value.setFromMatrixPosition( originalLight.matrixWorld )
uniforms.lightPositionVS.value.applyMatrix4( _currentCamera.matrixWorldInverse );

var vec = uniforms.lightDirectionVS.value;
var vec2 = _tmpVector3;

vec.setFromMatrixPosition( originalLight.matrixWorld );
vec2.setFromMatrixPosition( originalLight.target.matrixWorld );
vec.sub( vec2 ).normalize().transformDirection( _currentCamera.matrixWorldInverse );
originalLight.getWorldDirection( uniforms.lightDirectionVS.value ).negate();
uniforms.lightDirectionVS.value.transformDirection( _currentCamera.matrixWorldInverse );

updateDeferredLightCommonUniforms( uniforms );

Expand Down Expand Up @@ -933,12 +930,8 @@ THREE.WebGLDeferredRenderer = function ( parameters ) {
uniforms.lightColor.value.copy( originalLight.color );
uniforms.lightIntensity.value = originalLight.intensity;

var vec = uniforms.lightDirectionVS.value;
var vec2 = _tmpVector3;

vec.setFromMatrixPosition( originalLight.matrixWorld );
vec2.setFromMatrixPosition( originalLight.target.matrixWorld );
vec.sub( vec2 ).normalize().transformDirection( _currentCamera.matrixWorldInverse );
originalLight.getWorldDirection( uniforms.lightDirectionVS.value ).negate();
uniforms.lightDirectionVS.value.transformDirection( _currentCamera.matrixWorldInverse );

updateDeferredLightCommonUniforms( uniforms );

Expand Down
1 change: 1 addition & 0 deletions examples/webgl_lights_spotlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@

spotLight = new THREE.SpotLight( 0xffffff, 1 );
spotLight.position.set( 15, 40, 35 );
spotLight.lookAt( 0, 0, 0 );
spotLight.angle = Math.PI / 4;
spotLight.penumbra = 0.05;
spotLight.decay = 2;
Expand Down
2 changes: 2 additions & 0 deletions examples/webgl_lights_spotlights.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
newObj.shadow.mapSize.width = 1024;
newObj.shadow.mapSize.height = 1024;

newObj.target = scene;

return newObj;

}
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_md2_control.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@

var gyro = new THREE.Gyroscope();
gyro.add( camera );
light.target = new THREE.Object3D();
gyro.add( light, light.target );

characters[ Math.floor( nSkins / 2 ) ].root.add( gyro );
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_nrrd.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
dirLight.position.set( 200, 200, 1000 ).normalize();

camera.add( dirLight );
dirLight.target = new THREE.Object3D();
camera.add( dirLight.target );

var loader = new THREE.NRRDLoader();
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_vrml.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
dirLight.position.set( 200, 200, 1000 ).normalize();

camera.add( dirLight );
dirLight.target = new THREE.Object3D();
camera.add( dirLight.target );

var loader = new THREE.VRMLLoader();
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_vtk.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
dirLight.position.set( 200, 200, 1000 ).normalize();

camera.add( dirLight );
dirLight.target = new THREE.Object3D();
camera.add( dirLight.target );

var material = new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } );
Expand Down
5 changes: 2 additions & 3 deletions examples/webgl_materials_cubemap_dynamic.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

spotLight = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI/2 );
spotLight.position.set( 0, 1800, 1500 );
spotLight.target.position.set( 0, 0, 0 );
spotLight.lookAt( 0, 0, 0 );
spotLight.castShadow = true;

spotLight.shadow.camera.near = 100;
Expand Down Expand Up @@ -947,8 +947,7 @@

// update shadows

spotLight.target.position.x = currentCar.root.position.x;
spotLight.target.position.z = currentCar.root.position.z;
spotLight.lookAt( currentCar.root.position.x, 0, currentCar.root.position.z );

// render cube map

Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_shadowmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2 );
light.position.set( 0, 1500, 1000 );
light.target.position.set( 0, 0, 0 );
light.lookAt( 0, 0, 0 );

light.castShadow = true;

Expand Down
1 change: 1 addition & 0 deletions examples/webgl_shadowmap_pcss.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@

var light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
light.position.set( 2, 8, 4 );
light.lookAt( 0, 0, 0 );

light.castShadow = true;
light.shadow.mapSize.width = 1024;
Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_shadowmap_performance.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI/2 );
light.position.set( 0, 1500, 1000 );
light.target.position.set( 0, 0, 0 );
light.lookAt( 0, 0, 0 );

light.castShadow = true;

Expand Down
2 changes: 2 additions & 0 deletions examples/webgl_shadowmap_viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.3;
spotLight.position.set( 10, 10, 5 );
spotLight.lookAt( 0, 0, 0 );
spotLight.castShadow = true;
spotLight.shadow.camera.near = 8;
spotLight.shadow.camera.far = 30;
Expand All @@ -87,6 +88,7 @@
dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.name = 'Dir. Light';
dirLight.position.set( 0, 10, 0 );
dirLight.lookAt( 0, 0, 0 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 1;
dirLight.shadow.camera.far = 10;
Expand Down
6 changes: 2 additions & 4 deletions examples/webvr_sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,20 @@

var light = new THREE.DirectionalLight( 0x8800ff );
light.position.set( - 1, 1.5, - 1.5 );
light.lookAt( 0, 0, - 2 );
light.castShadow = true;
light.shadow.camera.zoom = 4;
scene.add( light );
light.target.position.set( 0, 0, - 2 );
scene.add( light.target );

var helper = new THREE.CameraHelper( light.shadow.camera );
// scene.add( helper );

var light = new THREE.DirectionalLight( 0xff0000 );
light.position.set( 1, 1.5, - 2.5 );
light.lookAt( 0, 0, - 2 );
light.castShadow = true;
light.shadow.camera.zoom = 4;
scene.add( light );
light.target.position.set( 0, 0, - 2 );
scene.add( light.target );

var helper = new THREE.CameraHelper( light.shadow.camera );
// scene.add( helper );
Expand Down
34 changes: 8 additions & 26 deletions src/helpers/DirectionalLightHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @author WestLangley / http://github.com/WestLangley
*/

import { Vector3 } from '../math/Vector3.js';
import { Object3D } from '../core/Object3D.js';
import { Line } from '../objects/Line.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
Expand Down Expand Up @@ -63,36 +62,19 @@ DirectionalLightHelper.prototype.dispose = function () {

DirectionalLightHelper.prototype.update = function () {

var v1 = new Vector3();
var v2 = new Vector3();
var v3 = new Vector3();
if ( this.color !== undefined ) {

return function update() {
this.lightPlane.material.color.set( this.color );
this.targetLine.material.color.set( this.color );

v1.setFromMatrixPosition( this.light.matrixWorld );
v2.setFromMatrixPosition( this.light.target.matrixWorld );
v3.subVectors( v2, v1 );
} else {

this.lightPlane.lookAt( v3 );
this.lightPlane.material.color.copy( this.light.color );
this.targetLine.material.color.copy( this.light.color );

if ( this.color !== undefined ) {
}

this.lightPlane.material.color.set( this.color );
this.targetLine.material.color.set( this.color );

} else {

this.lightPlane.material.color.copy( this.light.color );
this.targetLine.material.color.copy( this.light.color );

}

this.targetLine.lookAt( v3 );
this.targetLine.scale.z = v3.length();

};

}();
};


export { DirectionalLightHelper };
33 changes: 10 additions & 23 deletions src/helpers/SpotLightHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @author WestLangley / http://github.com/WestLangley
*/

import { Vector3 } from '../math/Vector3.js';
import { Object3D } from '../core/Object3D.js';
import { LineSegments } from '../objects/LineSegments.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
Expand Down Expand Up @@ -68,36 +67,24 @@ SpotLightHelper.prototype.dispose = function () {

SpotLightHelper.prototype.update = function () {

var vector = new Vector3();
var vector2 = new Vector3();

return function update() {

this.light.updateMatrixWorld();

var coneLength = this.light.distance ? this.light.distance : 1000;
var coneWidth = coneLength * Math.tan( this.light.angle );

this.cone.scale.set( coneWidth, coneWidth, coneLength );

vector.setFromMatrixPosition( this.light.matrixWorld );
vector2.setFromMatrixPosition( this.light.target.matrixWorld );
this.light.updateMatrixWorld();

this.cone.lookAt( vector2.sub( vector ) );
var coneLength = this.light.distance ? this.light.distance : 1000;
var coneWidth = coneLength * Math.tan( this.light.angle );

if ( this.color !== undefined ) {
this.cone.scale.set( coneWidth, coneWidth, coneLength );

this.cone.material.color.set( this.color );
if ( this.color !== undefined ) {

} else {
this.cone.material.color.set( this.color );

this.cone.material.color.copy( this.light.color );
} else {

}
this.cone.material.color.copy( this.light.color );

};
}

}();
};


export { SpotLightHelper };
9 changes: 6 additions & 3 deletions src/lights/DirectionalLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ function DirectionalLight( color, intensity ) {
this.type = 'DirectionalLight';

this.position.copy( Object3D.DefaultUp );
this.updateMatrix();
this.lookAt( 0, 0, 0 );

this.target = new Object3D();
this.target = undefined;

this.shadow = new DirectionalLightShadow();

this.add( this.shadow.camera );
this.shadow.camera.scale.set( - 1, 1, - 1 );

}

DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {
Expand All @@ -32,7 +35,7 @@ DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), {

Light.prototype.copy.call( this, source );

this.target = source.target.clone();
this.target = source.target ? source.target.clone() : undefined;

this.shadow = source.shadow.clone();

Expand Down
5 changes: 3 additions & 2 deletions src/lights/DirectionalLightShadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { OrthographicCamera } from '../cameras/OrthographicCamera.js';
* @author mrdoob / http://mrdoob.com/
*/

function DirectionalLightShadow( ) {
function DirectionalLightShadow( camera ) {

LightShadow.call( this, new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
camera = camera || new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 );
LightShadow.call( this, camera );

}

Expand Down
9 changes: 6 additions & 3 deletions src/lights/SpotLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function SpotLight( color, intensity, distance, angle, penumbra, decay ) {
this.type = 'SpotLight';

this.position.copy( Object3D.DefaultUp );
this.updateMatrix();
this.lookAt( 0, 0, 0 );

this.target = new Object3D();
this.target = undefined;

Object.defineProperty( this, 'power', {
get: function () {
Expand All @@ -41,6 +41,9 @@ function SpotLight( color, intensity, distance, angle, penumbra, decay ) {

this.shadow = new SpotLightShadow();

this.add( this.shadow.camera );
this.shadow.camera.scale.set( - 1, 1, - 1 );

}

SpotLight.prototype = Object.assign( Object.create( Light.prototype ), {
Expand All @@ -58,7 +61,7 @@ SpotLight.prototype = Object.assign( Object.create( Light.prototype ), {
this.penumbra = source.penumbra;
this.decay = source.decay;

this.target = source.target.clone();
this.target = source.target ? source.target.clone() : undefined;

this.shadow = source.shadow.clone();

Expand Down
5 changes: 3 additions & 2 deletions src/lights/SpotLightShadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
* @author mrdoob / http://mrdoob.com/
*/

function SpotLightShadow() {
function SpotLightShadow( camera ) {

LightShadow.call( this, new PerspectiveCamera( 50, 1, 0.5, 500 ) );
camera = camera || new PerspectiveCamera( 50, 1, 0.5, 500 );
LightShadow.call( this, camera );

}

Expand Down
Loading

0 comments on commit 7065b86

Please sign in to comment.