One of the key features of Paint the Town Red that we’re adding to elevate the over-the-top gore from what we had in the 7DFPS game jam version is the that the main shapes of the enemies like the head, torso and upper and lower arms and legs are made up of "voxels". What that essentially means is instead of the head being one big cube-shaped mesh, it's a shape based on the combination of a series of smaller cubes. Being able to then dynamically change the cubes that make it up by changing or removing some and having the overall head shape reflect that means we can have chunks of the head or other body part be severed and destroyed when hit.

Creating the Mesh

The naive approach to rendering voxel objects would be to simply make each voxel a separate renderable cube mesh in the game. While that might work for certain types of games where there might only be a very limited number of voxels, it's not a viable approach for a game where you're concerned with hitting high framerates and not having tight restrictions on the number of voxels.

The approach that we've taken with creating the mesh to be rendered for each voxel-based object is to create a single mesh that contains the triangles of all the exposed faces of the voxels. So something like the head of an enemy may start as a 10x10x10 3d grid/matrix of voxels that are all intact. If we look at all these voxels and check what each of their 6 faces are touching, we can see when a face is touching nothing (i.e. not touching another voxel) and add that face as 2 triangles to our objects overall mesh. We also need to account for whether that voxel face requires a UV coordinates for a texture and pass that information to the new vertices along with other particular rendering information for that voxel stored in the vertices for our custom shader.

This approach involves looking over the state of all voxels at creation and every time they change and recreating the entire mesh. Fortunately our voxel mesh creation code is already very mature and efficient as it was brought across from another game we were working on prior to Paint the Town Red.