VoxFileParser / Palette¶
Every voxel stores a palette index rather than a color, and the Palette holds the 256 RGBA entries those indices point at. When you load a scene the parser exposes its palette through $vox->palette, and you are free to edit it before generating a mesh to recolor the model. The colors are owned by a 256 * 4 entry UByteBuffer under the hood, but the helpers below let you work in the friendlier 0..1 float range.
Usage¶
Recoloring the scene¶
$palette = $vox->palette;
// paint a couple of entries
$palette->setColorf(1, 1.0, 0.5, 0.1); // warm orange
$palette->setColor(2, new \GL\Math\Vec4(0.1, 0.8, 0.3, 1.0));
// then generate a mesh using the edited palette
$model->generateTriangleMesh($vertices, $indices, $palette, 'greedy');
Starting from a color scheme¶
$palette->replaceFromArray([
[1.0, 0.0, 0.0], // index 0, red
[0.0, 1.0, 0.0, 0.5], // index 1, semi-transparent green
]);
$palette->fillDefault(); // reset to the default MagicaVoxel colors
Methods¶
__construct¶
- arguments
-
?\GL\Buffer\UByteBuffer$bufferOptional existing buffer to wrap. When omitted a default palette is created.
getBuffer¶
Retrieve the underlying RGBA byte buffer (256 * 4 entries).
- returns
-
\GL\Buffer\UByteBufferThe 256 * 4 byte RGBA palette buffer.
getColor¶
Fetch a color at the given palette index as a Vec4 object in the 0..1 range.
- arguments
-
int$indexThe palette index to read (0..255).
- returns
-
\GL\Math\Vec4The color at the given index in the 0..1 range.
setColor¶
Assign a color at the given palette index using a Vec4 color.
- arguments
-
int$indexThe palette index to write (0..255).\GL\Math\Vec4$colorThe color in the 0..1 range.
- returns
-
void
setColorf¶
Assign a color at the given palette index using individual float components.
Components are expected in the 0..1 range.
- arguments
-
int$indexThe palette index to write (0..255).float$rThe red component (0..1).float$gThe green component (0..1).float$bThe blue component (0..1).float$aThe alpha component (0..1), defaults to 1.0.
- returns
-
void
replaceFromBuffer¶
Replace the palette contents from another buffer. Accepts RGBA byte or float layouts.
- arguments
-
\GL\Buffer\UByteBuffer|\GL\Buffer\FloatBuffer$bufferA buffer holding RGBA color data.
- returns
-
void
replaceFromArray¶
Replace palette entries from an array of colors. Each color may be [r, g, b] or [r, g, b, a] in 0..1 range.
- arguments
-
array<int,array{0: float, 1: float, 2: float, 3?: float}>$colorsThe colors to write, starting at index 0.
- returns
-
void
fillDefault¶
Reset the palette to the default MagicaVoxel color table.
- returns
-
void