ws-voxel: docs/guide/voxel.md (NBT / SNBT, schematics, regions, Qubicle; tested snippets) + inputs generator; bench lines for NBT, SNBT, packed arrays, schem, qb
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
87c8476302
commit
71435c12a9
6 files changed
+315
-1
No files matched your search
@@ -0,0 +1,145 @@
|
||||
# Minecraft and voxel files
|
||||
|
||||
Minecraft's NBT data, its schematics (`.schem`, `.schematic`, `.litematic`) and region files (`.mca`), and Qubicle
|
||||
`.qb` models. NBT and SNBT load as plain values (the [Data](data.md) category); schematics, regions and Qubicle files
|
||||
load as a `Scene` whose `scene.voxels` holds `Voxels` grids (see [3D scenes](scene.md#voxels)), so they mesh and save
|
||||
like a `.vox`.
|
||||
|
||||
| Format | Read | Write | Value |
|
||||
|---|---|---|---|
|
||||
| NBT (`.nbt`, `.dat`, `.mcstructure`) | yes | yes | typed plain values; Java big-endian, Bedrock little-endian, gzip / zlib / raw |
|
||||
| SNBT (`.snbt`) | yes | yes | the same values as text |
|
||||
| Sponge `.schem` v1 / v2 / v3 | yes | yes | Scene, one grid with block states |
|
||||
| MCEdit / WorldEdit `.schematic` | yes | yes | Scene; pre-1.13 numeric ids |
|
||||
| Litematica `.litematic` | yes | yes | Scene, one grid per region |
|
||||
| Anvil region `.mca` | yes | | Scene, the chosen chunks as one grid, plus each chunk's NBT |
|
||||
| Qubicle `.qb` | yes | yes | Scene, one grid per matrix, exact colours |
|
||||
|
||||
## NBT and SNBT
|
||||
|
||||
NBT values are plain Luau with their tag type kept, so what you load saves back the same:
|
||||
|
||||
| NBT tag | Luau value |
|
||||
|---|---|
|
||||
| int | number |
|
||||
| string | string |
|
||||
| byte array | buffer |
|
||||
| compound | ordered table (`F.Types.object`, keys in file order) |
|
||||
| list | array (its element type: `Nbt.listType(list)`) |
|
||||
| byte, short, long, float, double | a tag `{ type = "byte", value = 1 }` (`tostring` gives SNBT: `1b`); longs past 2^53 hold their exact decimal text |
|
||||
| int array, long array | a tag with `value` = numbers / a buffer of 8-byte little-endian longs |
|
||||
|
||||
```luau
|
||||
local level = assert(F.load(levelDatBytes, { name = "level.dat" })) -- gzip NBT
|
||||
local data = level.Data
|
||||
assert(data.LevelName == "My World" and data.SpawnY == 64) -- ints and strings are plain
|
||||
assert(data.Difficulty.type == "byte" and data.Difficulty.value == 2)
|
||||
assert(data.RandomSeed.value == "-4011880356862312345") -- past 2^53: exact text
|
||||
assert(tostring(data.Player.Health) == "20.0f")
|
||||
|
||||
local Nbt = F.format("nbt").Nbt -- the engine: codecs/text/Nbt
|
||||
data.Difficulty = Nbt.byte(3)
|
||||
data.Tags = Nbt.list("string", { "a", "b" })
|
||||
local saved = assert(F.save(level, nil, { format = "nbt" })) -- gzip again, like the source
|
||||
assert(F.load(saved, { format = "nbt" }).Data.Difficulty.value == 3)
|
||||
local info = F.format("nbt").info(level) -- how it was stored
|
||||
assert(info.compression == "gzip" and info.endian == "big" and info.name == "")
|
||||
```
|
||||
|
||||
Plain inputs work too: a whole number writes as an int, other numbers as a double, `true` / `false` as bytes, a table
|
||||
with string keys as a compound and an array as a list. Bedrock files (little-endian, with or without the `level.dat`
|
||||
header) are detected; `endian = "little"`, `varint = true` (network NBT) and `compression` force a layout when saving.
|
||||
|
||||
```luau
|
||||
local text = assert(F.convert(levelDatBytes, "snbt", { format = "nbt" })) -- NBT -> SNBT text
|
||||
assert(string.find(buffer.tostring(text), 'LevelName:"My World"', 1, true))
|
||||
local v = assert(F.load("{pos:[I;1,2,3],name:'x',scale:1.5f,big:9007199254740993L}", { format = "snbt" }))
|
||||
assert(v.pos.value[2] == 2 and v.scale.type == "float" and v.big.value == "9007199254740993")
|
||||
assert(F.saveString(v, { format = "snbt" }) == '{pos:[I;1,2,3],name:"x",scale:1.5f,big:9007199254740993L}')
|
||||
local pretty = assert(F.saveString(v, { format = "snbt", indent = 2 }))
|
||||
local bedrock = assert(F.save(v, nil, { format = "nbt", endian = "little", compression = "none" }))
|
||||
```
|
||||
|
||||
## Schematics
|
||||
|
||||
A schematic loads as a Scene with `meta.up = "y"` and one unit per block. Each grid has its usual colour view
|
||||
(`data` / `palette`, from a built-in table of block colours) and `voxels.blocks` with the exact blocks:
|
||||
|
||||
| `voxels.blocks` field | Meaning |
|
||||
|---|---|
|
||||
| `palette` | block states (`"minecraft:oak_log[axis=y]"`), entry 1 is index 0 |
|
||||
| `data` | a buffer, one u16 per cell (same cell order as `voxels.data`): the 0-based palette index |
|
||||
| `blockEntities` | `{ { pos = { x, y, z }, id, data } }`: chests, signs, ... (`data` is their NBT) |
|
||||
| `entities` | entity NBT records |
|
||||
|
||||
```luau
|
||||
local scene = assert(F.load(schemBytes)) -- detected by content: F.detect gives "schem"
|
||||
local grid = scene.voxels[1]
|
||||
assert(grid.size[1] == 5 and grid.size[2] == 4) -- x, y (up), z
|
||||
local blocks = grid.blocks
|
||||
local function blockAt(x: number, y: number, z: number): string
|
||||
local i = x + grid.size[1] * (y + grid.size[2] * z)
|
||||
return blocks.palette[buffer.readu16(blocks.data, i * 2) + 1]
|
||||
end
|
||||
assert(blockAt(0, 0, 0) == "minecraft:stone" and blockAt(2, 2, 0) == "minecraft:glass")
|
||||
assert(F.Types.Voxels.get(grid, 1, 1, 1) == 0) -- air is empty in the colour view
|
||||
|
||||
local lite = assert(F.load(litematicBytes))
|
||||
assert(#lite.voxels == 2 and lite.nodes[2].name == "pillar") -- a node per region, placed by translation
|
||||
local legacy = assert(F.save(lite, nil, { format = "schematic", variant = "schematic" })) -- regions merged
|
||||
local schem = assert(F.save(lite, nil, { format = "schem" })) -- Sponge v3
|
||||
assert(F.detect(schem) == "schem")
|
||||
```
|
||||
|
||||
`F.save` picks the flavour from `variant`, the destination's extension (`"out.litematic"`), `format = "schem"` /
|
||||
`"litematic"`, or the loaded file's. A grid without `blocks` (a `.vox`, a `.qb`) gets the block nearest in colour for
|
||||
each palette colour, so converting voxel art works:
|
||||
|
||||
```luau
|
||||
local vox = assert(F.load(voxBytes))
|
||||
local schem = assert(F.save(vox, nil, { format = "schem" }))
|
||||
local back = assert(F.load(schem))
|
||||
assert(back.voxels[1].blocks.palette[1] == "minecraft:air")
|
||||
```
|
||||
|
||||
## Region files
|
||||
|
||||
An Anvil region holds 32 x 32 chunks. Loading it builds one grid over the chunks you pick (`chunks = { { x, z } }`,
|
||||
local 0..31), or the blocks inside `area = { minX, minY, minZ, maxX, maxY, maxZ }` (world coordinates), between the
|
||||
lowest and highest sections holding blocks; `maxCells` (default 2^25) guards against loading a whole world.
|
||||
|
||||
```luau
|
||||
local Mca = F.format("mca")
|
||||
local region = assert(F.load(regionBytes, { name = "r.0.0.mca", chunks = { { 0, 0 }, { 1, 0 } } }))
|
||||
local grid = region.voxels[1]
|
||||
assert(grid.size[1] == 32 and grid.size[3] == 16)
|
||||
local corner = region.nodes[1].translation -- the grid's world position
|
||||
assert(corner[1] == 0 and corner[2] == -64)
|
||||
local chunk = region.meta.source.chunks[1]
|
||||
assert(chunk.dataVersion == 3953 and chunk.nbt.Status == "minecraft:full") -- the chunk's whole NBT
|
||||
|
||||
local list = assert(Mca.list(F.Bytes.reader(regionBytes))) -- the chunks present, with timestamps
|
||||
local nbt = assert(Mca.chunk(F.Bytes.reader(regionBytes), 3, 2))
|
||||
assert(nbt.xPos == 3)
|
||||
```
|
||||
|
||||
All chunk layouts read: 1.18+ sections, 1.13 - 1.17 palettes and pre-1.13 numeric ids (as state names such as
|
||||
`minecraft:red_wool`, with `[data=N]` for the rest of a data value). Chunk payloads may be gzip, zlib, LZ4 or stored;
|
||||
chunks in external `.mcc` files load through `opts.resolve` (filled in when loading by path). Regions are read-only.
|
||||
|
||||
## Qubicle
|
||||
|
||||
Each matrix becomes a grid with `voxels.colors`, the exact colour of every cell (`r, g, b, a` bytes; `a = 0` is
|
||||
empty), next to the indexed `data` / `palette` view. Left-handed files are mirrored into the scene's right-handed
|
||||
axes; saving writes the source's settings unless `compressed`, `colorFormat` (`"rgba"` / `"bgra"`), `rightHanded` or
|
||||
`visibilityMask` say otherwise.
|
||||
|
||||
```luau
|
||||
local box = assert(F.load(qbBytes))
|
||||
local grid = box.voxels[1]
|
||||
local first = buffer.readu32(grid.colors, 0) -- r + g * 256 + b * 65536 + a * 2^24
|
||||
assert(first % 256 == 200 and first // 16777216 == 255)
|
||||
assert(box.nodes[1].name == "box")
|
||||
local again = assert(F.save(box, nil, { format = "qb", compressed = false }))
|
||||
local vox = assert(F.save(box, nil, { format = "vox" })) -- or .schem, .glb after F.ops.mesh.meshVoxels
|
||||
```
|
||||
Reference in new issue
Block a user