Procedural mesh triangulation wraps back on itself

Hello. I’m trying to triangulate an array of vertices into a mesh, However, my triangulation keeps wrapping back at the midpoint of my vertex array.
image?alt text
Here i have an image illustrating the order in which the vertices are generated.

And below that is the result of the triangulation, which wraps back on the other side halfway through the vertex array. The vertices are shown as sphere gizmos.
image?alt text
This is what my triangulation code looks like:

        _tris = new int[_count * 6];
        for (int x = 0, tri = 0; x < _count; x++, tri += 6)
        {
            _tris[tri] = x;
            _tris[tri+1] = x+1;
            _tris[tri+2] = x+2;

            _tris[tri+3] = x + 2;
            _tris[tri + 4] = x + 1;
            _tris[tri + 5] = x + 3;
        }
        mesh.triangles = _tris;

I’m out of ideas as to why this occurs.
Any help would be greatly appreciated.

edit Moved from answer

This is my vertex buffer code:

         _elements = new TempElement[_count];
         GetComponent().mesh = mesh
 = new Mesh();
         mesh.name = "blade"+gameObject.name;
         for (int i = 0; i < _count; i++)
         {
             TempElement e = new TempElement();
             e._index=i;
             e._tempVal = 1.0f;
             e._pos = new Vector3(transform.position.x +  (i+_spacing), transform.position.y,
 transform.position.z);
             _elements *= e;*

_verts.Add(e._pos);
_verts.Add(e._pos+(Vector3.up*e._tempVal));
}
mesh.vertices = _verts.ToArray();

Well, since you initialize your triangles array with _count * 6 we would assume that _count contains the number of quads. Since you increment “x” by one each iteration and tri by 6, we assume you want to process a quad per iteration, which is fine. However your vertex indices are not correct that way. You have 2 vertices per quad. So instead of x you have to use x*2 in all of your 6 lines.

edit

Since you added more information on how you generate your vertices, I can update my answer. The issue I’ve mentioned still stands, though now you have an additional issue :slight_smile: If you have “_count” vertex columns, you can only have “_count - 1” quads between them since the last quad would not have a right edge. Quads always have to be in between vertex columns. So if you have 3 vertex columns, you only have 2 quads. So this is the first issue. The second issue is still the same. Each vertex column has 2 vertices but you increase x only by one each iteration. So you would start the next quad in the same column just with the second vertex, essentially turning every second quad into a flipped parallelogram.

You have to do something like this:

int qCount = _count -1;
_tris = new int[qCount * 6];
for (int x = 0, tri = 0; x < qCount; x++, tri += 6)
{
    _tris[tri] = x*2;
    _tris[tri+1] = x*2 + 1;
    _tris[tri+2] = x*2 + 2;
    
    _tris[tri+3] = x*2 + 2;
    _tris[tri + 4] = x*2 + 1;
    _tris[tri + 5] = x*2 + 3;
}
mesh.triangles = _tris;

Keep in mind that most shaders only render the front side of a mesh with some rare exception (usually particle shaders and such). That means only the front side of the mesh would be rendered. If you want to use a shader that can be affected by light and you want your mesh to be visible from both sides, you would need to double up the vertices, so you have one set for each side with the proper normal vectors for each side, or implement your own double sided shader. Though in most cases single sided is the way to go anyways :slight_smile:

This is my vertex buffer code:

         _elements = new TempElement[_count];
         GetComponent().mesh = mesh
 = new Mesh();
         mesh.name = "blade"+gameObject.name;
         for (int i = 0; i < _count; i++)
         {
             TempElement e = new TempElement();
             e._index=i;
             e._tempVal = 1.0f;
             e._pos = new Vector3(transform.position.x +  (i+_spacing), transform.position.y,
 transform.position.z);
             _elements *= e;*

_verts.Add(e._pos);
_verts.Add(e._pos+(Vector3.up*e._tempVal));
}
mesh.vertices = _verts.ToArray();