Files
rosetta/tests/model/smd.luau
T

173 lines
6.2 KiB
Luau

-- model/smd tests: generated SMD files against assimp's soup and the writer's exact values; assimp reads ours.
-- Fixtures: tools/fixtures/smd.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Smd = require("@rosetta/model/smd")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Soup = require("@tests/model/_soup")
local Fx = require("@tests/gen/smd")
local t = T.suite("smd")
t:eq(Smd.name, "smd", "name")
local function near(a: { number }, b: { number }, tol: number): boolean
for i = 1, #b do
if math.abs(a[i] - b[i]) > tol then
return false
end
end
return true
end
-- same rotation (q or -q)
local function sameQ(a: { number }, b: { number }): boolean
return near(a, b, 1e-5) or near(a, { -b[1], -b[2], -b[3], -b[4] }, 1e-5)
end
local function check(s: any, c: any, label: string)
local skin = s.skins[1]
t:eq(#skin.joints, #c.bones, label .. ": joints")
for i, bn in c.bones do
local node = s.nodes[skin.joints[i]]
t:eq(node.name, bn.name, label .. ": bone name " .. i)
local pose = c.frames[1][i]
t:check(near(node.translation, pose.t, 1e-5) and sameQ(node.rotation, pose.q), label .. ": bind pose " .. bn.name)
end
if #c.tris > 0 then
local prims = s.meshes[1].primitives
t:eq(#prims, #c.materials, label .. ": a primitive per material")
-- every triangle of the writer, in file order per material
for pi, p in prims do
local mat = s.materials[p.material].name
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local uv = Types.Scene.reader(p.attributes.TEXCOORD_0) :: any
local jr = Types.Scene.reader(p.attributes.JOINTS_0) :: any
local wr = Types.Scene.reader(p.attributes.WEIGHTS_0) :: any
local k = 0
local ok, wok = true, true
for _, tri in c.tris do
if tri.material == mat then
for ci, cn in tri.corners do
local v = buffer.readu32(p.indices.data, (k * 3 + ci - 1) * 4)
ok = ok and near({ pos(v, 0), pos(v, 1), pos(v, 2) }, cn.p, 1e-5) and near({ uv(v, 0), 1 - uv(v, 1) }, cn.uv, 1e-6)
local got = {}
for wi = 0, 3 do
if wr(v, wi) > 0 then
got[jr(v, wi)] = wr(v, wi)
end
end
for _, w in cn.w do
wok = wok and got[w[1]] ~= nil and math.abs(got[w[1]] - w[2]) < 1e-5
end
end
k += 1
end
end
t:check(ok, label .. ": " .. mat .. " positions / texture coordinates")
t:check(wok, label .. ": " .. mat .. " joints / weights")
t:eq(p.indices.count, k * 3, label .. ": " .. mat .. " triangles")
local _ = pi
end
end
if #c.frames > 1 then
local a = s.animations[1]
local byNode = {}
for _, ch in a.channels do
byNode[ch.node .. ch.path] = ch
end
local ok = true
for f, pose in c.frames do
for i, bp in pose do
local node = skin.joints[i]
local tr, rot = byNode[node .. "translation"], byNode[node .. "rotation"]
local tv, qv = {}, {}
for k = 1, 3 do
tv[k] = buffer.readf32(tr.output.data, ((f - 1) * 3 + k - 1) * 4)
end
for k = 1, 4 do
qv[k] = buffer.readf32(rot.output.data, ((f - 1) * 4 + k - 1) * 4)
end
ok = ok and near(tv, bp.t, 1e-5) and sameQ(qv, bp.q)
end
end
t:check(ok, label .. ": every skeleton frame")
t:eq(a.channels[1].input.count, #c.frames, label .. ": keys")
end
end
for _, c in Fx.cases do
local s, err = Smd.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(s, c.name .. ": " .. tostring(err))
t:eq(F.detect(c.data), "smd", c.name .. ": F.detect")
check(s, c, c.name)
if not c.assimp.failed then
local matched = 0
for mi, m in c.assimp.per do
local nextStart = if c.assimp.per[mi + 1] then c.assimp.per[mi + 1].start else c.assimp.faces
local nf = nextStart - m.start
local apos = Soup.slice(c.assimp.pos, m.start * 9 + 1, nf * 9)
local auv = Soup.slice(c.assimp.uv, m.start * 6 + 1, nf * 6)
for _, p in s.meshes[1].primitives do
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-5) then
t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), c.name .. ": texture coordinates against assimp")
matched += 1
break
end
end
end
t:eq(matched, #c.materials, c.name .. ": every material mesh matches assimp's positions")
end
local out = Smd.encode(s) :: buffer
local back, berr = Smd.decode(Bytes.reader(out))
t:check(back, c.name .. ": re-read our SMD: " .. tostring(berr))
local again = table.clone(c)
again.frames = { c.frames[1] }
check(back, again, c.name .. " (written)")
if #c.frames > 1 then
local anim = Smd.decode(Bytes.reader(Smd.encode(s, { animation = 1 }) :: buffer)) :: any
local only = table.clone(c)
only.tris = {}
check(anim, only, c.name .. " (written animation)")
end
local ours = Fx.ours[c.name]
if ours and #c.tris > 0 then
t:eq(ours.faces, #c.tris, c.name .. ": assimp reads our triangles")
local soupOk = ours.soup and not ours.soup.failed and ours.soup.faces == #c.tris
t:check(soupOk, c.name .. ": assimp soup of our file")
end
end
-- a scene without a skin writes one root bone
do
local prim: any = {
attributes = { POSITION = Types.Scene.accessor(buffer.create(36), "f32", 3, 3) },
mode = "triangles",
}
buffer.writef32(prim.attributes.POSITION.data, 12, 1)
buffer.writef32(prim.attributes.POSITION.data, 28, 1)
local text = buffer.tostring(Smd.encode(Types.Scene.fromPrimitive(prim)) :: buffer)
t:check(string.find(text, '0 "root" -1', 1, true) ~= nil, "static scene: a root bone")
local back = Smd.decode(Bytes.reader(buffer.fromstring(text))) :: any
t:eq(back.meshes[1].primitives[1].indices.count, 3, "static scene round trip")
local _, e1 = Smd.encode({ kind = "image" } :: any)
t:check(e1 and e1.code == Err.ARG, "encode a non-scene")
local _, e2 = Smd.decode(Bytes.reader(buffer.fromstring("version 1\nnodes\n0 \"a\" -1\n")))
t:check(e2 and e2.code == Err.TRUNCATED, "nodes without end: " .. tostring(e2))
local _, e3 = Smd.decode(Bytes.reader(buffer.fromstring("version 2\nnodes\nend\n")))
t:check(e3 and e3.code == Err.UNSUPPORTED, "version 2")
end
local samples = {}
for _, c in Fx.cases do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
local s = Smd.decode(Bytes.reader(b))
if s then
Smd.encode(s)
end
return s
end, samples, { mutations = 60 })
t:done()