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

Properly calculate binormal when creating SurfaceTool from arrays #88725

Merged
merged 1 commit into from
Feb 27, 2024

Conversation

clayjohn
Copy link
Member

@clayjohn clayjohn commented Feb 23, 2024

Fixes: #86131

I suspect that this will also fix some other subtle bugs.

This bug has existed in the SurfaceTool code for many many years. But it wasn't caught as there were very few situations where this code made a major impact. However, we started using create_vertex_array_from_triangle_arrays() in the default GLTF import path a few years ago. The result was that we ended up forcing the binormal sign of all GLTF meshes to 1.0. This is fine for most meshes, but the binormal sign is necessary when mirroring normal maps, so losing it totally broke those meshes.

edit: I have also pushed a related fix that ensures that the binormal sign is properly encoded when sending it to the shader. This fixes a related bug when using the compressed mesh format

Before:
Screenshot from 2024-02-23 10-17-23
After
Screenshot from 2024-02-23 10-16-48

@clayjohn clayjohn added bug topic:import regression cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Feb 23, 2024
@clayjohn clayjohn added this to the 4.3 milestone Feb 23, 2024
@clayjohn clayjohn requested a review from fire February 23, 2024 18:29
@clayjohn clayjohn requested a review from a team as a code owner February 23, 2024 19:06
@fire
Copy link
Member

fire commented Feb 23, 2024

@clayjohn clayjohn removed the cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release label Feb 23, 2024
@fire
Copy link
Member

fire commented Feb 23, 2024

Does U mirroring look correct to you?

Copy link
Member

@fire fire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the tests show that the geometry, normal and mirrored v looks correct. I think mirrored U is broken, but this is already an improvement even if incomplete.

@clayjohn
Copy link
Member Author

clayjohn commented Feb 24, 2024

Overall the tests show that the geometry, normal and mirrored v looks correct. I think mirrored U is broken, but this is already an improvement even if incomplete.

Move the camera over to the right and mirrored U will look the same as the others

image

@@ -340,7 +340,7 @@ void _get_axis_angle(const Vector3 &p_normal, const Vector4 &p_tangent, float &r
if (d < 0.0) {
r_angle = CLAMP((1.0 - r_angle / Math_PI) * 0.5, 0.0, 0.49999);
} else {
r_angle = (r_angle / Math_PI) * 0.5 + 0.5;
r_angle = CLAMP((r_angle / Math_PI) * 0.5 + 0.5, 0.500008, 1.0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, where does that 0.500008 come from? It might be worth adding a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the smallest number representable by a 16 bit UNORM value in the range 0.5 - 1.0.

Its the flipside of 0.49999 above.

@akien-mga akien-mga merged commit b21328d into godotengine:master Feb 27, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@akien-mga
Copy link
Member

Cherry-picked for 4.2.2.

@akien-mga akien-mga removed the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Mar 9, 2024
@clayjohn clayjohn deleted the GLTF-binormal-sign branch March 10, 2024 07:15
@jitspoe
Copy link
Contributor

jitspoe commented Mar 21, 2024

Seems to fix some of my problem cases. Thanks a bunch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mirrored UVs break normal mapping
5 participants