Voxplosion is an experimental voxel framework for Unity, started mostly out of curiosity. It represents the virtual world as a matrix of voxels and each voxel is associated with a global metrial and color palette. The world is further divided into chunks, which in turn are triangulated to be rendered by Unity. Voxplosion also includes a world builder within unity and can import voxel models from MagicaVoxel.
I used this little engine to create a twin stick shooter with zombies and destructible terrain while organizing and simultaneously taking part in a 48h game jam.
How it Started
The roots of this project are based on a life-long fascination with voxel-based games, starting with the old Novalogic games like Comanche I played when a was a child. Returning to this fascination lead to the discovery of MagicaVoxel and playing around with it. Building these model I wondered if they could be imported and used in Unity. Therefore I began this little side project out to see if I was able to write a voxel engine with reasonably good performance and to find a particularly silly name for it. I’m inclined to say I succeeded in at least one of those tasks.
At the heart of Voxplosion is a Chunk system that contains a configurable number of voxels. A voxel is simply the combination of a unique position in the world, determined by the chunk it’s part of, a color id for lookup in the palette, and a material id which determines the voxels physical properties. Chunks are combined into a world, which can contain an arbitrary number of them. Most of the parameters are configurable, e.g., the dimensions of a single voxel in unity units and the size of the chunks.
To be able to build simple worlds directly inside the unity editor, I also wrote a little world builder. One of its essential feature is to import qb-files, which can be exported from MagicaVoxel. The choice for qb was mainly because of very easy parsing. Each different color in the qb file can be mapped to the global color and material palette defined in unity. Afterwards, the objects are converted to VOB files, which stands for voxel object, and is what the voxel engine works with. To build a level, voxel objects can be dragged into the scene and placed normally using Unity transformation tools. It can than be exported to a json file which contains the individual objects and their position. These level files can than be loaded by the Voxplosion.
When a level is loaded, all voxel objects it contains are loaded and their individual voxel data is converted into the voxel space of the world, divided into the individual chunks. At this step, individual objects like trees or walls cease to exists and only a giant voxel cloud remains. Of course Unity has no concept of voxels per se, so they have to be converted to something Unity can understand. There are several strategies, like using 3D textures and a volume renderer. I choose to simply generate triangle meshes from the individual chunks, so that each voxel is essentially a cube, and render them normally.
Creating triangle meshes from voxels results in a fairly big number of vertices and faces. To reduce them, only the sides of a voxel are generated which are not adjacent to another voxel. Other optimizations would be to generate only faces which are visible to the camera and to merge adjacent faces which share the same color. However, I did not implement those as it became apparent that the performance issues I faced would likely not be substantially reduced by these measures.
Voxplosion can reliably render several million voxel at a steady 60 frames per second. However, I had decided to work with a resolution of 16 voxels per meter to achieve a 3D pixel art like look. While this doesn’t sound much, a single m³ already contains 4096 voxels. So even an area of 100x100m would contain several billions of them. The level of detail I choose for the world was simply to large to render anything with a larger scope. Reducing the level of detail however would not have allowed the look I was aiming for.
While I did manage to build a voxel engine, even a fairly performant one, it was not as much as I had hoped it would be. At some point I realized that I would need to invest a vast amount of time to optimize the engine and decided that this time would be better spend at creating an actual game. However, it was a fun project and a good exercise, so I still consider it a success.
On another note, MagicaVoxe is a truly great program and I can wholeheartedly recommend it to anyone who want’s to build voxel models.