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

Question Best way to optimize 800 cube

Discussion in 'General Discussion' started by Ali-Alhakeem, Apr 21, 2023.

  1. Ali-Alhakeem

    Ali-Alhakeem

    Joined:
    Apr 21, 2023
    Posts:
    3
    Hello , So i was curious
    I Have a scene with bunch of cubes ;
    Like 800 cube in one scene . it's just bunch of cubes in one scene with different textures.
    They don't move neither have rigged body.
    What is the best way to optimize the scene ?
    like can you hide them off camera, or is baking the light for them is a good idea . would this increase preformance . Or is there any other way to optimize it even more to reach maximum fps .

    would love feed backs
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,799
    Ali-Alhakeem likes this.
  3. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,986
    If you have 800 cubes and you don't need to collide with them, I would suggest using DrawMeshInstanced. That would let you render all of the cubes using one draw call.
     
    Ali-Alhakeem likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Do I hear Minecraft? ;)

    If you want to create a voxel world then Cubes and one GameObject per Voxel won't scale well beyond 100k voxels - no matter what you do. In fact if you save these objects to a scene file, you will run into serious performance degradation issues in the editor way below 100k game objects. All the while the size of the scene quickly explodes to several hundreds of megabytes.

    Look into chunk generation, greedy meshing and for runtime generation/updates: DOTS (packages: Burst, Jobs, Collections, Mathematics - Entities could also be helpful if the voxels need to be heavily dynamic but I wouldn't recommend to start with it).
     
    Ali-Alhakeem and DungDajHjep like this.
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    761
    800 cubes for a voxel terrain would be too few, a 16x16x16 chunk alone would have 4096 cubes. (with no air)
     
    Ali-Alhakeem likes this.
  6. Ali-Alhakeem

    Ali-Alhakeem

    Joined:
    Apr 21, 2023
    Posts:
    3
    Thanks alot for the replys , did researches and learned alot today .
    For anyone wondering I'm new to modeling so i slapped bunch of cubes and models in one scene , i know it would be alot better to model everything in one large mesh but im stll new and it's easier and faster that way.

    GPU instancing works but must have the same mesh and material from what i read.
    DrawMeshInstanced was also good . doubled the fps

    One last question , is there a way to occlusion culling without baking the scene ? There are too many objects to bake. This would increase preformance exponentialy.

    thanks alot guys
     
  7. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    761
    Ali-Alhakeem likes this.
  8. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,986
    Another thing to try is MeshBaker:
    https://assetstore.unity.com/packages/tools/modeling/mesh-baker-5017

    With MeshBaker, you can bake multiple objects into one object. It is one of my favorite optimization tools. MeshBaker can combine different objects with different materials, and then create one new combined mesh with one new combined material.
     
    Ali-Alhakeem likes this.
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,005
    At this point, with no clear direction of the needs or purposes of these cubes, your best bet is to simply mark them all as static. Unity will then automatically batch them into larger collections of a single mesh at run or build time.

    However as you’ve now discovered automatic batching has limitations, such as the same material etc, just like instancing. Which is why the purpose of the models becomes important, as that will guide you toward the optimal solution. Unfortunately optimal solutions often require additional work, rarely do you get anything for free.

    In your case you could solve the batching issue either by producing a texture atlas that contains all the textures, or use textureArray’s ( though not supported on all platforms ). Both require effort either with code, shaders, or modelling. Someone mentioned Mesh baking which can produce the meshes and atlases automatically and that is a good approach, though you’ll have to find code or purchase an asset from the store.

    So the question becomes are any of these approaches beyond simple static batching or instancing worth it? If you are just playing around, then probably not, if you want to learn or need the performance in a product then it will be.
     
    Ali-Alhakeem likes this.
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,381
    Absolutely the most performant way to render 800 cubes is to delete all of the cubes and replace them with a UI text box that says "You see 800 cubes of various textures".
     
    Noisecrime likes this.
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    "It is pitch black. You are likely to be eaten by a grue."
     
    kdgalla likes this.
  12. Ali-Alhakeem

    Ali-Alhakeem

    Joined:
    Apr 21, 2023
    Posts:
    3
    That's is actually a pretty cool tool ! this can help me alot . definitely gonna check it out !

    Wait so by making objects as static , this will boost preformance automaticly or is there extra steps ? isn't this the same as the MeshBaker In the previous respones or is it different since both combine objects into one mesh?
    Also is using One large texture with multiple materials that has different Offset boost preformance or does it require code?
    Yeah i can see that works lol
     
  13. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    It is similar. Object will be combined, but they'll be combined into part of the level and become non-moveable. With mesh baker, I'd assume you'd be able to move the result.
     
    Ali-Alhakeem likes this.
  14. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,986
    Setting objects static and enabling static batching can boost performance. It is different than baking the meshes using MeshBaker. Static objects cannot move. Baked objects can move (or also be static if you want). I often use MeshBaker to bake multiple meshes into one mesh, and then I GPU instance that combined mesh at runtime.

    When you experiment with batching, you will probably see that Unity's automatic batching helps but not as much as it should. Unity's dynamic batching is even less effective than it's static batching. I did a bunch of tests with batching years ago. I had one test scene with a thousand identical objects, and Unity's dynamic batching was creating groups of 2-7 items. Unity's dynamic batching was not smart enough to grab all thousand items into one batch.
     
    ippdev, Ryiah and Ali-Alhakeem like this.