Triangulation Problems

I have a collection of points. The vertex order looks like this:

0     1     2     3     4
5     6     7     8     9
10   11    12    13     14

The points are lay out like a plane, and i want the final result to look like a plane. I tried using the Triangulation script on the Unify Community Wiki, but it did not work, due to having points “inside” the outer points.

Currently, my code for triangulation is this:

Vector3[] vertices = new Vector3[xMax * yMax];
int[] triangles = new int[vertices.Length * 3];

for (int x = 0; x < xMax; x++) {
	for (int y = 0; y < yMax; y++) {

		int v = x + (y * xMax);

		// Assign the vertex position (this isnt the problem)
		vertices[v] = vertexPos;

		// Assign triangles (the thing that doesn't work)
		// The if then statements are to make sure the indicies dont go out of bounds
		if (t < triangles.Length - 5) {
			if (v < vertices.Length - xMax) {
				triangles[t++] = v;
				triangles[t++] = v + 1;
				triangles[t++] = v + xMax;
				
				triangles[t++] = v + 1;
				triangles[t++] = v + xMax;
				triangles[t++] = v + xMax + 1;
			}
		}
	}
}

All the things I’ve tried have not resulted in a nice smooth flat plane like i would like it to be.

You have to multiply y by xMax, not yMax

int v = x + (y * xMax);

Further more when creating the triangle indices you should reduce the limits of your for loops by 1

for (int x = 0; x < xMax-1; x++) {
    for (int y = 0; y < yMax-1; y++) {

I just checked your 2 triangles and your second triangle is facing the wrong way. It need to be clockwise. The first one is clockwise, the second one is counter clockwise. So the second one is facing backwards.

The size of the triangles array is also too small. There are two rows with each 4 quads:

 0    1    2    3    4
   Q1   Q2   Q3   Q4
 5    6    7    8    9
   Q5   Q6   Q7   Q8
10   11   12   13   14

That means since each quad requires two triangles and each triangle 3 indices you need

int[] triangles = new int[ (xMax-1) * (yMax-1) * 2 * 3 ];

So finally it should look something like:

Vector3[] vertices = new Vector3[xMax * yMax];
int[] triangles = new int[ (xMax-1) * (yMax-1) * 6 ];
// vertices
for (int y = 0; y < yMax; y++) 
{
    for (int x = 0; x < xMax; x++) 
    {
        int v = x + (y * xMax);
        vertices[v] = new Vector3(x,y,0);
    }
}
// triangles
int t = 0;
for (int y = 0; y < yMax-1; y++) 
{
    for (int x = 0; x < xMax-1; x++) 
    {
        int v = x + (y * xMax);
        triangles[t++] = v;
        triangles[t++] = v + 1;
        triangles[t++] = v + xMax;
        
        triangles[t++] = v + 1;
        triangles[t++] = v + xMax + 1;
        triangles[t++] = v + xMax;
    }
}