Skip to content

Commit 3adb436

Browse files
committed
lights_pars: check if light is in range.
1 parent ef257c0 commit 3adb436

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/renderers/shaders/ShaderChunk/bsdfs.glsl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
bool testLightInRange( const in float lightDistance, const in float cutoffDistance ) {
2+
3+
return any( bvec2( lightDistance == 0.0, lightDistance < cutoffDistance ) );
4+
5+
}
6+
17
float calcLightAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
28

39
if ( decayExponent > 0.0 ) {
@@ -141,4 +147,4 @@ vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in Ge
141147
// source: http://simonstechblog.blogspot.ca/2011/12/microfacet-brdf.html
142148
float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
143149
return ( 2.0 / square( ggxRoughness + 0.0001 ) - 2.0 );
144-
}
150+
}

src/renderers/shaders/ShaderChunk/lights_pars.glsl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@
4949
vec3 lVector = pointLight.position - geometry.position;
5050
directLight.direction = normalize( lVector );
5151

52-
directLight.color = pointLight.color;
53-
directLight.color *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );
52+
float lightDistance = length( lVector );
53+
54+
if ( testLightInRange( lightDistance, pointLight.distance ) ) {
55+
56+
directLight.color = pointLight.color;
57+
directLight.color *= calcLightAttenuation( lightDistance, pointLight.distance, pointLight.decay );
58+
59+
} else {
60+
61+
directLight.color = vec3( 0.0 );
62+
63+
}
5464

5565
return directLight;
5666

@@ -85,15 +95,16 @@
8595
vec3 lVector = spotLight.position - geometry.position;
8696
directLight.direction = normalize( lVector );
8797

98+
float lightDistance = length( lVector );
8899
float spotEffect = dot( directLight.direction, spotLight.direction );
89100

90-
if ( spotEffect > spotLight.angleCos ) {
101+
if ( all( bvec2( spotEffect > spotLight.angleCos, testLightInRange( lightDistance, spotLight.distance ) ) ) ) {
91102

92103
float spotEffect = dot( spotLight.direction, directLight.direction );
93104
spotEffect *= clamp( ( spotEffect - spotLight.angleCos ) / spotLight.penumbra, 0.0, 1.0 );
94105

95106
directLight.color = spotLight.color;
96-
directLight.color *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
107+
directLight.color *= ( spotEffect * calcLightAttenuation( lightDistance, spotLight.distance, spotLight.decay ) );
97108

98109
} else {
99110

0 commit comments

Comments
 (0)