- Home /
Efficient mesh instantiation
Is there a effecient way to place 10000 meshes on a terrain, without making the game lag. This is because I want to make a terrain with grass on it, so I would need to instantiate around 10000 meshes for a decent look. Note: The terrain is a mesh and is generated at run-time so I can't use the built-in tools..
You can place them out over the span of several frames ins$$anonymous$$d of trying to create them all in the same frame.
For example, with a coroutine you can use a while loop with a yield return null; inside it so it will skip a frame for each iteration
This way you could instantiate maybe a couple of hundred to a thousand per frame ins$$anonymous$$d of waiting for everything.
I have a problem, my yield return null line says that it needs a semicolon which it has.
function Grass() {
var chunkGrass = 0;
while (chunkGrass < grassPerChunk) {
chunkGrass++;
if (chunkGrass == grassPerChunk / 20) {
yield return null; //UCE0001: ';' expected. Insert a semicolon at the end.
}
}
}
In JS you do not need to specify 'yield return null', It is used in C#. In JS just use:
yield;
Hi, I don't know exactly what are your needs but 10k meshes or objects is a lot to manage for an engine. Why don't you create 1 mesh composed of 10k quads, each representing 1 grass leaf ?
@Harshad$$anonymous$$ Thx :)
@dns How would I do that? :)
Answer by iwHiteRabbiT · Oct 21, 2014 at 04:28 PM
Have already made a scatter tool which did what you want. Bad idea ^^'. Instancing a lot of gameObject slow down considerately the framerate.
However, if you can't create a "single" mesh for your all grass, you could try to manage yourself by saving each mesh transformation matrix in a single array. After, in Update() for exemple, drawing yourself via DrawMesh (async drawing with full rendering pipeline) or DrawMeshNow (sync drawing without unity rendering pipeline).
With this, you never instanciate anything, just yield the creation of all the matrix.
Answer by Little_Turtle · Feb 01, 2017 at 09:05 AM
A better answer is to group grass in the same mesh. This way you create one mesh spawning many grass instances (like a sector) in a single object. Each grass might consist just of two triangles but you are free to create as many triangles as far away from each other as needed.
If you putting you combine your different grass textures in a clever way you can even put many different grass and flower combinations closely (or with some space in between) together so you can use a single pair of triangles to draw two or more grass groups together (if you use real 3d to draw the grass).
Also remember creating 10000 times a mesh is expensive, creating 10000*2 triangles within a mesh is cheap but also creating 10000 times an object is expensive but having 10000 existing objects being moved as necessary (grass that is not visible any more gets reused by just moving it to a new location).
This way you can have almost instant appearing grass without the costs of creating too many objects.
Your answer
Follow this Question
Related Questions
Grass on mesh 0 Answers
Creating more objects with code. 2 Answers
How to create billboarding planes 6 Answers
Instantiating a gameobject causes a lagspike 2 Answers
how can I instantiate an object specifically at the intersection of a collider and a mesh 1 Answer