ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
This commit is contained in:
1 parent
763f4a7c04
commit
51b08575c4
283 files changed
+1877
-1660
No files matched your search
@@ -2,10 +2,10 @@
|
||||
--!native
|
||||
-- model/fbx/Binary: the binary FBX node tree reader and writer (versions 7.x: 32-bit node records before 7500, 64-bit
|
||||
-- from 7500). From the format as documented by Blender's io_scene_fbx (encode_bin.py / parse_fbx.py) and assimp's
|
||||
-- FBXBinaryTokenizer. Arrays (f, d, l, i, b) are inflated with codecs/Deflate when compressed and kept as Tree.Array
|
||||
-- FBXBinaryTokenizer. Arrays (f, d, l, i, b) are inflated with codecs/compress/Deflate when compressed and kept as Tree.Array
|
||||
-- values (every element as an f64; 64-bit integers past 2^53 lose precision there); scalar L values past 2^53 become
|
||||
-- the string "L<hex>" so ids stay exact. Never throws: errors are returned (nil, message, offset).
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
|
||||
local Binary = {}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Xform = require("@formats/model/fbx/Xform")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -486,13 +487,13 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
order = pnum(p, "RotationOrder", 0),
|
||||
}
|
||||
end
|
||||
local localM: { [number]: Xform.M4 } = {}
|
||||
local localM: { [number]: Mat4.M4 } = {}
|
||||
for _, o in models do
|
||||
local ni = nodeOf[o.id]
|
||||
local nd = scene.nodes[ni]
|
||||
local m = Xform.localMatrix(xprops(o))
|
||||
localM[ni] = m
|
||||
local t, r, s = Xform.decompose(m)
|
||||
local t, r, s = Mat4.decompose(m)
|
||||
if t[1] ~= 0 or t[2] ~= 0 or t[3] ~= 0 then
|
||||
nd.translation = t
|
||||
end
|
||||
@@ -543,9 +544,9 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
-- world matrices (rest pose)
|
||||
local worldM: { [number]: Xform.M4 } = {}
|
||||
local function world(i: number, parent: Xform.M4)
|
||||
worldM[i] = Xform.mul(parent, localM[i] or Xform.identity())
|
||||
local worldM: { [number]: Mat4.M4 } = {}
|
||||
local function world(i: number, parent: Mat4.M4)
|
||||
worldM[i] = Mat4.mul(parent, localM[i] or Mat4.identity())
|
||||
for _, c in scene.nodes[i].children do
|
||||
if not worldM[c] then
|
||||
world(c, worldM[i])
|
||||
@@ -553,7 +554,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
for _, r in scene.roots do
|
||||
world(r, Xform.identity())
|
||||
world(r, Mat4.identity())
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------ meshes
|
||||
@@ -566,7 +567,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
local perCp: { { { number } } } = {}
|
||||
local joints: { number } = {}
|
||||
local ibms: { Xform.M4 } = {}
|
||||
local ibms: { Mat4.M4 } = {}
|
||||
for _, cl in sources(skin.id, "Deformer", "Cluster") do
|
||||
local bone = sources(cl.id, "Model")[1]
|
||||
local bi = bone and nodeOf[bone.id]
|
||||
@@ -575,13 +576,13 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
joints[ji] = bi
|
||||
local idx, w = array(cl.node, "Indexes"), array(cl.node, "Weights")
|
||||
local T, TL = array(cl.node, "Transform"), array(cl.node, "TransformLink")
|
||||
local tm = Xform.identity()
|
||||
local tm = Mat4.identity()
|
||||
if T and T.count >= 16 then
|
||||
for k = 1, 16 do
|
||||
tm[k] = at(T, k - 1)
|
||||
end
|
||||
end
|
||||
local tl: Xform.M4? = nil
|
||||
local tl: Mat4.M4? = nil
|
||||
if TL and TL.count >= 16 then
|
||||
tl = {}
|
||||
for k = 1, 16 do
|
||||
@@ -590,8 +591,8 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
else
|
||||
tl = worldM[bi]
|
||||
end
|
||||
local inv = Xform.inverse(tl :: Xform.M4) or Xform.identity()
|
||||
ibms[ji] = Xform.mul(inv, tm)
|
||||
local inv = Mat4.inverse(tl :: Mat4.M4) or Mat4.identity()
|
||||
ibms[ji] = Mat4.mul(inv, tm)
|
||||
if idx and w then
|
||||
for k = 0, math.min(idx.count, w.count) - 1 do
|
||||
local cp = at(idx, k)
|
||||
@@ -648,7 +649,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
local nodeSkin: { [number]: number } = {}
|
||||
local skinCache: { [any]: number } = {}
|
||||
|
||||
local function buildMesh(geo: Obj, mats: { number }, G: Xform.M4?): (number?, Err.Err?)
|
||||
local function buildMesh(geo: Obj, mats: { number }, G: Mat4.M4?): (number?, Err.Err?)
|
||||
local g = geo.node
|
||||
local verts = array(g, "Vertices")
|
||||
local pvi = array(g, "PolygonVertexIndex")
|
||||
@@ -784,15 +785,15 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
table.sort(slots)
|
||||
local perCp, skin = skinOf(geo)
|
||||
local shapes = shapesOf(geo)
|
||||
local N3: Xform.M4? = nil -- normal matrix (inverse transpose of G)
|
||||
local N3: Mat4.M4? = nil -- normal matrix (inverse transpose of G)
|
||||
if G then
|
||||
local inv = Xform.inverse(G)
|
||||
local inv = Mat4.inverse(G)
|
||||
if inv then
|
||||
N3 = { inv[1], inv[5], inv[9], 0, inv[2], inv[6], inv[10], 0, inv[3], inv[7], inv[11], 0, 0, 0, 0, 1 }
|
||||
end
|
||||
end
|
||||
local mesh: Types.Mesh = { name = geo.name, primitives = {} }
|
||||
local function v3(dst: buffer, o: number, x: number, y: number, z: number, M: Xform.M4?, w: number)
|
||||
local function v3(dst: buffer, o: number, x: number, y: number, z: number, M: Mat4.M4?, w: number)
|
||||
if M then
|
||||
x, y, z = M[1] * x + M[5] * y + M[9] * z + M[13] * w, M[2] * x + M[6] * y + M[10] * z + M[14] * w, M[3] * x + M[7] * y + M[11] * z + M[15] * w
|
||||
end
|
||||
@@ -1012,9 +1013,9 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
local gt = pvec(o.props, "GeometricTranslation", { 0, 0, 0 })
|
||||
local gr = pvec(o.props, "GeometricRotation", { 0, 0, 0 })
|
||||
local gsc = pvec(o.props, "GeometricScaling", { 1, 1, 1 })
|
||||
local G: Xform.M4? = nil
|
||||
local G: Mat4.M4? = nil
|
||||
if gt[1] ~= 0 or gt[2] ~= 0 or gt[3] ~= 0 or gr[1] ~= 0 or gr[2] ~= 0 or gr[3] ~= 0 or gsc[1] ~= 1 or gsc[2] ~= 1 or gsc[3] ~= 1 then
|
||||
G = Xform.mul(Xform.translate(gt[1], gt[2], gt[3]), Xform.mul(Xform.euler(gr[1], gr[2], gr[3], 0), Xform.scale(gsc[1], gsc[2], gsc[3])))
|
||||
G = Mat4.mul(Mat4.translation(gt[1], gt[2], gt[3]), Mat4.mul(Xform.euler(gr[1], gr[2], gr[3], 0), Mat4.scaling(gsc[1], gsc[2], gsc[3])))
|
||||
end
|
||||
for _, geo in sources(o.id, "Geometry") do
|
||||
if geo.sub == "Mesh" or geo.sub == "" then
|
||||
@@ -1294,7 +1295,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
over[key] = v
|
||||
end
|
||||
local tt, rr, ss = Xform.decompose(Xform.localMatrix(xprops(o, over)))
|
||||
local tt, rr, ss = Mat4.decompose(Xform.localMatrix(xprops(o, over)))
|
||||
if prevQ and rr[1] * prevQ[1] + rr[2] * prevQ[2] + rr[3] * prevQ[3] + rr[4] * prevQ[4] < 0 then
|
||||
rr = { -rr[1], -rr[2], -rr[3], -rr[4] }
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
-- BlendShape / BlendShapeChannel / Shape; animations -> AnimationStack / Layer / CurveNode / Curve.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Xform = require("@formats/model/fbx/Xform")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
|
||||
local Write = {}
|
||||
|
||||
@@ -284,26 +284,26 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
|
||||
end
|
||||
end
|
||||
-- world matrices (rest pose) for clusters and the bind pose
|
||||
local worldM: { [number]: Xform.M4 } = {}
|
||||
local worldM: { [number]: Mat4.M4 } = {}
|
||||
local visiting: { [number]: boolean } = {}
|
||||
local function world(i: number): Xform.M4
|
||||
local function world(i: number): Mat4.M4
|
||||
if worldM[i] then
|
||||
return worldM[i]
|
||||
end
|
||||
if visiting[i] then
|
||||
return Xform.identity()
|
||||
return Mat4.identity()
|
||||
end
|
||||
visiting[i] = true
|
||||
local nd = nodes[i]
|
||||
local l = if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then nd.matrix else Xform.compose(nd.translation, nd.rotation, nd.scale)
|
||||
local l = if nd.matrix and #(nd.matrix :: Mat4.M4) == 16 then nd.matrix else Mat4.compose(nd.translation, nd.rotation, nd.scale)
|
||||
local p = parentOf[i]
|
||||
local w = if p then Xform.mul(world(p), l) else l
|
||||
local w = if p then Mat4.mul(world(p), l) else l
|
||||
worldM[i] = w
|
||||
return w
|
||||
end
|
||||
local function trs(nd: Types.Node): ({ number }, { number }, { number })
|
||||
if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then
|
||||
return Xform.decompose(nd.matrix)
|
||||
if nd.matrix and #(nd.matrix :: Mat4.M4) == 16 then
|
||||
return Mat4.decompose(nd.matrix)
|
||||
end
|
||||
return nd.translation or { 0, 0, 0 }, nd.rotation or { 0, 0, 0, 1 }, nd.scale or { 1, 1, 1 }
|
||||
end
|
||||
@@ -387,7 +387,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
|
||||
for i, nd in nodes do
|
||||
if not skip[i] then
|
||||
local t, r, s = trs(nd)
|
||||
local euler = Xform.eulerXYZ(r)
|
||||
local euler = Mat4.eulerXYZ(r)
|
||||
local ftype = nd.extras and (nd.extras :: any).fbx and (nd.extras :: any).fbx.type
|
||||
local sub = if nd.mesh then "Mesh" elseif jointSet[i] or ftype == "LimbNode" then "LimbNode" else "Null"
|
||||
-- an orientation child folded into this node gives it its attribute
|
||||
@@ -697,7 +697,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
|
||||
|
||||
local poseNodes: { WNode } = {}
|
||||
local posed: { [number]: boolean } = {}
|
||||
local function pose(mid: number, m: Xform.M4)
|
||||
local function pose(mid: number, m: Mat4.M4)
|
||||
if posed[mid] then
|
||||
return
|
||||
end
|
||||
@@ -752,14 +752,14 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
|
||||
for j, ji in skin.joints do
|
||||
local jm = modelIds[ji]
|
||||
if jm then
|
||||
local IB = Xform.identity()
|
||||
local IB = Mat4.identity()
|
||||
if ibm and skin.inverseBindMatrices and j <= skin.inverseBindMatrices.count then
|
||||
for k = 1, 16 do
|
||||
IB[k] = ibm(j - 1, k - 1)
|
||||
end
|
||||
end
|
||||
local inv = Xform.inverse(IB) or Xform.identity()
|
||||
local link = Xform.mul(meshWorld, inv)
|
||||
local inv = Mat4.inverse(IB) or Mat4.identity()
|
||||
local link = Mat4.mul(meshWorld, inv)
|
||||
local cid = id()
|
||||
add("Deformer", N("Deformer", "LSS", { cid, cls(nodes[ji].name, "SubDeformer"), "Cluster" }, {
|
||||
N("Version", "I", { 100 }),
|
||||
@@ -859,7 +859,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
|
||||
else
|
||||
q = { 0, 0, 0, 1 }
|
||||
end
|
||||
v = Xform.eulerXYZ(q)
|
||||
v = Mat4.eulerXYZ(q)
|
||||
if prev then
|
||||
for a = 1, 3 do
|
||||
while v[a] - prev[a] > 180 do
|
||||
|
||||
@@ -1,80 +1,15 @@
|
||||
--!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).
|
||||
-- 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 = {}
|
||||
|
||||
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
|
||||
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
|
||||
@@ -86,113 +21,20 @@ export type Props = {
|
||||
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])
|
||||
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 = Xform.mul(m, Xform.euler(p.Rpre[1], p.Rpre[2], p.Rpre[3], 0))
|
||||
m = Mat4.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))
|
||||
m = Mat4.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)
|
||||
m = Mat4.mul(m, Mat4.transpose3(Xform.euler(p.Rpost[1], p.Rpost[2], p.Rpost[3], 0)))
|
||||
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]))
|
||||
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 = 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]
|
||||
m = Mat4.mul(m, Mat4.translation(-p.Sp[1], -p.Sp[2], -p.Sp[3]))
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
-- model/fbx: Autodesk FBX 7.x, binary (7.0 - 7.7: 32-bit records before 7500, 64-bit after; zlib-compressed arrays
|
||||
-- through codecs/Deflate) and ASCII. New code, from the format as reverse engineered by Blender's io_scene_fbx and
|
||||
-- through codecs/compress/Deflate) and ASCII. New code, from the format as reverse engineered by Blender's io_scene_fbx and
|
||||
-- assimp's FBX importer, and the FBX SDK's documentation of the transform model.
|
||||
-- Reads: the node tree (Fbx.tree), then Objects / Connections -> Scene: Model nodes with the full transform (Lcl
|
||||
-- T / R / S, pre / post rotation, rotation / scaling pivots and offsets, the six rotation orders; geometric transforms
|
||||
@@ -35,7 +35,7 @@ local Fbx = {
|
||||
category = "model",
|
||||
extensions = { "fbx" },
|
||||
mime = { "application/vnd.autodesk.fbx", "model/fbx" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Deflate", "core/Base64" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "core/Base64", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, binary = true, ascii = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, polygons = true },
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user