Voxel Project: Slice & Dice
- Kyle W

- Jul 6, 2022
- 2 min read
Slicing
The main focus of this week was the ability to separate portions of the grid into their own grids. In order to accomplish this, whenever a voxel is removed from the grid, all voxels adjacent to it are placed in a list, and a function is run recursively to check that all adjacent voxels are still physically connected to each other. If an adjacent voxel is found, then it is removed from the list; this function continues until all adjacent voxels are found or the entire grid is checked. In order to prevent an infinite loop, I also created a matrix of bools to track which voxels have already been checked.

If any adjacent voxels are not found, I then run a while loop that goes through the list and removes any remaining adjacent voxels and connected voxels and places them into their own grid. The while loop means that if the grid has been split into more than two pieces, each piece gets turned into their own grid.

Additionally, to make it more apparent that the grid has been split apart, I added a Rigidbody component to the grid, so now separated parts will fall off.
Reorganizing
While working on being able to split grids, I figured it was about time to address the issue of the grid's dimensions being static. Up until now, the maximum size of the grid was determined as soon as the scene was loaded, and couldn't be changed afterwards. As a first step, the grid now tracks the outermost X, Y, and Z bounds of the voxels in the grid, which is used for centering the grid and making sure the new dimensions of the grid don't cut off any existing voxels. A new matrix is created, and the existing voxels are copied over; their new position is adjusted so that the original grid is in the center of the new grid, and the GameObject's position is adjusted so that the voxel's position does not move.

I also made a function for transposing the voxels across the current grid, mainly for moving the voxels to the center of the grid. In order to prevent the same voxel from being transposed twice, the direction that the grid is traversed through changes based on the direction of the offset; for example, if the voxels are being moved 2 blocks to the right, the the grid will be traversed from right to left on the X axis. Similar to the resize function, after the grid is transposed, the position is adjusted so that the voxels stay in the same position from the users' perspective, since the purpose of this function is to transfer empty space to different sides of the grid.

Next Week
While the voxel grid is uv mapped, until now I have been using a wireframe shader for the voxel grid. Across the next two weeks, I would like to work on a system for improving the uv mapping, as well as the ability to repaint individual voxels in the grid.



Comments