Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add penumbra angle to spotLight #5080

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api/lights/SpotLight.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ <h3>.[page:Float angle]</h3>
Default — *Math.PI/3*.
</div>

<h3>.[page:Float penumbraAngle]</h3>
<div>
Additional angle, in radians, for the light to smoothly fall off.<br />
Default — 0.
</div>

<h3>.[page:Float exponent]</h3>
<div>
Rapidity of the falloff of light from its target direction.<br />
Expand Down
2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ <h1><a href="http://threejs.org">three.js</a> / examples</h1>
"webgl_lights_hemisphere",
"webgl_lights_pointlights",
"webgl_lights_pointlights2",
"webgl_lights_spotlights",
"webgl_lights_spotlights_penumbra",
"webgl_lines_colors",
"webgl_lines_cubes",
"webgl_lines_dashed",
Expand Down
Binary file added examples/textures/WoodFloor_DIF.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/textures/WoodFloor_NRM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/textures/WoodFloor_SPC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions examples/webgl_lights_spotlights.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lights - spot lights</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}

#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
}

a {
color: #ff0080;
text-decoration: none;
}

a:hover {
color: #0080ff;
}
</style>
</head>
<body>

<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - spot lights penumbra WebGL demo.<br />
</div>

<script src="../build/three.js"></script>

<script src="js/loaders/BinaryLoader.js"></script>

<script src="js/Detector.js"></script>
<script src="js/libs/dat.gui.min.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var camera, scene, renderer, light;

init();
animate();

function init() {

var container = document.getElementById( 'container' );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 4;
camera.position.z = 3;
camera.lookAt(new THREE.Vector3(0, 0, 0));

scene = new THREE.Scene();

var createTexture = function(url) {
var texture = THREE.ImageUtils.loadTexture("textures/" + url);
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(100, 100);
return texture
};

var geometry = new THREE.PlaneGeometry(100, 100, 100, 100);
var material = new THREE.MeshPhongMaterial({
map: createTexture("WoodFloor_DIF.png"),
specularMap: createTexture("WoodFloor_SPC.png"),
normalMap: createTexture("WoodFloor_NRM.png"),
side: THREE.DoubleSide,
shininess: 150
});

var floor = new THREE.Mesh(geometry, material);
floor.rotation.x = Math.PI / 2;

scene.add( floor );

var light = new THREE.SpotLight();
light.position.set( 0, 2, 0 );
light.color.setHex( 0xffffff );
light.intensity = 0.5;
light.angle = (5 * Math.PI) / 24; // 75 degrees
light.penumbraAngle = 0.174532925; // 10 degrees in radians
light.exponent = 0;

scene.add( light );

var gui = new dat.GUI();

gui.add(light, 'penumbraAngle', 0, 0.3);
gui.add(light, 'angle', 0, Math.PI / 2);
gui.add(light, 'exponent', 0, 20)

renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

//

window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

//

function animate() {

requestAnimationFrame( animate );

render();

}

function render() {

renderer.render( scene, camera );

}

</script>
</body>
</html>

168 changes: 168 additions & 0 deletions examples/webgl_lights_spotlights_penumbra.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lights - spot lights - penumbra</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}

#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: center;
}

a {
color: #ff0080;
text-decoration: none;
}

a:hover {
color: #0080ff;
}
</style>
</head>
<body>

<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - spot lights penumbra WebGL demo.<br />
</div>

<script src="../build/three.js"></script>

<script src="js/loaders/BinaryLoader.js"></script>

<script src="js/Detector.js"></script>

<script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var camera, scene, renderer, lights;

init();
animate();

