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?

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.