Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Reduce drawcalls when instantiate many objects like in Cities Skylines

Discussion in 'General Graphics' started by SuperChihuahua, Sep 30, 2015.

  1. SuperChihuahua

    SuperChihuahua

    Joined:
    Mar 3, 2015
    Posts:
    8
    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...
     
  2. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    197
    Try using Graphics.DrawMesh and mesh combining. Im using it to display forects and grass on procedural planet.
     
    theANMATOR2b and SuperChihuahua like this.
  3. SuperChihuahua

    SuperChihuahua

    Joined:
    Mar 3, 2015
    Posts:
    8
    Omg, it worked! From 5000 batches to 30 :)

    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.
     
    Last edited: Oct 5, 2015
  4. SuperChihuahua

    SuperChihuahua

    Joined:
    Mar 3, 2015
    Posts:
    8
    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.