]> git.tdb.fi Git - libs/gl.git/commitdiff
Avoid NaN issues in rare cases in the Cook-Torrance shader
authorMikko Rasa <tdb@tdb.fi>
Wed, 14 Apr 2021 15:09:36 +0000 (18:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 14 Apr 2021 15:09:36 +0000 (18:09 +0300)
The dot product may sometimes be very slightly over 1.0, making the
first argument to pow negative and giving undefined results.

shaderlib/cooktorrance.glsl

index d834cee462d429ca2d439107caca5ad3e1c55c8d..02d697b087c640572a2ed0ebad15fe655ff4f1d2 100644 (file)
@@ -114,7 +114,7 @@ vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
 {
        // 0.04 is a decent approximation for dielectric base reflectivity
        vec3 f0 = mix(vec3(0.04), base_color, metalness);
 {
        // 0.04 is a decent approximation for dielectric base reflectivity
        vec3 f0 = mix(vec3(0.04), base_color, metalness);
-       return mix(f0, vec3(1.0), pow(1.0-dot(halfway, look), 5.0));
+       return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0));
 }
 
 /* Computes the full contribution of a single light */
 }
 
 /* Computes the full contribution of a single light */