Search Unity

Help Wanted Reorder mesh triangles

Discussion in 'General Graphics' started by MatheusMarkies, Sep 16, 2021.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    62
    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:


    Triangles (Made by shader):

    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.

    Code (CSharp):
    1.  
    2. public void buildObjectList()
    3. {
    4.     int indicesCount = 0;
    5.     int verticesCount = 0;
    6.  
    7.         int[] triangles;
    8.         Vector3[] vertices;
    9.  
    10.         triangles = pto.objectMesh.triangles;//Take a sequence of vertices that form a triangle.
    11.         vertices = pto.objectMesh.vertices;//Take vertex list
    12.  
    13.         pto._pathTracingObject.indicesOffset = indicesCount;//Offset: Where the list of indices belonging to the object starts. | indicesCount: How many indices were added before these.
    14.         pto._pathTracingObject.indicesCount = triangles.Length;//How many indexes does this object have.
    15.  
    16.         foreach (Vector3 vertice in vertices)
    17.             this.vertices.Add(vertice);
    18.  
    19.         for (int i = 0; i < triangles.Length; i++)
    20.         {
    21.             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
    22.  
    23.             /*Example:
    24.             If the previous object has 270 vertices.
    25.             The first triangle of the next objects will be connected to the vertices(271, 272, 273) instead of(0,1,2).
    26.             +1 because vertex 270 belongs to a different object.*/
    27.         }
    28.  
    29.         indicesCount += triangles.Length;//update index
    30.         verticesCount += vertices.Length;//update index
    31. }
    32.  
    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:

    Code (CSharp):
    1.  
    2. Vector3 v0 = vertices[indices[pto._pathTracingObject.indicesOffset + i]];
    3.    Vector3 v1 = vertices[indices[pto._pathTracingObject.indicesOffset + i + 1]];
    4.    Vector3 v2 = vertices[indices[pto._pathTracingObject.indicesOffset + i + 2]];
    5.  
    "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.


     
unityunity