Saturday, August 29, 2015

Voxel brushes

Playform now comes equipped with voxel brushes! They are very rough and very buggy, and the only one exposed directly to the user is a sphere brush, but the core functionality is still there.



This has been slowly in the works for over a month, and things were crashing pretty consistently for most of that time.

One big source of crashes was the code that turns the terrain voxels into a surface mesh (see this post). One invariant of that code is that if a voxel edge crosses the terrain surface, all the voxels containing that edge must be aware that they too cross the terrain, and keep extra data around to help build the mesh. It turns out it's really easy to violate that invariant if you're not being careful, and I wasn't. Another source of problems was that sometimes the "eraser" brush would expose parts of the world that hadn't been evaluated yet, because they'd been buried underground, and therefore weren't relevant to creating the mesh. Yet another series of crashes came up when a brush ended exactly between two voxels, because then they would "disagree" about whether they were crossing the terrain surface. After lots of debug logs and code comments, a version was reached that didn't constantly crash. It's still far from perfect, but it kindasortamaybesometimes works enough that I'm considering it functional for now, and doing other things for a while.

I started with a cube brush, because it's easy to define a cube and test whether it intersects various other things (especially points and cube-shaped voxels). This ended up being great since it exercised a lot of edge cases (pun intended), because the numbers were "too perfect". The sphere brush gave me an unreasonable amount of headache, because I couldn't find a clever way to place points inside some voxel that rested on the surface of some sphere (said another way: it's easy to pick some point inside a given voxel that rests on the surface of a given sphere, but it's a lot harder to find a good point that will make the mesh look nice). In the end, I just approached it from the point of view of "you have some shape, find a point on it", and reworked the terrain generation code that has to deal with exactly the same problem.

No comments:

Post a Comment