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 #5078

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
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ <h1><a href="http://threejs.org">three.js</a> / examples</h1>
"webgl_lights_hemisphere",
"webgl_lights_pointlights",
"webgl_lights_pointlights2",
"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.
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 ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cosines can be negative. What are the range of values for the relevant SpotLight parameters that make sense to you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought was that penumbraAngle would always be positive, and you'd decrease angle appropriately if you want an inner fade instead of an outer fade. However, I wouldn't be opposed to handling negative penumbraAngle values.

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
9 changes: 7 additions & 2 deletions src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {

float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );

if ( spotEffect > spotLightAngleCos[ i ] ) {
if ( spotEffect > spotLightOuterAngleCos[ i ] ) {

float falloff = 0.0;
falloff = (spotEffect - spotLightOuterAngleCos[ i ]) / spotLightAngleCosDiff[ i ];
falloff = clamp( falloff, 0.0, 1.0 );

spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );
spotEffect *= falloff;

float lDistance = 1.0;
if ( spotLightDistance[ i ] > 0.0 )
Expand Down Expand Up @@ -199,4 +204,4 @@ vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;

vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;

#endif
#endif
9 changes: 7 additions & 2 deletions src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ vec3 viewPosition = normalize( vViewPosition );

float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );

if ( spotEffect > spotLightAngleCos[ i ] ) {
if ( spotEffect > spotLightOuterAngleCos[ i ] ) {

float falloff = 0.0;
falloff = (spotEffect - spotLightOuterAngleCos[ i ]) / spotLightAngleCosDiff[ i ];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can result in a divide-by-zero. Is that an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, doesn't cause any problems.

falloff = clamp( falloff, 0.0, 1.0 );

spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );
spotEffect *= falloff;

// diffuse

Expand Down Expand Up @@ -275,4 +280,4 @@ vec3 totalSpecular = vec3( 0.0 );

gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ uniform vec3 ambientLightColor;
uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
uniform vec3 spotLightDirection[ 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 ];

uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
Expand All @@ -49,4 +51,4 @@ uniform vec3 ambientLightColor;
#endif

varying vec3 vViewPosition;
varying vec3 vNormal;
varying vec3 vNormal;
2 changes: 2 additions & 0 deletions src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ THREE.UniformsLib = {
"spotLightDirection" : { type: "fv", value: [] },
"spotLightDistance" : { type: "fv1", value: [] },
"spotLightAngleCos" : { type: "fv1", value: [] },
"spotLightOuterAngleCos" : { type: "fv1", value: [] },
"spotLightAngleCosDiff" : { type: "fv1", value: [] },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the difference can be computed in the shader, instead. This may be more efficient, but it seems strange to me to pass in redundant information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to actually benchmark the performance difference, but at first glance it seemed pretty significant

"spotLightExponent" : { type: "fv1", value: [] }

},
Expand Down