Skip to content

Commit

Permalink
Fix #527: Broken radial_tangent when normal == axis
Browse files Browse the repository at this point in the history
  • Loading branch information
pragma37 committed Oct 22, 2023
1 parent f67c135 commit 5e90bbf
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Malt/Shaders/Common/Normal.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ vec3 sample_normal_map(sampler2D normal_texture, int uv_index, vec2 uv)
*/
vec3 radial_tangent(vec3 normal, vec3 axis)
{
float d = abs(dot(normal, axis));
float epsilon = 1e-5;
if(d > 1.0 - epsilon)
{
// Avoid singularities when normal == axis.
vec3 alternative_axis = normalize(axis + vec3(0.1, 0.2, 0.3));
axis = mix(axis, alternative_axis, map_range_clamped(d, 1.0 - epsilon, 1.0, 0.0, epsilon));
}
return normalize(cross(axis, normal));
}

Expand Down

0 comments on commit 5e90bbf

Please sign in to comment.