From: Mikko Rasa Date: Wed, 14 Apr 2021 15:09:36 +0000 (+0300) Subject: Avoid NaN issues in rare cases in the Cook-Torrance shader X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=47fa42594ef79dfe8c95edeeda11e9a9492e35bf;p=libs%2Fgl.git Avoid NaN issues in rare cases in the Cook-Torrance shader The dot product may sometimes be very slightly over 1.0, making the first argument to pow negative and giving undefined results. --- diff --git a/shaderlib/cooktorrance.glsl b/shaderlib/cooktorrance.glsl index d834cee4..02d697b0 100644 --- a/shaderlib/cooktorrance.glsl +++ b/shaderlib/cooktorrance.glsl @@ -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); - 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 */