It has been a while since the last blog post I made. I am currently back at school and have been busy reseaching for my third year specializations (2 times 8 weeks to research a game related subject). The first specialization was focused around creating a setup for a C++ game-engine and the second specialization was focused on the creation of a voxel terrain editor plugin for the Unreal Engine.
For the second specialization I wanted to dive into some, for me, new and unknown territory which was the Unreal Engine. The goal for this specialization was to make an edit mode plugin for creating and managing voxel terrains in the engine with C++. My work during this specialization consisted of, among other thing, creating a tri-planar material for the voxel terrain, setting up a user interface for the editor and creating plugin modules with different functionalities. In this blogpost I will first talk about the generation and rendering of a smooth voxel terrain in the engine.
Voxels in games are essentially points of data that are stored in a 3D grid. For this I have created a 3D array in Unreal which can be achieved by nesting multiple structs. To make accessing the data in the nested structs easier I manually overloaded the ‘’ operator that takes an index number and then returns the value in that struct’s array like a traditional multidimensional array. The fun thing about voxel terrains is that you can alter them at runtime and dig holes and caves in your terrain but also build on top of your terrain.
In order to render the voxel data in a 3D environment we need to convert it into mesh data. There are multiple ways to turn voxel data into a mesh, you could do it the Minecraft way where you create a cube if a voxel point is surrounded by nothing. Next to turning the data into cubes you can also use the “Marching Cubes” algorithm which I have selected to use which will give you a smoother mesh. The algorithm was first described in 1987 by William E. Lorensen and Harvey E. and was used in the medical branch to visualize 3D point data acquired from scans of the human body.
Marching cubes checks a cubic group of 8 points in the voxel data and checks which points are under and which points are above the surface of the object. Depending on which points are occupied you can generate a mesh with 256 possible solutions of which most are trivial. Leaving the trivial solutions away leaves us with 15 possible mesh occurrences as described in figure 1. In the voxel data I’ve set the occupied voxels to be 1 and the free voxels to -1. Performing the marching cubes algorithm on this data will result in a terrain as seen in figure 2.
As you can see the terrain in figure 2 is a bit blocky, this is because of how the marching cubes algorithm works (P. Bourke). The algorithm takes floating point values and defines an intersection value at which the mesh should be created (I set this value to be zero). Marching cubes checks if a corner point is below or above the intersection value and interpolates between two opposite points if an intersection is found to add a vertex at the exact position of the intersection. In order to achieve smooth voxel terrains we need to make use of the floating point values. In figure 3 I used perlin noise to get a height value and used that value to alter the intersection position at the surface of the terrain. If you check figure 4 you can see that the terrain still consists of the blocky mesh as seen in figure 2 but is now properly interpolated with the use of perlin noise.
To render the mesh data acquired by the marching cubes algorithm in Unreal I am using Epic’s experimental “Procedural Mesh Component” or PMC for short. This actor component allows you to create and modify meshes at runtime. User “Koderz” on the Unreal Engine forums created a custom implementation of the PMC called the “Runtime Mesh Component” which fixes some issues with the Epic’s own PMC. The voxel terrain- and mesh generation is handled in my customVoxelTerrain actor.
The next blog post will be on how I have created the voxel terrain edit mode plugin in the editor.
Bourke, P. (1994, May). Polygonising a scalar field. Retrieved June 16, 2016, from http://paulbourke.net/geometry/polygonise/
Lysenko, M. (2012, June 12). Smooth Voxel Terrain (Part 2). Retrieved June 16, 2016, from https://0fps.net/2012/07/12/smooth-voxel-terrain-part-2/
Koderz. (2016, June 8). Runtime Mesh Component. Retrieved June 16, 2016, from https://forums.unrealengine.com/showthread.php?113432