Files
rosetta/tests/model/sourcemdl.luau
T

286 lines
12 KiB
Luau

-- model/sourcemdl tests: generated MDL v44 / v48 / v49 + VVD / VTX / PHY / ANI against the writer's exact values and
-- srctools' reading of the same files. Fixtures: tools/fixtures/sourcemdl.py.
local T = require("@tests/T")
local F = require("@rosetta")
local SourceMdl = require("@rosetta/model/sourcemdl")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/sourcemdl")
local t = T.suite("sourcemdl")
t:eq(SourceMdl.name, "sourcemdl", "name")
t:eq(SourceMdl.category, "model", "category")
local function floats(acc: any): { number }
local rd = Types.Scene.reader(acc) :: any
local out = {}
for i = 0, acc.count - 1 do
for c = 0, acc.components - 1 do
table.insert(out, rd(i, c))
end
end
return out
end
local function near(a: { number }, b: { number }, tol: number): boolean
if #a ~= #b then
return false
end
for i = 1, #b do
if math.abs(a[i] - b[i]) > tol then
return false
end
end
return true
end
local function decode(c: any, extra: { [string]: any }?): (any, any)
local o: { [string]: any } = { files = c.files }
for k, v in extra or {} do
o[k] = v
end
return SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl)), o :: any)
end
local function normDir(s: string): string
local d = string.gsub(s, "\\", "/")
d = string.gsub(d, "^/+", "")
if d ~= "" and string.sub(d, -1) ~= "/" then
d ..= "/"
end
return d
end
for _, c in Fx.cases do
local tag = "v" .. c.version .. ": "
local e = c.expect
local st = c.srctools
local s, err = decode(c)
t:check(s, tag .. tostring(err))
if not s then
continue
end
local src = s.meta.source
t:eq(F.detect(c.mdl), "sourcemdl", tag .. "F.detect")
t:eq(#src.warnings, 0, tag .. "no warnings (" .. table.concat(src.warnings, "; ") .. ")")
t:eq(src.vtx, "model.dx90.vtx", tag .. "the dx90 strips")
-- header against srctools
t:eq(src.name, st.name, tag .. "name (srctools)")
t:eq(src.version, st.version, tag .. "version (srctools)")
t:eq(src.surfaceProp, st.surfaceprop, tag .. "surfaceprop (srctools)")
t:eq(src.keyValues, st.keyvalues, tag .. "keyvalues (srctools)")
t:check(math.abs(src.mass - st.mass) < 1e-6, tag .. "mass (srctools)")
t:check(near(src.eye, st.eye, 1e-6) and near(src.hull[1], st.hull[1], 1e-6) and near(src.hull[2], st.hull[2], 1e-6), tag .. "eye / hull (srctools)")
for i, d in src.cdmaterials do
t:eq(normDir(d), st.cdmaterials[i], tag .. "cdmaterials " .. i .. " (srctools)")
end
t:eq(#src.includeModels, #st.included, tag .. "include models (srctools)")
t:eq(src.includeModels[1].file, st.included[1][2], tag .. "include model file (srctools)")
t:eq(#src.skins, #st.skins, tag .. "skin families (srctools)")
for f, fam in src.skins do
local names = {}
for r, mi in fam do
names[r] = s.materials[mi].name
end
t:eq(table.concat(names, ","), table.concat(st.skins[f], ","), tag .. "skin family " .. f .. " (srctools)")
end
t:eq(#src.sequences, #st.sequences, tag .. "sequences (srctools)")
for i, q in st.sequences do
local ours = src.sequences[i]
t:eq(ours.name, q.label, tag .. "sequence " .. i .. " label (srctools)")
t:eq(ours.activity, q.act, tag .. "sequence " .. i .. " activity (srctools)")
t:eq(ours.flags, q.flags, tag .. "sequence " .. i .. " flags (srctools)")
t:eq(ours.weight, q.weight, tag .. "sequence " .. i .. " weight (srctools)")
t:eq(ours.keyValues, q.keyvalues, tag .. "sequence " .. i .. " keyvalues (srctools)")
t:check(near(ours.bboxMin, q.bbox[1], 1e-6) and near(ours.bboxMax, q.bbox[2], 1e-6), tag .. "sequence " .. i .. " bbox (srctools)")
t:eq(#ours.events, #q.events, tag .. "sequence " .. i .. " events (srctools)")
for k, ev in q.events do
t:check(math.abs(ours.events[k].cycle - ev[1]) < 1e-6 and ours.events[k].options == ev[2], tag .. "sequence " .. i .. " event " .. k .. " (srctools)")
end
end
local total = 0
for _, p in src.bodyParts do
for _, m in p.models do
total += m.vertices
end
end
t:eq(total, st.total_verts, tag .. "total vertices (srctools)")
t:check(string.find(src.physics.text, '"mass" "12.5', 1, true) ~= nil and st.phys_mass == 12.5, tag .. "phy keyvalues (srctools)")
-- skeleton
local skin = s.skins[1]
t:eq(#skin.joints, #e.bones, tag .. "joints")
local ibm = floats(skin.inverseBindMatrices)
for i, b in e.bones do
local node = s.nodes[skin.joints[i]]
t:eq(node.name, b.name, tag .. "bone " .. i .. " name")
t:check(near(node.translation, b.pos, 0) and near(node.rotation, b.quat, 0), tag .. "bone " .. i .. " rest pose")
if b.parent >= 0 then
t:check(table.find(s.nodes[skin.joints[b.parent + 1]].children, skin.joints[i]) ~= nil, tag .. "bone " .. i .. " parent")
end
local m = Mat4.fromRows(b.poseToBone)
local got = table.move(ibm, (i - 1) * 16 + 1, i * 16, 1, {})
t:check(near(got, m, 1e-6), tag .. "bone " .. i .. " poseToBone as the inverse bind matrix")
end
t:eq(s.nodes[skin.joints[5]].extras.procedural.type, "jiggle", tag .. "jiggle bone")
t:eq(s.nodes[skin.joints[6]].extras.procedural.type, "aimatbone", tag .. "aim-at bone")
t:check(near(src.lodVertices, e.lodCounts, 0) or (src.lodVertices[1] == e.lodCounts[1] and src.lodVertices[2] == e.lodCounts[2]), tag .. "vvd LOD vertex counts")
-- meshes
for p, part in e.parts do
for m, model in part.models do
local info = src.bodyParts[p].models[m]
t:eq(info.name, model.name, tag .. "model " .. model.name)
if #model.meshes == 0 then
t:check(info.mesh == nil, tag .. model.name .. ": no mesh")
continue
end
local mesh = s.meshes[info.mesh]
t:eq(#mesh.primitives, #model.meshes, tag .. model.name .. ": primitives")
for k, x in model.meshes do
local prim = mesh.primitives[k]
local lab = tag .. model.name .. " mesh " .. k .. ": "
t:check(near(floats(prim.attributes.POSITION), x.positions, 0), lab .. "positions")
t:check(near(floats(prim.attributes.NORMAL), x.normals, 0), lab .. "normals")
t:check(near(floats(prim.attributes.TEXCOORD_0), x.uvs, 0), lab .. "uvs")
t:check(near(floats(prim.attributes.TANGENT), x.tangents, 0), lab .. "tangents")
t:check(near(floats(prim.attributes.WEIGHTS_0), x.weights, 0), lab .. "weights")
t:check(near(floats(prim.attributes.JOINTS_0), x.joints, 0), lab .. "joints")
t:check(near(floats(prim.indices), x.lods[1], 0), lab .. "LOD 0 triangles (strips, groups, winding)")
t:check(near(floats(prim.extras.lods[2].indices), x.lods[2], 0), lab .. "LOD 1 triangles")
t:eq(prim.extras.lods[2].switchPoint, 40, lab .. "LOD 1 switch point")
t:eq(prim.material, x.material, lab .. "material through skin family 1")
t:eq(prim.extras.skinRef, x.skinRef, lab .. "skin reference")
local names = mesh.extras.targetNames
local want = 0
for d, verts in x.targets do
local slot = table.find(names, e.flexdescs[tonumber(d) :: number + 1])
t:check(slot ~= nil, lab .. "flex target " .. d)
if slot then
local pos, nrm = floats(prim.targets[slot].POSITION), floats(prim.targets[slot].NORMAL)
local ok = true
for v, dd in verts do
local vi = tonumber(v) :: number
for cc = 1, 3 do
ok = ok and math.abs(pos[vi * 3 + cc] - dd[cc]) < 1e-6 and math.abs(nrm[vi * 3 + cc] - dd[cc + 3]) < 1e-6
end
want += 1
end
t:check(ok, lab .. "flex " .. names[slot] .. " deltas")
end
end
if names then
t:eq(#prim.targets, #names, lab .. "a target per flex")
end
end
end
end
t:eq(s.nodes[s.roots[1]].name, src.name, tag .. "root node")
-- attachments
for i, a in e.attachments do
local at = src.attachments[i]
local node = s.nodes[at.node]
t:eq(node.name, a.name, tag .. "attachment " .. i .. " name")
t:check(table.find(s.nodes[skin.joints[a.bone + 1]].children, at.node) ~= nil, tag .. "attachment " .. i .. " parent bone")
local m = Mat4.compose(node.translation, node.rotation, node.scale)
t:check(near(m, Mat4.fromRows(a.matrix), 1e-5), tag .. "attachment " .. i .. " matrix")
end
t:eq(#src.hitboxSets[1].boxes, 2, tag .. "hitboxes")
t:eq(src.hitboxSets[1].boxes[2].name, "head_box", tag .. "hitbox name")
-- animations
for _, a in e.anims do
local anim
for _, x in s.animations do
if x.name == a.name then
anim = x
end
end
t:check(anim, tag .. "animation " .. a.name)
if not anim then
continue
end
for bone, want in a.bones do
local node = skin.joints[tonumber(bone) :: number]
local tr, rot
for _, ch in anim.channels do
if ch.node == node then
if ch.path == "translation" then
tr = ch
elseif ch.path == "rotation" then
rot = ch
end
end
end
t:check(tr and rot, tag .. a.name .. " bone " .. bone .. ": channels")
if tr and rot then
t:eq(tr.input.count, a.frames, tag .. a.name .. ": frames")
t:check(math.abs(floats(tr.input)[2] - 1 / a.fps) < 1e-6, tag .. a.name .. ": fps")
t:check(near(floats(tr.output), want.pos, 1e-4), tag .. a.name .. " bone " .. bone .. ": positions per frame")
t:check(near(floats(rot.output), want.rot, 1e-5), tag .. a.name .. " bone " .. bone .. ": rotations per frame")
end
end
end
if c.version == 49 then
t:check(c.files["model.ani"] ~= nil, tag .. "an .ani block")
end
-- physics
local soup = {}
for _, sol in src.physics.solids do
for _, h in sol.hulls do
for _, v in h.indices do
for cc = 1, 3 do
table.insert(soup, h.positions[v * 3 + cc])
end
end
end
end
t:check(near(soup, e.physics, 1e-3), tag .. "phy convex hulls")
-- options
local s2 = decode(c, { bodies = { 2, 2 }, lod = 2 }) :: any
local names = {}
for _, ni in s2.nodes[s2.roots[1]].children do
if s2.nodes[ni].mesh then
table.insert(names, s2.meshes[s2.nodes[ni].mesh].name)
end
end
t:eq(table.concat(names, ","), "body_lo,hat", tag .. "opts.bodies")
t:check(near(floats(s2.meshes[1].primitives[1].indices), e.parts[1].models[1].meshes[1].lods[2], 0), tag .. "opts.lod")
local s3 = SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl))) :: any
t:check(s3 and #s3.meshes == 0 and #s3.meta.source.warnings > 0 and #s3.skins == 1, tag .. "no siblings: skeleton only, with warnings")
local s4 = decode(c, { name = "renamed.mdl", files = { ["renamed.vvd"] = c.files["model.vvd"], ["renamed.sw.vtx"] = c.files["model.dx90.vtx"] } }) :: any
t:check(s4 and #s4.meshes == 3 and s4.meta.source.vtx == "renamed.sw.vtx", tag .. "siblings named after opts.name")
-- fuzz: the mdl, and each sibling file
local samples = { c.mdl }
t:fuzz(tag .. "mdl", function(b: buffer)
return SourceMdl.decode(Bytes.reader(b), { files = c.files })
end, samples, { mutations = 40 })
for _, name in { "model.vvd", "model.dx90.vtx", "model.phy" } do
t:fuzz(tag .. name, function(b: buffer)
local files = table.clone(c.files)
files[name] = buffer.tostring(b)
return SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl)), { files = files })
end, { c.files[name] }, { mutations = 30, garbage = 20 })
end
end
-- errors
local bad = buffer.fromstring(Fx.cases[1].mdl)
buffer.writei32(bad, 4, 52)
local _, e1 = SourceMdl.decode(Bytes.reader(bad))
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 52: UNSUPPORTED")
local _, e2 = SourceMdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.cases[1].mdl, 1, 100))))
t:check(e2 and (e2.code == Err.TRUNCATED or e2.code == Err.CORRUPT), "truncated header")
local s5 = SourceMdl.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].mdl)), { files = { ["model.vvd"] = "IDSV" } })
t:check(s5 == nil, "a broken .vvd is an error")
t:eq(SourceMdl.sniff(Bytes.reader(buffer.fromstring(Fx.cases[1].mdl))), 1, "sniff")
t:eq(SourceMdl.sniff(Bytes.reader(buffer.fromstring("IDST\10\0\0\0"))), 0, "sniff: GoldSrc is model/mdl")
t:done()