Quite a few people have asked about the voxel explosion effects in Rot Purge, so I thought I'd try and cover it off in a fairly brief overview.
We're using a 3rd party app from the Unity store called "PicaVoxel".
Well that's cheating!
No, it's getting a game shipped.
I learned a long time ago there's no point re-inventing the wheel for every game, just by using Unity we've moved a lot of the heavy lifting to someone else's code.
I digress, so PV ( PicaVoxel ) let's us load vox file in, as created by the excellent ( And free ) "MagicaVoxel", stores the voxels in a 3D array and creates an optimised mesh for us. Perfect.
Now PV comes with a load of methods for blowing things up and other cool stuff, but they're really not as optimised as they could be, and this is where actual work comes into it.
When a bullet impacts a zombie we have it's position and power, from there we can work our way through the 3d array cutting out a big chunk of the voxels for the bullet hole.
This image of the actual zombie with a grid overlay should help you picture the 3D array.
If the zombies health is less than the bullets power we don't bother cutting out a bullet wound, we just blow it up into pieces of voxel meat and blood.
To do that we loop through the array, y then x then z ( So from the bottom left corner of that image, the grid location closest to us ) looking for active voxels. Once we find one we fire off a particle from that position and colour, and repeat until every voxel has a particle counter part and then we can kill the actual zombie mesh.
To optimise things and stop the performance suffering ( It's 60fps or go home ) we skip some voxels in our array, not only does it help performance wise but it actually looks better, originally we had too many particles ( Which is something I thought I've never write ).
In a perfect world I'd store the voxels in a linked list, so we wouldn't have to check a lot of dead space which is just time wasting, but there are other easier performance games to be made elsewhere in the game so that's either a major nice to have, or a last ditch attempt to keep the frame rate high.
And I think that's about it, it's an ultra simple approach that works really well.
Thanks for reading.