ws-3d2/fbx: model/fbx (binary 7.x + ASCII read, binary 7.4 / 7.5 + ASCII write): node tree, Objects / Connections -> Scene (full transform model, layer elements, materials, textures, skins, blend shapes, animation stacks, cameras, lights); tests against assimp + Blender, round trips, fuzz; bench lines

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 09:46:25 -05:00
1 parent 75c677074a
commit 8e2e96e198
14 files changed
+4403

No files matched your search

+200
View File
@@ -0,0 +1,200 @@
--!strict
-- model/fbx/Xform: the FBX transform model (FBX SDK "Computing transformation matrix"): the local matrix of a Model
-- node T * Roff * Rp * Rpre * R * Rpost^-1 * Rp^-1 * Soff * Sp * S * Sp^-1, Euler rotations in degrees in the six
-- orders, column-major 4x4 matrices (flat arrays of 16, translation at 13..15 as glTF and FBX store them), inverse,
-- TRS decomposition (quaternion x, y, z, w) and Euler XYZ from a quaternion (the writer).
local Xform = {}
export type M4 = { number }
local rad = math.pi / 180
function Xform.identity(): M4
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
end
function Xform.mul(a: M4, b: M4): M4
local m = table.create(16, 0)
for col = 0, 3 do
for row = 0, 3 do
m[col * 4 + row + 1] = a[row + 1] * b[col * 4 + 1] + a[4 + row + 1] * b[col * 4 + 2] + a[8 + row + 1] * b[col * 4 + 3] + a[12 + row + 1] * b[col * 4 + 4]
end
end
return m
end
function Xform.translate(x: number, y: number, z: number): M4
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
end
function Xform.scale(x: number, y: number, z: number): M4
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
end
local function rx(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1 }
end
local function ry(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 }
end
local function rz(a: number): M4
local c, s = math.cos(a), math.sin(a)
return { c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
end
-- FBX RotationOrder: 0 XYZ (R = Rz * Ry * Rx: X applied first), 1 XZY, 2 YZX, 3 YXZ, 4 ZXY, 5 ZYX, 6 spheric (= XYZ)
local ORDERS = { [0] = "xyz", "xzy", "yzx", "yxz", "zxy", "zyx", "xyz" }
function Xform.euler(x: number, y: number, z: number, order: number?): M4
local o = ORDERS[order or 0] or "xyz"
local R = { x = rx(x * rad), y = ry(y * rad), z = rz(z * rad) }
local a, b, c = string.sub(o, 1, 1), string.sub(o, 2, 2), string.sub(o, 3, 3)
return Xform.mul(R[c], Xform.mul(R[b], R[a]))
end
-- inverse of an affine matrix (general 3x3 part) | nil when singular
function Xform.inverse(m: M4): M4?
local a, b, c = m[1], m[5], m[9]
local d, e, f = m[2], m[6], m[10]
local g, h, i = m[3], m[7], m[11]
local A, B, C = e * i - f * h, -(d * i - f * g), d * h - e * g
local det = a * A + b * B + c * C
if det == 0 or det ~= det then
return nil
end
local k = 1 / det
local r = {
A * k, B * k, C * k, 0,
-(b * i - c * h) * k, (a * i - c * g) * k, -(a * h - b * g) * k, 0,
(b * f - c * e) * k, -(a * f - c * d) * k, (a * e - b * d) * k, 0,
0, 0, 0, 1,
}
local tx, ty, tz = m[13], m[14], m[15]
r[13] = -(r[1] * tx + r[5] * ty + r[9] * tz)
r[14] = -(r[2] * tx + r[6] * ty + r[10] * tz)
r[15] = -(r[3] * tx + r[7] * ty + r[11] * tz)
return r
end
-- the local matrix of a node from its (already defaulted) FBX properties p: vectors of 3 numbers
export type Props = {
T: { number }, R: { number }, S: { number },
Rpre: { number }, Rpost: { number }, Roff: { number }, Rp: { number }, Soff: { number }, Sp: { number },
order: number,
}
local function isZero(v: { number }): boolean
return v[1] == 0 and v[2] == 0 and v[3] == 0
end
function Xform.localMatrix(p: Props): M4
local m = Xform.translate(p.T[1] + p.Roff[1] + p.Rp[1], p.T[2] + p.Roff[2] + p.Rp[2], p.T[3] + p.Roff[3] + p.Rp[3])
if not isZero(p.Rpre) then
m = Xform.mul(m, Xform.euler(p.Rpre[1], p.Rpre[2], p.Rpre[3], 0))
end
m = Xform.mul(m, Xform.euler(p.R[1], p.R[2], p.R[3], p.order))
if not isZero(p.Rpost) then
local post = Xform.euler(p.Rpost[1], p.Rpost[2], p.Rpost[3], 0)
-- the inverse of a rotation is its transpose
local tr = { post[1], post[5], post[9], 0, post[2], post[6], post[10], 0, post[3], post[7], post[11], 0, 0, 0, 0, 1 }
m = Xform.mul(m, tr)
end
m = Xform.mul(m, Xform.translate(-p.Rp[1] + p.Soff[1] + p.Sp[1], -p.Rp[2] + p.Soff[2] + p.Sp[2], -p.Rp[3] + p.Soff[3] + p.Sp[3]))
m = Xform.mul(m, Xform.scale(p.S[1], p.S[2], p.S[3]))
if not isZero(p.Sp) then
m = Xform.mul(m, Xform.translate(-p.Sp[1], -p.Sp[2], -p.Sp[3]))
end
return m
end
-- quaternion (x, y, z, w) of a pure rotation matrix
local function quat(m: M4): { number }
local m00, m01, m02 = m[1], m[5], m[9]
local m10, m11, m12 = m[2], m[6], m[10]
local m20, m21, m22 = m[3], m[7], m[11]
local tr = m00 + m11 + m22
local x, y, z, w
if tr > 0 then
local s = math.sqrt(tr + 1) * 2
w, x, y, z = s / 4, (m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s
elseif m00 > m11 and m00 > m22 then
local s = math.sqrt(math.max(1 + m00 - m11 - m22, 1e-30)) * 2
w, x, y, z = (m21 - m12) / s, s / 4, (m01 + m10) / s, (m02 + m20) / s
elseif m11 > m22 then
local s = math.sqrt(math.max(1 + m11 - m00 - m22, 1e-30)) * 2
w, x, y, z = (m02 - m20) / s, (m01 + m10) / s, s / 4, (m12 + m21) / s
else
local s = math.sqrt(math.max(1 + m22 - m00 - m11, 1e-30)) * 2
w, x, y, z = (m10 - m01) / s, (m02 + m20) / s, (m12 + m21) / s, s / 4
end
local n = math.sqrt(x * x + y * y + z * z + w * w)
if n == 0 or n ~= n then
return { 0, 0, 0, 1 }
end
return { x / n, y / n, z / n, w / n }
end
Xform.quat = quat
-- decompose(m) -> translation, rotation (quaternion), scale (a negative determinant flips the x scale)
function Xform.decompose(m: M4): ({ number }, { number }, { number })
local t = { m[13], m[14], m[15] }
local sx = math.sqrt(m[1] * m[1] + m[2] * m[2] + m[3] * m[3])
local sy = math.sqrt(m[5] * m[5] + m[6] * m[6] + m[7] * m[7])
local sz = math.sqrt(m[9] * m[9] + m[10] * m[10] + m[11] * m[11])
local det = m[1] * (m[6] * m[11] - m[10] * m[7]) - m[5] * (m[2] * m[11] - m[10] * m[3]) + m[9] * (m[2] * m[7] - m[6] * m[3])
if det < 0 then
sx = -sx
end
local ix = if sx ~= 0 then 1 / sx else 0
local iy = if sy ~= 0 then 1 / sy else 0
local iz = if sz ~= 0 then 1 / sz else 0
local r = { m[1] * ix, m[2] * ix, m[3] * ix, 0, m[5] * iy, m[6] * iy, m[7] * iy, 0, m[9] * iz, m[10] * iz, m[11] * iz, 0, 0, 0, 0, 1 }
return t, quat(r), { sx, sy, sz }
end
-- the rotation matrix of a quaternion
function Xform.fromQuat(q: { number }): M4
local x, y, z, w = q[1], q[2], q[3], q[4]
local xx, yy, zz, xy, xz, yz, wx, wy, wz = x * x, y * y, z * z, x * y, x * z, y * z, w * x, w * y, w * z
return {
1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy), 0,
2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx), 0,
2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy), 0,
0, 0, 0, 1,
}
end
-- Euler XYZ degrees (R = Rz * Ry * Rx) of a quaternion
function Xform.eulerXYZ(q: { number }): { number }
local m = Xform.fromQuat(q)
-- m[3] = r20 = -sin(y)
local sy = -m[3]
sy = math.clamp(sy, -1, 1)
local y = math.asin(sy)
local x, z
if math.abs(sy) < 0.9999999 then
x = math.atan2(m[7], m[11])
z = math.atan2(m[2], m[1])
else
x = math.atan2(-m[10], m[6])
z = 0
end
return { x / rad, y / rad, z / rad }
end
-- TRS -> matrix (glTF semantics)
function Xform.compose(t: { number }?, r: { number }?, s: { number }?): M4
local m = if r then Xform.fromQuat(r) else Xform.identity()
if s then
for k = 0, 2 do
m[k * 4 + 1] *= s[k + 1]
m[k * 4 + 2] *= s[k + 1]
m[k * 4 + 3] *= s[k + 1]
end
end
if t then
m[13], m[14], m[15] = t[1], t[2], t[3]
end
return m
end
return Xform