-- model/x tests: assimp's DirectX models and generated variants (64-bit floats, MSZIP text) against assimp. -- Fixtures: tools/fixtures/x.py. local T = require("@tests/T") local X = require("@rosetta/model/x") local MeshOps = require("@rosetta/ops/MeshOps") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Cases = require("@tests/gen/x_cases") local t = T.suite("x") local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return X.decode(Bytes.reader(b), opts) end t:eq(X.name, "x", "name") t:eq(X.category, "model", "category") t:check(X.caps.decode and not X.caps.encode, "caps") local function near(a: number, b: number, eps: number): boolean return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b)) end -- assimp's counts: meshes (one per material), faces, corners, bones (joints with weight in each mesh) local function counts(scene: any): (number, number, number, number) local meshes, faces, corners, bones = 0, 0, 0, 0 for _, m in scene.meshes do for _, p in m.primitives do meshes += 1 corners += p.indices.count faces += if p.mode == "polygons" then p.faces.count else p.indices.count // 3 local J, W = p.attributes.JOINTS_0, p.attributes.WEIGHTS_0 if J and W then local used = {} local gj, gw = assert(Types.Scene.reader(J)), assert(Types.Scene.reader(W)) for k = 0, p.indices.count - 1 do local v = buffer.readu32(p.indices.data, k * 4) for c = 0, 3 do if gw(v, c) > 0 then used[gj(v, c)] = true end end end for _ in used do bones += 1 end end end end return meshes, faces, corners, bones end local function bounds(scene: any): ({ number }?, { number }?) local merged = MeshOps.merge(scene) if not merged then return nil, nil end return MeshOps.bounds(merged) end local function boundsMatch(lo: { number }?, hi: { number }?, a: any, eps: number): boolean if not lo or not hi then return false end for k = 1, 3 do if not near(lo[k], a.min[k], eps) or not near(hi[k], a.max[k], eps) then return false end end return true end local scenes, byName = {}, {} local checked = 0 for i, c in Cases do byName[c.name] = c local a = c.assimp local scene, err = decode(c.data, { triangulate = false }) if a.failed then t:note("%s: assimp rejects it; we: %s", c.name, if scene then "decoded" else tostring(err)) else t:check(scene, c.name .. ": decode: " .. tostring(err)) scenes[c.name] = scene local meshes, faces, corners, bones = counts(scene) t:eq(meshes, a.meshes, c.name .. ": meshes (assimp)") t:eq(faces + scene.meta.source.skippedFaces, a.faces, c.name .. ": faces (assimp)") if scene.meta.source.skippedFaces == 0 then t:eq(corners, a.vertices, c.name .. ": corners (assimp)") end t:eq(math.max(#scene.materials, 1), a.materials, c.name .. ": materials (assimp, its default when none)") t:eq(#scene.animations, a.animations or 0, c.name .. ": animations (assimp)") t:eq(bones, a.bones or 0, c.name .. ": bones (assimp)") if a.min then local lo, hi = bounds(scene) t:check(boundsMatch(lo, hi, a, 1e-3), string.format("%s: world bounds (assimp) %s / %s", c.name, if lo then table.concat(lo, " ") else "-", if hi then table.concat(hi, " ") else "-")) end checked += 1 end end t:note("%d files checked against assimp", checked) for _, c in Cases do if c.of then local a, b = scenes[c.name], scenes[c.of] if a and b then local m1, f1, c1 = counts(a) local m2, f2, c2 = counts(b) t:check(m1 == m2 and f1 == f2 and c1 == c2, c.name .. ": same counts as " .. c.of) local la, ha = bounds(a) local lb, hb = bounds(b) t:check(la and lb and boundsMatch(la, ha, { min = lb, max = hb }, 1e-5), c.name .. ": same bounds as " .. c.of) t:eq(#a.animations, #b.animations, c.name .. ": same animations") end end end do local s = scenes["models/kwxport_test_cubewithvcolors.x"] local p = s.meshes[1].primitives[1] t:check(p.attributes.COLOR_0 and p.attributes.COLOR_0.components == 4, "vertex colours -> COLOR_0") t:eq(s.meta.source.encoding, "text", "text encoding") local b = scenes["models/test_cube_binary.x"] t:eq(b.meta.source.encoding, "binary", "binary encoding") local z = scenes["models/test_cube_compressed.x"] t:check(z.meta.source.compressed, "bzip") local v = scenes["variant/test_cube_text.bin64.x"] t:eq(v.meta.source.floatSize, 64, "64-bit floats") end do local s = scenes["models/test.x"] local any = false for _, m in s.materials do if m.baseColorTexture then local tex = s.textures[m.baseColorTexture.texture] any = any or (type(tex.uri) == "string" and #tex.uri > 0) end end t:check(any, "test.x: a texture file name") t:check(s.meshes[1].primitives[1].attributes.TEXCOORD_0, "test.x: TEXCOORD_0") end do -- skins: the inverse bind matrix of each joint times the joint's world bind matrix is about the same for all -- joints (the mesh node's world matrix): checks the matrix layout and the handedness conversion together local checkedSkins, consistent = 0, 0 for name, s in scenes do for _, node in s.nodes do if node.skin then local skin = s.skins[node.skin] local world = {} local function worldOf(i: number): { number } if world[i] then return world[i] end local m = Types.Scene.nodeMatrix(s.nodes[i]) for pi, pn in s.nodes do if table.find(pn.children, i) then m = Types.Scene.mul(worldOf(pi), m) break end end world[i] = m return m end local get = assert(Types.Scene.reader(skin.inverseBindMatrices)) local ref: { number }? = nil local ok, total = 0, 0 for j, joint in skin.joints do local inv = {} for k = 1, 16 do inv[k] = get(j - 1, k - 1) end local m = Types.Scene.mul(worldOf(joint), inv) total += 1 if not ref then ref = m ok += 1 else local same = true for k = 1, 16 do same = same and math.abs(m[k] - (ref :: any)[k]) < 1e-2 * math.max(1, math.abs(m[k])) end ok += if same then 1 else 0 end end t:note("%s: skin %d: bind pose consistent for %d / %d joints", name, node.skin, ok, total) checkedSkins += 1 consistent += if ok >= total * 0.9 then 1 else 0 end end end -- (anim_test.x's frames are not in the bind pose) t:check(checkedSkins >= 4 and consistent >= checkedSkins * 0.7, string.format("skins consistent with the frames (%d / %d)", consistent, checkedSkins)) end do -- the rotation key convention: an animation's first rotation key mostly equals the bone's bind rotation local same, total = 0, 0 for _, s in scenes do for _, anim in s.animations do for _, ch in anim.channels do if ch.path == "rotation" then local node = s.nodes[ch.node] local r = node.rotation if r then local get = assert(Types.Scene.reader(ch.output)) local dot = 0 for k = 0, 3 do dot += get(0, k) * r[k + 1] end total += 1 same += if math.abs(dot) > 0.999 then 1 else 0 end end end end end t:check(total > 0 and same >= total * 0.5, string.format("first rotation keys match the bind pose (%d / %d)", same, total)) end local CUBE = [[xof 0303txt 0032 // a quad and a triangle, two materials, one animated frame Material Red { 1.0; 0.0; 0.0; 1.0;; 10.0; 0.5; 0.5; 0.5;; 0.0; 0.0; 0.0;; TextureFilename { "tex\\red.png"; } } AnimTicksPerSecond { 100; } Frame Root { FrameTransformMatrix { 1,0,0,0, 0,1,0,0, 0,0,1,0, 1,2,3,1;; } Mesh Shape { 5; 0;0;0;, 1;0;0;, 1;1;0;, 0;1;0;, 2;0;0;; 2; 4;0,1,2,3;, 3;1,4,2;; MeshMaterialList { 2; 2; 0, 1;; { Red } Material Blue { 0;0;1;0.5;; 1; 0;0;0;; 0;0;0;; } } MeshNormals { 1; 0;0;-1;; 2; 4;0,0,0,0;, 3;0,0,0;; } MeshTextureCoords { 5; 0;0;, 1;0;, 1;1;, 0;1;, 0.5;0.5;; } } } AnimationSet Move { Animation { { Root } AnimationKey { 2; 2; 0; 3; 1;2;3;;, 100; 3; 1;2;5;;; } } } ]] do local s, err = decode(CUBE, { triangulate = false }) t:check(s, "inline: " .. tostring(err)) t:eq(#s.nodes, 1, "inline: one frame") local root = s.nodes[1] t:check(root.translation and root.translation[1] == 1 and root.translation[3] == -3, "inline: translation, z negated") t:eq(#s.materials, 2, "inline: global + inline material") t:eq(s.materials[1].name, "Red", "inline: material name") t:eq(s.textures[1].uri, "tex/red.png", "inline: texture uri (backslash -> slash)") t:eq(s.materials[2].alphaMode, "blend", "inline: alpha < 1 -> blend") local prims = s.meshes[1].primitives t:eq(#prims, 2, "inline: one primitive per material") t:eq(prims[1].material, 1, "inline: first face -> Red (referenced)") t:eq(prims[2].material, 2, "inline: second face -> Blue") t:eq(prims[1].faces.count, 1, "inline: the quad kept") local N = assert(Types.Scene.reader(prims[1].attributes.NORMAL)) t:eq(N(0, 2), 1, "inline: normal z negated") local anim = s.animations[1] t:eq(anim.name, "Move", "inline: animation name") local ch = anim.channels[1] t:eq(ch.path, "translation", "inline: position key") local ti, to = assert(Types.Scene.reader(ch.input)), assert(Types.Scene.reader(ch.output)) t:eq(ti(1, 0), 1, "inline: ticks -> seconds") t:eq(to(1, 2), -5, "inline: key z negated") local tri = decode(CUBE) t:eq(tri.meshes[1].primitives[1].indices.count, 6, "inline: quad fan-triangulated") end do local _, e1 = decode("xof 0303txt 00") t:check(e1 and e1.code == Err.TRUNCATED, "short: " .. tostring(e1)) local _, e2 = decode("xof 0303zzz 0032") t:check(e2 and e2.code == Err.UNSUPPORTED, "encoding: " .. tostring(e2)) local _, e3 = decode("xof 0303txt 0016 Frame {}") t:check(e3 and e3.code == Err.CORRUPT, "float size: " .. tostring(e3)) local _, e4 = decode("xof 0303txt 0032 Frame A { Mesh { 3; 0;0;0;, 1;0;0;, 0;1;0;; 1; 3; 0,1,7;; } }") t:check(e4 and e4.code == Err.CORRUPT, "face index out of range: " .. tostring(e4)) local _, e5 = decode("xof 0303txt 0032 Frame A { ") t:check(e5 and e5.code == Err.CORRUPT, "unterminated: " .. tostring(e5)) local _, e7 = decode("xof 0303txt 0032 Mesh { 33554431; 1; }") t:check(e7 and e7.code == Err.TRUNCATED, "a vertex count past the data is refused before allocating: " .. tostring(e7)) local _, e6 = decode(CUBE, { maxValues = 10 }) t:check(e6 and e6.code == Err.LIMIT, "maxValues: " .. tostring(e6)) t:eq(X.sniff(Bytes.reader(buffer.fromstring(CUBE))), 1, "sniff") end do local samples = { CUBE } for _, c in Cases do if #c.data < 12000 then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) local s = X.decode(Bytes.reader(b), { triangulate = false }) if s then MeshOps.merge(s) end return s end, samples, { mutations = 16, keepHeader = 16 }) end t:done()