-- model/ase tests: assimp's ASE models and a hand-written file against assimp (meshes, faces, corners, cameras, lights, -- world bounds); the hand file's values; round trips; assimp reading our output. Fixtures: tools/fixtures/ase.py. local T = require("@tests/T") local F = require("@rosetta") local Ase = require("@rosetta/model/ase") 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/ase_cases") local Xcheck = require("@tests/gen/ase_xcheck") local t = T.suite("ase") local S = Types.Scene local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return Ase.decode(Bytes.reader(b), opts) end t:eq(Ase.name, "ase", "name") t:eq(Ase.category, "model", "category") t:check(Ase.caps.decode and Ase.caps.encode, "contract") 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 local function counts(scene: any): (number, number, number) local prims, faces, corners = 0, 0, 0 for _, pl in S.flatten(scene) do prims += 1 faces += pl.primitive.indices.count // 3 corners += pl.primitive.indices.count end return prims, faces, corners end -- assimp's ASE root rotates Z-up to Y-up: (x, z, -y) local function boundsMatch(lo: { number }, hi: { number }, want: any, eps: number): boolean local a, b = { lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] } for k = 1, 3 do if not near(a[k], want.min[k], eps) or not near(b[k], want.max[k], eps) then return false end end return true end local function bstr(lo: any, hi: any): string if not lo then return "-" end return string.format("(%.4g %.4g %.4g) (%.4g %.4g %.4g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3]) end -- assimp mis-reads the UTF-16 copies of ThreeCubesGreen (one mesh of 8 faces, a bone): they are compared with the -- UTF-8 file instead local UTF16 = { ["assimp/models/ThreeCubesGreen_UTF16BE.ASE"] = true, ["assimp/models/ThreeCubesGreen_UTF16LE.ASE"] = true } local scenes: { [number]: any } = {} local byName: { [string]: any } = {} local samples = {} for i, c in Cases do local scene, err = decode(c.data) t:check(scene, c.name .. ": decode: " .. tostring(err)) t:eq(Ase.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff") scenes[i] = scene byName[c.name] = scene if #c.data < 60000 then table.insert(samples, c.data) end local a = c.assimp if scene and not a.failed and not UTF16[c.name] then local prims, faces, corners = counts(scene) t:eq(prims, a.meshes, c.name .. ": meshes (assimp)") t:eq(faces, a.faces, c.name .. ": faces (assimp)") t:eq(corners, a.vertices, c.name .. ": corners (assimp)") t:eq(#scene.cameras, a.cameras, c.name .. ": cameras (assimp)") t:eq(#scene.lights, a.lights, c.name .. ": lights (assimp)") if a.min then local lo, hi = MeshOps.bounds(MeshOps.merge(scene)) t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": world bounds (assimp) " .. bstr(lo, hi) .. " want " .. bstr(a.min, a.max)) end end end do local u8 = byName["assimp/models/ThreeCubesGreen.ASE"] for name in UTF16 do local s = byName[name] if s and u8 then local p1, f1 = counts(s) local p2, f2 = counts(u8) local lo1, hi1 = MeshOps.bounds(MeshOps.merge(s)) local lo2, hi2 = MeshOps.bounds(MeshOps.merge(u8)) t:check(p1 == p2 and f1 == f2 and bstr(lo1, hi1) == bstr(lo2, hi2), name .. ": same scene as the UTF-8 file") end end end -- the hand-written file do local s = byName["hand.ase"] t:eq(s.meta.up, "z", "hand: up") t:eq(s.meta.source.frameSpeed, 25, "hand: frame speed") t:eq(#s.materials, 2, "hand: sub-materials") local brick, glass = s.materials[1], s.materials[2] t:eq(brick.name, "Brick", "hand: material name") t:check(near(brick.baseColor[1], 0.8, 1e-6) and near(brick.baseColor[3], 0.2, 1e-6), "hand: diffuse") t:eq(s.textures[brick.baseColorTexture.texture].uri, "textures\\brick.png", "hand: bitmap path") t:check(brick.baseColorTexture.transform and brick.baseColorTexture.transform.scale[2] == 3, "hand: UVW tiling") t:check(glass.alphaMode == "blend" and near(glass.baseColor[4], 0.5, 1e-6) and glass.doubleSided, "hand: transparency, two-sided") local quad for _, n in s.nodes do if n.name == "Quad" then quad = n end end local pivot = s.nodes[s.roots[1]] t:eq(pivot.name, "Pivot", "hand: helper root") t:check(table.find(pivot.children, table.find(s.nodes, quad)) ~= nil, "hand: NODE_PARENT") -- local frame: Quad's world TM rotates 90 degrees about z at (10, 5, 0); relative to Pivot at (10, 0, 0) t:check(near(quad.matrix[13], 0, 1e-6) and near(quad.matrix[14], 5, 1e-6) and near(quad.matrix[2], 1, 1e-6), "hand: matrix relative to the parent") local mesh = s.meshes[quad.mesh] t:eq(#mesh.primitives, 2, "hand: one primitive per sub-material") local p1 = mesh.primitives[1] t:eq(p1.material, 1, "hand: MESH_MTLID 0") local P = S.reader(p1.attributes.POSITION) :: any -- vertex 1 (10, 7, 0) world -> local: inverse of (rot z 90, t (10, 5, 0)) = (2, 0, 0) t:check(near(P(1, 0), 2, 1e-6) and near(P(1, 1), 0, 1e-6), "hand: vertices in the node frame") local UV = S.reader(p1.attributes.TEXCOORD_0) :: any t:check(near(UV(1, 0), 1, 1e-6) and near(UV(1, 1), 1, 1e-6) and near(UV(2, 1), 0, 1e-6), "hand: UVs (v flipped)") local UV2 = S.reader(p1.attributes.TEXCOORD_1) :: any t:check(near(UV2(0, 0), 0.25, 1e-6) and near(UV2(0, 1), 0.25, 1e-6), "hand: mapping channel 2") local C = S.reader(p1.attributes.COLOR_0) :: any t:check(C(0, 0) == 1 and C(1, 1) == 1 and C(2, 0) == 1, "hand: vertex colours") local N = S.reader(p1.attributes.NORMAL) :: any -- (0, 0.6, 0.8) world under the inverse rotation: (0.6, 0, 0.8) t:check(near(N(2, 0), 0.6, 1e-6) and near(N(2, 2), 0.8, 1e-6), "hand: explicit vertex normals") t:eq(s.cameras[1].zfar, 500, "hand: camera far") t:check(s.lights[1].type == "spot" and s.lights[1].intensity == 2 and near(s.lights[1].outerCone, math.rad(30), 1e-9), "hand: spot light") end -- round trips: ours -> ours keeps counts, bounds, materials, UVs local function sameScene(a: any, b: any, label: string) local p1, f1, c1 = counts(a) local p2, f2, c2 = counts(b) t:check(p1 == p2 and f1 == f2 and c1 == c2, label .. ": counts " .. p1 .. "/" .. f1 .. " vs " .. p2 .. "/" .. f2) local lo1, hi1 = MeshOps.bounds(MeshOps.merge(a)) local lo2, hi2 = MeshOps.bounds(MeshOps.merge(b)) if lo1 and lo2 then local ok = true for k = 1, 3 do ok = ok and near(lo1[k], lo2[k], 1e-5) and near(hi1[k], hi2[k], 1e-5) end t:check(ok, label .. ": bounds " .. bstr(lo1, hi1) .. " vs " .. bstr(lo2, hi2)) end t:eq(#b.cameras, #a.cameras, label .. ": cameras") t:eq(#b.lights, #a.lights, label .. ": lights") end for i, c in Cases do local s = scenes[i] if s then local out, err = Ase.encode(s) t:check(out, c.name .. ": encode: " .. tostring(err)) if out then local back, berr = decode(out) t:check(back, c.name .. ": re-decode: " .. tostring(berr)) if back then sameScene(s, back, c.name .. ": round trip") end end end end do local s = byName["hand.ase"] local back = decode(assert(Ase.encode(s))) local m = back.materials[1] t:eq(m.name, "Brick", "hand round trip: material") t:eq(back.textures[m.baseColorTexture.texture].uri, "textures\\brick.png", "hand round trip: bitmap") local q for _, n in back.nodes do if n.name == "Quad" then q = n end end local p = back.meshes[q.mesh].primitives[1] t:check(p.attributes.TEXCOORD_1 and p.attributes.COLOR_0 and p.attributes.NORMAL, "hand round trip: attributes") end -- glTF -> ASE (any Scene): a mesh with no materials, an n-gon, a strip do local s = S.new() local P = S.accessor(nil, "f32", 3, 5) for i, v in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0.5, 1.5, 0 }, { 0, 1, 0 } } do for k = 1, 3 do buffer.writef32(P.data, ((i - 1) * 3 + k - 1) * 4, v[k]) end end local faces = S.accessor(nil, "u32", 1, 1) buffer.writeu32(faces.data, 0, 5) s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "polygons", faces = faces } } } s.meshes[2] = { primitives = { { attributes = { POSITION = P }, mode = "triangle_strip" } } } s.nodes = { { name = "ngon", children = { 2 }, mesh = 1, translation = { 0, 0, 3 } }, { name = "ngon", children = {}, mesh = 2, scale = { 2, 2, 2 } } } s.roots = { 1 } local out = assert(Ase.encode(s)) local back = assert(decode(out)) local _, faces2 = counts(back) t:eq(faces2, 6, "n-gon (3) + strip (3) as triangles") t:eq(back.nodes[2].name, "ngon_2", "duplicate names made unique") local lo, hi = MeshOps.bounds(MeshOps.merge(back)) t:check(near(lo[3], 3, 1e-6) and near(hi[1], 2, 1e-6), "child world transform " .. bstr(lo, hi)) local via = F.save(s, nil, { format = "ase" }) t:check(type(via) == "buffer" and F.detect(via) == "ase", "F.save / F.detect") end -- assimp reads what we write for _, x in Xcheck do local c = Cases[x.case] local s = scenes[x.case] local a = x.assimp t:check(not a.failed, x.name .. " (" .. c.name .. "): assimp reads it") if s and not a.failed then local prims, faces = counts(s) t:eq(a.faces, faces, c.name .. ": assimp faces of ours") t:eq(a.meshes, prims, c.name .. ": assimp meshes of ours") t:eq(a.cameras, #s.cameras, c.name .. ": assimp cameras of ours") t:eq(a.lights, #s.lights, c.name .. ": assimp lights of ours") if a.min then local lo, hi = MeshOps.bounds(MeshOps.merge(s)) t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": assimp bounds of ours " .. bstr(lo, hi) .. " vs " .. bstr(a.min, a.max)) end end end -- errors do local _, e = decode("*3DSMAX_ASCIIEXPORT 200\n*GEOMOBJECT { *MESH { *MESH_NUMVERTEX 1 *MESH_NUMFACES 1 *MESH_FACE_LIST { *MESH_FACE 0: A: 0 B: 5 C: 0 } } }") t:check(e and e.code == Err.CORRUPT, "face index out of range: " .. tostring(e)) local _, e2 = decode("*3DSMAX_ASCIIEXPORT 200\n}") t:check(e2 and e2.code == Err.CORRUPT, "unbalanced brace") local _, e3 = decode("hello") t:check(e3 and e3.code == Err.CORRUPT, "not ASE") local _, e4 = decode(Cases[1].data, { maxItems = 10 }) t:check(e4 and e4.code == Err.LIMIT, "item limit") local _, e5 = Ase.encode({} :: any) t:check(e5 and e5.code == Err.ARG, "encode arg") end t:fuzz("decode", function(b: buffer) return Ase.decode(Bytes.reader(b)) end, samples) t:done()