voxolith

writeVox()

Serialize one model to a MagicaVoxel ".vox" ArrayBuffer (SIZE + XYZI + RGBA). voxels are [x, y, z, colorIndex] with a 1-based colour index; colorFor maps a 1-based index (1..255) to an RGB triple or null (transparent/unused). Pure (no fs) — callers write the returned bytes to disk themselves....

function writeVox(
   size: object, 
   voxels: readonly [number, number, number, number][], 
   colorFor: (index1: number) => [number, number, number] | null
): ArrayBuffer;

Defined in: vox.ts:494

Serialize one model to a MagicaVoxel ".vox" ArrayBuffer (SIZE + XYZI + RGBA). voxels are [x, y, z, colorIndex] with a 1-based colour index; colorFor maps a 1-based index (1..255) to an RGB triple or null (transparent/unused). Pure (no fs) — callers write the returned bytes to disk themselves. Coordinates are written as bytes, so every axis must fit in 0..255.

Parameters

ParameterTypeDescription
size{ x: number; y: number; z: number; }Model extent, Z-up as MagicaVoxel expects.
size.xnumber-
size.ynumber-
size.znumber-
voxelsreadonly [number, number, number, number][]Solid voxels only; there is no empty value to write.
colorFor(index1: number) => [number, number, number] | nullRGB 0..255 for each palette index that is used.

Returns

ArrayBuffer

The file's bytes.

Example

import { writeVox } from "@voxolith/renderer/vox";

// A 2×2×1 slab in palette index 1 (red).
const bytes = writeVox(
  { x: 2, y: 2, z: 1 },
  [[0, 0, 0, 1], [1, 0, 0, 1], [0, 1, 0, 1], [1, 1, 0, 1]],
  (i) => (i === 1 ? [220, 40, 40] : null),
);
await Bun.write("slab.vox", bytes);

On this page