Reduce drawcalls when instantiate many objects like in Cities Skylines

Hello!

I’ve made a forest fire simulator (playable here: http://www.habrador.com/labs/forest-fire/). The forest is random so I have to instantiate all the trees one by one. I also need to change the individual trees to tress that have burned down. I’m doing that by deactivating the green tree and activate the tree that has burned down, which I have already instantiated.
The problem now is performance, the game is running slower thanks to all drawcalls. My question is how a game like Cities Skylines can instantiate so many trees and buildings, while my little forest can’t. What is their secret? I can’t combine all trees to one mesh because I have to remove individual trees…

Try using Graphics.DrawMesh and mesh combining. Im using it to display forects and grass on procedural planet.

2 Likes

Omg, it worked! From 5000 batches to 30 :slight_smile:

Update! It worked, but is really slow when the trees are changing, so I have to think of a smarter version of it…

Update 2.0! Ok, After 3 days of ups and down I figured it out. I began by combining the trees into chunks of 20000 vertices. To remove trees from a large chunk, I had to find the tree in the combined mesh and then just remove the vertices and the triangles. To the add a burned tree, I find a chunk with vertices left and then added the tree to that chunk with CombineMeshes(). And yes, this was so messy and Unity’s documentation wasn’t the best so I will write a tutorial.

If anyone is interested, the tutorial is finished and can be found here: Dynamic Mesh Combining, and you will learn how to make a Tree Brush tool like in Cities: Skylines.

4 Likes