GameObjects vs. Submeshes


I’m creating a Voxel Terrain engine. My chunks’ dimensions are: 16x16x16. It means that I need a lot of them to fill up the space I want.

It also means (for now) instantiating and removing a lot of gameObjects. I need to optimize it, because it produces terrible laaaggg.

So my question is: Is it better to reuse the same gameobjects or to create just a few of them and use submeshes to place several chunks inside, or maybe a combination of these two?

(I heard that submeshes allocate extra memory and I don’t know how they affect MeshCollider and Mesh update performance, but gameobjects as they’re now are horrible too).

Thanks for your help in advance.

I cannot comment on the use of submeshes for this, since I have never done that for terrain stuff, but reusing GameObjects did go a long way when I did a terrain engine. There is an older question that would indicate that GameObjects are the better choice.

When an object falls outside the range or your view, you can add it to a queue of free objects and then use what’s in there to fill any gaps coming up. Have a look at the Minecraft Starter Package for an example.

Also, you want to be careful when updating static mesh colliders, it can be slow, so you want to spread the updates over several frames if you are pushing more than one chunk at the time.

Also keep your eye on the profiler to make sure you do not force PhysX to make unnecessary updates, such as moving the old collider.

My memory is a bit rusty on this, so I could well be wrong but I seem to remember that it’s a good idea to disable the collider, move it, replace the mesh then re-enable but this is definitely something you would need to test and verify, it’s been ages.

You would not use separate GameObjects, but instead you would create mesh chunks using the Mesh class. You can easily find more info on this with a search, since this subject has been covered repeatedly.