- Home /
clarification about mesh triangles - vertices order
Hi,
I've looked up for this over and over again, and wasn't able to find a post that explains it clearly enough for me.
I understand that for a triangle face to be visible, the order of the vertices has to be in clockwise order. What I'm not understanding is how does Unity interpret whether you're specifying the triangles in a clockwise or counter-clockwise order.
Let's say I have this code:
void Awake () {
Mesh myMesh = gameObject.GetComponent<MeshFilter>().mesh;
myMesh.Clear();
myMesh.vertices = new Vector3[] { new Vector3(-1,0,1), new Vector3(-1,0,-1), new Vector3(1,0,-1), new Vector3(1,0,1) };
myMesh.normals = new Vector3[] { Vector3.up,Vector3.up,Vector3.up,Vector3.up};
myMesh.triangles = new int[] {0,1,2,0,2,3};
myMesh.RecalculateNormals();
myMesh.RecalculateBounds();
}
How does Unity know that the triangles vertices are in a CW or a CCW order?
I mean, when I just say: myMesh.triangles = new int[] {0,1,2,0,2,3};
I am specifying a certain order, but relative to what? What is my starting point? Where am I saying 1 is to the right or to the left of 0? And how does it know which vertex each number corresponds to?
I would get different results for the two images below, wouldn't I? For the first image, both triangles would be visible, for the second, both would be invisible.
But again, how is the order established?
Answer by MarshallN · Nov 15, 2017 at 05:18 PM
The order that the vertices is in is the order in which they're listed in the myMesh.vertices array. You're setting the vertices in the order 'Upper-left, lower-left, lower-right, upper-right',
0 3
1 2
which means that both triangles will be invisible from above and visible from below. The clockwise/counterclockwise visibility is dependent on which side it's viewed from, so you could flip it and make it visible from above either by flipping the order of the vertices or the order of the triangle indices (e.g., having the triangle array be {0,2,1,0,3,2} with the current vertex order, or having the vertex order be flipped such that the current triangle array makes them clockwise from above.
3 0
2 1
So long story short, Unity doesn't know whether you're listing the triangles in clockwise/ccw order, that's for you to figure out based on the order of the vertices and where you're viewing from.
Thanks for clarifying "that the order that the vertices is in is the order in which they're listed in the my$$anonymous$$esh.vertices array". Didn't know that.
Very clear your explanation.
The only part I am confused about is when you said: "(...) which means that both triangles will be invisible from above and visible from below (...) "
With the vertices configuration: 'Upper-left, lower-left, lower-right, upper-right' aren't both triangles invisible from above AND from below with {0,1,2,0,2,3} ?
From above (both triangles CCW => invisible)
0 3
1 2
From below (both triangles CCW => invisible)
1 2
0 3
Thanks.
Alright, so if the Camera in the scene is looking down at the mesh that the code you posted made, the triangles won't be visible, because the triangles are counter-clockwise when viewed from above. However, if you put the camera underneath the mesh and look up, the triangles will be visible, because from underneath the triangles are clockwise. Flipping which side of the triangle you're viewing it from changes whether it's clockwise or counter-clockwise.
When you assign a triangle array to vertices, the triangle will always be visible on one side and invisible on the other.
From above (both triangles CCW => invisible)
0 3
1 2
From below (both triangles clockwise => visible)
1 2
0 3
because in this case, {0,1,2} and {0,2,3} are both clockwise.
Your answer
Follow this Question
Related Questions
contains two different ids for the same vertex 0 Answers
Object odd light problem with mesh? 1 Answer
How to find the center of quad polygons in a mesh 1 Answer
Problem drawing a mesh with Graphics.DrawMeshNow 1 Answer
Having trouble implementing edge collapse operation with half-edge data structure 0 Answers