-- model/robloxmesh tests: meshes of every version from the fixture's spec writer against the values written. -- Fixtures: tools/fixtures/robloxmesh.py. local T = require("@tests/T") local RobloxMesh = require("@rosetta/model/robloxmesh") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Mat4 = require("@rosetta/codecs/model/Mat4") local Fx = require("@tests/gen/robloxmesh") local t = T.suite("robloxmesh") t:eq(RobloxMesh.name, "robloxmesh", "name") t:eq(RobloxMesh.category, "model", "category") t:check(table.find(RobloxMesh.extensions, "mesh") ~= nil, "extensions") t:check(RobloxMesh.caps.decode and RobloxMesh.caps.encode, "caps") local function decode(data: any, opts: any?): (any, any) return RobloxMesh.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts) end local function world(scene: any, i: number): { number } local parent = {} for k, n in scene.nodes do for _, c in n.children do parent[c] = k end end local m = Types.Scene.nodeMatrix(scene.nodes[i]) local p = parent[i] while p do m = Mat4.mul(Types.Scene.nodeMatrix(scene.nodes[p]), m) p = parent[p] end return m end local ncases = 0 for _, c in Fx.cases do local scene, err = decode(c.data) if c.unsupported then -- a COREMESH v2 whose body is not a Draco stream (real Draco meshes: tests/model/robloxmesh_draco.luau) t:check(scene == nil and err and err.code == Err.CORRUPT, c.name .. ": not Draco data (" .. tostring(err) .. ")") continue end t:check(scene ~= nil, c.name .. ": " .. tostring(err)) if not scene then continue end ncases += 1 local prim = scene.meshes[1].primitives[1] local a = prim.attributes t:eq(scene.meta.source.version, c.version, c.name .. ": version") t:eq(a.POSITION.count, c.vertices, c.name .. ": vertices") t:eq(Crc32.of(a.POSITION.data), c.pos, c.name .. ": POSITION") t:eq(Crc32.of(a.NORMAL.data), c.nrm, c.name .. ": NORMAL") t:eq(Crc32.of(a.TEXCOORD_0.data), c.uv, c.name .. ": TEXCOORD_0") if c.tan then t:check(a.TANGENT and Crc32.of(a.TANGENT.data) == c.tan, c.name .. ": TANGENT") end if c.col then t:check(a.COLOR_0 and a.COLOR_0.type == "unorm8" and Crc32.of(a.COLOR_0.data) == c.col, c.name .. ": COLOR_0") else t:check(a.COLOR_0 == nil, c.name .. ": no COLOR_0") end local lods = { prim } for _, l in (scene.meshes[1].extras or {}).lods or {} do table.insert(lods, l) end t:eq(#lods, #c.lods, c.name .. ": LODs") for k, want in c.lods do t:check(lods[k] and Crc32.of(lods[k].indices.data) == want, c.name .. ": LOD " .. (k - 1) .. " indices") end if c.joints then t:check(a.JOINTS_0 and Crc32.of(a.JOINTS_0.data) == c.joints, c.name .. ": JOINTS_0") t:check(a.WEIGHTS_0 and Crc32.of(a.WEIGHTS_0.data) == c.weights, c.name .. ": WEIGHTS_0") local skin = scene.skins[1] t:eq(#skin.joints, #c.bones, c.name .. ": joints") t:eq(skin.inverseBindMatrices.count, #c.bones, c.name .. ": inverse bind matrices") t:eq(scene.nodes[1].skin, 1, c.name .. ": the mesh node is skinned") local worst = 0 for i, b in c.bones do local j = skin.joints[i] local node = scene.nodes[j] t:eq(node.name, b.name, c.name .. ": bone name") t:eq(node.extras.culling, b.culling, c.name .. ": culling") local isRoot = table.find(scene.roots, j) ~= nil t:eq(isRoot, b.parent == nil, c.name .. ": " .. b.name .. " root") if b.parent then t:check(table.find(scene.nodes[skin.joints[b.parent + 1]].children, j) ~= nil, c.name .. ": " .. b.name .. " parent") end local m = world(scene, j) local cf = b.cf local want = { cf[1], cf[4], cf[7], 0, cf[2], cf[5], cf[8], 0, cf[3], cf[6], cf[9], 0, cf[10], cf[11], cf[12], 1 } for k = 1, 16 do worst = math.max(worst, math.abs(m[k] - want[k])) end end t:check(worst < 1e-4, c.name .. ": bone world transforms (" .. worst .. ")") end if c.facs then t:check(scene.meta.source.facs and Crc32.of(scene.meta.source.facs) == c.facs, c.name .. ": FACS kept raw") end if c.chunks then t:eq(scene.meta.source.chunks[1].name, c.chunks[1], c.name .. ": raw chunk") end t:eq(scene.meta.up, "y", c.name .. ": up") end t:note("%d meshes match", ncases) for _, v in Fx.ours do t:check(v.ok, v.name .. ": " .. v.detail) end t:note("%d of our files read back by the fixture's reader", #Fx.ours) do for _, name in { "v2.00-40", "v4.00-lods", "v4.00-40bones", "v6.00" } do local c for _, x in Fx.cases do if x.name == name then c = x end end local s = decode(c.data) local out = RobloxMesh.encode(s, { version = "4.00" }) :: buffer local back = decode(out) t:check(back ~= nil, name .. ": re-read") local p0, p1 = s.meshes[1].primitives[1], back.meshes[1].primitives[1] t:eq(p1.indices.count, p0.indices.count, name .. ": triangles") if not c.joints then t:eq(Crc32.of(p1.attributes.POSITION.data), Crc32.of(p0.attributes.POSITION.data), name .. ": positions") t:eq(Crc32.of(p1.indices.data), Crc32.of(p0.indices.data), name .. ": indices") t:eq(Crc32.of(p1.attributes.TANGENT.data), Crc32.of(p0.attributes.TANGENT.data), name .. ": tangents") else t:eq(#back.skins[1].joints, #s.skins[1].joints, name .. ": joints") end local v2 = decode(RobloxMesh.encode(s) :: buffer) t:eq(v2.meta.source.version, "2.00", name .. ": 2.00 by default") end -- a glTF-style scene: a quad under a transformed node, no normals / colours local s = Types.Scene.new() local pos = buffer.create(48) for i, v in { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 } do buffer.writef32(pos, (i - 1) * 4, v) end local idx = buffer.create(24) for i, v in { 0, 1, 2, 0, 2, 3 } do buffer.writeu32(idx, (i - 1) * 4, v) end s.meshes[1] = { primitives = { { attributes = { POSITION = Types.Scene.accessor(pos, "f32", 3, 4) }, indices = Types.Scene.accessor(idx, "u32", 1, 6), mode = "triangles" } } } s.nodes[1] = { children = {}, mesh = 1, translation = { 10, 0, 0 } } s.roots[1] = 1 local q = decode(RobloxMesh.encode(s) :: buffer) local qa = q.meshes[1].primitives[1].attributes t:eq(buffer.readf32(qa.POSITION.data, 12), 11, "node transform applied") t:eq(buffer.readf32(qa.NORMAL.data, 8), 1, "computed normal +Z") t:check(qa.COLOR_0 == nil, "white / no colours: 36-byte vertices") end do local c = Fx.cases[3] t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff") t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("version 1.0"))), 0, "sniff short") t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("solid cube\n"))), 0, "sniff STL") local _, e1 = decode("version 9.00\n") t:check(e1 and e1.code == Err.UNSUPPORTED, "version 9: " .. tostring(e1)) local _, e2 = decode(string.sub(c.data, 1, 60)) t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2)) local _, e3 = decode(c.data, { maxVertices = 10 }) t:check(e3 and e3.code == Err.LIMIT, "maxVertices: " .. tostring(e3)) local bad = buffer.fromstring(c.data) buffer.writeu32(bad, buffer.len(bad) - 4, 99999) local _, e4 = decode(bad) t:check(e4 and e4.code == Err.CORRUPT, "face index out of range: " .. tostring(e4)) local _, e5 = decode("hello there, a mesh") t:check(e5 and e5.code == Err.CORRUPT, "no version line: " .. tostring(e5)) local _, e6 = RobloxMesh.encode(Types.Scene.new()) t:check(e6 and e6.code == Err.ARG, "an empty scene: " .. tostring(e6)) local _, e7 = RobloxMesh.encode(decode(c.data), { version = "3.00" } :: any) t:check(e7 and e7.code == Err.ARG, "version 3.00 is not written") end do local samples = {} for _, c in Fx.cases do if #c.data < 40000 then table.insert(samples, c.data) end end t:fuzz("decode", function(b: buffer) return RobloxMesh.decode(Bytes.reader(b)) end, samples, { mutations = 30, garbage = 300, keepHeader = 13 }) end t:done()