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

Combining Meshs

Discussion in 'Scripting' started by Willy-The-Kid, Jun 25, 2015.

  1. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello.

    Following the "Optimizing Graphics performance Manual", I would like to combine meshs similar meshs to save some CPU calculations.

    In my case, I have a set of trees generated around my map, theses trees can be attacked by the player and destroyed. Theses tree share the same material and have 3 states, "New", "Broken" and "Destroyed", each state has is own mesh but still share the same texture.

    I have two questions about it.

    1 : If I combine thoses trees, does each tree still keep his own collision box and scripts or does it make all tree to be one single game object ? Will each tree still be able to change his own state ?
    2 : If I combine all trees into one mesh, does trees that are out of the screen will still be ignored for rendering ?


    Thanks for your help.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Hi,

    For what you need I would not recommend joining the meshes by hand(in a 3D modelling package). You will need to create colliders by hand(or script) if you need one per tree, the same goes for scripts and the entire mesh will need to be rendered even the parts not on screen.
    You should set your trees to be static so they can beneift from static batching - read about it here
     
    Willy-The-Kid likes this.
  3. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello karl.jones

    Thanks for your answer. I'm just a little confused by the doc you linked.

    There is multiple clues here that let me think this is not for my case :
    1 - I do not create the level in editor, instead I do have a procedural system that will places trees around the map in different place each time the game starts (this suppose that the transform moves one time in the game, once at start)
    2 - Once the level as been generated, trees does not move their transform, but they do have animations. Is that a limitation?
    3 - Trees can changes their mesh during the game (going from "New state" to "Broken state"). It's still the same game object, but it's not the same mesh displayed.

    Let me know if I'm wrong, I'ld love to benefit from static batching :)
    Thanks.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Static batching are by nature batching of objects that are static - they don't move. If you animate your trees, they're definitely moving.

    What you can get, on the other hand, is dynamic batching, which batches together moving objects. The link karl.jones posted contains info about dynamic batching. See the information there about how to make objects do dynamic batching. Try to set up your trees to benefit, and check the frame debugger to see if they're getting drawn in the same call.

    For performance, it's just as important to make sure that each individual tree is a single mesh. Note that if you have several materials (several submeshes), you'll get several draw calls per tree.
     
    karl_jones and Willy-The-Kid like this.
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Hi Willy,

    I interpreted you OP as you doing this outside of Unity such as a 3d modelling program so I assumed they would not move or be placed procedurally etc.
    So static batching will not work however dynamic batching should be fine. You could also benefit from creating LODs if the trees are particularly detailed.
     
    Willy-The-Kid likes this.