bryanedds / Nu

Repository hosting the open-source Nu Game Engine and related projects.
MIT License
1.1k stars 155 forks source link

Shader-generated tangents and binormals possibly producing artifacts at extreme close view. #663

Closed bryanedds closed 1 week ago

bryanedds commented 9 months ago

Zoom extremely close to terrain to see this sort of pattern -

image

I'm guessing this is due to floating point precision issues in code like here -

https://github.com/bryanedds/Nu/blob/93c57aed68250c181b4fc8016d46002d6d052d55/Nu/Nu.Gaia/Assets/Default/PhysicallyBasedDeferredTerrain.glsl#L140-L145

bryanedds commented 1 month ago

I'm pretty sure the issue is something else and more general than with the terrain.

bryanedds commented 1 week ago

I'm starting to suspect it has something to do with the way we contrive tangents and binormals in fragment shaders -

    // compute spatial converters
    vec3 q1 = dFdx(positionOut.xyz);
    vec3 q2 = dFdy(positionOut.xyz);
    vec2 st1 = dFdx(texCoordsOut);
    vec2 st2 = dFdy(texCoordsOut);
    vec3 tangent = normalize(q1 * st2.t - q2 * st1.t);
    vec3 binormal = -normalize(cross(normal, tangent));
    mat3 toWorld = mat3(tangent, binormal, normal);
    mat3 toTangent = transpose(toWorld);

This convenience approach means we don't have to pass in tangent data, but perhaps its the cause of these extreme close-up rendering artifacts.

bryanedds commented 1 week ago

Actually, this might originate from the PBR algos we employ. Additionally, we've made the issue basically invisible from increasing the near plane distance from 1/16m to 1/8m, so closing.