ws-3d: model/vox (MagicaVoxel read/write: every chunk, scene graph, materials, layers, render objects, cameras, notes, IMAP, META, unknown chunks kept), ops/MeshOps (triangulate, transform, normals, tangents, weld/unweld, bounds, merge, scale/setUnit/setUp, greedy voxel meshing); Types: Scene.reader/mul/nodeMatrix/flatten helpers; compat: __iter visits t[0] on LuaJIT (was skipped by ipairs)
This commit is contained in:
1 parent
a55b58b102
commit
b37c61a96c
22 files changed
+3269
-1
No files matched your search
@@ -212,6 +212,12 @@ do
|
||||
count += 1
|
||||
end
|
||||
t:eq(count, 3, "for over a mixed table visits every key")
|
||||
local zero = { [0] = 5, 6, 7 }
|
||||
local zsum = 0
|
||||
for _, v in zero do
|
||||
zsum += v
|
||||
end
|
||||
t:eq(zsum, 18, "for over a table with key 0 visits it (LuaJIT keeps t[0] in the array part)")
|
||||
local w = {}
|
||||
for word in string.gmatch("a b c", "%a") do
|
||||
table.insert(w, word)
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
* there can be multiple SIZE and XYZI chunks for multiple models; model id is their index in the stored order
|
||||
* the palette chunk is always stored into the file, so default palette is not needed any more
|
||||
* the MATT chunk is deprecated, replaced by the MATL chunk, see (4)
|
||||
* (a), (b), (c) are special data types; (d) is the scene graph in the world editor
|
||||
|
||||
=================================
|
||||
(a) STRING type
|
||||
|
||||
int32 : buffer size (in bytes)
|
||||
int8xN : buffer (without the ending "\0")
|
||||
|
||||
=================================
|
||||
(b) DICT type
|
||||
|
||||
int32 : num of key-value pairs
|
||||
|
||||
// for each key-value pair
|
||||
{
|
||||
STRING : key
|
||||
STRING : value
|
||||
}xN
|
||||
|
||||
=================================
|
||||
(c) ROTATION type
|
||||
|
||||
store a row-major rotation in the bits of a byte
|
||||
|
||||
for example :
|
||||
R =
|
||||
0 1 0
|
||||
0 0 -1
|
||||
-1 0 0
|
||||
==>
|
||||
unsigned char _r = (1 << 0) | (2 << 2) | (0 << 4) | (1 << 5) | (1 << 6)
|
||||
|
||||
bit | value
|
||||
0-1 : 1 : index of the non-zero entry in the first row
|
||||
2-3 : 2 : index of the non-zero entry in the second row
|
||||
4 : 0 : the sign in the first row (0 : positive; 1 : negative)
|
||||
5 : 1 : the sign in the second row (0 : positive; 1 : negative)
|
||||
6 : 1 : the sign in the third row (0 : positive; 1 : negative)
|
||||
|
||||
=================================
|
||||
(d) Scene Graph
|
||||
|
||||
T : Transform Node
|
||||
G : Group Node
|
||||
S : Shape Node
|
||||
|
||||
T
|
||||
|
|
||||
G
|
||||
/ \
|
||||
T T
|
||||
| |
|
||||
G S
|
||||
/ \
|
||||
T T
|
||||
| |
|
||||
S S
|
||||
|
||||
=================================
|
||||
(1) Transform Node Chunk : "nTRN"
|
||||
|
||||
int32 : node id
|
||||
DICT : node attributes
|
||||
(_name : string)
|
||||
(_hidden : 0/1)
|
||||
int32 : child node id
|
||||
int32 : reserved id (must be -1)
|
||||
int32 : layer id
|
||||
int32 : num of frames (must be greater than 0)
|
||||
|
||||
// for each frame
|
||||
{
|
||||
DICT : frame attributes
|
||||
(_r : int8) ROTATION, see (c)
|
||||
(_t : int32x3) translation
|
||||
(_f : int32) frame index, start from 0
|
||||
}xN
|
||||
|
||||
=================================
|
||||
(2) Group Node Chunk : "nGRP"
|
||||
|
||||
int32 : node id
|
||||
DICT : node attributes
|
||||
int32 : num of children nodes
|
||||
|
||||
// for each child
|
||||
{
|
||||
int32 : child node id
|
||||
}xN
|
||||
|
||||
=================================
|
||||
(3) Shape Node Chunk : "nSHP"
|
||||
|
||||
int32 : node id
|
||||
DICT : node attributes
|
||||
int32 : num of models (must be greater than 0)
|
||||
|
||||
// for each model
|
||||
{
|
||||
int32 : model id
|
||||
DICT : model attributes : reserved
|
||||
(_f : int32) frame index, start from 0
|
||||
}xN
|
||||
|
||||
=================================
|
||||
(4) Material Chunk : "MATL"
|
||||
|
||||
int32 : material id
|
||||
DICT : material properties
|
||||
(_type : str) _diffuse, _metal, _glass, _emit
|
||||
(_weight : float) range 0 ~ 1
|
||||
(_rough : float)
|
||||
(_spec : float)
|
||||
(_ior : float)
|
||||
(_att : float)
|
||||
(_flux : float)
|
||||
(_plastic)
|
||||
|
||||
=================================
|
||||
(5) Layer Chunk : "LAYR"
|
||||
|
||||
int32 : layer id
|
||||
DICT : layer attribute
|
||||
(_name : string)
|
||||
(_hidden : 0/1)
|
||||
int32 : reserved id, must be -1
|
||||
|
||||
=================================
|
||||
(6) Render Objects Chunk : "rOBJ"
|
||||
|
||||
DICT : rendering attributes
|
||||
|
||||
=================================
|
||||
(7) Render Camera Chunk : "rCAM"
|
||||
|
||||
int32 : camera id
|
||||
DICT : camera attribute
|
||||
(_mode : string)
|
||||
(_focus : vec(3))
|
||||
(_angle : vec(3))
|
||||
(_radius : int)
|
||||
(_frustum : float)
|
||||
(_fov : int)
|
||||
|
||||
=================================
|
||||
(8) Palette Note Chunk : "NOTE"
|
||||
|
||||
int32 : num of color names
|
||||
|
||||
// for each name
|
||||
{
|
||||
STRING : color name
|
||||
}xN
|
||||
|
||||
=================================
|
||||
(9) Index MAP Chunk : "IMAP"
|
||||
|
||||
size : 256
|
||||
// for each index
|
||||
{
|
||||
int32 : palette index association
|
||||
}x256
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
MagicaVoxel .vox File Format [10/18/2016]
|
||||
|
||||
1. File Structure : RIFF style
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
1x4 | char | id 'VOX ' : 'V' 'O' 'X' 'space', 'V' is first
|
||||
4 | int | version number : 150
|
||||
|
||||
Chunk 'MAIN'
|
||||
{
|
||||
// pack of models
|
||||
Chunk 'PACK' : optional
|
||||
|
||||
// models
|
||||
Chunk 'SIZE'
|
||||
Chunk 'XYZI'
|
||||
|
||||
Chunk 'SIZE'
|
||||
Chunk 'XYZI'
|
||||
|
||||
...
|
||||
|
||||
Chunk 'SIZE'
|
||||
Chunk 'XYZI'
|
||||
|
||||
// palette
|
||||
Chunk 'RGBA' : optional
|
||||
}
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
2. Chunk Structure
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
1x4 | char | chunk id
|
||||
4 | int | num bytes of chunk content (N)
|
||||
4 | int | num bytes of children chunks (M)
|
||||
|
||||
N | | chunk content
|
||||
|
||||
M | | children chunks
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
3. Chunk id 'MAIN' : the root chunk and parent chunk of all the other chunks
|
||||
|
||||
|
||||
4. Chunk id 'PACK' : if it is absent, only one model in the file; only used for the animation in 0.98.2
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
4 | int | numModels : num of SIZE and XYZI chunks
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
5. Chunk id 'SIZE' : model size
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
4 | int | size x
|
||||
4 | int | size y
|
||||
4 | int | size z : gravity direction
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
6. Chunk id 'XYZI' : model voxels, paired with the SIZE chunk
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
4 | int | numVoxels (N)
|
||||
4 x N | int | (x, y, z, colorIndex) : 1 byte for each component
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
7. Chunk id 'RGBA' : palette
|
||||
-------------------------------------------------------------------------------
|
||||
# Bytes | Type | Value
|
||||
-------------------------------------------------------------------------------
|
||||
4 x 256 | int | (R, G, B, A) : 1 byte for each component
|
||||
| * <NOTICE>
|
||||
| * color [0-254] are mapped to palette index [1-255], e.g :
|
||||
|
|
||||
| for ( int i = 0; i <= 254; i++ ) {
|
||||
| palette[i + 1] = ReadRGBA();
|
||||
| }
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
8. Default Palette : if chunk 'RGBA' is absent
|
||||
-------------------------------------------------------------------------------
|
||||
unsigned int default_palette[256] = {
|
||||
0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff, 0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff,
|
||||
0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff, 0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff,
|
||||
0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc, 0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc,
|
||||
0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc, 0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc,
|
||||
0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc, 0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99,
|
||||
0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999, 0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699,
|
||||
0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099, 0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66,
|
||||
0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66, 0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666,
|
||||
0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366, 0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066,
|
||||
0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33, 0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933,
|
||||
0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633, 0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033,
|
||||
0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00, 0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00,
|
||||
0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600, 0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300,
|
||||
0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000, 0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044,
|
||||
0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700, 0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000,
|
||||
0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd, 0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111
|
||||
};
|
||||
-------------------------------------------------------------------------------
|
||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,289 @@
|
||||
-- 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()
|
||||
Reference in new issue
Block a user