MagicaX

Hex.pmDocumentation

A comprehensive Elixir toolkit for parsing and generating MagicaVoxel (.vox) files.

github

Features

🔍 Parser (MagicaX.VoxParser)

🎨 Generator (MagicaX.VoxGenerator)

Installation

Add magicax to your list of dependencies in mix.exs:

def deps do
  [
    {:magicax, "~> 0.1.0"}
  ]
end

Then run mix deps.get to fetch the dependency.

Quick Start

Library Usage

# Parse a VOX file
{:ok, data} = MagicaX.parse_file("model.vox")
IO.inspect(data.size)           # {32, 32, 32}
IO.inspect(length(data.voxels)) # 1024

# Generate from JSON file
{:ok, message} = MagicaX.generate_from_json_file("model.json")

# Generate basic shapes
{:ok, message} = MagicaX.generate_cube("cube.vox", 10)
{:ok, message} = MagicaX.generate_sphere("sphere.vox", 8)

# Generate programmatically
dimensions = {10, 10, 10}
voxels = [{0, 0, 0, 1}, {1, 1, 1, 2}]
{:ok, message} = MagicaX.generate_vox_file("model.vox", dimensions, voxels)

Standalone Scripts (Development)

For development and testing, you can also run the examples directly using Mix.install:

# Parse VOX files
elixir examples/parser_example.exs path/to/model.vox

# Generate VOX files (includes recursive fractal generation)
elixir examples/generator_example.exs

# Basic usage examples  
elixir examples/basic_usage.exs

The examples use Mix.install([{:magicax, path: "../"}]) to automatically compile and load the library.

JSON Format

Required Fields

{
  "dimensions": [x, y, z],           // MANDATORY: 3D dimensions
  "voxels": [                        // MANDATORY: Array of voxel objects
    {
      "x": 0,                        // MANDATORY: X coordinate (0-255)
      "y": 0,                        // MANDATORY: Y coordinate (0-255)  
      "z": 0,                        // MANDATORY: Z coordinate (0-255)
      "color_index": 1               // MANDATORY: Color index (0-255)
    }
  ]
}

Optional Fields

{
  "palette": [                       // OPTIONAL: Custom color palette
    {
      "r": 255,                      // MANDATORY: Red component (0-255)
      "g": 255,                      // MANDATORY: Green component (0-255)
      "b": 255,                      // MANDATORY: Blue component (0-255)
      "a": 255                       // MANDATORY: Alpha component (0-255)
    }
  ],
  "metadata": {                      // OPTIONAL: File metadata
    "name": "My VOX Model",
    "author": "Creator Name",
    "description": "Model description"
  }
}

Examples

Simple 3x3x3 Cube

{
  "dimensions": [3, 3, 3],
  "voxels": [
    {"x": 0, "y": 0, "z": 0, "color_index": 1},
    {"x": 1, "y": 0, "z": 0, "color_index": 2},
    {"x": 2, "y": 0, "z": 0, "color_index": 3}
  ]
}

Using the Generator

# Generate from JSON string
json_data = """
{
  "dimensions": [5, 5, 5],
  "voxels": [
    {"x": 0, "y": 0, "z": 0, "color_index": 1}
  ]
}
"""

VoxGenerator.generate_from_json("output.vox", json_data)

# Generate from map
model_data = %{
  "dimensions" => [4, 4, 4],
  "voxels" => [
    %{"x" => 0, "y" => 0, "z" => 0, "color_index" => 1}
  ]
}

VoxGenerator.generate_from_map("output.vox", model_data)

# Generate from local JSON file (recommended for most use cases)
VoxGenerator.generate_from_json_file("output.vox", "my_model.json")

Working with Local JSON Files

The easiest way to create VOX files is using local JSON files:

  1. Create your JSON file (e.g., my_model.json)
  2. Run the generator: elixir -e "VoxGenerator.generate_from_json_file("output.vox", "my_model.json")"

This approach is perfect for:

API Reference

Main Module (MagicaX)

The main module provides convenient access to all functionality:

Parser Module (MagicaX.VoxParser)

Generator Module (MagicaX.VoxGenerator)

Project Structure

magicax/
├── lib/
│   ├── magicax.ex           # Main module and public API
│   ├── vox_parser.ex        # VOX file parser
│   └── vox_generator.ex     # VOX file generator
├── examples/
│   ├── json/               # Example JSON models
│   ├── parser_example.exs  # Parser usage examples
│   ├── generator_example.exs # Generator usage examples
│   └── basic_usage.exs     # Basic library usage
├── vox/                    # Sample VOX files
│   ├── teapot.vox
│   ├── castle.vox
│   └── chr_knight.vox
├── mix.exs                 # Project configuration
├── CLAUDE.md               # Development instructions
└── README.md               # This file

Requirements

Performance

Contributing

Feel free to submit issues, feature requests, or pull requests to improve the toolkit!