VoxFileParser / Model¶
A Model holds the voxel data for one grid inside a MagicaVoxel scene: its dimensions and a palette index for every filled cell. A model has no position in the world, an Instance is what places it. The most common thing you will do with a model is turn it into a renderable triangle mesh with generateTriangleMesh.
Usage¶
Generating a mesh¶
use GL\Buffer\FloatBuffer;
use GL\Buffer\UIntBuffer;
$vertices = new FloatBuffer();
$indices = new UIntBuffer();
$model->generateTriangleMesh($vertices, $indices, null, 'greedy');
By default every vertex is 9 floats: position (3), normal (3), and an RGB color (3) baked from the palette. See the MagicaVoxel Files guide for the full list of $options that reshape this layout.
Reading a single voxel¶
Constants¶
The meshing modes are also available as integer constants. You may pass them through the mode key of the $options array. The positional $mode argument of generateTriangleMesh expects the equivalent string ('simple', 'greedy', 'polygon').
public const MODE_SIMPLE = 0; // 'simple' - one quad per exposed voxel face
public const MODE_GREEDY = 1; // 'greedy' - merge coplanar same-color faces
public const MODE_POLYGON = 2; // 'polygon' - compact polygonal surfaces
Properties¶
$index¶
The sequential index of this model inside the scene.
$sizeX, $sizeY, $sizeZ¶
The dimensions of the voxel grid.
$voxelCount¶
The number of filled voxels in the grid.
$voxelHash¶
A hash identifying the voxel contents, useful for spotting models that share identical geometry.
$voxelData¶
The raw palette indices for the grid, exposed as a byte buffer.
Methods¶
generateTriangleMesh¶
Generate a triangle mesh for this voxel grid.
function generateTriangleMesh(\GL\Buffer\FloatBuffer $vertices, \GL\Buffer\UIntBuffer $indices, ?\GL\Geometry\VoxFileParser\Palette $palette = null, string $mode = 'simple', ?array $options = null) : bool
- arguments
-
\GL\Buffer\FloatBuffer$verticesDestination buffer for vertex attributes (position, normal, colors).\GL\Buffer\UIntBuffer$indicesDestination buffer for triangle indices.\GL\Geometry\VoxFileParser\Palette|null$paletteOptional palette override or editor instance.string$modeMesh generation mode name (simple, greedy, polygon).array{colors?: "rgb"|"rgba"|"none", includePaletteIndex?: bool, origin?: "corner"|"center", originOffset?: array<int,float>, stats?: array{vertexCount: int, indexCount: int}|null}|null$optionsAdditional mesh generation controls. When provided, this array receives astatsentry on success.
- returns
-
boolTrue on success, false otherwise.
getVoxel¶
Retrieve the palette index stored at the given voxel coordinate.
- arguments
-
int$xThe voxel x coordinate.int$yThe voxel y coordinate.int$zThe voxel z coordinate.
- returns
-
int|nullThe palette index at the coordinate, or null when the cell is empty or out of bounds.