ws-game: model/md5 (Doom 3 md5mesh + md5anim: skeleton, weighted bind pose, skin, animations via opts.anims / resolve / files; standalone md5anim)
This commit is contained in:
1 parent
08045ba813
commit
9b2b50fe75
6 files changed
+847
No files matched your search
@@ -0,0 +1,153 @@
|
||||
-- model/md5 tests: an .md5mesh and two .md5anim files written by tools/fixtures/md5.py: the bind pose against
|
||||
-- assimp's triangle soup (positions, texture coordinates) and the writer's exact weighted positions, the joints'
|
||||
-- model-space pose through the node tree, weights, animations through opts.files / opts.resolve (every frame's
|
||||
-- parent-relative pose), a standalone .md5anim, missing animations, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Md5 = require("@formats/model/md5")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/md5")
|
||||
|
||||
local t = T.suite("md5")
|
||||
t:eq(Md5.name, "md5", "name")
|
||||
|
||||
local function near(a: { number }, b: { number }, tol: number): boolean
|
||||
for i = 1, #b do
|
||||
if math.abs(a[i] - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
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 M = Fx.mesh
|
||||
local files = {}
|
||||
for _, a in Fx.anims do
|
||||
files[a.name .. ".md5anim"] = a.data
|
||||
end
|
||||
local s, err = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "idle.md5anim", "walk.md5anim" }, files = files })
|
||||
t:check(s, "md5mesh: " .. tostring(err))
|
||||
t:eq(F.detect(M.data), "md5", "F.detect")
|
||||
|
||||
-- 1. geometry
|
||||
do
|
||||
local prims = s.meshes[1].primitives
|
||||
t:eq(#prims, #M.positions, "a primitive per mesh")
|
||||
for i, p in prims do
|
||||
local pos = Soup.list(p.attributes.POSITION.data)
|
||||
t:check(near(pos, M.positions[i], 1e-5), "mesh " .. i .. ": weighted positions exact")
|
||||
t:eq(p.indices.count, M.tris[i] * 3, "mesh " .. i .. ": triangles")
|
||||
t:check(near(Soup.list(p.attributes.TEXCOORD_0.data), M.uvs[i], 1e-6), "mesh " .. i .. ": texture coordinates")
|
||||
t:eq(s.textures[s.materials[p.material].baseColorTexture.texture].uri, "models/test/skin" .. (i - 1), "mesh " .. i .. ": shader")
|
||||
end
|
||||
local matched = 0
|
||||
for mi, m in M.assimp.per do
|
||||
local nextStart = if M.assimp.per[mi + 1] then M.assimp.per[mi + 1].start else M.assimp.faces
|
||||
local nf = nextStart - m.start
|
||||
local apos = Soup.slice(M.assimp.pos, m.start * 9 + 1, nf * 9)
|
||||
local auv = Soup.slice(M.assimp.uv, m.start * 6 + 1, nf * 6)
|
||||
for _, p in prims do
|
||||
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-4) then
|
||||
t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), "texture coordinates against assimp")
|
||||
matched += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(matched, #M.positions, "every mesh matches assimp's bind pose")
|
||||
end
|
||||
|
||||
-- 2. skeleton: the model-space joint poses through the node tree
|
||||
do
|
||||
local skin = s.skins[1]
|
||||
t:eq(#skin.joints, #M.joints, "joints")
|
||||
local parent = {}
|
||||
for ni, nd in s.nodes do
|
||||
for _, c in nd.children do
|
||||
parent[c] = ni
|
||||
end
|
||||
end
|
||||
local function world(ni: number): { number }
|
||||
local m = Types.Scene.nodeMatrix(s.nodes[ni])
|
||||
local p = parent[ni]
|
||||
return if p then Types.Scene.mul(world(p), m) else m
|
||||
end
|
||||
for i, j in M.joints do
|
||||
local nd = skin.joints[i]
|
||||
t:eq(s.nodes[nd].name, j.name, "joint name " .. i)
|
||||
local w = world(nd)
|
||||
t:check(near({ w[13], w[14], w[15] }, j.pos, 1e-4), "joint " .. j.name .. " model-space position")
|
||||
end
|
||||
t:check(s.meshes[1].primitives[1].attributes.JOINTS_0 ~= nil, "skinned")
|
||||
end
|
||||
|
||||
-- 3. animations
|
||||
local function checkAnim(scene: any, a: any, anim: any, label: string)
|
||||
t:eq(anim.name, a.name, label .. ": name")
|
||||
local byNode = {}
|
||||
for _, ch in anim.channels do
|
||||
byNode[ch.node .. ch.path] = ch
|
||||
end
|
||||
local skin = scene.skins[1]
|
||||
local ok = true
|
||||
for f, pose in a.poses do
|
||||
for ji, jp in pose do
|
||||
local nd = skin.joints[ji]
|
||||
local tr, rot = byNode[nd .. "translation"], byNode[nd .. "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, jp.t, 1e-5) and sameQ(qv, jp.q)
|
||||
end
|
||||
end
|
||||
t:check(ok, label .. ": every frame's pose")
|
||||
t:eq(anim.channels[1].input.count, a.frames, label .. ": keys")
|
||||
t:check(math.abs(buffer.readf32(anim.channels[1].input.data, 4) - 1 / a.rate) < 1e-6, label .. ": frame rate")
|
||||
end
|
||||
t:eq(#s.animations, 2, "two animations")
|
||||
for i, a in Fx.anims do
|
||||
checkAnim(s, a, s.animations[i], "mesh + " .. a.name)
|
||||
end
|
||||
do
|
||||
local asked = {}
|
||||
local r = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), {
|
||||
anims = { "walk.md5anim" },
|
||||
resolve = function(name: string): string?
|
||||
table.insert(asked, name)
|
||||
return files[name]
|
||||
end,
|
||||
}) :: any
|
||||
t:check(r and #r.animations == 1 and asked[1] == "walk.md5anim", "opts.resolve")
|
||||
local _, e = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "run.md5anim" } })
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "missing animation")
|
||||
local skip = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "run.md5anim" }, skipMissing = true }) :: any
|
||||
t:check(skip and #skip.animations == 0, "skipMissing")
|
||||
-- a standalone .md5anim
|
||||
local a = Fx.anims[2]
|
||||
local alone = Md5.decode(Bytes.reader(buffer.fromstring(a.data)), { name = "models/walk.md5anim" }) :: any
|
||||
t:check(alone and #alone.skins == 1, "md5anim alone: skeleton")
|
||||
checkAnim(alone, a, alone.animations[1], "md5anim alone")
|
||||
t:eq(alone.meta.source.kind, "anim", "md5anim alone: kind")
|
||||
end
|
||||
|
||||
-- 4. errors, fuzz
|
||||
do
|
||||
local _, e1 = Md5.decode(Bytes.reader(buffer.fromstring("MD5Version 11\n")))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 11")
|
||||
local _, e2 = Md5.decode(Bytes.reader(buffer.fromstring(string.sub(M.data, 1, 900))))
|
||||
t:check(e2 ~= nil, "truncated: " .. tostring(e2))
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Md5.decode(Bytes.reader(b), { anims = { "idle.md5anim" }, files = files })
|
||||
end, { M.data, Fx.anims[1].data }, { mutations = 80 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user