mrfixit.iqm matches assimp and an independent Python IQM reader (its skinning reproduces the bounds the IQM tool stored; ours matches it to 1e-4); round trips and glTF skinned models written by us are skinned identically by the Python reader.
93 lines
4.8 KiB
Luau
93 lines
4.8 KiB
Luau
-- codecs/model/Sampler tests: STEP / LINEAR / CUBICSPLINE channels (glTF's Hermite definition), quaternion slerp
|
|
-- (shortest path, unit length), clamping outside the keys, weights channels, Sampler.times and Sampler.pose; glTF's
|
|
-- InterpolationTest sample model (every interpolation) sampled at its keys gives the key values; bad channels.
|
|
local T = require("@tests/T")
|
|
local Sampler = require("@formats/codecs/model/Sampler")
|
|
local Gltf = require("@formats/model/gltf")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Picks = require("@tests/gen/iqm_gltf")
|
|
|
|
local t = T.suite("codecs/sampler")
|
|
local S = Types.Scene
|
|
|
|
local function acc(list: { number }, comps: number): Types.Accessor
|
|
local b = buffer.create(#list * 4)
|
|
for i, v in list do
|
|
buffer.writef32(b, (i - 1) * 4, v)
|
|
end
|
|
return S.accessor(b, "f32", comps, #list // comps)
|
|
end
|
|
local function near(a: number, b: number, eps: number?): boolean
|
|
return math.abs(a - b) < (eps or 1e-5)
|
|
end
|
|
|
|
do
|
|
local ch: Types.Channel = { node = 1, path = "translation", input = acc({ 0, 1, 3 }, 1), output = acc({ 0, 0, 0, 2, 4, 6, 2, 0, 0 }, 3), interpolation = "LINEAR" }
|
|
local s = assert(Sampler.channel(ch))
|
|
t:check(near(s(0.5)[1], 1) and near(s(0.5)[2], 2), "linear: halfway")
|
|
t:check(near(s(2)[1], 2) and near(s(2)[2], 2), "linear: second span")
|
|
t:check(near(s(-1)[2], 0) and near(s(9)[1], 2), "linear: clamped outside the keys")
|
|
ch.interpolation = "STEP"
|
|
local st = assert(Sampler.channel(ch))
|
|
t:check(near(st(0.99)[1], 0) and near(st(1)[1], 2), "step")
|
|
end
|
|
do
|
|
-- rotation: 0 -> 90 degrees about z, and a key stored with the opposite sign (shortest path)
|
|
local h = math.sqrt(0.5)
|
|
local ch: Types.Channel = { node = 1, path = "rotation", input = acc({ 0, 1, 2 }, 1), output = acc({ 0, 0, 0, 1, 0, 0, h, h, 0, 0, -h, -h }, 4), interpolation = "LINEAR" }
|
|
local s = assert(Sampler.channel(ch))
|
|
local q = s(0.5)
|
|
local a = 2 * math.acos(q[4])
|
|
t:check(near(a, math.pi / 4, 1e-4) and near(q[1] ^ 2 + q[2] ^ 2 + q[3] ^ 2 + q[4] ^ 2, 1), "slerp: 45 degrees, unit length")
|
|
local q2 = s(1.5)
|
|
t:check(near(math.abs(q2[3]), h, 1e-4), "slerp: the same orientation stored negated stays put")
|
|
end
|
|
do
|
|
-- cubic spline: in-tangent, value, out-tangent per key; p(t) for p0 = 0, p1 = 1, m0 = m1 = 0 is smoothstep
|
|
local ch: Types.Channel = { node = 1, path = "scale", input = acc({ 0, 2 }, 1), output = acc({ 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 9, 9, 9 }, 3), interpolation = "CUBICSPLINE" }
|
|
local s = assert(Sampler.channel(ch))
|
|
t:check(near(s(1)[1], 0.5) and near(s(0.5)[1], 3 * 0.25 ^ 2 - 2 * 0.25 ^ 3), "cubic: smoothstep")
|
|
t:check(near(s(2)[2], 1), "cubic: the last value")
|
|
-- tangents scale with the key spacing
|
|
local ch2: Types.Channel = { node = 1, path = "translation", input = acc({ 0, 2 }, 1), output = acc({ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 3), interpolation = "CUBICSPLINE" }
|
|
local s2 = assert(Sampler.channel(ch2))
|
|
-- h10(0.5) * dt * m0 = 0.125 * 2 * 1
|
|
t:check(near(s2(1)[1], 0.25), "cubic: tangents times the key spacing")
|
|
end
|
|
do
|
|
local ch: Types.Channel = { node = 2, path = "weights", input = acc({ 0, 1 }, 1), output = acc({ 1, 0, 0, 1 }, 1), interpolation = "LINEAR" }
|
|
local s = assert(Sampler.channel(ch))
|
|
local w = s(0.25)
|
|
t:check(#w == 2 and near(w[1], 0.75) and near(w[2], 0.25), "weights")
|
|
local anim: Types.Animation = { name = "a", channels = { ch, { node = 1, path = "translation", input = acc({ 0.5, 1 }, 1), output = acc({ 0, 0, 0, 1, 1, 1 }, 3), interpolation = "STEP" } } }
|
|
local times = Sampler.times(anim)
|
|
t:eq(table.concat(times, " "), "0 0.5 1", "times: sorted, unique")
|
|
local pose = Sampler.pose(S.new(), anim, 1)
|
|
t:check(pose[2].weights and pose[1].translation and near(pose[1].translation[1], 1), "pose")
|
|
t:check(Sampler.channel({ node = 1, path = "translation", input = acc({ 0, 1 }, 1), output = acc({ 1, 2 }, 1), interpolation = "LINEAR" }) == nil, "short output: nil")
|
|
t:check(Sampler.channel({ node = 1, path = "translation", input = {} :: any, output = {} :: any, interpolation = "LINEAR" }) == nil, "junk: nil")
|
|
end
|
|
do
|
|
-- a real file: sampling a glTF sample model's skin animation at its own keys returns the key values
|
|
local g = assert(Gltf.decode(Bytes.reader(buffer.fromstring(Picks[1].data)), { files = Picks[1].files }))
|
|
local ok, total = true, 0
|
|
for _, anim in g.animations do
|
|
for _, ch in anim.channels do
|
|
local s = assert(Sampler.channel(ch))
|
|
local IN, OUT = assert(S.reader(ch.input)), assert(S.reader(ch.output))
|
|
local comps = if ch.path == "rotation" then 4 else 3
|
|
for k = 0, ch.input.count - 1 do
|
|
local v = s(IN(k, 0))
|
|
for c = 1, comps do
|
|
ok = ok and near(v[c], OUT(k, c - 1), 1e-4)
|
|
end
|
|
total += 1
|
|
end
|
|
end
|
|
end
|
|
t:check(ok and total > 0, Picks[1].name .. ": samples at the keys are the keys (" .. total .. ")")
|
|
end
|
|
|
|
t:done()
|