--!strict -- codecs/model/Mat4: 4x4 matrix and quaternion helpers for the model formats: column-major flat arrays of 16 -- (glTF / FBX / Types.Scene layout: translation at 13..15), quaternions { x, y, z, w }, angles in degrees where a -- format stores degrees. Shared by FBX, Collada, USD and MeshOps instead of a copy in each. New in formats-luau (the -- functions come from the former model/fbx/Xform, model/dae/Math and model/usda/Build helpers). -- -- local m = Mat4.compose({ 1, 2, 3 }, quat, { 2, 2, 2 }) -- T * R * S -- local t, r, s = Mat4.decompose(m) -- (decomposeStrict: nil for shear / projective / singular) -- local w = Mat4.mul(parent, m) -- = Types.Scene.mul -- local inv = Mat4.inverse(m) -- affine inverse | nil -- local x, y, z = Mat4.point(m, px, py, pz) local Types = require("@formats/Types") local Mat4 = {} export type M4 = { number } local sqrt, abs, sin, cos, rad = math.sqrt, math.abs, math.sin, math.cos, math.rad function Mat4.identity(): M4 return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } end -- mul(a, b) -> a * b Mat4.mul = Types.Scene.mul function Mat4.translation(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 Mat4.scaling(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 -- rotation(axis, degrees) -> the rotation about axis 1 (X), 2 (Y) or 3 (Z) function Mat4.rotation(axis: number, degrees: number): M4 local a = rad(degrees) local c, s = cos(a), sin(a) if axis == 1 then return { 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1 } elseif axis == 2 then return { c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 } end return { c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 } end local AXIS = { x = 1, y = 2, z = 3 } -- euler(x, y, z, order?) -> the rotation applying the axes in order (default "xyz": R = Rz * Ry * Rx), degrees function Mat4.euler(x: number, y: number, z: number, order: string?): M4 local o = order or "xyz" local deg = { x = x, y = y, z = z } local m = Mat4.identity() for i = 1, 3 do local a = string.sub(o, i, i) m = Mat4.mul(Mat4.rotation(AXIS[a] or i, deg[a] or 0), m) end return m end -- axisAngle(x, y, z, degrees) -> quaternion function Mat4.axisAngle(x: number, y: number, z: number, degrees: number): { number } local l = sqrt(x * x + y * y + z * z) if l < 1e-12 then return { 0, 0, 0, 1 } end local h = rad(degrees) / 2 local s = sin(h) / l return { x * s, y * s, z * s, cos(h) } end -- fromQuat(q) -> the rotation matrix of a quaternion function Mat4.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 -- quat(m) -> the unit quaternion of the rotation in m's upper 3x3 (columns assumed orthonormal) function Mat4.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 = 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 = 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 = 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 = 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 = 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 -- eulerXYZ(q) -> Euler XYZ degrees (R = Rz * Ry * Rx) of a quaternion function Mat4.eulerXYZ(q: { number }): { number } local m = Mat4.fromQuat(q) local sy = math.clamp(-m[3], -1, 1) -- m[3] = r20 = -sin(y) local y = math.asin(sy) local x, z if 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 { math.deg(x), math.deg(y), math.deg(z) } end -- compose(t?, r?, s?) -> T * R * S (glTF node semantics) function Mat4.compose(t: { number }?, r: { number }?, s: { number }?): M4 local m = if r then Mat4.fromQuat(r) else Mat4.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 local function det3(m: M4): number return 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]) end local function split(m: M4, strict: boolean): ({ number }?, { number }?, { number }?) if strict and (abs(m[4]) > 1e-6 or abs(m[8]) > 1e-6 or abs(m[12]) > 1e-6 or abs(m[16] - 1) > 1e-6) then return nil, nil, nil end local sx = sqrt(m[1] * m[1] + m[2] * m[2] + m[3] * m[3]) local sy = sqrt(m[5] * m[5] + m[6] * m[6] + m[7] * m[7]) local sz = sqrt(m[9] * m[9] + m[10] * m[10] + m[11] * m[11]) if strict and (sx < 1e-12 or sy < 1e-12 or sz < 1e-12) then return nil, nil, nil end if det3(m) < 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 } if strict then local dxy = r[1] * r[5] + r[2] * r[6] + r[3] * r[7] local dxz = r[1] * r[9] + r[2] * r[10] + r[3] * r[11] local dyz = r[5] * r[9] + r[6] * r[10] + r[7] * r[11] if abs(dxy) > 1e-4 or abs(dxz) > 1e-4 or abs(dyz) > 1e-4 then return nil, nil, nil end end return { m[13], m[14], m[15] }, Mat4.quat(r), { sx, sy, sz } end -- decompose(m) -> translation, rotation (quaternion), scale (a negative determinant flips the x scale; shear is lost) function Mat4.decompose(m: M4): ({ number }, { number }, { number }) local t, r, s = split(m, false) return t :: { number }, r :: { number }, s :: { number } end -- decomposeStrict(m) -> translation, rotation, scale | nil for matrices TRS cannot express (a projective row, shear, -- a zero scale) function Mat4.decomposeStrict(m: M4): ({ number }?, { number }?, { number }?) return split(m, true) end -- inverse(m) -> the inverse of an affine matrix (general upper 3x3) | nil when singular function Mat4.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 -- inverseFull(m) -> the inverse of any invertible 4x4 (projective rows too) | nil when singular function Mat4.inverseFull(m: M4): M4? local a = m local inv = table.create(16, 0) inv[1] = a[6] * a[11] * a[16] - a[6] * a[12] * a[15] - a[10] * a[7] * a[16] + a[10] * a[8] * a[15] + a[14] * a[7] * a[12] - a[14] * a[8] * a[11] inv[5] = -a[5] * a[11] * a[16] + a[5] * a[12] * a[15] + a[9] * a[7] * a[16] - a[9] * a[8] * a[15] - a[13] * a[7] * a[12] + a[13] * a[8] * a[11] inv[9] = a[5] * a[10] * a[16] - a[5] * a[12] * a[14] - a[9] * a[6] * a[16] + a[9] * a[8] * a[14] + a[13] * a[6] * a[12] - a[13] * a[8] * a[10] inv[13] = -a[5] * a[10] * a[15] + a[5] * a[11] * a[14] + a[9] * a[6] * a[15] - a[9] * a[7] * a[14] - a[13] * a[6] * a[11] + a[13] * a[7] * a[10] inv[2] = -a[2] * a[11] * a[16] + a[2] * a[12] * a[15] + a[10] * a[3] * a[16] - a[10] * a[4] * a[15] - a[14] * a[3] * a[12] + a[14] * a[4] * a[11] inv[6] = a[1] * a[11] * a[16] - a[1] * a[12] * a[15] - a[9] * a[3] * a[16] + a[9] * a[4] * a[15] + a[13] * a[3] * a[12] - a[13] * a[4] * a[11] inv[10] = -a[1] * a[10] * a[16] + a[1] * a[12] * a[14] + a[9] * a[2] * a[16] - a[9] * a[4] * a[14] - a[13] * a[2] * a[12] + a[13] * a[4] * a[10] inv[14] = a[1] * a[10] * a[15] - a[1] * a[11] * a[14] - a[9] * a[2] * a[15] + a[9] * a[3] * a[14] + a[13] * a[2] * a[11] - a[13] * a[3] * a[10] inv[3] = a[2] * a[7] * a[16] - a[2] * a[8] * a[15] - a[6] * a[3] * a[16] + a[6] * a[4] * a[15] + a[14] * a[3] * a[8] - a[14] * a[4] * a[7] inv[7] = -a[1] * a[7] * a[16] + a[1] * a[8] * a[15] + a[5] * a[3] * a[16] - a[5] * a[4] * a[15] - a[13] * a[3] * a[8] + a[13] * a[4] * a[7] inv[11] = a[1] * a[6] * a[16] - a[1] * a[8] * a[14] - a[5] * a[2] * a[16] + a[5] * a[4] * a[14] + a[13] * a[2] * a[8] - a[13] * a[4] * a[6] inv[15] = -a[1] * a[6] * a[15] + a[1] * a[7] * a[14] + a[5] * a[2] * a[15] - a[5] * a[3] * a[14] - a[13] * a[2] * a[7] + a[13] * a[3] * a[6] inv[4] = -a[2] * a[7] * a[12] + a[2] * a[8] * a[11] + a[6] * a[3] * a[12] - a[6] * a[4] * a[11] - a[10] * a[3] * a[8] + a[10] * a[4] * a[7] inv[8] = a[1] * a[7] * a[12] - a[1] * a[8] * a[11] - a[5] * a[3] * a[12] + a[5] * a[4] * a[11] + a[9] * a[3] * a[8] - a[9] * a[4] * a[7] inv[12] = -a[1] * a[6] * a[12] + a[1] * a[8] * a[10] + a[5] * a[2] * a[12] - a[5] * a[4] * a[10] - a[9] * a[2] * a[8] + a[9] * a[4] * a[6] inv[16] = a[1] * a[6] * a[11] - a[1] * a[7] * a[10] - a[5] * a[2] * a[11] + a[5] * a[3] * a[10] + a[9] * a[2] * a[7] - a[9] * a[3] * a[6] local det = a[1] * inv[1] + a[2] * inv[5] + a[3] * inv[9] + a[4] * inv[13] if det == 0 or det ~= det then return nil end for k = 1, 16 do inv[k] /= det end return inv end -- transpose3(m) -> m with its upper 3x3 transposed and no translation (the inverse of a pure rotation) function Mat4.transpose3(m: M4): M4 return { m[1], m[5], m[9], 0, m[2], m[6], m[10], 0, m[3], m[7], m[11], 0, 0, 0, 0, 1 } end -- fromRows(v, at?) -> column-major from 16 row-major values at v[at] (default 1; missing values: identity) function Mat4.fromRows(v: { number }, at: number?): M4 local o = (at or 1) - 1 local m = table.create(16, 0) for r = 0, 3 do for c = 0, 3 do m[c * 4 + r + 1] = v[o + r * 4 + c + 1] or (if r == c then 1 else 0) end end return m end -- toRows(m) -> the 16 values row-major function Mat4.toRows(m: M4): { number } local v = table.create(16, 0) for r = 0, 3 do for c = 0, 3 do v[r * 4 + c + 1] = m[c * 4 + r + 1] end end return v end -- point(m, x, y, z) -> m * (x, y, z, 1) function Mat4.point(m: M4, x: number, y: number, z: number): (number, number, number) return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15] end -- normalMatrix(m) -> the inverse transpose of the upper 3x3 (for normals) as a 4x4 (identity when singular) function Mat4.normalMatrix(m: M4): M4 local inv = Mat4.inverse({ m[1], m[2], m[3], 0, m[5], m[6], m[7], 0, m[9], m[10], m[11], 0, 0, 0, 0, 1 }) if not inv then return Mat4.identity() end return Mat4.transpose3(inv) end return Mat4