function init() {

var container = document.getElementById( 'container' );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 4;
camera.position.z = 3;
camera.lookAt(new THREE.Vector3(0, 0, 0));

scene = new THREE.Scene();

var createTexture = function(url) {
var texture = THREE.ImageUtils.loadTexture("textures/" + url);
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(100, 100);
return texture
};

var geometry = new THREE.PlaneGeometry(100, 100, 100, 100);
var material = new THREE.MeshPhongMaterial({
map: createTexture("WoodFloor_DIF.png"),
specularMap: createTexture("WoodFloor_SPC.png"),
normalMap: createTexture("WoodFloor_NRM.png"),
side: THREE.DoubleSide,
shininess: 150
});

var floor = new THREE.Mesh(geometry, material);
floor.rotation.x = Math.PI / 2;

scene.add( floor );

lights = [];

var light = new THREE.SpotLight();
light.position.set( 0, 1.8407174058250002, 1.8 );
light.target.position.set( 0, 0.9746920020405615, 1.3 );
lights.push( light );

var light = new THREE.SpotLight();
light.position.set( 0, 1.8407174058250002, -1.8 );
light.target.position.set( 0, 0.9746920020405615, -1.3 );
lights.push( light );

var light = new THREE.SpotLight();
light.position.set( 1.8, 1.8407174058250002, 0 );
light.target.position.set( 1.3, 0.9746920020405615, 0 );
lights.push( light );

var light = new THREE.SpotLight();
light.position.set( -1.8, 1.8407174058250002, 0 );
light.target.position.set( -1.3, 0.9746920020405615, 0 );
lights.push( light );

for (i in lights) {
var light = lights[ i ];
light.color.setHex( 0xffffff );
light.intensity = 0.5;
light.angle = (5 * Math.PI) / 24; // 75 degrees
light.exponent = 0

scene.add( light );
}

renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

//

window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

//

function animate() {

// Toggle the display of the penumbra effect every 5 seconds
var togglePenumbra = Math.round(new Date().getTime() / 5000) % 2;

for (i in lights) {
if (togglePenumbra) {
lights[ i ].penumbraAngle = 0.174532925; // 10 degrees in radians
} else {
lights[ i ].penumbraAngle = 0;
}
}

requestAnimationFrame( animate );

render();

}

function render() {

renderer.render( scene, camera );

}

</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/lights/SpotLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ THREE.SpotLight = function ( color, intensity, distance, angle, exponent ) {
this.intensity = ( intensity !== undefined ) ? intensity : 1;
this.distance = ( distance !== undefined ) ? distance : 0;
this.angle = ( angle !== undefined ) ? angle : Math.PI / 3;
this.penumbraAngle = 0;
this.exponent = ( exponent !== undefined ) ? exponent : 10;

this.castShadow = false;
Expand Down
8 changes: 7 additions & 1 deletion src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ THREE.WebGLRenderer = function ( parameters ) {
ambient: [ 0, 0, 0 ],
directional: { length: 0, colors:[], positions: [] },
point: { length: 0, colors: [], positions: [], distances: [] },
spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [] },
spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], outerAnglesCos: [], angleCosDiffs: [], exponents: [] },
hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }

};
Expand Down Expand Up @@ -4728,6 +4728,8 @@ THREE.WebGLRenderer = function ( parameters ) {
uniforms.spotLightDistance.value = lights.spot.distances;
uniforms.spotLightDirection.value = lights.spot.directions;
uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
uniforms.spotLightOuterAngleCos.value = lights.spot.outerAnglesCos;
uniforms.spotLightAngleCosDiff.value = lights.spot.angleCosDiffs;
uniforms.spotLightExponent.value = lights.spot.exponents;

uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
Expand Down Expand Up @@ -5208,6 +5210,8 @@ THREE.WebGLRenderer = function ( parameters ) {
spotDistances = zlights.spot.distances,
spotDirections = zlights.spot.directions,
spotAnglesCos = zlights.spot.anglesCos,
spotOuterAnglesCos = zlights.spot.outerAnglesCos,
spotAngleCosDiffs = zlights.spot.angleCosDiffs,
spotExponents = zlights.spot.exponents,

hemiSkyColors = zlights.hemi.skyColors,
Expand Down Expand Up @@ -5350,6 +5354,8 @@ THREE.WebGLRenderer = function ( parameters ) {
spotDirections[ spotOffset + 2 ] = _direction.z;

spotAnglesCos[ spotLength ] = Math.cos( light.angle );
spotOuterAnglesCos[ spotLength ] = Math.cos( light.angle + light.penumbraAngle );
spotAngleCosDiffs[ spotLength ] = spotAnglesCos[ spotLength ] - spotOuterAnglesCos[ spotLength ]
spotExponents[ spotLength ] = light.exponent;

spotLength += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ uniform vec3 ambientLightColor;
uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
uniform float spotLightOuterAngleCos[ MAX_SPOT_LIGHTS ];
uniform float spotLightAngleCosDiff[ MAX_SPOT_LIGHTS ];
uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];

#endif
Expand Down
Loading