Search code examples
unity-game-enginerenderingvoxel

Unity: Help needed with rendering voxels and performance


So here is the deal, i have an enemy that is rigged and consists out of quite a few voxels. enter image description here

When i run the game i get very bad performance overall when its rendering the model because of the amount of objects it needs to render. How can i improve that?

Here's the things i have thought about: Only rendering the faces we actually can see, Or maybe using some sort of GPU Instancing?

Anyways, i would like to know how i could resolve such a problem. Any help is much appreciated!


Solution

  • Without knowing how you've actually implemented your voxels in game (i.e what components each voxel consists of and how you're currently managing them) it's hard to offer much specific advice.

    In general though there are a few things to consider:

    • Faces that aren't oriented towards the camera won't be rendered by default. What you might want to investigate is Occlusion Culling, but since your character is not static within the scene you might not get much performance improvement from that. The code to constantly check whether objects are occluded might end up costing more CPU than not implementing it.
    • Rendering might not be your actual bottleneck for performance. If each of your voxels is doing some work every update (such as collision detection), then that's going to hit your performance much harder than rendering the objects, particularly for cubic voxels. You'll need to use the Profiler to figure that out.
    • You need to consider whether you actually REALLY need all those voxels to be in the scene all the time or whether you can replace them with a single GameObject until you need to interact with them, then bring them into the scene as needed. For example, you might replace the upper arm with a mesh that mirrors the shape of the total voxels for that body part. Then when that part is shot, for example, you can detect the point of collision and instantiate the necessary voxels around that point to react as desired, then rebuild the arm mesh to reflect the changed shape.

    It might also be worth looking into Unity's Data-oriented Technology Stack (DOTS) features, although that could be overkill for this situation.