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

Only triangulate n-gons when necessary for calc_tangents #379

Merged
merged 7 commits into from
Nov 25, 2020
Merged

Only triangulate n-gons when necessary for calc_tangents #379

merged 7 commits into from
Nov 25, 2020

Conversation

scurest
Copy link
Contributor

@scurest scurest commented Nov 5, 2020

Hi.

There is a problem with triangulate_mesh; it can calculate the wrong triangulation, and it can damage normals. But triangulating is necessary to calculate tangents if there are (>4)-gons in a mesh. The goal of this PR is to limit the damage caused to only (>4)-gons. In particular, meshes with only tris/quads should now be fixed.

(I am not aware of a fix that works for everything; for reference, the FBX and glTF exporters instead opt to omit tangents when there are (>4)-gons.)

What I changed:

  • First, triangulating then iterating over mesh.polygons is replaced with iterating over mesh.loop_triangles. This holds the "true" triangulation of the mesh (ie. the one Blender actually draws). This is done in both mesh.py and physics.py.
  • Then, MeshConverter.to_mesh has the triangulate argument removed. triangulate_mesh (renamed triangulate_ngons) is only called if calc_tangents fails, and if it is called, it now only triangulates (>4)-gons.
  • I also added a logging.info message if triangulating n-gons is done, but I'm not sure if you want that...

Fixes #319 (or at least the collar); this one had its normals damaged.
Fixes #361; this one had the wrong triangulation.

Example: a Cylinder with rotated normals.

  • On the left, exported with master, both the sides (quads) and base (n-gon) are damaged.
  • On the right, exported with this branch, the base is still damaged, but the sides are fixed.

Cylinders

Copy link
Collaborator

@Jason0214 Jason0214 left a comment

Choose a reason for hiding this comment

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

Good catch! Thank you very much for the fix! I agree that it is good enough to fix the triangular meshes for now.
To finally merge this PR, we need to update those reference exports in the repo, so that we can keep using them for future regression tests.
If you have a Blender2.8x environment available, you can rebase to the current master HEAD, do a make update-examples and commit the changes. It's okay if you don't have one, hold on a while and I will come up with a way to simplify the flow. Thanks 🙂

mesh.calc_tangents()
except RuntimeError:
# Mesh must have n-gons
logging.info(
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use logging.warning here, we have a hook to populate warning message to UI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if I should use a warning since it's much more visible. I updated it and also tweaked the message slightly for more friendliness, see if it's okay.

@Calinou Calinou added the bug label Nov 22, 2020
This shouldn't change anything atm since the mesh is triangulated
beforehand, but this will allow us a non-triangulated mesh in the
future, which avoids problems where the mesh triangulation is wrong;
loop_triangles is always the "true" triangulation.
No longer need to ask the mesh converter to triangulate.
This means it is no longer required to ask for triangulation if
asking for tangents.
It is now as if it were always False. Triangulation will only
occur if needed for calc_tangents.

This was only True in mesh.py. Now that it is False there,
triangulate_mesh will be skipped for any meshes without (>4)-gons,
which solves issues of bad triangulation/damaged normals for these
meshes.
This should confine damage caused by triangulation to n-gons only.
@scurest
Copy link
Contributor Author

scurest commented Nov 22, 2020

Neat. I rebased and ran make; make update-examples with Blender 2.81 from https://download.blender.org/release/Blender2.81/blender-2.81-linux-glibc217-x86_64.tar.bz2. Let's see if CI passes...

@Jason0214
Copy link
Collaborator

Thanks!

@Jason0214 Jason0214 merged commit a353957 into godotengine:master Nov 25, 2020
@scurest scurest deleted the triangulation branch November 25, 2020 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model broken on export to godot Incorrect normals
3 participants