This is the first in a blog series I will be doing on a research project about voxels; specifically, my goal is to delve into various applications of voxel technology in gameplay. This week, I started with the basics, and made a simple voxel grid, with the ability to add & remove blocks.
I picked Unity as my engine of choice for this project, mostly because I am most comfortable with it, but also because A. efficiency is not important here, as the goal is to learn how voxels can be applied, and B. Unity is very well documented, and has an active community. I looked around and found many different ways that voxel grids can be created, from treating each cube as its own GameObject to using GPU instantiation, and even some using the particle system & Shadergraph (Couldn't find the link to this one, sorry). Ultimately, I generated a single mesh representing the whole voxel grid (Board to Bits Games & Flaroon's tutorials were especially helpful); this means that I can generate a grid with a reasonably small amount of bloat compared to making each block its own GameObject, since each block doesn't have it's own collision, rotation, or scale. However, it also means the whole grid needs to be regenerated each time the values are edited, which will only become more costly as the size and complexity of the grid increases.

Now that I can create a grid, the next step is to add the ability to add & remove blocks. Targeting a block with a raycast is easy enough, but because the whole grid is one GameObject, we need to calculate which block we hit on our own. There's just one problem; when I hit a block, it will always be on the border of one filled block and one unfilled block (for example, A raycast hitting (8.50, 6.27, 0.99) could be rounded down to (8,6,1) or up to (9,6,1)). I decided the solution was to check both adjacent blocks; if I'm trying to add a block, then it will fill the empty space, and if I'm removing a block, then it'll clear a filled space.

Now, we have a simple grid that we can add & remove blocks from. Great!
Comments