Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Assigning triangles to mesh give a different result then expected

Discussion in 'Scripting' started by Trebbit, Sep 11, 2023.

  1. Trebbit

    Trebbit

    Joined:
    Jan 5, 2016
    Posts:
    5
    I'm working on reading different kinds of 3D models so I can use them in Unity but I had this problem a couple of times. After I get the data out of the files I create a mesh and assign the vertices and triangles. For some reason, the mesh gets completely messed up. So I tried drawing the triangles before assigning them to the mesh. With the result shown below the red figure shows at least the shape of the mesh while the right one has the same data it looks like a tent. When looking inside the mesh the triangles and vertices are still the same.
    I draw the triangles like this:
    Code (CSharp):
    1.             for (int i = 0; i < triangles.Count; i += 3)
    2.             {
    3.                 Debug.DrawLine(vertices[triangles[i + 0]], vertices[triangles[i + 1]], Color.red, 1000);
    4.                 Debug.DrawLine(vertices[triangles[i + 1]], vertices[triangles[i + 2]], Color.red, 1000);
    5.                 Debug.DrawLine(vertices[triangles[i + 2]], vertices[triangles[i + 0]], Color.red, 1000);
    6.             }
    I think it may have something to do with the order of the triangles or something I have to do after creating a mesh from scratch. Does anyone have any idea what could cause the difference?

    Note: It also looks sometimes that after a certain index, the triangles start from the start so let's say you have a human from the neck to the top of the head All those triangles will use the vertices at the feet while drawing the triangles doesn't show this issue.
     
    Last edited: Sep 11, 2023
    Bunny83 likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,344
    I would suspect one of these:
    • incorrect triangle winding order (must be clockwise I think)
    • incorrect handling of shared vertices
    • incorrect coordinates, eg local vs global space
    • mismatch of triangle indices to vertex array (point to wrong vertices)
    • bugs in your code ;)
    What file format are you reading? Maybe an importer or converter for that format already exists? What‘s the use case for doing this anyway?
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,604
    Your mesh looks like it has a lot of vertices. Have you paid attention to the index format of your mesh? By default a Mesh would use a 16 bit index buffer which means you're limited to 64k vertices. You have to explicitly set the index format to 32 bits in order to represent a mesh with more than 64k vertices. Keep in mind that using a 32 bit index buffer doubles the size of the index buffer given the same amount of data. So only do that when you really need more than 64k vertices.

    Of course a 32 bit index buffer can handle up to 4 billion (4 milliard) vertices.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,526
    How do you put them in a mesh? If they draw correctly then you're interpreting the data correctly there, but clearly it's not going into the mesh correctly, somehow.
     
  5. Trebbit

    Trebbit

    Joined:
    Jan 5, 2016
    Posts:
    5
    Changing the index format solved the issue thank you so much must have missed this somewhere this also explains why it stops after certain amount of triangles.
     
    angrypenguin and Bunny83 like this.