Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renderer: fix normalmap dark blotches #260

Merged
merged 1 commit into from Jan 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/engine/renderer/glsl_source/reliefMapping_fp.glsl
Expand Up @@ -59,9 +59,22 @@ vec3 NormalInTangentSpace(vec2 texNormal)
normal = texture2D(u_NormalMap, texNormal).rga;
normal.x *= normal.z;
normal.xy = 2.0 * normal.xy - 1.0;
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
// In a perfect world this code must be enough:
// normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
//
// Unvanquished texture known to trigger black normalmap artifacts
// when doing Z reconstruction:
// textures/shared_pk02_src/rock01_n
//
// Although the normal vector is supposed to have a length of 1,
// dot(normal.xy, normal.xy) may be greater than 1 due to compression
// artifacts: values as large as 1.27 have been observed with crunch -dxn.
// https://github.com/DaemonEngine/Daemon/pull/260#issuecomment-571010935
//
// This might happen with other formats too. So we must take care not to
// take the square root of a negative number here.
normal.z = sqrt(max(0, 1.0 - dot(normal.xy, normal.xy)));
#endif // !USE_HEIGHTMAP_IN_NORMALMAP

// HACK: 0 normal Z channel can't be good
if (u_NormalScale.z != 0)
{
Expand Down