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 CombineMeshes creates an ampty object

Discussion in 'Scripting' started by imagoFX, Mar 29, 2021.

  1. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    81
    Hi,
    I'm trying to combine a bunch of meshes into one. Copied and modified code from unity docs and it creates an empty mesh. The remark in the middle is a long code that instantiates prefabs and adds them as children to the island object - removed it here to make things more readable. What am I missing?

    Code (CSharp):
    1.  
    2.  
    3.         GameObject  island = new GameObject("island" + ix.ToString() + iy.ToString());
    4.  
    5.        //Code that creates child objects
    6.  
    7.         MeshFilter[] meshFilters = island.GetComponentsInChildren<MeshFilter>();
    8.         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
    9.  
    10.        
    11.         for (int i = 0; i < meshFilters.Length; i++)
    12.         {
    13.             combine[i].mesh = meshFilters[i].sharedMesh;
    14.             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
    15.             meshFilters[i].gameObject.SetActive(false);
    16.         }
    17.  
    18.         island.AddComponent<MeshFilter>();
    19.         island.AddComponent<MeshRenderer>();
    20.         island.transform.GetComponent<MeshFilter>().mesh = new Mesh();
    21.         island.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
    22.  
    23.         island.AddComponent<MeshCollider>();
    24.         island.transform.gameObject.SetActive(true);
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,750
    There's a lot of different ways to combine meshes. Most of the examples I have seen have one issue or another, and they ALL have required some extra engineering to make them actually work. For instance, some of them mis-handle source meshes that have more than one submesh, or otherwise mix up the materials.

    If you want to track down what's going wrong above, the only way is to strip down the input meshes to the simplest thing possible (two triangles being combined for example) and then debug it. I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you want to see my own implementation of a mesh combiner that works pretty well, check out my MakeGeo project and look for the CombineMeshes directory for some demo scenes:

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    81
    Thank you for the detailed response Kurt but I know how to debug code, I'm posting here after hours of unsuccessful attempts...