From 47fa42594ef79dfe8c95edeeda11e9a9492e35bf Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 14 Apr 2021 18:09:36 +0300 Subject: [PATCH] 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. --- shaderlib/cooktorrance.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 */ -- 2.43.0