Loading Voxel Models into the Game

I didn’t get as much time as I wanted to work on my game last week. Spent too much time with Skyrim (coming very late to the game, but nonetheless). Still there are some new updates to show. First I have re-opened my Twitter account for game dev related stuff, so make sure to follow me there. Also, I am now able to load models created with the MagicaVoxel editor, which will likely take a big part of my game development time in the future.

I discovered MagicaVoxel from Giawa’s dev blog, and he already made some code models into his program, which is alreadyavailable for use. After modifying the code a bit, I was able to get a model loading correctly into my game. I had to flip some coordinate values around, because Magica treats Z axis as vertical, while I use the Y axis for that. Also, I had trouble expanding the 16-bit color values back into a 32-bit integer. The colors were approximate but still looked off, especially with skin tones and lighter colors. So my importer code stores voxels with 24-bit color values. Here is a comparison with the model in Magica and in the game with my importer:

Posted Image

Models made with Magica use a palette of 256 colors which can be user-defined. My model format in the game uses the first 768 bytes to store the RGB values for the palette (alpha not supported yet), and the rest of the bytes are X, Y, Z and color index for each solid voxel. At the end I store two more X, Y, Z locations to get the bounding box of the model. This helps me center and position it for player movement.

The code for converting the voxels to meshes is a lot like the code for the chunk meshes. The process is split up into three steps – copy all voxels to a 3D array, cull visible voxels, and finally check neighbors to make the voxel cubes with the right combination of sides.

As with the chunks, each step is also thread-able to speed up loading of models. It now seems possible to condense a lot of this code into one generic class or set of functions that can work with any kind of voxel object. But the code still needs more cleaning up in order to make this happen.
One trick I use to make this code more readable is to pad the voxel array with a 1 unit “wall” of empty voxels on each side. For example, if I want to import a model 32x32x128 in size, I load it into a 34x34x130 array. Then instead of looping from 0 to 31 or 0 to 127, I loop from 1 to 32, 1 to 128, etc. This way I can guarantee that each voxel in the model doesn’t have any neighbors that fall out of bounds in the array.

Posted Image

Now it’s time for me to get a bit creative. Time to start making my own voxel models for the landscape, as I will be adding grass, bushes, etc. and coming up with items that can be picked up.

Advertisements
Tagged with: ,
Posted in Engine, Graphics
2 comments on “Loading Voxel Models into the Game
  1. Modrain says:

    Converting the colors from the MagicaVoxel palette is normally straightforward. It might seems off in your engine due to the editor being non-neutral on the ambient lighting and diffuse color. It’s the cost of having a visually appealing editor.

    And good to see another voxel game developper coming to Twitter 🙂
    Some people that you might be interested to follow:
    https://twitter.com/ephtracy : MagicaVoxel’s developer
    https://twitter.com/y2bcrazy : an inspiring voxel artist
    https://twitter.com/wol_lay : CubeWorld’s developer, rarely around but still a good source since he posts on twitter when updating the news about the game.

    Like

    • Chris says:

      Thanks, and I actually got started on this project because of Wollay. I want to try my own spin at a voxel RPG. I recognize Y2bcrazy’s work on Vox as well (though AlwaysGeeky may not recognize me yet). Foxel looks amazing. It looks like an update version of Fez.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: