MagicaVoxel File: PHP parsing library

Application

MagicaVoxel

File extension

vox

KS implementation details

License: MIT

This page hosts a formal specification of MagicaVoxel File using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

PHP source code to parse MagicaVoxel File

Vox.php

<?php
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

class Vox extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Kaitai\Struct\Struct $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_magic = $this->_io->ensureFixedContents("\x56\x4F\x58\x20");
        $this->_m_version = $this->_io->readU4le();
        $this->_m_main = new \Vox\Chunk($this->_io, $this, $this->_root);
    }
    protected $_m_magic;
    protected $_m_version;
    protected $_m_main;
    public function magic() { return $this->_m_magic; }

    /**
     * 150 expected
     */
    public function version() { return $this->_m_version; }
    public function main() { return $this->_m_main; }
}

namespace \Vox;

class Chunk extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Kaitai\Struct\Struct $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_chunkId = $this->_io->readU4be();
        $this->_m_numBytesOfChunkContent = $this->_io->readU4le();
        $this->_m_numBytesOfChildrenChunks = $this->_io->readU4le();
        if ($this->numBytesOfChunkContent() != 0) {
            switch ($this->chunkId()) {
                case \Vox\ChunkType::SIZE:
                    $this->_m__raw_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    $io = new \Kaitai\Struct\Stream($this->_m__raw_chunkContent);
                    $this->_m_chunkContent = new \Vox\Size($io, $this, $this->_root);
                    break;
                case \Vox\ChunkType::MATT:
                    $this->_m__raw_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    $io = new \Kaitai\Struct\Stream($this->_m__raw_chunkContent);
                    $this->_m_chunkContent = new \Vox\Matt($io, $this, $this->_root);
                    break;
                case \Vox\ChunkType::XYZI:
                    $this->_m__raw_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    $io = new \Kaitai\Struct\Stream($this->_m__raw_chunkContent);
                    $this->_m_chunkContent = new \Vox\Xyzi($io, $this, $this->_root);
                    break;
                case \Vox\ChunkType::PACK:
                    $this->_m__raw_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    $io = new \Kaitai\Struct\Stream($this->_m__raw_chunkContent);
                    $this->_m_chunkContent = new \Vox\Pack($io, $this, $this->_root);
                    break;
                case \Vox\ChunkType::RGBA:
                    $this->_m__raw_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    $io = new \Kaitai\Struct\Stream($this->_m__raw_chunkContent);
                    $this->_m_chunkContent = new \Vox\Rgba($io, $this, $this->_root);
                    break;
                default:
                    $this->_m_chunkContent = $this->_io->readBytes($this->numBytesOfChunkContent());
                    break;
            }
        }
        if ($this->numBytesOfChildrenChunks() != 0) {
            $this->_m_childrenChunks = [];
            $i = 0;
            while (!$this->_io->isEof()) {
                $this->_m_childrenChunks[] = new \Vox\Chunk($this->_io, $this, $this->_root);
                $i++;
            }
        }
    }
    protected $_m_chunkId;
    protected $_m_numBytesOfChunkContent;
    protected $_m_numBytesOfChildrenChunks;
    protected $_m_chunkContent;
    protected $_m_childrenChunks;
    protected $_m__raw_chunkContent;
    public function chunkId() { return $this->_m_chunkId; }
    public function numBytesOfChunkContent() { return $this->_m_numBytesOfChunkContent; }
    public function numBytesOfChildrenChunks() { return $this->_m_numBytesOfChildrenChunks; }
    public function chunkContent() { return $this->_m_chunkContent; }
    public function childrenChunks() { return $this->_m_childrenChunks; }
    public function _raw_chunkContent() { return $this->_m__raw_chunkContent; }
}

namespace \Vox;

