Voxel2Pixel is a C# .NET Standard 2.0 library for rendering voxel models as pixel art assets from a variety of fixed camera perspectives and palette limitations intended for use in retro-styled video games.
VoxFileModel model = new(@"..\..\..\Sora.vox");
IVoxelColor voxelColor = new NaiveDimmer(model.Palette);
The VoxFileModel
class is used for importing MagicaVoxel files. The Sora.vox
file contains a test model based on the character Sora from Kingdom Hearts.
Colors are specified in RGBA8888 format. The IVoxelColor
interface is used to determine which colors to draw sprites with. The NaiveDimmer
class implements IVoxelColor
by interpolating lighter and darker versions from the provided palette, which in this snippet comes from the imported MagicaVoxel file.
Sprite sprite = new(VoxelDraw.IsoWidth(model), VoxelDraw.IsoHeight(model))
{
VoxelColor = voxelColor,
};
VoxelDraw.Iso(model, sprite);
sprite = sprite.Crop2Content().Upscale(2);
The Sprite
class provides a byte[] Texture
array containing RGBA8888 format pixels and a ushort Width
property for the texture's size. These would be referenced to export images.
This snippet constructs a Sprite
instance by calling methods that use the model's size to determine the texture's size and references the model
and voxelColor
variables from the previous section.
The VoxelDraw.Iso
method draws the provided model
onto the provided renderer sprite
from an isometric camera perspective. The VoxelDraw
class also contains similar methods for other camera perspectives such as Above
, Diagonal
, Front
, Overhead
and Underneath
.
The Crop2Content
method crops out transparent pixels from the edges and the Upscale
method stretches out the sprite to double the width, which tends to look better for isometric sprites.
IEnumerable<ISprite> sprites = Sprite.Iso8(model, voxelColor)
.Select(sprite => sprite.CropOutline());
Dictionary<string, ISprite> dictionary = new();
byte direction = 0;
foreach (ISprite iSprite in sprites)
dictionary.Add("Sora" + direction++, iSprite);
Sprite output = new(dictionary, out TextureAtlas textureAtlas);
The VoxelDraw.Iso8
method draws a series of 8 sprites using alternating isometric and 3/4ths camera perspectives from the 8 winds of the compass. The CropOutline
method crops each sprite to remove transparent pixels from the edges and adds a 1 pixel black outline.
The sprites are then placed into a Dictionary<string, ISprite>
data structure to associate each sprite with a unique name.
Finally, all the sprites are combined into one texture atlas, where output
is a Sprite
instance containing the texture and textureAtlas
is an object containing the metadata which is serializable to XML.
Package | License | Purpose |
---|---|---|
Cromulent.Encoding.Z85 | MIT | Experimental voxel model file format encoding |
FileToVoxCore | MIT | Parses MagicaVoxel files |
PolySharp | MIT | Polyfills newer C# language features |
RectPackSharp | MIT | Packs rectangles for texture atlases |
The test project also depends on SixLabors.ImageSharp for output, but the actual library does not include this.