model/sourcemdl: Source MDL v44-49 with .vvd / .vtx / .phy / .ani (WIP, no tests yet); codecs/model/SourceAnim shared with model/mdl's GoldSrc animation values

This commit is contained in:
4DBug committed 2026-11-01 18:34:50 -06:00
1 parent 01c044805f
commit c7e0639913
12 files changed
+1752 -31

No files matched your search

+219
View File
@@ -0,0 +1,219 @@
--!strict
--!native
-- codecs/model/SourceAnim: studio model animation data: the run-length coded mstudioanimvalue_t shorts (GoldSrc and
-- Source), Source mstudioanim_t bone records with Quaternion48 / Quaternion64 / Vector48, from the Source SDK 2013
-- studio.h / bone_setup.cpp layouts and SourceIO (MIT) notes; no code taken.
local Half = require("@rosetta/codecs/image/Half")
local SourceAnim = {}
local readu8, readi16, readu16, readu32 = buffer.readu8, buffer.readi16, buffer.readu16, buffer.readu32
local sqrt, sin, cos = math.sqrt, math.sin, math.cos
local HALF = Half.TABLE
export type Bone = {
pos: { number },
quat: { number },
rot: { number }, -- radian euler x, y, z
posScale: { number },
rotScale: { number },
}
export type Track = { pos: { number }, rot: { number } } -- frames * 3 / frames * 4 (quaternion x, y, z, w)
-- ExtractAnimValue: frame k of a run list ({ valid, total } then valid shorts); a frame past valid repeats
-- value[valid] (with valid = 0 that is the run header itself, as the engine reads it)
function SourceAnim.value(b: buffer, at: number, limit: number, k: number): number?
local p = at
for _ = 1, 65536 do
if p + 2 > limit then
return nil
end
local valid, total = readu8(b, p), readu8(b, p + 1)
if total == 0 then
return nil
end
if total > k then
local idx = if valid > k then k + 1 else valid
if p + idx * 2 + 2 > limit then
return nil
end
return readi16(b, p + idx * 2)
end
k -= total
p += (valid + 1) * 2
end
return nil
end
function SourceAnim.values(b: buffer, at: number, limit: number, count: number): { number }?
local out = table.create(count, 0)
local p, f = at, 0
while f < count do
if p + 2 > limit then
return nil
end
local valid, total = readu8(b, p), readu8(b, p + 1)
if total == 0 or p + 2 + valid * 2 > limit then
return nil
end
local n = math.min(total, count - f)
for i = 0, n - 1 do
local idx = if i < valid then i + 1 else valid
out[f + i + 1] = readi16(b, p + idx * 2)
end
f += n
p += (valid + 1) * 2
end
return out
end
-- Quaternion48: x, y u16 (bias 32768), z 15 bits (bias 16384), w sign bit
function SourceAnim.quat48(b: buffer, o: number): (number, number, number, number)
local x = (readu16(b, o) - 32768) / 32768
local y = (readu16(b, o + 2) - 32768) / 32768
local zw = readu16(b, o + 4)
local z = (zw % 32768 - 16384) / 16384
local w = sqrt(math.max(0, 1 - x * x - y * y - z * z))
return x, y, z, if zw >= 32768 then -w else w
end
-- Quaternion64: x, y, z 21 bits each (bias 1048576, scale 1 / 1048576.5), w sign bit 63
function SourceAnim.quat64(b: buffer, o: number): (number, number, number, number)
local lo, hi = readu32(b, o), readu32(b, o + 4)
local xs = lo % 2097152
local ys = (hi % 1024) * 2048 + (lo - xs) / 2097152
local zs = math.floor(hi / 1024) % 2097152
local x, y, z = (xs - 1048576) / 1048576.5, (ys - 1048576) / 1048576.5, (zs - 1048576) / 1048576.5
local w = sqrt(math.max(0, 1 - x * x - y * y - z * z))
return x, y, z, if hi >= 0x80000000 then -w else w
end
function SourceAnim.vec48(b: buffer, o: number): (number, number, number)
return HALF[readu16(b, o)], HALF[readu16(b, o + 2)], HALF[readu16(b, o + 4)]
end
-- AngleQuaternion (RadianEuler: x roll, y pitch, z yaw)
function SourceAnim.angles(x: number, y: number, z: number): (number, number, number, number)
local sy, cy = sin(z / 2), cos(z / 2)
local sp, cp = sin(y / 2), cos(y / 2)
local sr, cr = sin(x / 2), cos(x / 2)
return sr * cp * cy - cr * sp * sy, cr * sp * cy + sr * cp * sy, cr * cp * sy - sr * sp * cy, cr * cp * cy + sr * sp * sy
end
local RAWPOS, RAWROT, ANIMPOS, ANIMROT, DELTA, RAWROT2 = 0x01, 0x02, 0x04, 0x08, 0x10, 0x20
local function channels(b: buffer, vp: number, limit: number, frames: number, scale: { number }): ({ { number }? }?)
if vp + 6 > limit then
return nil
end
local out: { { number }? } = {}
for k = 0, 2 do
local off = readi16(b, vp + k * 2)
if off > 0 then
local v = SourceAnim.values(b, vp + off, limit, frames)
if not v then
return nil
end
local s = scale[k + 1]
for i = 1, frames do
v[i] *= s
end
out[k + 1] = v
end
end
return out
end
-- mstudioanim_t chain at `at` (bone u8, flags u8, next i16, data): `frames` frames of every bone it lists, as the
-- engine's CalcBoneQuaternion / CalcBonePosition (delta records add nothing of the bone's rest pose)
function SourceAnim.decode(b: buffer, at: number, limit: number, bones: { Bone }, frames: number): ({ [number]: Track }?, string?)
local out: { [number]: Track } = {}
local p = at
for _ = 1, #bones + 1 do
if p + 4 > limit then
return nil, "truncated animation"
end
local bone, flags, nxt = readu8(b, p), readu8(b, p + 1), readi16(b, p + 2)
if bone == 255 then
break
end
local bn = bones[bone + 1]
if not bn then
return nil, "animation bone out of range"
end
local delta = bit32.band(flags, DELTA) ~= 0
local d = p + 4
local rot = table.create(frames * 4, 0)
if bit32.band(flags, RAWROT) ~= 0 then
if d + 6 > limit then
return nil, "truncated animation"
end
local x, y, z, w = SourceAnim.quat48(b, d)
for f = 0, frames - 1 do
rot[f * 4 + 1], rot[f * 4 + 2], rot[f * 4 + 3], rot[f * 4 + 4] = x, y, z, w
end
d += 6
elseif bit32.band(flags, RAWROT2) ~= 0 then
if d + 8 > limit then
return nil, "truncated animation"
end
local x, y, z, w = SourceAnim.quat64(b, d)
for f = 0, frames - 1 do
rot[f * 4 + 1], rot[f * 4 + 2], rot[f * 4 + 3], rot[f * 4 + 4] = x, y, z, w
end
d += 8
elseif bit32.band(flags, ANIMROT) ~= 0 then
local ch = channels(b, d, limit, frames, bn.rotScale)
if not ch then
return nil, "bad rotation values"
end
local bx, by, bz = if delta then 0 else bn.rot[1], if delta then 0 else bn.rot[2], if delta then 0 else bn.rot[3]
local cx, cy, cz = ch[1], ch[2], ch[3]
for f = 0, frames - 1 do
local x, y, z, w = SourceAnim.angles(bx + (if cx then cx[f + 1] else 0), by + (if cy then cy[f + 1] else 0), bz + (if cz then cz[f + 1] else 0))
rot[f * 4 + 1], rot[f * 4 + 2], rot[f * 4 + 3], rot[f * 4 + 4] = x, y, z, w
end
d += 6
else
local q = if delta then { 0, 0, 0, 1 } else bn.quat
for f = 0, frames - 1 do
rot[f * 4 + 1], rot[f * 4 + 2], rot[f * 4 + 3], rot[f * 4 + 4] = q[1], q[2], q[3], q[4]
end
end
local pos = table.create(frames * 3, 0)
if bit32.band(flags, RAWPOS) ~= 0 then
if d + 6 > limit then
return nil, "truncated animation"
end
local x, y, z = SourceAnim.vec48(b, d)
for f = 0, frames - 1 do
pos[f * 3 + 1], pos[f * 3 + 2], pos[f * 3 + 3] = x, y, z
end
elseif bit32.band(flags, ANIMPOS) ~= 0 then
local ch = channels(b, d, limit, frames, bn.posScale)
if not ch then
return nil, "bad position values"
end
local bx, by, bz = if delta then 0 else bn.pos[1], if delta then 0 else bn.pos[2], if delta then 0 else bn.pos[3]
local cx, cy, cz = ch[1], ch[2], ch[3]
for f = 0, frames - 1 do
pos[f * 3 + 1] = bx + (if cx then cx[f + 1] else 0)
pos[f * 3 + 2] = by + (if cy then cy[f + 1] else 0)
pos[f * 3 + 3] = bz + (if cz then cz[f + 1] else 0)
end
elseif not delta then
for f = 0, frames - 1 do
pos[f * 3 + 1], pos[f * 3 + 2], pos[f * 3 + 3] = bn.pos[1], bn.pos[2], bn.pos[3]
end
end
out[bone + 1] = { pos = pos, rot = rot }
if nxt <= 0 then
break
end
p += nxt
end
return out, nil
end
return SourceAnim