Procedurally, I’m creating a world of zones, each one 50x50 units. Each zone is simply a quad. It looks like a chess board, where the bottom right zone is (0, 0), the top right is (x10, x10), there are no negative positioned zones.
I next randomly place objects(trees) on each zone as a child to that zone. I then use the following code to combine all the objects(trees) into one mesh as a child under their respective zone.
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class CombineMeshes : MonoBehaviour {
void Start()
{
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
combine_.mesh = meshFilters*.sharedMesh;*_
combine_.transform = meshFilters*.transform.localToWorldMatrix;_ _meshFilters.gameObject.SetActive(false); i++; } transform.GetComponent().mesh = new Mesh(); transform.GetComponent().mesh.CombineMeshes(combine); transform.gameObject.SetActive(true); } } Zone (0, 0) works perfectly, but the farther away the zone is from origin, the farther away the gameObjects(trees) get placed. As you can see from the picture by placing each gameObject(tree) in the center of the zone, there an obvious a pattern to it, but I clearly don’t understand why. Tree “A” is correct, Tree “B” is 50 units off, Tree “C” is 100 and so on… [63804-unitypick.jpg*|63804]* _*
So the answer came to me in a dream…and some trial and error over breakfast. Here is a working code that combines meshes one at a time (each frame in a coroutine) in the correct locations. Evidently the main problem was that combining meshes need to be done around world origin (0, 0, 0). This isn’t cut-and-paste code, rather written to make more sense, and I’m sure my comments aren’t completely accurate, but it does work as intended for me.
while numberOfTreesToBePlaced > 0)
{
//Random placement of Tree inside Zone boundaries
posX = Random.Range(0, zoneSize);
posZ = Random.Range(0, zoneSize);
//Raycast at random position created above
Ray rayTarget = new Ray(new Vector3(transform.position.x + posX, rayStartPoint, transform.position.z + posZ), Vector3.down);
if (Physics.Raycast(rayTarget, out rayHit, rayBeamLength))
{
//To make sure raycast hit the ground so our tree isn't floating in air
if (rayHit.collider.tag == "Zone")
{
//Retrieve Tree mesh (Probably can be simplified since this gameObject will be destroyed soon)
GameObject newObj = (GameObject)Instantiate(Resources.Load("Prefabs/Pine1.1High"));
//The center of each Zone is the bottom left corner, so place newObj as if the Zone was positioned at (0, 0), but use the rayCast Y value
newObj.transform.position = new Vector3(posX, rayHit.point.y, posZ);
//Retrieve Tree mesh to be copied
MeshFilter copyMesh = newObj.GetComponent<MeshFilter>();
CombineInstance[] combine = new CombineInstance[2];
//"single Mesh" is the main mesh that new meshes are combined with, it is a GameObject
combine[0].mesh = singleMesh.GetComponent<MeshFilter>().sharedMesh;
//Instead of moving singleMesh to world center, I already have a gameObject at (0, 0, 0), called WorldCenter. I just steal it's coordinates
//and pretend that is where singleMesh is at.
combine[0].transform = worldCenter.transform.localToWorldMatrix;
//Grab my Tree mesh and it's coordinates
combine[1].mesh = copyMesh.sharedMesh;
combine[1].transform = copyMesh.transform.localToWorldMatrix;
//Deletes original singleMesh
singleMesh.transform.GetComponent<MeshFilter>().mesh = new Mesh();
//Combines singleMesh in CombineInstance[] with Tree mesh
singleMesh.transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
//Destroys gameObject that had the Tree mesh I needed
Destroy(newObj);
}
}
numberOfTreesToBePlaced--;
}