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 Problem with splitting a mesh

Discussion in 'Scripting' started by TTuPC, May 4, 2022.

  1. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    I already wrote about this but I'll try again.
    I'm writing a code that split a mesh in half. The cut point is Z = 0, so I have two meshes, one with everything on the left of Z = 0 and one with everything on the right.
    My code is this:

    Code (CSharp):
    1.  
    2.     Mesh mesh;
    3.     Mesh newMesh;
    4.     public List<Vector3> _Vertices;
    5.     public List<int> _Triangles;
    6.     int[] triangles;
    7.     Vector3[] verts;
    8.     public void AddTriangles(Vector3 vert1, Vector3 vert2, Vector3 vert3)
    9.     {
    10.         _Triangles.Add(_Vertices.Count);
    11.         _Vertices.Add(vert1);
    12.         _Triangles.Add(_Vertices.Count);
    13.         _Vertices.Add(vert2);
    14.         _Triangles.Add(_Vertices.Count);
    15.         _Vertices.Add(vert3);
    16.     }
    17.     private void Awake()
    18.     {
    19.         GameObject newObj = GameObject.Find("default");
    20.         mesh = GetComponent<MeshFilter>().mesh;
    21.         newMesh = newObj.GetComponent<MeshFilter>().mesh;
    22.         triangles = newMesh.triangles;
    23.         verts = newMesh.vertices;
    24.         for (int i = 0; i < triangles.Length; i += 3)
    25.         {
    26.             if ((verts[triangles[i]].z >= 0) && (verts[triangles[i + 1]].z >= 0) && (verts[triangles[i + 2]].z >= 0))
    27.             {
    28.                 AddTriangles(verts[triangles[i]], verts[triangles[i + 1]], verts[triangles[i + 2]]);
    29.             }
    30.         }
    31.         mesh.vertices = _Vertices.ToArray();
    32.         mesh.triangles = _Triangles.ToArray();
    33.     }
    But everytime that I run my code, if the mesh have a lot of triangles I obtain this kind of result:

    upload_2022-5-4_12-38-38.png

    Someone can tell me what I'm doing wrong?
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hello,

    Have you watched this?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    Why? I told you how to fix your data / logic error here:

    https://forum.unity.com/threads/split-mesh-error.1276370/#post-8097947

    Did you learn ANYTHING from that process? Remember we don't have your code running in our setup with your data and we don't even know exactly what you expect from the final output, nor were we privy to your thinking as you engineered it.

    Just because you didn't like that answer and you failed to even have the common courtesy to respond to my post above, doesn't mean you just keep spamming the forum with fresh posts of the same question again and again.

    I quote you the relevant forum section:

    Screen Shot 2022-05-04 at 6.26.15 AM.png

    https://forum.unity.com/threads/unity-community-code-of-conduct.743180/
     
    Last edited: May 4, 2022
  4. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36


    I apologize for not replying, I really appreciated the answer and it was absolutely not my intention to be rude. Unfortunately, despite trying all the suggestions, the error persists, so I thought I'd write the question again