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. Dismiss Notice

Question Split mesh in half

Discussion in 'Scripting' started by TTuPC, Apr 29, 2022.

  1. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    Hello!

    I already wrote here unsuccessfully, but I try another time.
    I'm trying to split my mesh in half, more precisely, I want to split my mesh in two part for a certain value of Z.
    For example, all vertices of my mesh that are after a value of Z of 13 generate another mesh.
    At the moment I'm trying to do it without consider UVs and normals.
    This is my code:

    Code (CSharp):
    1.  GameObject newObj = GameObject.Find("default");
    2.  
    3.         verts = new List<Vector3>();
    4.         tri = new List<int>();
    5.  
    6.         int[] trin;
    7.         int[] newMeshCopy;
    8.  
    9.         Vector3[] newMeshVerts;
    10.  
    11.  
    12.         mesh = GetComponent<MeshFilter>().mesh;
    13.         newMesh = newObj.GetComponent<MeshFilter>().mesh;
    14.  
    15.  
    16.  
    17.         trin = newMesh.triangles;
    18.  
    19.  
    20.         newMeshCopy = newMesh.triangles;
    21.         newMeshVerts = newMesh.vertices;
    22.  
    23.  
    24.         for (int i = 0; i < newMeshVerts.Length; i++)
    25.         {
    26.             if (newMeshVerts[i].z >= 0)
    27.             {
    28.                 verts.Add(newMeshVerts[i]);
    29.             }
    30.             else
    31.             {
    32.                 for (int j = 0; j < trin.Length; j += 3)
    33.                 {
    34.                     if ((trin[j] == i)
    35.                         || (trin[j + 1] == i)
    36.                         || (trin[j + 2] == i))
    37.                     {
    38.                         trin[j] = 999;
    39.                         trin[j + 1] = 999;
    40.                         trin[j + 2] = 999;
    41.                     }
    42.                 }
    43.  
    44.             }
    45.         }
    46.  
    47.  
    48.         for (int i = 0; i < newMeshCopy.Length; i++)
    49.         {
    50.             if (trin[i] != 999)
    51.             {
    52.                 tri.Add(newMeshCopy[i]);
    53.             }
    54.         }
    55.  
    56.         Vector3[] ver = verts.ToArray();
    57.         int[] tr = tri.ToArray();
    58.  
    59.  
    60.  
    61.         for (int i = 0; i < ver.Length; i++)
    62.         {
    63.             for(int j = 0; j < newMeshVerts.Length; j++)
    64.             {
    65.                 if(ver[i] == newMeshVerts[j])
    66.                 {
    67.                     for(int h = 0; h < tr.Length; h++)
    68.                     {
    69.                         if(tr[h] == j)
    70.                         {
    71.                             tr[h] = i;
    72.                         }
    73.                     }
    74.                 }
    75.             }
    76.         }
    77.  
    78.  
    79.         mesh.Clear();
    80.         mesh.vertices = ver;
    81.         mesh.triangles = tr;
    82.  
    83.  
    84.  
    Unfortunately, when I run my code the triangles are not correct, and I think that the problem is due the existence of duplicate vertices that contain different normal and uv informations.
    Someone can tell me what I'm doing wrong?
    And maybe does someone know of to achieve my objective with different approaches?
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777

    Hi,

    Why not open the mesh in a 3d modelling editor, like Blender? Or test with some simple known good meshes, like a plane, or a cube for example. Its probably much easier to correct any broken meshes, than wasting time guessing what may, or may not be the problem.
     
  3. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    It must be a runtime feature
     
  4. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    For anyone that will need it, the problem was in the horrible triple for cycle, because the already seen vertices were considered more and more times.
    The correct code is:

    Code (CSharp):
    1.         for (int i = 0; i < ver.Length; i++)
    2.         {
    3.             for(int j = 0; j < newMeshVerts.Length; j++)
    4.             {
    5.                 if(ver[i] == newMeshVerts[j])
    6.                 {
    7.                     for(int h = 0; h < tr.Length; h++)
    8.                     {
    9.                         if(tr[h] == j)
    10.                         {
    11.                             tr[h] = i;
    12.                             continue;
    13.                         }
    14.                     }
    15.                     newMeshVerts[j] = new Vector3(999,999,999);
    16.                     continue;
    17.                 }
    18.             }
    19.             continue;
    20.         }
    Which is still horrible for the triple for cycle but, hey, it works