--!strict -- codecs/model/Sampler: evaluates Scene animation channels at any time (STEP, LINEAR with quaternion slerp for -- rotations, CUBICSPLINE Hermite splines as glTF 2.0 defines them; times before the first / after the last key hold -- the end values), and the node poses of an animation at a time, for writers of formats that store sampled frames -- (IQM, MD5, BVH, ...). New in formats-luau. -- -- local sample = Sampler.channel(ch) -- nil for an unreadable channel -- local v = sample(0.5) -- { x, y, z } / { x, y, z, w } / the morph weights -- local times = Sampler.times(anim) -- every key time of the animation, sorted, unique -- local pose = Sampler.pose(scene, anim, 0.5) -- pose[node] = { translation?, rotation?, scale?, weights? } -- -- Never throws on bad data: unreadable channels are skipped. local Types = require("@formats/Types") local Sampler = {} export type Sample = (t: number) -> { number } export type Pose = { [number]: { [string]: { number } } } local function normalize(q: { number }): { number } local l = math.sqrt(q[1] * q[1] + q[2] * q[2] + q[3] * q[3] + q[4] * q[4]) if l <= 0 then return { 0, 0, 0, 1 } end return { q[1] / l, q[2] / l, q[3] / l, q[4] / l } end local function slerp(a: { number }, b: { number }, s: number): { number } local d = a[1] * b[1] + a[2] * b[2] + a[3] * b[3] + a[4] * b[4] local bx, by, bz, bw = b[1], b[2], b[3], b[4] if d < 0 then d, bx, by, bz, bw = -d, -bx, -by, -bz, -bw end local wa, wb if d > 0.9995 then wa, wb = 1 - s, s else local th = math.acos(math.min(d, 1)) local sn = math.sin(th) wa, wb = math.sin((1 - s) * th) / sn, math.sin(s * th) / sn end return normalize({ a[1] * wa + bx * wb, a[2] * wa + by * wb, a[3] * wa + bz * wb, a[4] * wa + bw * wb }) end -- channel(ch) -> sample(t) | nil function Sampler.channel(ch: Types.Channel): Sample? local inp, out = ch.input, ch.output if type(inp) ~= "table" or type(out) ~= "table" then return nil end local IN, OUT = Types.Scene.reader(inp), Types.Scene.reader(out) local n = inp.count if not IN or not OUT or n < 1 then return nil end local cubic = ch.interpolation == "CUBICSPLINE" local per = if cubic then 3 else 1 local comps: number if ch.path == "weights" then comps = (out.count * out.components) // (n * per) else comps = if ch.path == "rotation" then 4 else 3 end if comps < 1 or out.count * out.components < n * per * comps then return nil end local times = table.create(n) :: { number } for k = 1, n do times[k] = IN(k - 1, 0) end local oc = out.components -- element e (0-based, of comps values) of the flat output local function get(e: number): { number } local v = table.create(comps) :: { number } for c = 0, comps - 1 do local flat = e * comps + c v[c + 1] = OUT(flat // oc, flat % oc) end return v end local function value(k: number): { number } -- key k (1-based) return get(if cubic then (k - 1) * 3 + 1 else k - 1) end local rot = ch.path == "rotation" local step = ch.interpolation == "STEP" return function(t: number): { number } if t <= times[1] or n == 1 then return if rot then normalize(value(1)) else value(1) end if t >= times[n] then return if rot then normalize(value(n)) else value(n) end local lo, hi = 1, n while hi - lo > 1 do local mid = (lo + hi) // 2 if times[mid] <= t then lo = mid else hi = mid end end local dt = times[hi] - times[lo] local s = if dt > 0 then (t - times[lo]) / dt else 0 if step then return if rot then normalize(value(lo)) else value(lo) end if cubic then local p0, m0 = value(lo), get((lo - 1) * 3 + 2) local p1, m1 = value(hi), get((hi - 1) * 3) local s2, s3 = s * s, s * s * s local h00, h10, h01, h11 = 2 * s3 - 3 * s2 + 1, s3 - 2 * s2 + s, -2 * s3 + 3 * s2, s3 - s2 local v = table.create(comps) :: { number } for c = 1, comps do v[c] = h00 * p0[c] + h10 * dt * m0[c] + h01 * p1[c] + h11 * dt * m1[c] end return if rot then normalize(v) else v end local a, b = value(lo), value(hi) if rot then return slerp(normalize(a), normalize(b), s) end local v = table.create(comps) :: { number } for c = 1, comps do v[c] = a[c] + (b[c] - a[c]) * s end return v end end -- times(anim) -> every key time, sorted and unique function Sampler.times(anim: Types.Animation): { number } local seen: { [number]: boolean } = {} local out: { number } = {} for _, ch in anim.channels do local inp = ch.input local IN = type(inp) == "table" and Types.Scene.reader(inp) if IN then for k = 0, inp.count - 1 do local t = IN(k, 0) if not seen[t] and t == t then seen[t] = true table.insert(out, t) end end end end table.sort(out) return out end -- pose(scene, anim, t, samplers?) -> pose[node] = { [path] = values } for the animated nodes. samplers: a cache table -- (reuse it across calls for the same animation) function Sampler.pose(_scene: Types.Scene, anim: Types.Animation, t: number, samplers: { [any]: any }?): Pose local cache: { [any]: any } = samplers or {} local pose: Pose = {} for _, ch in anim.channels do if cache[ch] == nil then cache[ch] = Sampler.channel(ch) or false end local s: Sample? = cache[ch] or nil if s then local p = pose[ch.node] if not p then p = {} pose[ch.node] = p end p[ch.path] = s(t) end end return pose end return Sampler