class Size extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Chunk $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_sizeX = $this->_io->readU4le();
        $this->_m_sizeY = $this->_io->readU4le();
        $this->_m_sizeZ = $this->_io->readU4le();
    }
    protected $_m_sizeX;
    protected $_m_sizeY;
    protected $_m_sizeZ;
    public function sizeX() { return $this->_m_sizeX; }
    public function sizeY() { return $this->_m_sizeY; }
    public function sizeZ() { return $this->_m_sizeZ; }
}

namespace \Vox;

class Rgba extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Chunk $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_colors = [];
        $n = 256;
        for ($i = 0; $i < $n; $i++) {
            $this->_m_colors[] = new \Vox\Color($this->_io, $this, $this->_root);
        }
    }
    protected $_m_colors;
    public function colors() { return $this->_m_colors; }
}

namespace \Vox;

class Pack extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Chunk $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_numModels = $this->_io->readU4le();
    }
    protected $_m_numModels;
    public function numModels() { return $this->_m_numModels; }
}

namespace \Vox;

class Matt extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Chunk $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_id = $this->_io->readU4le();
        $this->_m_materialType = $this->_io->readU4le();
        $this->_m_materialWeight = $this->_io->readF4le();
        $this->_m_propertyBits = $this->_io->readU4le();
        if ($this->hasPlastic()) {
            $this->_m_plastic = $this->_io->readF4le();
        }
        if ($this->hasRoughness()) {
            $this->_m_roughness = $this->_io->readF4le();
        }
        if ($this->hasSpecular()) {
            $this->_m_specular = $this->_io->readF4le();
        }
        if ($this->hasIor()) {
            $this->_m_ior = $this->_io->readF4le();
        }
        if ($this->hasAttenuation()) {
            $this->_m_attenuation = $this->_io->readF4le();
        }
        if ($this->hasPower()) {
            $this->_m_power = $this->_io->readF4le();
        }
        if ($this->hasGlow()) {
            $this->_m_glow = $this->_io->readF4le();
        }
        if ($this->hasIsTotalPower()) {
            $this->_m_isTotalPower = $this->_io->readF4le();
        }
    }
    protected $_m_hasIsTotalPower;
    public function hasIsTotalPower() {
        if ($this->_m_hasIsTotalPower !== null)
            return $this->_m_hasIsTotalPower;
        $this->_m_hasIsTotalPower = ($this->propertyBits() & 128) != 0;
        return $this->_m_hasIsTotalPower;
    }
    protected $_m_hasPlastic;
    public function hasPlastic() {
        if ($this->_m_hasPlastic !== null)
            return $this->_m_hasPlastic;
        $this->_m_hasPlastic = ($this->propertyBits() & 1) != 0;
        return $this->_m_hasPlastic;
    }
    protected $_m_hasAttenuation;
    public function hasAttenuation() {
        if ($this->_m_hasAttenuation !== null)
            return $this->_m_hasAttenuation;
        $this->_m_hasAttenuation = ($this->propertyBits() & 16) != 0;
        return $this->_m_hasAttenuation;
    }
    protected $_m_hasPower;
    public function hasPower() {
        if ($this->_m_hasPower !== null)
            return $this->_m_hasPower;
        $this->_m_hasPower = ($this->propertyBits() & 32) != 0;
        return $this->_m_hasPower;
    }
    protected $_m_hasRoughness;
    public function hasRoughness() {
        if ($this->_m_hasRoughness !== null)
            return $this->_m_hasRoughness;
        $this->_m_hasRoughness = ($this->propertyBits() & 2) != 0;
        return $this->_m_hasRoughness;
    }
    protected $_m_hasSpecular;
    public function hasSpecular() {
        if ($this->_m_hasSpecular !== null)
            return $this->_m_hasSpecular;
        $this->_m_hasSpecular = ($this->propertyBits() & 4) != 0;
        return $this->_m_hasSpecular;
    }
    protected $_m_hasIor;
    public function hasIor() {
        if ($this->_m_hasIor !== null)
            return $this->_m_hasIor;
        $this->_m_hasIor = ($this->propertyBits() & 8) != 0;
        return $this->_m_hasIor;
    }
    protected $_m_hasGlow;
    public function hasGlow() {
        if ($this->_m_hasGlow !== null)
            return $this->_m_hasGlow;
        $this->_m_hasGlow = ($this->propertyBits() & 64) != 0;
        return $this->_m_hasGlow;
    }
    protected $_m_id;
    protected $_m_materialType;
    protected $_m_materialWeight;
    protected $_m_propertyBits;
    protected $_m_plastic;
    protected $_m_roughness;
    protected $_m_specular;
    protected $_m_ior;
    protected $_m_attenuation;
    protected $_m_power;
    protected $_m_glow;
    protected $_m_isTotalPower;
    public function id() { return $this->_m_id; }
    public function materialType() { return $this->_m_materialType; }
    public function materialWeight() { return $this->_m_materialWeight; }
    public function propertyBits() { return $this->_m_propertyBits; }
    public function plastic() { return $this->_m_plastic; }
    public function roughness() { return $this->_m_roughness; }
    public function specular() { return $this->_m_specular; }
    public function ior() { return $this->_m_ior; }
    public function attenuation() { return $this->_m_attenuation; }
    public function power() { return $this->_m_power; }
    public function glow() { return $this->_m_glow; }
    public function isTotalPower() { return $this->_m_isTotalPower; }
}

