Skip to content
Snippets Groups Projects
Commit ae9a5d28 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Optimize normal encoding

parent c10c2273
No related branches found
No related tags found
2 merge requests!3Update to main branch,!2Rebase onto current main branch
......@@ -149,11 +149,11 @@ vec3 getNorm(vec2 screenpos)
vec2 f = texture(normalMap, screenpos.xy).xy;
f = f * 2.0 - 1.0;
// https://twitter.com/Stubbesaurus/status/937994790553227264
vec3 n = vec3( f.x, f.y, 1.0 - abs( f.x ) - abs( f.y ) );
float t = clamp( -n.z , 0.0, 1.0);
n.xy += vec2(n.x >= 0.0 ? -t : t, n.y >= 0.0 ? -t : t);
return normalize( n );
vec3 n = vec3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y));
float t = max(-n.z, 0.0);
n.x += n.x >= 0.0 ? -t : t;
n.y += n.y >= 0.0 ? -t : t;
return normalize(n);
}
vec3 getNormalFromPacked(vec4 packedNormalEnvIntensityFlags)
......@@ -161,11 +161,11 @@ vec3 getNormalFromPacked(vec4 packedNormalEnvIntensityFlags)
vec2 f = packedNormalEnvIntensityFlags.xy;
f = f * 2.0 - 1.0;
// https://twitter.com/Stubbesaurus/status/937994790553227264
vec3 n = vec3( f.x, f.y, 1.0 - abs( f.x ) - abs( f.y ) );
float t = clamp( -n.z , 0.0, 1.0);
n.xy += vec2(n.x >= 0.0 ? -t : t, n.y >= 0.0 ? -t : t);
return normalize( n );
vec3 n = vec3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y));
float t = max(-n.z, 0.0);
n.x += n.x >= 0.0 ? -t : t;
n.y += n.y >= 0.0 ? -t : t;
return normalize(n);
}
// return packedNormalEnvIntensityFlags since GBUFFER_FLAG_HAS_PBR needs .w
......
......@@ -27,16 +27,11 @@
// https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/
//
vec2 OctWrap( vec2 v )
{
return ( 1.0 - abs( v.yx ) ) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);
}
vec2 encode_normal(vec3 n)
{
n /= ( abs( n.x ) + abs( n.y ) + abs( n.z ) );
n.xy = n.z >= 0.0 ? n.xy : OctWrap( n.xy );
n.xy = n.xy * 0.5 + 0.5;
return n.xy;
n *= 1.0 / max(dot(abs(n), vec3(1.0)), 1e-6);
float t = clamp(-n.z, 0.0, 1.0);
n.x += n.x >= 0.0 ? t : -t;
n.y += n.y >= 0.0 ? t : -t;
return n.xy * 0.5 + 0.5;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment