How to combine a mesh with a copy of itself?

I’m trying to write a script that scatters grass turfs on the ground at runtime, but I don’t want to use Instantiate all of the time, since it is a slow process and it creates separate gameobjects. So what I would like to do is to use one Instantiate for the first turf and then simply copy that mesh (from the Mesh Filter) and offset it to a new position (determined using a raycast to the ground) and then combine them back together to one mesh, attached to only one game object.
Here is my code so far:

    public GameObject grass;
    void Start()
    {
        Mesh orgGrassMesh = grass.GetComponent<MeshFilter>().sharedMesh;
        Mesh finalGrassMesh = new Mesh();
        finalGrassMesh.name = "All Grass";

        for (int i = 0; i < 36; i++) // scatter 36 grass turfs
        {
            Vector3 pos = new Vector3(Random.Range(-3f, 3f), 500f, Random.Range(-3f, 3f));

            RaycastHit hit;
            Ray ray = new Ray(pos, Vector3.down);

            if (Physics.Raycast(ray, out hit, 600f))
            {
                Mesh newMesh = orgGrassMesh;
                //newMesh = MoveNewMesh(newMesh, grass.transform.position, hit.point);

                CombineInstance[] combine = new CombineInstance[1];

                combine[0].mesh = newMesh;
                combine[0].subMeshIndex = 0;
                combine[0].transform = hit.transform.localToWorldMatrix;

                finalGrassMesh.CombineMeshes(combine);
            }
        }

        grass.GetComponent<MeshFilter>().sharedMesh = finalGrassMesh;
    }

I also tried using a separate method for offsetting the turf mesh by moving all the vertices in the mesh. This method looks like this:

    private Mesh MoveNewMesh(Mesh mesh, Vector3 pivot, Vector3 newPos)
    {
        Vector3 posDif = newPos - pivot;

        for (int v = 0; v < mesh.vertices.Length; v++)
        {
            mesh.vertices[v] += posDif;
        }

        return mesh;
    }

In the first code example I have commented the call to this out, since I’ve tried using both methods, unfortunately none of the methods work. What happens is that I don’t get any new mesh at all. Either the mesh is just the original mesh that is showing up, or all the meshes disappears and I don’t see anything. Even when I try to inspect the mesh it still just says that it is 224 triangles (which is the count for the original mesh. So the mesh does not appear to be combined at all, nothing happens, or it just disappears.

So just to iterate. I want to Instantiate one GameObject with a mesh, and then just duplicate and offset that mesh in the scene (without creating new gameobjects) and then have all the duplicated meshes combined into one mesh with the original mesh.

You’re on the right track! You definitely should be using CombineMeshes. However, you’re using it wrong. What you’re doing is iterating through a for loop, and 36 times, you create the same mesh over and over, in a different position each time. CombineMeshes doesn’t add onto what’s already there. It combines the array of CombineInstance into a single mesh, using their transform.

Try something more like this:

var combine = new List<CombineInstance>();

for (int i = 0; i < 36; i++) // scatter 36 grass turfs
{
    Vector3 pos = new Vector3(Random.Range(-3f, 3f), 500f, Random.Range(-3f, 3f));
    Ray ray = new Ray(pos, Vector3.down);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, 600f))
    {
        var combineInstance = new CombineInstance();
        combineInstance.mesh = orgGrassMesh;
        combineInstance.subMeshIndex = 0;
        combineInstance.transform = hit.transform.localToWorldMatrix;

        combine.Add( combineInstance );
    }
}

finalGrassMesh.CombineMeshes(combine.ToArray());

Good luck!

Edit: I didn’t spot this at first, because I was paying more attention to CombineInstance, but the scattering won’t work because hit.transform is the Transform of the ground you hit, which is always the same. To get the 3D position of the point you hit, you should instead do this:

combineInstance.transform = Matrix4x4.Translate( hit.point );

You can combine rotation as well, but I’m not the best with Matrices as I’ve never needed to use them. You can do this:

// Edited to be in the correct order (See comments)
combineInstance.transform = Matrix4x4.Translate( hit.point ) * Matrix4x4.Rotate( Quaternion.identity ) ;

And replace Quaternion.identity with your actual rotation. However, I’m sure this is not an optimized way of doing things. I’d ask a new question on how to efficiently create a matrix with a rotation, followed by a translation, if you want to do this.