namespace \Vox;

class Xyzi extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Chunk $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_numVoxels = $this->_io->readU4le();
        $this->_m_voxels = [];
        $n = $this->numVoxels();
        for ($i = 0; $i < $n; $i++) {
            $this->_m_voxels[] = new \Vox\Voxel($this->_io, $this, $this->_root);
        }
    }
    protected $_m_numVoxels;
    protected $_m_voxels;
    public function numVoxels() { return $this->_m_numVoxels; }
    public function voxels() { return $this->_m_voxels; }
}

namespace \Vox;

class Color extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Rgba $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_r = $this->_io->readU1();
        $this->_m_g = $this->_io->readU1();
        $this->_m_b = $this->_io->readU1();
        $this->_m_a = $this->_io->readU1();
    }
    protected $_m_r;
    protected $_m_g;
    protected $_m_b;
    protected $_m_a;
    public function r() { return $this->_m_r; }
    public function g() { return $this->_m_g; }
    public function b() { return $this->_m_b; }
    public function a() { return $this->_m_a; }
}

namespace \Vox;

class Voxel extends \Kaitai\Struct\Struct {
    public function __construct(\Kaitai\Struct\Stream $_io, \Vox\Xyzi $_parent = null, \Vox $_root = null) {
        parent::__construct($_io, $_parent, $_root);
        $this->_read();
    }

    private function _read() {
        $this->_m_x = $this->_io->readU1();
        $this->_m_y = $this->_io->readU1();
        $this->_m_z = $this->_io->readU1();
        $this->_m_colorIndex = $this->_io->readU1();
    }
    protected $_m_x;
    protected $_m_y;
    protected $_m_z;
    protected $_m_colorIndex;
    public function x() { return $this->_m_x; }
    public function y() { return $this->_m_y; }
    public function z() { return $this->_m_z; }
    public function colorIndex() { return $this->_m_colorIndex; }
}

namespace \Vox;

class ChunkType {
    const MAIN = 1296124238;
    const MATT = 1296127060;
    const PACK = 1346454347;
    const RGBA = 1380401729;
    const SIZE = 1397316165;
    const XYZI = 1482250825;
}

namespace \Vox;

class MaterialType {
    const DIFFUSE = 0;
    const METAL = 1;
    const GLASS = 2;
    const EMISSIVE = 3;
}

namespace \Vox;

class PropertyBitsType {
    const PLASTIC = 1;
    const ROUGHNESS = 2;
    const SPECULAR = 4;
    const IOR = 8;
    const ATTENUATION = 16;
    const POWER = 32;
    const GLOW = 64;
    const IS_TOTAL_POWER = 128;
}