merge ws/3d3-b (model/lwo, model/x, model/iqm, codecs/model/Sampler; brings ws/game af4488d for codecs/model/Rig)
This commit is contained in:
commit
44cda7256c
29 files changed
+5664
-4
No files matched your search
@@ -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()
|
||||
@@ -0,0 +1,92 @@
|
||||
-- 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()
|
||||
Reference in new issue
Block a user