# Reorder mesh triangles

Good Morning!

My problem is: I need to get the vertices of a mesh and send them to a shader.

Inside the shader I will reassemble the mesh from its vertices.

So far so good, I send the information and the shader tries to mount. But I'm not hitting the algorithm that assembles the new triangles. As seen in these images:

Mesh:

Each line of equal color; represents a triangle. Some colors end up repeating themselves, but I think you can understand.

Basically I build a list already in the order of the triangles and then put together 3 by 3.

public void buildObjectList()
{
int indicesCount = 0;
int verticesCount = 0;

int[] triangles;
Vector3[] vertices;

triangles = pto.objectMesh.triangles;//Take a sequence of vertices that form a triangle.
vertices = pto.objectMesh.vertices;//Take vertex list

pto._pathTracingObject.indicesOffset = indicesCount;//Offset: Where the list of indices belonging to the object starts. | indicesCount: How many indices were added before these.
pto._pathTracingObject.indicesCount = triangles.Length;//How many indexes does this object have.

foreach (Vector3 vertice in vertices)

for (int i = 0; i < triangles.Length; i++)
{
this.indices.Add(triangles[i] + verticesCount + 1);//Variable containing all indices of all objects in the scene. | triangles[i] (take an index) +verticesCount(Add to the amount of vetices already added) + 1

/*Example:
If the previous object has 270 vertices.
The first triangle of the next objects will be connected to the vertices(271, 272, 273) instead of(0,1,2).
+1 because vertex 270 belongs to a different object.*/
}

indicesCount += triangles.Length;//update index
verticesCount += vertices.Length;//update index
}


In other words, if vertices 1,2 and 3 form a triangle in the list, they will appear in this sequence.

So, when I need to form a triangle, I'll get the indices i, i+1 and i+2 from the list.

As done here:

    Vector3 v0 = vertices[indices[pto._pathTracingObject.indicesOffset + i]];
Vector3 v1 = vertices[indices[pto._pathTracingObject.indicesOffset + i + 1]];
Vector3 v2 = vertices[indices[pto._pathTracingObject.indicesOffset + i + 2]];


"pto._pathTracingObject.indicesOffset" It's good for when I have more than one object in the scene, then I skip the vertices of other objects using this offset.

For example if I have 2 objects in the scene and the first has 10 vertices, the vertices of the second will be counted after those 10.

Like this: 11.12,13...

But this algorithm is wrong, as it is connecting vertices that shouldn't be together. "buildObjectList()"

For example, unity says my cube has 12 triangles. However, the algorithm returned 716 triangles.

• This is usually caused by a disconnect with the vertex attributes in the shader and what is being sent to the shader, can you show the vertex shader source? Sep 16 at 19:01