- Home /
How to identify mesh faces
Hello,
I try to create a script to perform an analysis of every face of a mesh. I've read the documentation and as I understood, there is no definition of a face, "just" triangles or vertices. So in order to identify faces I guess first I need to identify the vertices, then their normals and then compare the vertices to each other in order to find out which are neighbors. Vertices with the same normal and neighboring vertices would then form one face. Right?
At the moment I am struggling in finding out the normals of the vertices. I printed out all vertices and normals (of a simple cube), but they seam not to match together - the vertices Index 3,4,5 (they form one triangle) doesn't share the same normal.
Am I interpreting this correctly? The documentation is not always easy to read for me.
Is there maybe an easier way to identify faces?
Thank you
Huxi
Answer by bdubbert · Sep 20, 2021 at 08:38 PM
Mesh.triangles has all your mesh faces for you
Each successive 3 integers in the triangles array are the 3 vertices of a mesh face. The int values in the triangles array are the index of the corresponding vertex in the vertices array.
Answer by Huxii · Sep 20, 2021 at 08:40 PM
Hello @bdubbert , and how do you know which triangles form one face?
Ah you are looking for entire faces, which may be a combo of 1 or more triangle. Yeah that is a bit trickier. Will not be an easy answer.
Looking at the vertex normals doesn't seem like the way to go for me, because the vertex normals are not necessarily the same as the normals of the triangles that they form, and you care about the normals of the triangles when trying to find faces. If you have triangles A and B and you want to know if they are part of the same face, then
All of the vertices in B should lie in the plane defined by the 3 vertices of A
A and B should share at least one vertex **
So I would start by separating out all of your triangles and then start comparing the vertices from there.
The idea of having a look if they lie on the same plane also crossed my $$anonymous$$d, but also didn't know how to find this out.
You can construct a Plane using the 3 vertices of the first triangle.
I'm sure there is a better way to check whether a point is on a plane or not, but you could always feed a point into ClosestPointOnPlane and check if it just gives you back your point.
Plane myPlane = new Plane(a, b, c);
bool isOnPlane = myPlane.ClosestPointOnPlane(d) == d;
Not sure how this function works, so you may have to allow for a little bit of variation just because checking if floats are equal is usually a bad idea. Usually you just want to check that your floats are close enough to equal.
bool isOnPlane = Mathf.Approximately(myPlane.ClosestPointOnPlane(d), d);