VoxFileParser¶
The Vox File Parser reads MagicaVoxel .vox scenes into your PHP runtime. It ships with PHP-GLFW and is built on top of the opengametools voxel library written in C. A single call loads the whole scene: the voxel models, the instances that place them in the world, the layers and groups from the editor, and the color palette.
If you are new to the voxel workflow, the MagicaVoxel Files guide walks through loading, meshing, and recoloring a scene end to end.
Usage¶
Loading a scene¶
use GL\Geometry\VoxFileParser;
$vox = new VoxFileParser(__DIR__ . '/house.vox');
printf("Loaded %d models\n", $vox->modelCount);
printf("Placed %d instances\n", $vox->instanceCount);
Walking the scene¶
A Model is voxel grid data with no position of its own, while an Instance places a model in the world through its modelIndex and transform. To render a scene you typically loop over the instances and resolve the model each one refers to.
foreach ($vox->instances as $instance) {
$model = $vox->models[$instance->modelIndex] ?? null;
if (!$model) {
continue;
}
// generate a mesh, apply $instance->transform, draw ...
}
Properties¶
$models¶
An array of all voxel Model objects contained in the scene.
$instances¶
An array of Instance objects, each placing a model into the scene.
$layers¶
An array of Layer objects describing the editor layers.
$groups¶
An array of Group objects describing the group hierarchy.
$palette¶
The scene Palette, holding the 256 RGBA colors every voxel refers to.
/*
* @var \GL\Geometry\VoxFileParser\Palette|null
*/
public readonly ?\GL\Geometry\VoxFileParser\Palette $palette;
$modelCount, $instanceCount, $layerCount, $groupCount¶
Convenience counts mirroring the size of the arrays above.
public readonly int $modelCount;
public readonly int $instanceCount;
public readonly int $layerCount;
public readonly int $groupCount;
Methods¶
__construct¶
Parse a MagicaVoxel scene from a file on disk.
- arguments
-
string$fileThe path to the .vox file to load.
getModel¶
Retrieve a single voxel model by its index.
- arguments
-
int$modelIndexThe index of the model inside the scene.
- returns
-
\VoxFileParser\Model|nullThe model, or null when the index is out of range.
getPaletteColor¶
Look up a color from the scene palette by its index.
- arguments
-
int$colorIndexThe palette index to read (0..255).
- returns
-
\GL\Math\Vec4|nullThe color in the 0..1 range, or null when the index is out of range.