merge ws/game af4488d (codecs/model/Rig) for the IQM / X skins and animations

This commit is contained in:
4DBug committed 2026-10-31 14:05:51 -05:00
commit 29be8113b4
9 files changed
+667

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
-- archive/pak tests: Quake ("PACK") and SiN ("SPAK") archives written by tools/fixtures/pak.py list the entries an
-- independent Python reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer
-- for the same files and round-trips; conversion from a tar; errors; the fuzz pass. Fixtures: tools/fixtures/pak.py.
local T = require("@tests/T")
local F = require("@formats")
local Pak = require("@formats/archive/pak")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/pak")
local t = T.suite("pak")
t:eq(Pak.name, "pak", "name")
t:check(Pak.caps.decode and Pak.caps.encode, "caps")
for _, c in Fx do
local a, err = Pak.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
local list = a:list()
t:eq(#list, #c.entries, c.name .. ": entry count")
for i, w in c.entries do
t:eq(list[i].name, w.name, c.name .. ": name " .. i)
t:eq(list[i].size, w.size, c.name .. ": size " .. w.name)
t:eq(Crc32.of((a:read(w.name))), w.crc, c.name .. ": data " .. w.name)
end
t:eq(a.meta.source.variant, c.variant, c.name .. ": variant")
if #c.data > 12 then
t:eq(F.detect(c.data), "pak", c.name .. ": F.detect")
end
-- the writer: same layout as the Python writer
local n = Pak.new(c.variant)
for _, f in c.files do
n:add(f.name, f.data)
end
local out = n:write() :: buffer
if c.layoutDefault then
t:check(buffer.tostring(out) == c.data, c.name .. ": bytes identical to the Python writer")
end
local back = Pak.decode(Bytes.reader(out))
t:eq(#back:list(), #c.files, c.name .. ": round trip count")
-- rewriting a decoded archive keeps its variant
local again = F.save(a) :: buffer
t:eq(buffer.readstring(again, 0, 4), if c.variant == "sin" then "SPAK" else "PACK", c.name .. ": F.save keeps the variant")
end
-- a sub-range reader, conversion from another archive, errors
do
local c = Fx[1]
local big = buffer.create(#c.data + 7)
buffer.writestring(big, 7, c.data)
local a = Pak.decode(Bytes.reader(big, 7, #c.data))
t:check(a and Crc32.of((a:read(c.entries[2].name))) == c.entries[2].crc, "decode from a sub-range")
local tar = F.format("tar").new()
tar:add("dir/", "", { dir = true })
tar:add("dir/x.txt", "x")
local p = F.load(F.save(tar, nil, { format = "pak" }) :: buffer)
t:check(p.format == "pak" and #p:list() == 1 and buffer.tostring(p:read("dir/x.txt")) == "x", "tar -> pak (directories dropped)")
local _, e1 = Pak.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 10))))
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
local bad = buffer.fromstring(c.data)
buffer.writeu32(bad, 8, 65)
local _, e2 = Pak.decode(Bytes.reader(bad))
t:check(e2 and e2.code == Err.CORRUPT, "directory size: " .. tostring(e2))
local long = Pak.new()
long:add(string.rep("n", 56), "x")
local _, e3 = long:write()
t:check(e3 and e3.code == Err.ARG, "a 56-byte name does not fit")
local _, e4 = Pak.encode({ kind = "image" })
t:check(e4 and e4.code == Err.ARG, "encode a non-archive")
end
local samples = {}
for _, c in Fx do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
local a = Pak.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
a:read(e.name)
end
a:write()
end
return a
end, samples, { mutations = 40 })
t:done()
+93
View File
@@ -0,0 +1,93 @@
-- codecs/model/Rig tests: morph targets and weights animations from frames, skeletons in local and world space
-- (node TRS, inverse bind matrices against Mat4), influences (top four, normalised), joint TRS animations; and
-- Pixels.palette.
local T = require("@tests/T")
local Rig = require("@formats/codecs/model/Rig")
local Mat4 = require("@formats/codecs/model/Mat4")
local Pixels = require("@formats/codecs/image/Pixels")
local Types = require("@formats/Types")
local t = T.suite("codecs/rig")
local Scene = Types.Scene
local function f32s(list: { number }): buffer
local b = buffer.create(#list * 4)
for i, v in list do
buffer.writef32(b, (i - 1) * 4, v)
end
return b
end
local function near(a: number, b: number): boolean
return math.abs(a - b) < 1e-5
end
-- morph
do
local base = { 0, 0, 0, 1, 0, 0 }
local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(f32s(base), "f32", 3, 2) }, mode = "points" }
local frames = { f32s(base), f32s({ 0, 1, 0, 1, 2, 0 }), f32s({ 0, 0, 3, 1, 0, 3 }) }
t:check(Rig.morph(prim, frames), "morph")
local tg = prim.targets :: any
t:eq(#tg, 3, "one target per frame")
t:eq(buffer.readf32(tg[2].POSITION.data, 4), 1, "delta y")
t:eq(buffer.readf32(tg[3].POSITION.data, 20), 3, "delta z")
t:check(not Rig.morph(prim, { buffer.create(4) }), "short frame refused")
local s = Scene.new()
s.nodes[1] = { children = {}, mesh = 1 }
Rig.morphAnimation(s, 1, 3, { { name = "a", first = 2, last = 3, fps = 5 }, { name = "none", first = 9, last = 12 } })
t:eq(#s.animations, 1, "out-of-range range skipped")
local ch = s.animations[1].channels[1]
t:eq(ch.path, "weights", "weights channel")
t:eq(ch.input.count, 2, "two keys")
t:check(near(buffer.readf32(ch.input.data, 4), 0.2), "fps")
t:eq(buffer.readf32(ch.output.data, 1 * 4), 1, "key 1 -> target 2")
t:eq(buffer.readf32(ch.output.data, (3 + 2) * 4), 1, "key 2 -> target 3")
end
-- skeleton
do
local q = Mat4.axisAngle(0, 0, 1, 90)
local joints = {
{ name = "root", parent = 0, translation = { 1, 0, 0 }, rotation = q },
{ name = "child", parent = 1, translation = { 0, 2, 0 }, rotation = { 0, 0, 0, 1 } },
}
local s = Scene.new()
local nodes, skin = Rig.skeleton(s, joints, "local")
t:eq(#nodes, 2, "two joint nodes")
t:eq(s.roots[1], nodes[1], "root")
t:eq(s.nodes[nodes[1]].children[1], nodes[2], "parenting")
local ibm = s.skins[skin].inverseBindMatrices :: Types.Accessor
-- child world = T(1,0,0) R90 T(0,2,0): its origin is at (-1, 0, 0); the inverse maps it back to 0
local inv = {}
for k = 1, 16 do
inv[k] = buffer.readf32(ibm.data, (16 + k - 1) * 4)
end
local x, y, z = Mat4.point(inv, -1, 0, 0)
t:check(near(x, 0) and near(y, 0) and near(z, 0), "inverse bind matrix")
-- the same joints given in world space give the same local TRS
local w = Scene.new()
local wn = Rig.skeleton(w, {
joints[1],
{ name = "child", parent = 1, translation = { -1, 0, 0 }, rotation = q },
}, "world")
local c = w.nodes[wn[2]]
t:check(near(c.translation[1], 0) and near(c.translation[2], 2) and near(c.rotation[4], 1), "world -> local")
local j, wt = Rig.influences({ { 0, 1 }, { 1, 0.2, 2, 0.6, 3, 0.1, 4, 0.05, 5, 0.05 } })
t:eq(buffer.readu16(j.data, 8), 2, "largest weight first")
t:check(near(buffer.readf32(wt.data, 16) + buffer.readf32(wt.data, 20) + buffer.readf32(wt.data, 24) + buffer.readf32(wt.data, 28), 1), "normalised")
t:eq(buffer.readf32(wt.data, 0), 1, "single influence")
local ai = Rig.animation(s, "wave", nodes, { 0, 0.5 }, { [2] = { rotation = { 0, 0, 0, 1, 0, 0, 1, 0 }, translation = { 0 } } })
t:eq(#s.animations[ai].channels, 1, "short track skipped, rotation kept")
t:eq(s.animations[ai].channels[1].node, nodes[2], "channel node")
end
-- palette
do
local pal = Pixels.palette(buffer.fromstring("\1\2\3\4\5\6"), 0, 2)
t:eq(buffer.readu32(pal, 4), 0xFF060504, "rgb triples")
t:eq(buffer.readu32(pal, 8), 0xFF000000, "missing entries black")
t:eq(buffer.readu32(Pixels.palette(buffer.fromstring("\1\2\3"), 0, 1, true), 0), 0xFF010203, "bgr")
t:eq(buffer.readu32(Pixels.palette(buffer.create(3), -5, 1), 0), 0xFF000000, "bad base")
end
t:done()