- Home /
Combining meshes at runtime
I feel like this is asking a lot but I'm completely lost on the subject. I have a script which instantiates small pieces to create a racetrack at the beginning of each level. It works fine but I need to reduce the draw calls. I have an empty game object named StartingPoint with the Create track script on it. There are half pipes, flat, and U shaped pieces of track. The pipes share one texture, flat has another, and U has one as well. Once all of the pieces are created I run Mesh.CombineMeshes in a function called CombineMesh. The Problem is the CombineMesh sets all of the created gameObjects to inactive because of this line(meshFilters[i].gameObject.active = false;) When i change it to true all of the gameObjects are active but not combined. I've looked through the docs, forums and answers but just can't grasp how to achieve this.
any help would be awesome! example code:
var PipeStraight : GameObject; var PipeTurnLeft : GameObject; var PipeTurnRight : GameObject; var UStraight : GameObject; var UTurnLeft : GameObject; var UTurnRight : GameObject; var nextPoint : GameObject; //empty gameoject that moves to next instantiation point//
function Awake() {
nextPoint.transform.position = Vector3(0, 0, 0);
nextPoint.transform.rotation = Quaternion.identity;
trackNumber = 1;//get variable from mainscript//
}
function Start() { CreateTrack(trackNumber);
}
function CreateTrack(trackNumber : int) {
if (trackNumber == 1) {
var Piece1 = Instantiate(Lpipe90_1200, transform.position, transform.rotation); Piece1.transform.parent = transform; NextPointMove("Lpipe90_1200");
var Piece2 = Instantiate(Lpipe90_1200, nextPoint.transform.position, nextPoint.transform.rotation); Piece2.transform.parent = transform; NextPointMove("Lpipe90_1200");
var Piece3 = Instantiate(Lpipe90_1200, nextPoint.transform.position, nextPoint.transform.rotation); Piece3.transform.parent = transform; NextPointMove("Lpipe90_1200");
var Piece4 = Instantiate(Lpipe90_1200, nextPoint.transform.position, nextPoint.transform.rotation); Piece4.transform.parent = transform; NextPointMove("Lpipe90_1200");
} CombineMesh();
}
function NextPointMove(moveName : String) {
if (moveName == "PipeStraight") {
nextPoint.transform.Translate(0, 0, -360);
}
if (moveName == "PipeTurnLeft") {
nextPoint.transform.Translate(0, 0, -360);
}
if (moveName == "PipeTurnRight") {
nextPoint.transform.Translate(0, 0, -360);
}
if (moveName == "UTurnLeft") {
nextPoint.transform.Translate(0, 0, -360);
}
if (moveName == "UTurnRight") {
nextPoint.transform.Translate(0, 0, -360);
}
if (moveName == "UStaight") { nextPoint.transform.Translate(0, 0, -360); } }
function CombineMesh () { 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); transform.gameObject.active = true; }
Answer by Peter G · Oct 13, 2010 at 10:59 PM
I would recommend looking at all these scripts.
http://www.unifycommunity.com/wiki/index.php?title=MeshMerger
http://www.unifycommunity.com/wiki/index.php?title=CombineSkinnedMeshes
http://www.unifycommunity.com/wiki/index.php?title=SkinMeshCombineUtility
The first one is probably your best bet.
I tried to use all of them but failed miserably. None seems to work.
Answer by yoyo · Dec 12, 2010 at 07:56 AM
You could consider procedurally generating the whole mesh, rather than creating a multitude of game objects. Look at the docs for Mesh, including how to create one from scratch.
I procedurally create a mesh to "draw" a trail on the ground behind a vehicle -- a similar technique could be used to generate a track. Rather than instantiating each of your ready made track pieces, you could use the Meshes in the ready made pieces as templates, but copy the data into one large new Mesh. You would need to transform the points as you copy the data, since you no longer have a separate transform per track piece.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075602im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fastest way to instantiate thousands of objects at runtime? 1 Answer
Checking if object intersects? 1 Answer
Projectile instantiation positioning is off. 3 Answers
Instantiate ground/enemies 1 Answer