Want to learn about combining meshes.

Do you guys have any good tutorials for me to read about combining meshes? I found the explanation of it online but I’m not sure how to approach it or if I even really understand it.

So basically instead of making 100 Instantiate() calls to draw 100 boxes, as long as they use the same mesh, I can combine them and make 1 call?

Sorry for my lack of knowledge… I’ve been programing for a long time but this is my first real foray into the 3d world. I’ve been trying to read as much as possible but my google skills have been lacking.

there should be scripts to do this in the standard assests, no? CombineChildren and CombineMeshes or something like that.
Also, as far as I know unity, (might be only a pro feature) would batch meshes that share the same material for you.

I see the scripts but am not exactly sure how to implement them. That’s why I thought I need to read more about it but I can’t find any good resources.

I’m exploring this feature of Unity3D as well. I used the CombineMesh sample included in the documentation:

@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)

function Start () {
    var meshFilters = GetComponentsInChildren(MeshFilter);
    var combine : CombineInstance[] = new CombineInstance[meshFilters.length];
	
    for ( i = 0; i < meshFilters.length; i++){
        combine[i].mesh = meshFilters[i].sharedMesh;
        combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
        meshFilters[i].gameObject.active = false;
    }
    transform.GetComponent(MeshFilter).mesh = new Mesh();
    transform.GetComponent(MeshFilter).mesh.CombineMeshes(combine, null, false);
    transform.gameObject.active = true;
}

I attached the code to the parent object (say Box 0), and then in the object hierarchy, all the other boxes are underneath Box 0. It seems to work perfectly, my draw count went down which was the intended effect. However, I’m having a problem with the world space location of the objects after I attach the script. It seems that once I apply the script, everything moves where it’s not supposed to be (or where I didn’t place it in the map editor). I tried setting useMatrices = false, but that makes all the children objects disappear! :expressionless:

Anyone have any clue what I’m doing wrong?

Try adapting this to your code.
Its saves the old position, sets to 0,0,0, then restores it later
Also I show how to reset a MeshCollider.

This is written to be used while in a regular class object not a GameObject.
But its easy to change, like Silencer has done it.

Vector3 oldPosition;
MeshFilter mf; 
MeshCollider mc;

public void CombineStuff(GameObject parentGO)
  {
    // save existing parentGO position
    oldPosition = parentGO.transform.position;

    // sets parentGO position to 0,0,0,
    parentGO.transform.position = Vector3.zero;
    // parentGO.transform.rotation = Quaternion.identity;  //include this if needed

    MeshFilter[] meshFilters = parentGO.GetComponentsInChildren<MeshFilter>();  //also gets parentGO mesh
    CombineInstance[] combine = new CombineInstance[meshFilters.Length - 1];    //destination is short by 1 to not include parent

    int index = 0;

    for (int i = 1; i < meshFilters.Length; i++)  			//i starts at 1 to skip parent MeshFilter
    {
      combine[index].mesh = meshFilters[i].sharedMesh;    
      combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
      meshFilters[i].renderer.enabled = false;  			// sets child renderers off
    }

    // set up the new mesh
    mf = parentGO.GetComponent<MeshFilter>();
    //Object.DestroyImmediate(mf.mesh);//<<<<<<<<<<<<<<<<< INCREASES VBO COUNT, mem steady
    //mf.mesh = new Mesh();//<<<<<<<<<<<<<<<<<<<<<<<<<<<<< INCREASES VBO COUNT, mem rises without Destroy
    mf.mesh.CombineMeshes(combine, true, true);

    // reset parentGO position
    parentGO.transform.position = oldPosition;

    //parentGO.GetComponent<MeshCollider>().sharedMesh = mf.sharedMesh;  //<<<<<<<<<<<<< IT WORKS ONLY ONCE  !!!!!

    mc = parentGO.GetComponent<MeshCollider>();      //<<<<<<<<<<<<< VBO Steady, mem steady
    mc.sharedMesh = null;
    mc.sharedMesh = mf.mesh;

    //Object.DestroyImmediate(parentGO.collider);    //<<<<<<<<<<<<< VBO Steady, mem steady
    //mc=parentGO.AddComponent<MeshCollider>();
    //mc.sharedMesh = mf.mesh;

    parentGO.renderer.enabled = true;

  }

Tzan, I tried adapting your save position to the example code from unity, but I’m not sure how to do it because CombineInstance uses a Matrix4x4 for the transform, and since you’re using an object, you’re saving the position as a Vector3.

How are you implementing the above? Are you attaching it to a parent object, etc?

Dont do anything to CombineInstance.

Save the position
Set to Vector3.zero
Then after all the combining reset to the old position
parentGO.transform.position = oldPosition; << maybe you missed this line

All you need to do is add those three lines to your code.