--!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, with FBX's numbered Euler orders. The -- matrix / quaternion arithmetic is codecs/model/Mat4. local Mat4 = require("@formats/codecs/model/Mat4") local Xform = {} -- 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?): Mat4.M4 return Mat4.euler(x, y, z, ORDERS[order or 0] or "xyz") 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): Mat4.M4 local m = Mat4.translation(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 = Mat4.mul(m, Xform.euler(p.Rpre[1], p.Rpre[2], p.Rpre[3], 0)) end m = Mat4.mul(m, Xform.euler(p.R[1], p.R[2], p.R[3], p.order)) if not isZero(p.Rpost) then -- the inverse of a rotation is its transpose m = Mat4.mul(m, Mat4.transpose3(Xform.euler(p.Rpost[1], p.Rpost[2], p.Rpost[3], 0))) end m = Mat4.mul(m, Mat4.translation(-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 = Mat4.mul(m, Mat4.scaling(p.S[1], p.S[2], p.S[3])) if not isZero(p.Sp) then m = Mat4.mul(m, Mat4.translation(-p.Sp[1], -p.Sp[2], -p.Sp[3])) end return m end return Xform