This site uses strictly necessary cookies. More Information

X- Home /

# 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.

**Answer** by Bunny83
·
Oct 18, 2015 at 10:26 PM

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;
}
}
```

No, but thats not the problem. Variable "v" is the final vertex from our 2 variables x and y. Say we want vertex "8":

```
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
x$$anonymous$$ax = 5
y$$anonymous$$ax = 3
```

Remember this:

```
x + (y * max);
```

X would be 3, and Y would be 1 (remember starting value is 0). So if we multiplied Y by y$$anonymous$$ax, and add x:

```
3 + (1 * 3) = 6
```

It does not equal 8.

But when we multiply it by x$$anonymous$$ax:

```
3+(1 * 5) = 8
```

We do get 8.

The problem I am having is about the triangulation algorithm that would fit with my type of vertex alignment.

I think you may want to re-read @Bunny83's reponse. What was said and how you responded are both saying the same thing, pointing at what you don't seem to realize you did wrong.

Just to restate it in full, you currently have

```
// Line 7
int v = x + (y * y$$anonymous$$ax);
```

but what should be there is

```
// Line 7
int v = x + (y * x$$anonymous$$ax);
```

Oh lol thanks I didn't realize that. But what about triangulation?

Just in case you haven't seen the edit of my answer...

Why do i need to subtract one from the max for the for loop? When x$$anonymous$$ax = 3, and do: for (int x = 0; x < x$$anonymous$$ax; x++) {

It will loop from 0 to 2, which is a total of 3 iterations, which is what i want.

No, it's not what you want. You have 3 vertices along the y axis but only2 quads in between. Likewise you have 5 vertices along x but only 4 quads in between.

I was talking about the triangle generation, not about the vertex generation. Those are two seperate things and your vertex generation isn't really in the code you posted. You should seperate the vertex generation from the triangle index generation.

If you have a vertex field of 5 x 3 vertices you have a quad field of 4 x 2 quads

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

*edit*

Actually your triangle array is too small btw ^^. You need:

```
int[] triangles = new int[(x$$anonymous$$ax-1) * (y$$anonymous$$ax-1) * 6];
```

Since each inner iteration you create 6 indices and you (should) loop to (x$$anonymous$$ax-1) and (y$$anonymous$$ax-1), that's exact the length you need.

Wow okay so it worked. So just throw all the final statements like correct size triangles array and for loop stuff into a answer and i can make it the best answer.

@Bunny83 I am having a problem that for some reason I am having trouble fixing.

The amount of triangles in the x and y axis is 1 less than the x$$anonymous$$ax and y$$anonymous$$ax. I tried modifying the for loop length to be in both axis:

```
for (int x = 0; x < x$$anonymous$$ax; x++) {
```

I adjusted the length of the arrays to match the for loop, but the problem is, is that that this method only works on 1 axis, which is the x axis.

The result in the y axis was that the last triangles on that axis was connecting to the other side of the mesh. How would I go about fixing this?

That shouldn't be the case. You either have setup your vertices wrong or your triangles.

The highest vertex index is this one:

```
triangles[t++] = v + x$$anonymous$$ax + 1;
```

Since x and y is at max "(x$$anonymous$$ax-2) * (y$$anonymous$$ax-2)" the highest index is:

```
v + x$$anonymous$$ax + 1;
x + y * x$$anonymous$$ax + x$$anonymous$$ax + 1;
(x$$anonymous$$ax-2) + (y$$anonymous$$ax-2)* x$$anonymous$$ax + x$$anonymous$$ax + 1;
```

If you rearrange you get

```
(x$$anonymous$$ax-2 + 1) + (y$$anonymous$$ax-2 + 1) * x$$anonymous$$ax
```

Which simply is

```
x$$anonymous$$ax-1 + (y$$anonymous$$ax-1) * x$$anonymous$$ax
```

which is:

```
x$$anonymous$$ax * y$$anonymous$$ax -1
```

and that is the highest index in the vertices array. The indices never cross the border so they won't wrap around unless you did not use the right limits.

$$anonymous$$eep in mind that in line 17 of my code example in my answer you still need to use x$$anonymous$$ax, not (x$$anonymous$$ax-1) since you want to calculate the offset in the vertices array:

```
int v = x + (y * x$$anonymous$$ax);
```

@Bunny83 I don't think you understand my problem.

What i am trying to ask is that I want my x and y values to go from x = 0 to x <= x$$anonymous$$ax WITHOUT modifying my value of x$$anonymous$$ax. This will result in x going through the loop x$$anonymous$$ax + 1 times, which is what I want.

When I do create a for loop that follows my current need, it results in the y axis having sets of triangles wrapping back to the other side.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Building a prism (adding thickness) from non-convex mesh 0 Answers

Procedural cave mesh only lighting in one direction 0 Answers

How to create a 2D mesh from a vertex array? 1 Answer

moving hole 3 Answers

When meshes are loaded ? 1 Answer