Voxel engines are something I seem to circle around to every year or so. There’s something about the precision of the voxel aesthethic which appeals to me over polygonal meshes. Sure enough over the summer I started playing around in Unity with recursively generating octrees of voxels.
Here’s a video of an early attempt, using coroutines to visualise the process:
The motivation of using octrees for this rather than a simple 2D array is that it allows a sparse data structure, with each octree being subdivided only as the underlying data requires. For example, if a space contains no data at all the octree can be pruned entirely.
It also allows solid regions of voxels to be combined into a single voxel. This is simply done by pruning the children and creating a mesh at the parent instead (with 8 times the volume).
The example above represents each voxel as a single polygonal mesh of a cube. This is very easy to implement but not terribly performant – in Unity the bottleneck is more in the creation of so many GameObjects (one per voxel) than vertex count (which can be pushed to ridiculous levels with modern GPUs). But it does make the destruction and creation of new voxels trivial.
It does however give you Unity’s lighting and physics features for free, as the engine (like most) is built entirely around models being polygonal meshes. For that reason it’s very common to generate polygonal meshes which efficiently wrap the voxels data to create an optimal mesh. However this is slow and difficult to implement – plus I’ve done it before so I had no interest in going down that path again.
In my next post I’ll describe how I subverted Unity’s fixation with polygonal meshes to achieve a more efficient visual representation of voxels.