Browse Source

lights_pars: check if light is in range.

Mr.doob 9 years ago
parent
commit
3adb436bad

+ 7 - 1
src/renderers/shaders/ShaderChunk/bsdfs.glsl

@@ -1,3 +1,9 @@
+bool testLightInRange( const in float lightDistance, const in float cutoffDistance ) {
+
+	return any( bvec2( lightDistance == 0.0, lightDistance < cutoffDistance ) );
+
+}
+
 float calcLightAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
 
 	if ( decayExponent > 0.0 ) {
@@ -141,4 +147,4 @@ vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in Ge
 // source: http://simonstechblog.blogspot.ca/2011/12/microfacet-brdf.html
 float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
 	return ( 2.0 / square( ggxRoughness + 0.0001 ) - 2.0 );
-}
+}

+ 15 - 4
src/renderers/shaders/ShaderChunk/lights_pars.glsl

@@ -49,8 +49,18 @@
 		vec3 lVector = pointLight.position - geometry.position;
 		directLight.direction = normalize( lVector );
 
-		directLight.color = pointLight.color;
-		directLight.color *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );
+		float lightDistance = length( lVector );
+
+		if ( testLightInRange( lightDistance, pointLight.distance ) ) {
+
+			directLight.color = pointLight.color;
+			directLight.color *= calcLightAttenuation( lightDistance, pointLight.distance, pointLight.decay );
+
+		} else {
+
+			directLight.color = vec3( 0.0 );
+
+		}
 
 		return directLight;
 
@@ -85,15 +95,16 @@
 		vec3 lVector = spotLight.position - geometry.position;
 		directLight.direction = normalize( lVector );
 
+		float lightDistance = length( lVector );
 		float spotEffect = dot( directLight.direction, spotLight.direction );
 
-		if ( spotEffect > spotLight.angleCos ) {
+		if ( all( bvec2( spotEffect > spotLight.angleCos, testLightInRange( lightDistance, spotLight.distance ) ) ) ) {
 
 			float spotEffect = dot( spotLight.direction, directLight.direction );
 			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 ) );
+			directLight.color *= ( spotEffect * calcLightAttenuation( lightDistance, spotLight.distance, spotLight.decay ) );
 
 		} else {