Files
rosetta/tests/codecs/rig.luau
T

94 lines
4.0 KiB
Luau

-- 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()