# Populate triangles array when you already have sorted vertices array

Input is a Vector3[] of verticies. Length is unknown, it could be anything above 2 - so these vertices form all kinds of multifaceted shapes with 3 being a triangle and 6 hexagon-ish.

Poor example to visualize:

vertices = [Vector3(..), Vector3(..), Vector3(..), Vector3(..), Vector3(..)];


Coordinates in that array are ordered counterclockwise. I also have the coordinates of center point that these verticies form (look at illustration below) - it doesn't have to be used but if it helps..

Few examples of which shapes can occur and which cannot: Now I need to build meshes out of them. For each shape I need:

• Vertices array (which I already have, nicely ordered)
• Triangles array - 3 indices (from vertices array) per triangle until all vertices are covered

How to populate triangles array with these relatively easy shapes where each vertex has "direct line of sight" to every other vertices? Number of vertices is unknown of course.

## 1 Answer

Unity meshes are indexed triangle lists. so one way is to construct the equivalent of a triangle fan:

int[] triangles new int[vertices.Length * 3 - 6];
for(int i=0; i<vertices.Length - 2; i++) {
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}