- Home /
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.
Then why are you trying to modify the vertices of the mesh directly? That is not possible.then have all the duplicated meshes combined into one mesh with the original mesh.
Well, that was something I tried. I don't want the grass turfs to be in the same place. So I need some way to make a copy of the original mesh (in memory, without creating a new gameobject) and move it to a new position (and ultimately also a new random rotation along the Y axis) and then combine this modified copy with the original mesh.
Answer by rainChu · Aug 19, 2018 at 11:54 AM
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.
Thanks! This solves the copying thing! :) I now have a mesh of 8064 triangles, but the grass turfs are all stacked on each other, so the scattering part is not working. So I get all the copies combined together, but they are still all in the same place. Any ideas on how to fix that issue? I tried modifying the vertex list with my $$anonymous$$oveNew$$anonymous$$esh() method, but that didn't help.
Oops, my bad! :) Of course it should be hit.point, that was a typo :) But thanks for the matrix stuff! The scattering part now works as well! :) The rotation works if I swap the multiplication order around :) Anyway huge thanks for your help! :D
Your answer
Follow this Question
Related Questions
GetComponentsInChildren(MeshFilter) not recognizing sharedMesh as variable 2 Answers
Meshes displayed wrongly after CombineMeshes 1 Answer
Combine meshes result in a larger file size on export (GLTF) 1 Answer
Combine meshes at runtime without all mesh as children of mesh 0? 1 Answer
Mesh.CombineMeshes causes objects to appear in front when they really aren't 0 Answers