Hi everybody,
I need to visualize a set of some 100 thousand objects:
Simple cubes (same material, same size, movable, no physics).
Instantiating 125000 GameObjects from a single prefab takes a significant amount of time (3 nested for loops, C#) and
the framerate drops to ~1 when the entire grid of cubes is visible.
Dynamic batching already reduces the drawcalls to around ~150.
Any suggestions on how to tackle this?
Thanks for your suggestions ;
rev
Procedural Solution:
I would seriously consider presenting these objects using a procedurally generated mesh instead of using 100,000’s of individual game objects. I would represent the objects using a chunked data structure and spread the procedural mesh across multiple chunks. This would greatly reduce the number of objects (because you would essentially just have one game object for each chunk).
For example,
YourGrid [ ProceduralGridBehaviour ]
--> Chunk [ ProceduralChunkBehaviour + MeshFilter + MeshRenderer ]
--> Chunk [ ProceduralChunkBehaviour + MeshFilter + MeshRenderer ]
--> Chunk [ ProceduralChunkBehaviour + MeshFilter + MeshRenderer ]
Personally I would avoid using colliders for said objects because the performance spike of adding and removing colliders at runtime in scenarios like this can be extremely costly. If you are creating a Minecraft-style voxel game then there are often better ways to simulate collision.
Pooled Solution:
Another option might be to keep a data structure that identifies which objects should be where and their current state. You could then spawn the blocks that are either a) visible or b) in proximity to reduce the overall number of objects present at any given time. You would generally utilise a pooling solution to both recycle objects and avoid instantiations at runtime.
PoolManager by Path-o-logical Games is a fantastic pooling solution and is certainly worth mentioning.