Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How Do I Have a LOT of Objects in a Scene?

Discussion in 'Scripting' started by notsteve, Dec 18, 2014.

  1. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    My scene consists of 524288 cubes, drawn as a landscape. The user should be able to traverse the landscape, freely. Right now, I'm drawing all of them, and my system slows to a crawl. Surprise!

    Obviously, I don't need to draw all the cubes. At any one time, they're obscured by other cubes, not in the viewport, beyond the horizon, or too small to be worth drawing.

    How do I deal with this in Unity, if I'm creating the objects with a script (specifically C#)?

    Thanks!

    Bill
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
  3. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Awesome, thanks!
     
  4. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Got ya. But these instructions (e.g. "baking") appear to be related to static objects in the scene. All my objects are generated by a script, and their size changes during program execution.

    How do I apply those concepts to my scripted objects? (The objects are all instantiations of a prefab.)

    Thanks!

    Bill
     
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I suggest developing a chunking system. It's basically necessary with voxel terrains until we have quantum computers.

    Having your interaction / physics code run against >500,000 objects could be what is slowing it down. You can have a script that checks the distance between you and other things in the scene in a round robin style so you check maybe 50,000 per second total instead of 500,000 every frame. When certain voxels are within a certain distance, enable their interactability. When they aren't, disable all the expensive things you don't need. Once you have that in place, you can also easily implement your own LOD system if you don't have unity pro :)
     
    notsteve likes this.
  6. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Thanks! I do have Unity Pro. What is LOD? (Sorry!)
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    notsteve likes this.
  8. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    You said you're working with cubes right now so maybe level of detail isn't too much of an optimization. If you have high quality textures on the cubes, you can use LOD to make them really low quality ones when no one is close enough to see them.
     
    notsteve likes this.
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The best way to have lots of objects is to not have lots of objects. Voxel systems construct meshes that contain a bunch of cubes each, instead of having an absurd number of actual cubes. You can't use occlusion culling with this (not the built-in system anyway), since the meshes are constructed dynamically.

    --Eric
     
    notsteve likes this.
  10. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    So it's agreed, use chunking to cut down the number of objects to the necessary number of objects?

    Probably a more sensible solution than creating your own culling with view frustum tests.
     
    notsteve likes this.
  11. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Thanks for your thoughts! All I have on the cubes (which are actually a set of quads I assembled in the editor) is a gradient done in shaders (not with textures). So…I guess I could turn off the gradients on objects not being seen?
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, Unity already uses mipmaps for textures, no need to do anything about that yourself (it would just slow things down). The point is to not use half a million cubes.

    --Eric
     
    notsteve likes this.
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I had decent performance with 400,000 objects in a scene before. Is 500,000 just the upper limit for the engine where computer specs don't matter?
     
    notsteve likes this.
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no upper limit as far as I know; computer specs are all that matters. However brute-forcing 400K objects is still a pretty bad idea even if you get OK performance on your rig, because you could get much better performance by using fewer objects.

    --Eric
     
    notsteve likes this.
  15. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Thanks, everyone for the responses!

    Below is a sample image from my scene. (Except that I want it to keep going beyond the horizon.)

    Tomnnn, where can I learn more about "chunking"?

    Eric, are you suggesting draw this as one giant mesh? I need to be able to raise/lower individual cubes, but never create or destroy them.

    Thanks again so much guys!

    BIll

    landscape.png
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not as one mesh (you can't use more than 65K vertices in a single mesh anyway), but several. You change the appropriate vertices in a mesh when you want to update cubes. This would be useful. Also this.

    --Eric
     
    notsteve likes this.
  17. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Chunking only applies to situations where you're not going to see it all at once. I'm not sure what you're trying to do now with that screenshot. Are all of the cubes visible at once, or will the player's view be somewhere in the middle? Implementing a chunk system, you'd divide that whole area into sections, as Eric suggested, and load&unload sections based on where the player is and what chunks the player can actually see. Minecraft implements fog so things fade after a certain view distance, and just beyond that view distance there is nothing. As you approach the fog, new sections of the world will be loaded in while sections you are moving away from will be disappearing behind you.
     
    notsteve likes this.
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd still need to use chunking because you can't have >65K vertices in a mesh. Also it would probably be useful to have smaller meshes (say 8-16K vertices) since updating vertices requires uploading them all, even if you're just changing a few, so updating an 8K mesh is faster than updating a 65K mesh. It depends on how many cubes you're updating at any one time.

    --Eric
     
    notsteve likes this.
  19. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    You are a gentleman and a scholar. The second link has been invaluable. Thank you.
     
  20. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Yes, all the cubase are visible at once. Fog is brilliant; that's the answer!!!!
     
  21. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Glad you got something out of it. I also saw yesterday whilst checking out creative mode that... 7 days to die does the same thing! lol

    I flew upwards some distance and could clearly see the edges of the map being loaded and unloaded as I moved. It's a pretty interesting thing to see, and on the ground you can never quite see far enough into the horizon to actually see the edge appear or disappear. It's a great trick :)
     
    notsteve likes this.
  22. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    I would only be updating a few thousand at a time; they would all be moving up or down by the same factor. I am only resizing them.

    Currently, I am using a prefab for my boxes, which is good, because I can use Unity's automatic hidden object stuff.

    Question: If I create a mesh landscape, how will I handle not drawing what can't be seen?

    Thanks you guys are awesome!

    Bill
     
  23. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity has frustum culling automatically for everything, but you can't use the built-in occlusion culling (assuming you have Pro in the first place) for dynamic content anyway, so prefabs or no prefabs makes no difference as far as that goes. GPUs these days don't really have an issue drawing lots of triangles.

    --Eric
     
    notsteve likes this.
  24. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    Ya, I have Pro. Unity is actually supporting my research for some reason; it's strange since I obviously don't know what I'm doing, lol.
     
  25. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    illuminati confirmed! :eek:
     
    notsteve likes this.
  26. notsteve

    notsteve

    Joined:
    Dec 13, 2013
    Posts:
    18
    I'm using a Mac Mini 2012 right now, which has basically no graphics capability. Good processor and memory. But triangles are actually a problem, lol.

    I'm remedying that by building a Windows beast with a kick-ass graphics card. When in doubt: throw hardware at the problem!

    Bill