Files
rosetta/tests/model/mdl.luau
T

156 lines
6.7 KiB
Luau

-- model/mdl tests: a Quake 1 and a Half-Life 1 model written by tools/fixtures/mdl.py. Quake: assimp's frame-0
-- triangle soup (positions, texture coordinates), every frame exact, skins through opts.palette / opts.resolve /
-- the grey ramp, frame-name animations. Half-Life: assimp's bind-pose soup per mesh (positions, texture
-- coordinates), the bones' TRS, the skin, every sequence's animation values against the writer's, textures with
-- their palettes, the skin family. Errors and the fuzz pass.
local T = require("@tests/T")
local F = require("@rosetta")
local Mdl = require("@rosetta/model/mdl")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Soup = require("@tests/model/_soup")
local Fx = require("@tests/gen/mdl")
local t = T.suite("mdl")
t:eq(Mdl.name, "mdl", "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
-- assimp mesh mi of a soup -> positions, uvs
local function assimpMesh(a: any, mi: number): ({ number }, { number }, number)
local m = a.per[mi]
local nextStart = if a.per[mi + 1] then a.per[mi + 1].start else a.faces
local nf = nextStart - m.start
return Soup.slice(a.pos, m.start * 9 + 1, nf * 9), Soup.slice(a.uv, m.start * 6 + 1, nf * 6), nf
end
-- 1. Quake
do
local q = Fx.quake
local s, err = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), { palette = q.palette })
t:check(s, "quake: " .. tostring(err))
t:eq(F.detect(q.data), "mdl", "quake: F.detect")
local prim = s.meshes[1].primitives[1]
local apos, auv = assimpMesh(q.assimp, 1)
t:check(Soup.triangles(Soup.list(Soup.positions(prim)), apos, 1e-5), "quake: positions against assimp")
t:check(Soup.triangles(Soup.uvs(prim, 0, true), auv, 1e-5, 2), "quake: texture coordinates against assimp")
t:check(Soup.close(Soup.uvs(prim, 0), q.uv, 1e-6), "quake: texture coordinates (seam) against the writer")
t:eq(#q.crcs, s.meta.source.frames, "quake: frames (groups unrolled)")
for k, crc in q.crcs do
local f = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), { frame = k, morph = false, palette = q.palette }) :: any
t:eq(Crc32.of(Soup.positions(f.meshes[1].primitives[1])), crc, "quake: frame " .. k)
end
t:eq(#prim.targets, #q.crcs, "quake: morph targets")
local groups = {}
for _, a in s.animations do
table.insert(groups, a.name)
end
t:eq(table.concat(groups, ","), "stand,walk,death", "quake: animation groups")
t:eq(#s.textures, #q.skins, "quake: skins")
for i, crc in q.skins do
t:eq(Crc32.of(s.textures[i].image.pixels), crc, "quake: skin " .. i .. " through the palette")
end
local viaResolve = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), {
resolve = function(uri: string): string?
return if uri == "gfx/palette.lmp" then q.palette else nil
end,
}) :: any
t:eq(Crc32.of(viaResolve.textures[1].image.pixels), q.skins[1], "quake: palette through opts.resolve")
local grey = Mdl.decode(Bytes.reader(buffer.fromstring(q.data))) :: any
t:eq(Crc32.of(grey.textures[1].image.pixels), q.grey, "quake: grey ramp without a palette")
t:eq(grey.meta.source.palette, false, "quake: meta.source.palette")
end
-- 2. Half-Life
do
local h = Fx.halflife
local s, err = Mdl.decode(Bytes.reader(buffer.fromstring(h.data)))
t:check(s, "halflife: " .. tostring(err))
t:eq(F.detect(h.data), "mdl", "halflife: F.detect")
local mesh = s.meshes[1]
t:eq(#mesh.primitives, #h.tris, "halflife: a primitive per mesh")
t:eq(h.assimp.meshes, #h.tris, "halflife: assimp meshes")
local matched = 0
for mi = 1, h.assimp.meshes do
local apos, auv, nf = assimpMesh(h.assimp, mi)
for _, p in mesh.primitives do
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-4) then
-- assimp's Half-Life loader stores v = -t / height
t:check(Soup.triangles(Soup.uvs(p, 0, "neg"), auv, 1e-5, 2), "halflife: texture coordinates against assimp")
matched += 1
break
end
end
end
t:eq(matched, #h.tris, "halflife: every mesh matches assimp's bind-pose positions")
for i, p in mesh.primitives do
t:eq(p.indices.count, h.tris[i] * 3, "halflife: mesh " .. i .. " triangles")
t:check(p.attributes.JOINTS_0 and p.attributes.WEIGHTS_0, "halflife: skinned")
end
-- bones
local skin = s.skins[1]
t:eq(#skin.joints, #h.bones, "halflife: joints")
for i, bn in h.bones do
local node = s.nodes[skin.joints[i]]
t:eq(node.name, bn.name, "halflife: bone name " .. i)
t:check(near(node.translation, bn.t, 1e-6) and near(node.rotation, bn.q, 1e-6), "halflife: bone " .. bn.name .. " TRS")
end
-- animations
t:eq(#s.animations, #h.sequences, "halflife: sequences")
for si, sq in h.sequences do
local a = s.animations[si]
t:eq(a.name, sq.name, "halflife: sequence name")
local byNode = {}
for _, ch in a.channels do
byNode[ch.node .. ch.path] = ch
end
local ok = true
for bi, frames in sq.bones do
local node = skin.joints[bi]
local tch, rch = byNode[node .. "translation"], byNode[node .. "rotation"]
for f, fr in frames do
for k = 1, 3 do
ok = ok and math.abs(buffer.readf32(tch.output.data, ((f - 1) * 3 + k - 1) * 4) - fr.t[k]) < 1e-4
end
for k = 1, 4 do
ok = ok and math.abs(buffer.readf32(rch.output.data, ((f - 1) * 4 + k - 1) * 4) - fr.q[k]) < 1e-5
end
end
t:eq(tch.input.count, sq.frames, "halflife: " .. sq.name .. " keys")
t:check(math.abs(buffer.readf32(tch.input.data, 4) - 1 / sq.fps) < 1e-6, "halflife: " .. sq.name .. " fps")
end
t:check(ok, "halflife: " .. sq.name .. ": every value against the writer")
end
-- textures, skin family 0
for i, tx in h.textures do
t:eq(s.textures[i].name, tx.name, "halflife: texture name")
t:eq(Crc32.of(s.textures[i].image.pixels), tx.crc, "halflife: texture " .. i .. " through its palette")
end
t:eq(mesh.primitives[1].material, 1, "halflife: mesh 1 -> skin ref 0 -> texture 1")
t:eq(mesh.primitives[2].material, 2, "halflife: mesh 2 -> skin ref 1 -> texture 2")
end
-- 3. errors, fuzz
do
local _, e1 = Mdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.quake.data, 1, 200))))
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "quake truncated: " .. tostring(e1))
local _, e2 = Mdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.halflife.data, 1, 300))))
t:check(e2 ~= nil, "halflife truncated: " .. tostring(e2))
local src = buffer.fromstring(Fx.halflife.data)
buffer.writei32(src, 4, 48)
local _, e3 = Mdl.decode(Bytes.reader(src))
t:check(e3 and e3.code == Err.UNSUPPORTED, "Source engine MDL")
end
t:fuzz("decode", function(b: buffer)
return Mdl.decode(Bytes.reader(b), { palette = Fx.quake.palette })
end, { Fx.quake.data, Fx.halflife.data }, { mutations = 200 })
t:done()