Skip to content

Commit

Permalink
Fix another graphical error showing a black box
Browse files Browse the repository at this point in the history
Is needed in addition to JoeyDeVries#92 to fix all graphical errors. Happens due to divide by 0 when `roughness = 0.0` and `NdotH = 1.0`.

Also use max to clamp similar fix for specular calculation instead of addition to fix the formula in normal cases
  • Loading branch information
DomGries authored Nov 26, 2017
1 parent 91bb8e8 commit b4982b4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/6.pbr/1.1.lighting/1.1.pbr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ float DistributionGGX(vec3 N, vec3 H, float roughness)
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;

return nom / denom;
return nom / max(denom, 0.001); // prevent divide by zero for roughness=0.0 and NdotH=1.0
}
// ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness)
Expand Down Expand Up @@ -85,8 +85,8 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero.
vec3 specular = nominator / denominator;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
vec3 specular = nominator / max(denominator, 0.001); // prevent divide by zero for NdotV=0.0 or NdotL=0.0

// kS is equal to Fresnel
vec3 kS = F;
Expand Down Expand Up @@ -118,4 +118,4 @@ void main()
color = pow(color, vec3(1.0/2.2));

FragColor = vec4(color, 1.0);
}
}

0 comments on commit b4982b4

Please sign in to comment.