What is a Voxel Engine?
In a nutshell, a Voxel Engine is a piece of software which attempts to simulate atoms. Every atom or “Voxel” occupies a defined space and has specific properties. The most basic property of a Voxel are solid or not solid. These can also contain additional values to define other properties such as Color or Material. The properties of each Voxel ideally fit the requirements of the application because memory requirement is gigantic. The most common sample is Minecraft (the one successful game of 100000). The right image shows 4 blocks with different visual hulls. These blocks can be sculpt using a variety of tools. There are many use cases like games of course but also visualisation of any volumetric dataset such as computer tomography.
Developing a Voxel Engine in Unity3D:
What I loved during development is the complexity of the Voxel Engine. It is a large piece of software consisting of multiple parts like a clock work with 1000+ cogs. Every cog must fit exactly into the system. If just one cog turns in the wrong direction, everything will fall apart (Unity3D crashes). Just the most basic Voxel Engine already requires these steps:
- You have to implement the data structure. My implementation uses a Sparse Octree which subdivides and merges Voxel whenever they are modified. This step massively reduces the memory consumption but getting this right was mind boggling. This alone took 5 months to get right without crashes, bugs and other stupid quirks.
- Soon you will need a visualisation system in order to check if you data structure even works. Start implementing simple hull construction using cubes where each cube represents one Voxel.
- Implement a simple tool which modifies the Voxel data. Start with a simple sphere which removes material when it collides with Voxel. I simply use the collision detection system provided by Unity3D itself so GameObjects can interact with Voxel.
If nothing goes wrong, you may be able to modify Voxel using a simple spherical tool. Congratulations, you now own a Voxel Engine….. or not. The result probably are ugly and is far away from those ingenious implementations found in commercial Voxel Engines. It was just an amazing feeling when a golden, burning sphere carves into the Voxel block for the first time. 5 minutes later after getting a cup filled with a strange black liquid, I attempt to generate a Voxel block with higher resolution. Then the gold sphere is launched into the block and the frame rate turn from “Frames per Seconds” into “Seconds per Frame” and Unity3D freezes. Now the grind part starts: OPTIMIZING.
- Optimize the data structure.
- Happy multithreading 🙂
- Use Job system and Burst compiler in Unity3D.
- Enter the unsafe abyss of C#
Of course the most engine was completely single threaded. Unity introduced Job and Burst system back in 2018. It is still pretty new and magical and you are not allowed to use classes anymore. Only structs and “blitable” data are allowed. Therefore the only way to reference to other data is using IntPtr. IntPtr is a blitable pointer to a memory address which can be read and written. Working with these have no safety functionality, no length checks, no debug functionality and no garbage collection!.Understanding this took more than 3 months with my lousy 115 IQ. People with 145 IQ probably would understand this in one hour or less…. sounds unfair but that’s how this world work. You also have to do the same thing with the visualisation system. Also subdivide the visualisation in multiple regions so only those regions where material is modified are updated. I had to completely rewrite the system but it taught me a lot about multithreading and high performance coding. After countless hours of debugging crashes and optimizing I figured everything out especially why things crash and were able to fix all issues. Some causes were absolutely ridiculous such as floating point precision errors. Unity3D crashed when modifying a block had a total side length of 10. After countless hours, there was nothing left to optimize.
Having fun with your own Voxel Engine:
After countless hours, I understood how everything has to connect itself and the Voxel Engine had decent performance and it was possible to create the first sculptures. Now I had the possibility to implement different visualisation systems where the most common system is marching cubes. It is still difficult to imagine how marching cubes work and I slowly start to grasp how this work after almost 2 years of development time. Marching cubes really generates nice results and it is crazy how people came up with such algorithms. When everything else works, you can start to implement more tools such as smoothing using filter algorithms used in painting software.
After working and tweaking the Engine for about 2 years as part of my master thesis, I finally began to prepare the Engine for the Unity Asset store which took another 2 months until assignment for review. On 05.March 2020 my asset got accepted and can be bought on the Unity Asset store for just 20 bucks. I developed this system because I always loved Voxel and I was lucky that I could use it as part for my master thesis about sculpting in VR. I develop Unity Assets not just for money but also as portfolio entry for future opportunities. Now I can tell future employers that I developed and published a whole.