Files

105 lines
4.5 KiB
Luau

-- codecs/model/SourceAnim tests: run-length anim values (repeat of value[valid], valid = 0), Quaternion48 / 64,
-- Vector48 and a bone record chain, against values built here from the studio.h bit layouts.
local T = require("@tests/T")
local SourceAnim = require("@rosetta/codecs/model/SourceAnim")
local Half = require("@rosetta/codecs/image/Half")
local t = T.suite("sourceanim")
local function shorts(list: { number }): buffer
local b = buffer.create(#list * 2)
for i, v in list do
buffer.writei16(b, (i - 1) * 2, v)
end
return b
end
-- runs: { valid 2, total 4 } 10 20 (then 20 20); { valid 1, total 2 } -5 (then -5)
local runs = buffer.create(12)
buffer.writeu8(runs, 0, 2)
buffer.writeu8(runs, 1, 4)
buffer.writei16(runs, 2, 10)
buffer.writei16(runs, 4, 20)
buffer.writeu8(runs, 6, 1)
buffer.writeu8(runs, 7, 2)
buffer.writei16(runs, 8, -5)
local v = SourceAnim.values(runs, 0, 10, 6)
t:eq(table.concat(v :: { number }, ","), "10,20,20,20,-5,-5", "values: runs and repeats")
for k = 0, 5 do
t:eq(SourceAnim.value(runs, 0, 10, k), (v :: { number })[k + 1], "value " .. k)
end
t:eq(SourceAnim.values(runs, 0, 10, 7), nil, "values past the data")
t:eq(SourceAnim.value(runs, 0, 10, 6), nil, "value past the data")
local zero = shorts({ 0x0300 })
t:eq(table.concat(SourceAnim.values(zero, 0, 2, 3) :: { number }, ","), "768,768,768", "valid = 0 repeats the run header (as the engine)")
-- Quaternion48: x = 0.5, y = -0.25, z = 0.125, w < 0
local q48 = buffer.create(6)
buffer.writeu16(q48, 0, 32768 + 16384)
buffer.writeu16(q48, 2, 32768 - 8192)
buffer.writeu16(q48, 4, (16384 + 2048) + 32768)
local x, y, z, w = SourceAnim.quat48(q48, 0)
t:check(x == 0.5 and y == -0.25 and z == 0.125 and math.abs(w + math.sqrt(1 - 0.25 - 0.0625 - 0.015625)) < 1e-12, "quat48")
-- Quaternion64: 21-bit fields
local xs, ys, zs = 1048576 + 524288, 1048576 - 262144, 1048576 + 1
local lo = xs + (ys % 2048) * 2097152
local hi = ys // 2048 + zs * 1024
local q64 = buffer.create(8)
buffer.writeu32(q64, 0, lo)
buffer.writeu32(q64, 4, hi)
x, y, z, w = SourceAnim.quat64(q64, 0)
t:check(math.abs(x - 524288 / 1048576.5) < 1e-12 and math.abs(y + 262144 / 1048576.5) < 1e-12 and math.abs(z - 1 / 1048576.5) < 1e-12 and w > 0, "quat64")
buffer.writeu32(q64, 4, hi + 0x80000000)
local _, _, _, w2 = SourceAnim.quat64(q64, 0)
t:check(w2 < 0 and math.abs(w2 + w) < 1e-12, "quat64 w sign")
local v48 = buffer.create(6)
buffer.writeu16(v48, 0, Half.fromFloat(1.5))
buffer.writeu16(v48, 2, Half.fromFloat(-2))
buffer.writeu16(v48, 4, Half.fromFloat(0.25))
local a, b, c = SourceAnim.vec48(v48, 0)
t:check(a == 1.5 and b == -2 and c == 0.25, "vec48")
-- a chain: bone 1 animated rotation x + raw position; bone 0 raw rotation (delta flag off)
local bones = {
{ pos = { 1, 2, 3 }, quat = { 0, 0, 0, 1 }, rot = { 0, 0, 0 }, posScale = { 1, 1, 1 }, rotScale = { 1, 1, 1 } },
{ pos = { 4, 5, 6 }, quat = { 0, 0, 0, 1 }, rot = { 0.1, 0.2, 0.3 }, posScale = { 0.5, 0.5, 0.5 }, rotScale = { 0.01, 0.01, 0.01 } },
}
local chain = buffer.create(64)
-- record 0: bone 1, ANIMROT | RAWPOS, next = 4 + 6 + 6 + 6
buffer.writeu8(chain, 0, 1)
buffer.writeu8(chain, 1, 0x08 + 0x01)
buffer.writei16(chain, 2, 22)
buffer.writei16(chain, 4, 12) -- x values at 4 + 12 (after the valueptr and Vector48)
buffer.writeu16(chain, 10, Half.fromFloat(7))
buffer.writeu16(chain, 12, Half.fromFloat(8))
buffer.writeu16(chain, 14, Half.fromFloat(9))
buffer.writeu8(chain, 16, 2)
buffer.writeu8(chain, 17, 2)
buffer.writei16(chain, 18, 10)
buffer.writei16(chain, 20, -10)
-- record 1 at 22: bone 0, RAWROT, last
buffer.writeu8(chain, 22, 0)
buffer.writeu8(chain, 23, 0x02)
buffer.writei16(chain, 24, 0)
buffer.writeu16(chain, 26, 32768)
buffer.writeu16(chain, 28, 32768)
buffer.writeu16(chain, 30, 16384)
local tracks, err = SourceAnim.decode(chain, 0, 32, bones, 2)
t:check(tracks, "decode: " .. tostring(err))
if tracks then
local t1, t0 = tracks[2], tracks[1]
t:eq(table.concat(t1.pos, ","), "7,8,9,7,8,9", "raw position")
local qx, qy, qz, qw = SourceAnim.angles(0.1 - 0.1, 0.2, 0.3)
t:check(math.abs(t1.rot[5] - qx) < 1e-12 and math.abs(t1.rot[6] - qy) < 1e-12 and math.abs(t1.rot[7] - qz) < 1e-12 and math.abs(t1.rot[8] - qw) < 1e-12, "animated rotation, frame 1")
t:eq(table.concat(t0.rot, ","), "0,0,0,1,0,0,0,1", "raw rotation")
t:eq(table.concat(t0.pos, ","), "1,2,3,1,2,3", "rest position without position data")
end
t:eq(SourceAnim.decode(chain, 0, 20, bones, 2), nil, "truncated chain")
local junk = { buffer.tostring(chain), buffer.tostring(runs) }
t:fuzz("decode", function(bb: buffer)
return SourceAnim.decode(bb, 0, buffer.len(bb), bones, 4)
end, junk)
t:done()