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

Fix bug in CylinderMesh when computing normals #67336

Merged
merged 1 commit into from
Oct 13, 2022

Conversation

jbcolli2
Copy link
Contributor

While writing unit tests for CylinderMesh I realized the following bug. When the top radius and bottom radius are different, the normal should be slanted in the y direction to some degree.

However, in primitive_meshes.cpp, the y component of the normals along the side of the cylinder was always set to zero. Essentially, the normals were being computed as if it were a right cylinder, as opposed to a sort of trapezoidal cylinder with different radii on top and bottom.

@@ -789,7 +789,7 @@ void CylinderMesh::create_mesh_array(Array &p_arr, float top_radius, float botto

Vector3 p = Vector3(x * radius, y, z * radius);
points.push_back(p);
normals.push_back(Vector3(x, 0.0, z));
normals.push_back(Vector3(x, (bottom_radius - top_radius)/height, z).normalized());
Copy link
Member

@akien-mga akien-mga Oct 13, 2022

Choose a reason for hiding this comment

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

This y component is constant so it could be computed only once outside the for loop.
Style wise, you should also have spaces around / (run clang-format locally to make sure to us the right style).

@akien-mga akien-mga added this to the 4.0 milestone Oct 13, 2022
@akien-mga akien-mga added cherrypick:3.x Considered for cherry-picking into a future 3.x release cherrypick:3.5 Considered for cherry-picking into a future 3.5.x release labels Oct 13, 2022
@akien-mga
Copy link
Member

The whole class might need a thorough review because there might be other calculations which expect a cylinder to be... a cylinder.
It's a bit weird IMO that this class supports making a cone frustum.

@jbcolli2 jbcolli2 force-pushed the Normal-of-CylinderMesh branch from b2eaff7 to 35844d9 Compare October 13, 2022 12:02
@jbcolli2
Copy link
Contributor Author

Thanks for the help. I've fixed the formatting.

I'm a first time contributor, so I could be wrong but I don't think the tangent needs to be changed. I'm not positive what the tangent is calculating as there are at least two orthogonal tangents to the normal. But it seems like the tangent is pointing along the rings of the cylinder, and so that vector wouldn't change if the side gets slanted. Let me know if I'm thinking of this wrong.

I have a PR in with unit tests for all the PrimitiveMesh subclasses. I tested CylinderMesh pretty well and it looks good. The class doesn't do much except create the mesh, and I tested the vertex positions and normals. The only thing I see that could be off are the texture coordinates, and it doesn't seem to me that they would obviously change if the shape is a cone frustrum.

@akien-mga
Copy link
Member

Makes sense yes.

Seems like you missed part of my review comment:

This y component is constant so it could be computed only once outside the for loop.

I.e. you could compute const real_t normal_y = (bottom_radius - top_radius) / height; before the for loop so it's computed only once.

@jbcolli2 jbcolli2 force-pushed the Normal-of-CylinderMesh branch from 35844d9 to 491ec62 Compare October 13, 2022 12:24
@akien-mga akien-mga merged commit 9eb8eb5 into godotengine:master Oct 13, 2022
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

@jbcolli2
Copy link
Contributor Author

Thanks for your help! :-)

@akien-mga
Copy link
Member

Cherry-picked for 3.6.

@akien-mga akien-mga removed the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Nov 30, 2022
@timothyqiu
Copy link
Member

Cherry-picked for 3.5.2

@timothyqiu timothyqiu removed the cherrypick:3.5 Considered for cherry-picking into a future 3.5.x release label Dec 5, 2022
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.

3 participants