Files
rosetta/tests/model/vox.luau
T

290 lines
12 KiB
Luau

-- model/vox tests: MagicaVoxel files (ephtracy samples, opengametools scene files saved by MagicaVoxel 0.99.x) and
-- synthetic files with every chunk type, against an independent reader (tools/fixtures/vox.py): grids (CRC), palette,
-- the default palette (parsed from the spec text), scene graph world transforms (rotations, mirrors, nesting), layers,
-- materials, notes, cameras, render objects; greedy meshes (MeshOps) against the exposed face count; round trips
-- through the writer; error codes; the fuzz no-throw pass. Fixtures: tools/fixtures/vox.py.
local T = require("@tests/T")
local Vox = require("@formats/model/vox")
local MeshOps = require("@formats/ops/MeshOps")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Cases = require("@tests/gen/vox_cases")
local DefaultPalette = require("@tests/gen/vox_default_palette")
local t = T.suite("vox")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Vox.decode(Bytes.reader(b), opts)
end
-- the contract
t:eq(Vox.name, "vox", "name")
t:eq(Vox.category, "model", "category")
t:check(Vox.extensions[1] == "vox" and Vox.caps.decode and Vox.caps.encode, "contract")
-- the default palette = the spec's array
do
t:eq(#Vox.DEFAULT_PALETTE, 255, "default palette size")
local same = true
for i = 1, 255 do
for k = 1, 4 do
if Vox.DEFAULT_PALETTE[i][k] ~= DefaultPalette[i][k] then
same = false
end
end
end
t:check(same, "default palette equals the spec's")
end
local function paletteCrc(pal: { { number } }): number
local b = buffer.create(255 * 4)
for i = 1, 255 do
for k = 1, 4 do
buffer.writeu8(b, (i - 1) * 4 + k - 1, pal[i][k])
end
end
return Crc32.of(b)
end
local function countKeys(tbl: any): number
local n = 0
for _ in tbl or {} do
n += 1
end
return n
end
-- shape nodes depth first with world matrices (column-major 4x4)
local function shapes(scene: any): { any }
local out = {}
local function walk(i: number, parent: { number })
local node = scene.nodes[i]
local world = Types.Scene.mul(parent, Types.Scene.nodeMatrix(node))
if node.voxels then
table.insert(out, { name = node.name or "", model = node.voxels, world = world })
end
for _, c in node.children do
walk(c, world)
end
end
for _, r in scene.roots do
walk(r, Types.Scene.IDENTITY)
end
return out
end
local function checkScene(scene: any, c: any, label: string)
t:eq(scene.kind, "scene", label .. ": a Scene")
t:eq(scene.meta.up, "z", label .. ": z up")
t:eq(#scene.voxels, #c.models, label .. ": models")
for i, m in c.models do
local v = scene.voxels[i]
t:check(v.size[1] == m.size[1] and v.size[2] == m.size[2] and v.size[3] == m.size[3], label .. ": model " .. i .. " size")
t:eq(Crc32.of(v.data), m.crc, label .. ": model " .. i .. " grid")
end
t:eq(paletteCrc(scene.voxels[1].palette), c.paletteCrc, label .. ": palette")
local vm = scene.meta.vox
t:eq(#(vm.layers or {}), c.layers, label .. ": layers")
t:eq(countKeys(scene.voxels[1].materials), c.materials, label .. ": materials")
t:eq(#(vm.notes or {}), c.notes, label .. ": notes")
t:eq(#(vm.cameras or {}), c.cameras, label .. ": cameras")
t:eq(#(vm.renderObjects or {}), c.robjs, label .. ": render objects")
local glass = 0
for _, m in scene.voxels[1].materials or {} do
if m.type == "glass" then
glass += 1
end
end
t:eq(glass, c.glassMaterials, label .. ": glass materials")
if #c.shapes > 0 then
local got = shapes(scene)
t:eq(#got, #c.shapes, label .. ": shape nodes")
for k, want in c.shapes do
local g = got[k]
t:eq(g.name, want.name, label .. ": shape " .. k .. " name")
t:eq(g.model, want.model, label .. ": shape " .. k .. " model")
local ok = true
for row = 0, 2 do
for col = 0, 3 do
local w = want.world[row * 4 + col + 1]
local v = g.world[col * 4 + row + 1]
if math.abs(w - v) > 1e-6 then
ok = false
end
end
end
t:check(ok, label .. ": shape " .. k .. " (" .. want.name .. ") world transform")
end
end
end
-- area and winding of a greedy mesh
local function meshFacts(prim: any): (number, boolean, number)
local pos, nrm = prim.attributes.POSITION.data, prim.attributes.NORMAL.data
local ind = prim.indices.data
local area, wound = 0, true
for tri = 0, prim.indices.count // 3 - 1 do
local a, b, c = buffer.readu32(ind, tri * 12), buffer.readu32(ind, tri * 12 + 4), buffer.readu32(ind, tri * 12 + 8)
local function p(i: number, k: number): number
return buffer.readf32(pos, i * 12 + k * 4)
end
local e1 = { p(b, 0) - p(a, 0), p(b, 1) - p(a, 1), p(b, 2) - p(a, 2) }
local e2 = { p(c, 0) - p(a, 0), p(c, 1) - p(a, 1), p(c, 2) - p(a, 2) }
local nx, ny, nz = e1[2] * e2[3] - e1[3] * e2[2], e1[3] * e2[1] - e1[1] * e2[3], e1[1] * e2[2] - e1[2] * e2[1]
area += math.sqrt(nx * nx + ny * ny + nz * nz) / 2
local dot = nx * buffer.readf32(nrm, a * 12) + ny * buffer.readf32(nrm, a * 12 + 4) + nz * buffer.readf32(nrm, a * 12 + 8)
if dot <= 0 then
wound = false
end
end
return area, wound, prim.indices.count // 3
end
local totalQuads, totalFaces = 0, 0
for _, c in Cases do
local label = c.name
local scene, err = decode(c.data)
t:check(scene, label .. ": decode: " .. tostring(err))
checkScene(scene, c, label)
t:eq(scene.meta.source.version, c.version, label .. ": version")
-- decodeVoxels, and from a sub-range of a bigger buffer
local big = buffer.create(#c.data + 9)
buffer.writestring(big, 5, c.data)
local grids = Vox.decodeVoxels(Bytes.reader(big, 5, #c.data))
t:check(grids and #grids == #c.models and Crc32.of(grids[1].data) == c.models[1].crc, label .. ": decodeVoxels from a sub-range")
-- greedy meshing: the area is the number of exposed unit faces, every triangle faces its normal
for i, m in c.models do
local prim, perr = MeshOps.voxelsToPrimitive(scene.voxels[i], { pivot = "corner" })
t:check(prim, label .. ": mesh: " .. tostring(perr))
local area, wound, tris = meshFacts(prim)
t:eq(area, m.faces, label .. ": model " .. i .. " greedy mesh area = exposed faces")
t:check(wound, label .. ": model " .. i .. " winding matches the normals")
t:check(tris <= m.faces * 2, label .. ": model " .. i .. " no more triangles than faces")
totalQuads += tris // 2
totalFaces += m.faces
end
-- round trip through the writer
local out, eerr = Vox.encode(scene)
t:check(out, label .. ": encode: " .. tostring(eerr))
local back, berr = Vox.decode(Bytes.reader(out :: buffer))
t:check(back, label .. ": decode our file: " .. tostring(berr))
checkScene(back, c, label .. " (round trip)")
t:eq(back.meta.source.version, 200, label .. ": written as version 200")
local out2 = Vox.encode(back) :: buffer
t:check(buffer.tostring(out2) == buffer.tostring(out :: buffer), label .. ": writing is stable")
if scene.meta.vox.info then
t:check(back.meta.vox.info and back.meta.vox.info._anim_range == scene.meta.vox.info._anim_range, label .. ": META")
end
end
t:note("%d files: grids, palettes, scene graphs and round trips match; greedy: %d quads for %d faces", #Cases, totalQuads, totalFaces)
-- the synthetic scene in detail: names, hidden, layers, animation frames, materials, notes, IMAP, unknown chunks
do
local c
for _, x in Cases do
if x.name == "syn-scene" then
c = x
end
end
local scene = decode(c.data)
local byName = {}
for _, n in scene.nodes do
if n.name then
byName[n.name] = n
end
end
t:check(byName.group and byName.group.extras.vox.attributes._hidden == "1" and byName.group.extras.vox.layer == 1, "hidden group on layer 1")
t:check(byName.b and byName.b.matrix ~= nil, "a mirrored node keeps a matrix")
t:check(byName.a and byName.a.matrix == nil and byName.a.rotation == nil and byName.a.translation[3] == 3, "identity rotation: translation only")
t:check(byName.anim and #byName.anim.extras.vox.frames == 2 and #byName.anim.extras.vox.shape.models == 2, "animation frames and shape models")
local mats = scene.voxels[1].materials
t:check(mats[1].type == "glass" and mats[2].type == "emit" and mats[1].rough == 0.25 and mats[1].ior == 1.5 and mats[200].type == "emit", "materials")
t:check(scene.meta.vox.notes[2] == "second" and scene.meta.vox.indexMap[256] == 255, "notes, IMAP")
t:check(scene.meta.vox.layers[2].name == "one" and scene.meta.vox.layers[2].hidden, "layers")
t:check(scene.meta.vox.cameras[1].attributes._fov == "45", "rCAM")
t:check(scene.meta.vox.chunks and scene.meta.vox.chunks[1].id == "ZZZZ", "unknown chunk kept")
local back = Vox.decode(Bytes.reader(Vox.encode(scene) :: buffer)) :: any
local bn = {}
for _, n in back.nodes do
if n.name then
bn[n.name] = n
end
end
t:check(bn.anim and #bn.anim.extras.vox.frames == 2 and bn.anim.extras.vox.frames[2]._t == "4 4 4", "frames round trip")
t:check(back.meta.vox.chunks and buffer.tostring(back.meta.vox.chunks[1].content) == "unknown payload", "unknown chunk round trip")
t:check(back.voxels[1].materials[200].flux == 2, "material round trip")
-- meshVoxels: every shape node gets the mesh of its grid
MeshOps.meshVoxels(back)
local meshed = 0
for _, n in back.nodes do
if n.voxels then
t:check(n.mesh ~= nil and back.meshes[n.mesh].primitives[1].attributes.COLOR_0 ~= nil, "meshVoxels sets node.mesh")
meshed += 1
end
end
t:check(meshed == 4 and #back.meshes == 4, "four shapes meshed")
local lo, hi = MeshOps.bounds(back)
t:check(lo and hi and hi[1] > lo[1], "scene bounds of the meshed shapes")
end
-- a Voxels grid alone; the centred pivot; sRGB colours
do
local v = Types.Voxels.new(3, 4, 5, Vox.DEFAULT_PALETTE)
Types.Voxels.set(v, 0, 0, 0, 1)
Types.Voxels.set(v, 2, 3, 4, 200)
local out, err = Vox.encode(v)
t:check(out, "encode a Voxels grid: " .. tostring(err))
local back = Vox.decode(Bytes.reader(out :: buffer)) :: any
t:check(back and #back.voxels == 1 and Types.Voxels.get(back.voxels[1], 2, 3, 4) == 200 and Types.Voxels.get(back.voxels[1], 1, 1, 1) == 0, "Voxels round trip")
local prim = MeshOps.voxelsToPrimitive(v, { colors = "srgb", indexAttribute = true }) :: any
local lo, hi = MeshOps.bounds(prim)
t:check(lo and hi and lo[1] == -1 and lo[2] == -2 and lo[3] == -2 and hi[1] == 2 and hi[2] == 2 and hi[3] == 3, "centre pivot")
t:check(prim.attributes.COLOR_0.type == "unorm8" and prim.attributes._PALETTE_INDEX.type == "u8", "srgb colours, index attribute")
t:eq(prim.indices.count, 72, "two separate cubes: 12 quads")
end
-- errors
do
local _, e = decode("nope")
t:check(e and e.code == Err.CORRUPT, "junk: " .. tostring(e))
local c = Cases[1]
local _, e2 = decode(c.data, { maxCells = 10 })
t:check(e2 and e2.code == Err.LIMIT, "maxCells: " .. tostring(e2))
local _, e3 = decode(string.sub(c.data, 1, 30))
t:check(e3 ~= nil, "truncated: " .. tostring(e3))
local _, e4 = Vox.encode({} :: any)
t:check(e4 and e4.code == Err.ARG, "encode junk")
local big = Types.Voxels.new(300, 1, 1)
local _, e5 = Vox.encode(big)
t:check(e5 and e5.code == Err.ARG, "over 256 cells: " .. tostring(e5))
local s = Vox.decode(Bytes.reader(buffer.fromstring(Cases[1].data))) :: any
s.nodes[s.roots[1]].rotation = { 0, 0, math.sin(0.3), math.cos(0.3) }
local _, e6 = Vox.encode(s)
t:check(e6 and e6.code == Err.ARG, "a 34 degree rotation: " .. tostring(e6))
local _, e7 = MeshOps.voxelsToPrimitive({} :: any)
t:check(e7 and e7.code == Err.ARG, "voxelsToPrimitive of junk")
end
-- fuzz
do
local samples = {}
for _, c in Cases do
if #c.data < 100000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
local s = Vox.decode(Bytes.reader(b))
if s then
MeshOps.meshVoxels(s)
Vox.encode(s)
end
return s
end, samples, { mutations = 30, keepHeader = 8 })
end
t:done()