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. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9c553b9d1a
commit
434a304315
283 files changed
+1877
-1660
No files matched your search
@@ -8,7 +8,7 @@
|
||||
-- texture groups give per-corner COLOR_0 / TEXCOORD_0, so vertices are split where corners of one vertex differ.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Zip = require("@formats/containers/Zip")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
-- Units: meta.unit picks the 3MF unit when it is one of them; otherwise millimetres and the build transforms scale.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Zip = require("@formats/containers/Zip")
|
||||
|
||||
local Write = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
-- model/3mf: 3D Manufacturing Format (3MF Consortium core specification 1.3, materials and properties extension 1.2,
|
||||
-- production extension 1.1 part references). New code. An OPC package (containers/Zip) holding XML model parts
|
||||
-- (codecs/Xml).
|
||||
-- (codecs/text/Xml).
|
||||
-- Reads: the root model part (_rels/.rels), objects (mesh vertices / triangles; components with transforms, also from
|
||||
-- other model parts through p:path), build items -> root nodes with their transforms (3MF's row-vector 4x3 matrices
|
||||
-- become column-major node.matrix), object names / types / part numbers (node / mesh extras["3mf"]), metadata
|
||||
@@ -31,7 +31,7 @@ local ThreeMf = {
|
||||
category = "model",
|
||||
extensions = { "3mf" },
|
||||
mime = { "model/3mf" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "containers/Zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Xml", "containers/Zip" },
|
||||
caps = { decode = true, encode = true, materials = true, vertexColors = true, textures = true, units = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -1,79 +1,11 @@
|
||||
--!strict
|
||||
-- model/dae/Math: the matrix helpers the Collada reader and writer share. Matrices are flat arrays of 16 in glTF's
|
||||
-- column-major order (Collada writes row-major: fromRows / toRows convert). Quaternions are { x, y, z, w }.
|
||||
-- model/dae/Math: the Collada-specific transform elements (<lookat>, <skew>); everything else (matrices,
|
||||
-- quaternions, decomposition) is codecs/model/Mat4. Matrices are flat arrays of 16 in column-major order.
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
|
||||
local DaeMath = {}
|
||||
|
||||
local sqrt, abs, rad, sin, cos = math.sqrt, math.abs, math.rad, math.sin, math.cos
|
||||
|
||||
function DaeMath.identity(): { number }
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
-- fromRows(v, at?) -> column-major from 16 row-major values starting at v[at] (default 1)
|
||||
function DaeMath.fromRows(v: { number }, at: number?): { number }
|
||||
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
|
||||
|
||||
function DaeMath.toRows(m: { number }): { 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
|
||||
|
||||
function DaeMath.mul(a: { number }, b: { number }): { number }
|
||||
local m = table.create(16, 0)
|
||||
for col = 0, 3 do
|
||||
for row = 0, 3 do
|
||||
local s = 0
|
||||
for k = 0, 3 do
|
||||
s += a[k * 4 + row + 1] * b[col * 4 + k + 1]
|
||||
end
|
||||
m[col * 4 + row + 1] = s
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
function DaeMath.translation(x: number, y: number, z: number): { number }
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
|
||||
end
|
||||
|
||||
function DaeMath.scaling(x: number, y: number, z: number): { number }
|
||||
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
-- axisAngle(x, y, z, degrees) -> quaternion
|
||||
function DaeMath.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
|
||||
|
||||
function DaeMath.quatMatrix(q: { number }): { number }
|
||||
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
|
||||
local sqrt, rad = math.sqrt, math.rad
|
||||
|
||||
local function cross(a: { number }, b: { number }): { number }
|
||||
return { a[2] * b[3] - a[3] * b[2], a[3] * b[1] - a[1] * b[3], a[1] * b[2] - a[2] * b[1] }
|
||||
@@ -101,7 +33,7 @@ function DaeMath.skew(v: { number }): { number }
|
||||
local r = norm({ v[2] or 0, v[3] or 0, v[4] or 0 })
|
||||
local t = norm({ v[5] or 0, v[6] or 0, v[7] or 0 })
|
||||
local s = math.tan(a)
|
||||
local m = DaeMath.identity()
|
||||
local m = Mat4.identity()
|
||||
-- M = I + s * t * r^T (column-major: m[col*4 + row])
|
||||
for row = 0, 2 do
|
||||
for col = 0, 2 do
|
||||
@@ -111,78 +43,4 @@ function DaeMath.skew(v: { number }): { number }
|
||||
return m
|
||||
end
|
||||
|
||||
-- decompose(m) -> translation, rotation, scale | nil (shear, a projective row, or a singular matrix)
|
||||
function DaeMath.decompose(m: { number }): ({ number }?, { number }?, { number }?)
|
||||
if 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 c1 = { m[1], m[2], m[3] }
|
||||
local c2 = { m[5], m[6], m[7] }
|
||||
local c3 = { m[9], m[10], m[11] }
|
||||
local sx = sqrt(c1[1] * c1[1] + c1[2] * c1[2] + c1[3] * c1[3])
|
||||
local sy = sqrt(c2[1] * c2[1] + c2[2] * c2[2] + c2[3] * c2[3])
|
||||
local sz = sqrt(c3[1] * c3[1] + c3[2] * c3[2] + c3[3] * c3[3])
|
||||
if sx < 1e-12 or sy < 1e-12 or sz < 1e-12 then
|
||||
return nil, nil, nil
|
||||
end
|
||||
local x, y, z = { c1[1] / sx, c1[2] / sx, c1[3] / sx }, { c2[1] / sy, c2[2] / sy, c2[3] / sy }, { c3[1] / sz, c3[2] / sz, c3[3] / sz }
|
||||
local dxy = x[1] * y[1] + x[2] * y[2] + x[3] * y[3]
|
||||
local dxz = x[1] * z[1] + x[2] * z[2] + x[3] * z[3]
|
||||
local dyz = y[1] * z[1] + y[2] * z[2] + y[3] * z[3]
|
||||
if abs(dxy) > 1e-4 or abs(dxz) > 1e-4 or abs(dyz) > 1e-4 then
|
||||
return nil, nil, nil
|
||||
end
|
||||
local cr = cross(x, y)
|
||||
if cr[1] * z[1] + cr[2] * z[2] + cr[3] * z[3] < 0 then
|
||||
sx = -sx
|
||||
x = { -x[1], -x[2], -x[3] }
|
||||
end
|
||||
-- rotation matrix (columns x, y, z) -> quaternion
|
||||
local m00, m01, m02 = x[1], y[1], z[1]
|
||||
local m10, m11, m12 = x[2], y[2], z[2]
|
||||
local m20, m21, m22 = x[3], y[3], z[3]
|
||||
local tr = m00 + m11 + m22
|
||||
local qx, qy, qz, qw
|
||||
if tr > 0 then
|
||||
local s = sqrt(tr + 1) * 2
|
||||
qw, qx, qy, qz = s / 4, (m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s
|
||||
elseif m00 > m11 and m00 > m22 then
|
||||
local s = sqrt(1 + m00 - m11 - m22) * 2
|
||||
qw, qx, qy, qz = (m21 - m12) / s, s / 4, (m01 + m10) / s, (m02 + m20) / s
|
||||
elseif m11 > m22 then
|
||||
local s = sqrt(1 + m11 - m00 - m22) * 2
|
||||
qw, qx, qy, qz = (m02 - m20) / s, (m01 + m10) / s, s / 4, (m12 + m21) / s
|
||||
else
|
||||
local s = sqrt(1 + m22 - m00 - m11) * 2
|
||||
qw, qx, qy, qz = (m10 - m01) / s, (m02 + m20) / s, (m12 + m21) / s, s / 4
|
||||
end
|
||||
local l = sqrt(qx * qx + qy * qy + qz * qz + qw * qw)
|
||||
return { m[13], m[14], m[15] }, { qx / l, qy / l, qz / l, qw / l }, { sx, sy, sz }
|
||||
end
|
||||
|
||||
-- transformPoint / transformDir (column-major m)
|
||||
function DaeMath.point(m: { number }, 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
|
||||
|
||||
-- the inverse transpose of the upper 3x3 (for normals), as a column-major 4x4
|
||||
function DaeMath.normalMatrix(m: { number }): { number }
|
||||
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 abs(det) < 1e-30 then
|
||||
return DaeMath.identity()
|
||||
end
|
||||
local k = 1 / det
|
||||
-- inverse = adj / det; its transpose has rows = cofactors
|
||||
return {
|
||||
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,
|
||||
}
|
||||
end
|
||||
|
||||
return DaeMath
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- model/dae/Read: a parsed Collada document (codecs/Xml DOM) -> Scene. Geometry (triangles / polylist / polygons /
|
||||
-- model/dae/Read: a parsed Collada document (codecs/text/Xml DOM) -> Scene. Geometry (triangles / polylist / polygons /
|
||||
-- lines / linestrips / tristrips / trifans with shared input offsets, de-indexed per index tuple), common-profile
|
||||
-- materials (phong / blinn / lambert / constant -> PBR + extras.phong) with their sampler2D / surface / image chains,
|
||||
-- images (kept encoded through the fetch callback, or embedded hex <data>), the visual scene's node tree (matrix /
|
||||
@@ -10,9 +10,10 @@
|
||||
-- channels; one animation per animation clip, else one for the file).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local M = require("@formats/model/dae/Math")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -919,14 +920,14 @@ local function meshFor(C: Ctx, geomEl: any, inst: any, key: string, bindShape: {
|
||||
local pos = copyAccessor(attrs.POSITION)
|
||||
for v = 0, pos.count - 1 do
|
||||
local x, y, z = buffer.readf32(pos.data, v * 12), buffer.readf32(pos.data, v * 12 + 4), buffer.readf32(pos.data, v * 12 + 8)
|
||||
x, y, z = M.point(bindShape, x, y, z)
|
||||
x, y, z = Mat4.point(bindShape, x, y, z)
|
||||
buffer.writef32(pos.data, v * 12, x)
|
||||
buffer.writef32(pos.data, v * 12 + 4, y)
|
||||
buffer.writef32(pos.data, v * 12 + 8, z)
|
||||
end
|
||||
attrs.POSITION = pos
|
||||
if attrs.NORMAL then
|
||||
local nm = M.normalMatrix(bindShape)
|
||||
local nm = Mat4.normalMatrix(bindShape)
|
||||
local nrm = copyAccessor(attrs.NORMAL)
|
||||
for v = 0, nrm.count - 1 do
|
||||
local x, y, z = buffer.readf32(nrm.data, v * 12), buffer.readf32(nrm.data, v * 12 + 4), buffer.readf32(nrm.data, v * 12 + 8)
|
||||
@@ -1126,31 +1127,31 @@ local XFORMS = { matrix = 16, translate = 3, rotate = 4, scale = 3, lookat = 9,
|
||||
-- the matrix of one transform element's values
|
||||
local function xfMatrix(kind: string, v: { number }): { number }
|
||||
if kind == "matrix" then
|
||||
return M.fromRows(v)
|
||||
return Mat4.fromRows(v)
|
||||
elseif kind == "translate" then
|
||||
return M.translation(v[1] or 0, v[2] or 0, v[3] or 0)
|
||||
return Mat4.translation(v[1] or 0, v[2] or 0, v[3] or 0)
|
||||
elseif kind == "rotate" then
|
||||
return M.quatMatrix(M.axisAngle(v[1] or 0, v[2] or 0, v[3] or 1, v[4] or 0))
|
||||
return Mat4.fromQuat(Mat4.axisAngle(v[1] or 0, v[2] or 0, v[3] or 1, v[4] or 0))
|
||||
elseif kind == "scale" then
|
||||
return M.scaling(v[1] or 1, v[2] or 1, v[3] or 1)
|
||||
return Mat4.scaling(v[1] or 1, v[2] or 1, v[3] or 1)
|
||||
elseif kind == "lookat" then
|
||||
return M.lookat(v)
|
||||
elseif kind == "skew" then
|
||||
return M.skew(v)
|
||||
end
|
||||
return M.identity()
|
||||
return Mat4.identity()
|
||||
end
|
||||
|
||||
local function compose(xf: { any }): { number }
|
||||
local m = M.identity()
|
||||
local m = Mat4.identity()
|
||||
for _, x in xf do
|
||||
m = M.mul(m, xfMatrix(x.kind, x.values))
|
||||
m = Mat4.mul(m, xfMatrix(x.kind, x.values))
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
local function setTransform(node: Types.Node, m: { number })
|
||||
local t, r, s = M.decompose(m)
|
||||
local t, r, s = Mat4.decomposeStrict(m)
|
||||
node.translation, node.rotation, node.scale, node.matrix = nil, nil, nil, nil
|
||||
if t and r and s then
|
||||
if t[1] ~= 0 or t[2] ~= 0 or t[3] ~= 0 then
|
||||
@@ -1164,7 +1165,7 @@ local function setTransform(node: Types.Node, m: { number })
|
||||
end
|
||||
else
|
||||
for i = 1, 16 do
|
||||
if math.abs(m[i] - M.identity()[i]) > 0 then
|
||||
if math.abs(m[i] - Mat4.identity()[i]) > 0 then
|
||||
node.matrix = m
|
||||
break
|
||||
end
|
||||
@@ -1219,7 +1220,7 @@ local function instances(C: Ctx, el: any, node: Types.Node, idx: number)
|
||||
end
|
||||
if src and lname(src) == "geometry" then
|
||||
local bsm = child(skinEl, "bind_shape_matrix")
|
||||
local bind = if bsm then M.fromRows(numbers(text(bsm), 16) or {}) else nil
|
||||
local bind = if bsm then Mat4.fromRows(numbers(text(bsm), 16) or {}) else nil
|
||||
local mi, built = meshFor(C, src, ch, "skin:" .. (attr(ctrl, "id") or ""), bind)
|
||||
if C.err then
|
||||
return
|
||||
@@ -1414,7 +1415,7 @@ local function resolveSkins(C: Ctx)
|
||||
if ibm and ibm.comps == 16 and ibm.count >= #names then
|
||||
local b = buffer.create(#names * 64)
|
||||
for i = 0, #names - 1 do
|
||||
local cm = M.fromRows(ibm.data, i * 16 + 1)
|
||||
local cm = Mat4.fromRows(ibm.data, i * 16 + 1)
|
||||
for k = 1, 16 do
|
||||
buffer.writef32(b, (i * 16 + k - 1) * 4, cm[k])
|
||||
end
|
||||
@@ -1715,7 +1716,7 @@ local function bake(C: Ctx, name: string?, byNode: { [number]: { Curve } }, orde
|
||||
end
|
||||
end
|
||||
end
|
||||
local tr, ro, sc = M.decompose(compose(xf))
|
||||
local tr, ro, sc = Mat4.decomposeStrict(compose(xf))
|
||||
tr, ro, sc = tr or { 0, 0, 0 }, ro or { 0, 0, 0, 1 }, sc or { 1, 1, 1 }
|
||||
if prev and prev[1] * ro[1] + prev[2] * ro[2] + prev[3] * ro[3] + prev[4] * ro[4] < 0 then
|
||||
ro = { -ro[1], -ro[2], -ro[3], -ro[4] }
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
-- extras.phong; images as files (opts.files), their uri, or data: URIs; the node tree as row-major <matrix>; cameras;
|
||||
-- lights. Not written: skins, morph targets, animations.
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local M = require("@formats/model/dae/Math")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
|
||||
local Write = {}
|
||||
|
||||
@@ -325,7 +325,7 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
return
|
||||
end
|
||||
seen[i] = true
|
||||
w(format('<node id="node%d"%s type="NODE"><matrix sid="transform">%s</matrix>', i, nameAttr(n.name), list(M.toRows(Scene.nodeMatrix(n)))))
|
||||
w(format('<node id="node%d"%s type="NODE"><matrix sid="transform">%s</matrix>', i, nameAttr(n.name), list(Mat4.toRows(Scene.nodeMatrix(n)))))
|
||||
if n.mesh and geoms[n.mesh] then
|
||||
for _, g in geoms[n.mesh] do
|
||||
if g.material then
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Zip = require("@formats/containers/Zip")
|
||||
local Read = require("@formats/model/dae/Read")
|
||||
local Write = require("@formats/model/dae/Write")
|
||||
@@ -37,7 +37,7 @@ local Dae = {
|
||||
category = "model",
|
||||
extensions = { "dae", "zae" },
|
||||
mime = { "model/vnd.collada+xml", "model/vnd.collada+zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "core/Base64", "containers/Zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Xml", "core/Base64", "containers/Zip", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, materials = true, polygons = true, skins = true, morphTargets = true, animation = true, cameras = true, lights = true, zae = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -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 },
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ local Err = require("@formats/core/Err")
|
||||
local Sniff = require("@formats/core/Sniff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
local Json = require("@formats/codecs/text/Json")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Read = require("@formats/model/gltf/Read")
|
||||
local Write = require("@formats/model/gltf/Write")
|
||||
@@ -31,7 +31,7 @@ local Gltf = {
|
||||
category = "model",
|
||||
extensions = { "gltf", "glb" },
|
||||
mime = { "model/gltf+json", "model/gltf-binary" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "core/Base64", "core/Sniff" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Json", "core/Base64", "core/Sniff" },
|
||||
caps = { decode = true, encode = true, binary = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Flatten = require("@formats/codecs/MeshFlatten")
|
||||
local Flatten = require("@formats/codecs/model/MeshFlatten")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Off = {
|
||||
@@ -29,7 +29,7 @@ local Off = {
|
||||
category = "model",
|
||||
extensions = { "off" },
|
||||
mime = { "model/off", "text/plain" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/MeshFlatten" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/MeshFlatten" },
|
||||
caps = { decode = true, encode = true, polygons = true, points = true, colors = true, binary = false },
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Binary = require("@formats/model/ply/Binary")
|
||||
local Flatten = require("@formats/codecs/MeshFlatten")
|
||||
local Flatten = require("@formats/codecs/model/MeshFlatten")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Ply = {
|
||||
@@ -33,7 +33,7 @@ local Ply = {
|
||||
category = "model",
|
||||
extensions = { "ply" },
|
||||
mime = { "model/ply", "application/ply" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/MeshFlatten" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/MeshFlatten" },
|
||||
caps = { decode = true, encode = true, ascii = true, binary = true, polygons = true, points = true, colors = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Decode = require("@formats/model/stl/Decode")
|
||||
local Flatten = require("@formats/codecs/MeshFlatten")
|
||||
local Flatten = require("@formats/codecs/model/MeshFlatten")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Stl = {
|
||||
@@ -29,7 +29,7 @@ local Stl = {
|
||||
category = "model",
|
||||
extensions = { "stl" },
|
||||
mime = { "model/stl", "application/sla" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/MeshFlatten" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/MeshFlatten" },
|
||||
caps = { decode = true, encode = true, ascii = true, binary = true, colors = true, indexed = false },
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
-- Not supported: composition arcs (references, payloads, inherits, specializes, subLayers: listed in
|
||||
-- meta.source.unsupported), UsdSkel, blend shapes, BasisCurves / Points / NURBS, instancing, MaterialX networks.
|
||||
local Types = require("@formats/Types")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Parse = require("@formats/model/usda/Parse")
|
||||
|
||||
local Build = {}
|
||||
@@ -149,95 +150,13 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ matrices
|
||||
-- column-major 4x4 (glTF layout = USD's row-major row-vector text order)
|
||||
local mul = Types.Scene.mul
|
||||
local function translateM(x: number, y: number, z: number): { number }
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
|
||||
end
|
||||
local function scaleM(x: number, y: number, z: number): { number }
|
||||
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
local function rotM(axis: number, deg: number): { number }
|
||||
local c, s = math.cos(deg * DEG), math.sin(deg * DEG)
|
||||
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 function quatM(x: number, y: number, z: number, w: number): { number }
|
||||
local l = math.sqrt(x * x + y * y + z * z + w * w)
|
||||
if l > 0 then
|
||||
x, y, z, w = x / l, y / l, z / l, w / l
|
||||
end
|
||||
return Types.Scene.nodeMatrix({ children = {}, rotation = { x, y, z, w } })
|
||||
return Mat4.fromQuat({ x, y, z, w })
|
||||
end
|
||||
local function inverse(m: { number }): { number }?
|
||||
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
|
||||
Build.inverse = inverse
|
||||
|
||||
-- decompose(m) -> translation, rotation (quaternion x y z w), scale (no shear)
|
||||
local function decompose(m: { number }): ({ number }, { number }, { number })
|
||||
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[7] * m[10]) - m[5] * (m[2] * m[11] - m[3] * m[10]) + m[9] * (m[2] * m[7] - m[3] * m[6])
|
||||
if det < 0 then
|
||||
sx = -sx
|
||||
end
|
||||
local function d(v: number, s: number): number
|
||||
return if s ~= 0 then v / s else 0
|
||||
end
|
||||
local r00, r10, r20 = d(m[1], sx), d(m[2], sx), d(m[3], sx)
|
||||
local r01, r11, r21 = d(m[5], sy), d(m[6], sy), d(m[7], sy)
|
||||
local r02, r12, r22 = d(m[9], sz), d(m[10], sz), d(m[11], sz)
|
||||
local x, y, z, w
|
||||
local tr = r00 + r11 + r22
|
||||
if tr > 0 then
|
||||
local s = math.sqrt(tr + 1) * 2
|
||||
w, x, y, z = 0.25 * s, (r21 - r12) / s, (r02 - r20) / s, (r10 - r01) / s
|
||||
elseif r00 > r11 and r00 > r22 then
|
||||
local s = math.sqrt(1 + r00 - r11 - r22) * 2
|
||||
w, x, y, z = (r21 - r12) / s, 0.25 * s, (r01 + r10) / s, (r02 + r20) / s
|
||||
elseif r11 > r22 then
|
||||
local s = math.sqrt(1 + r11 - r00 - r22) * 2
|
||||
w, x, y, z = (r02 - r20) / s, (r01 + r10) / s, 0.25 * s, (r12 + r21) / s
|
||||
else
|
||||
local s = math.sqrt(1 + r22 - r00 - r11) * 2
|
||||
w, x, y, z = (r10 - r01) / s, (r02 + r20) / s, (r12 + r21) / s, 0.25 * s
|
||||
end
|
||||
local l = math.sqrt(x * x + y * y + z * z + w * w)
|
||||
if l > 0 then
|
||||
x, y, z, w = x / l, y / l, z / l, w / l
|
||||
end
|
||||
return { m[13], m[14], m[15] }, { x, y, z, w }, { sx, sy, sz }
|
||||
end
|
||||
Build.decompose = decompose
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ xform ops
|
||||
type Op = { kind: string, axes: string?, prop: Prop, invert: boolean }
|
||||
@@ -283,10 +202,10 @@ local function opMatrix(op: Op, v: any): { number }?
|
||||
local m: { number }?
|
||||
local k = op.kind
|
||||
if k == "translate" then
|
||||
m = translateM(n[1] or 0, n[2] or 0, n[3] or 0)
|
||||
m = Mat4.translation(n[1] or 0, n[2] or 0, n[3] or 0)
|
||||
elseif k == "scale" then
|
||||
local s1 = n[1] or 1
|
||||
m = scaleM(s1, n[2] or s1, n[3] or s1)
|
||||
m = Mat4.scaling(s1, n[2] or s1, n[3] or s1)
|
||||
elseif k == "orient" then
|
||||
-- USD quaternions are written (real, i, j, k)
|
||||
m = quatM(n[2] or 0, n[3] or 0, n[4] or 0, n[1] or 1)
|
||||
@@ -302,14 +221,14 @@ local function opMatrix(op: Op, v: any): { number }?
|
||||
for c = 1, #axes do
|
||||
local a = AXIS[string.sub(axes, c, c)]
|
||||
local angle = if #axes == 1 then n[1] or 0 else n[a] or 0
|
||||
r = mul(rotM(a, angle), r)
|
||||
r = mul(Mat4.rotation(a, angle), r)
|
||||
end
|
||||
m = r
|
||||
else
|
||||
return nil
|
||||
end
|
||||
if op.invert and m then
|
||||
m = inverse(m)
|
||||
m = Mat4.inverseFull(m)
|
||||
end
|
||||
return m
|
||||
end
|
||||
@@ -1253,7 +1172,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
local sc = Types.Scene.accessor(nil, "f32", 3, count)
|
||||
local prev: { number }? = nil
|
||||
for k = 1, count do
|
||||
local T, R, S = decompose(localMatrix(ops, times[k]))
|
||||
local T, R, S = Mat4.decompose(localMatrix(ops, times[k]))
|
||||
if prev and prev[1] * R[1] + prev[2] * R[2] + prev[3] * R[3] + prev[4] * R[4] < 0 then
|
||||
R = { -R[1], -R[2], -R[3], -R[4] }
|
||||
end
|
||||
@@ -1270,7 +1189,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
table.insert(animation.channels, { node = ni, path = "translation", input = tin, output = tr, interpolation = "LINEAR" } :: Types.Channel)
|
||||
table.insert(animation.channels, { node = ni, path = "rotation", input = tin, output = ro, interpolation = "LINEAR" } :: Types.Channel)
|
||||
table.insert(animation.channels, { node = ni, path = "scale", input = tin, output = sc, interpolation = "LINEAR" } :: Types.Channel)
|
||||
local T, R, S = decompose(localMatrix(ops, times[1]))
|
||||
local T, R, S = Mat4.decompose(localMatrix(ops, times[1]))
|
||||
n.translation, n.rotation, n.scale = T, R, S
|
||||
elseif #ops > 0 then
|
||||
-- static: TRS when the stack is translate / rotate / scale in that order, else the matrix
|
||||
@@ -1285,7 +1204,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
local m = localMatrix(ops, nil)
|
||||
if simple then
|
||||
local T, R, S = decompose(m)
|
||||
local T, R, S = Mat4.decompose(m)
|
||||
local tx = ops[1].kind == "translate"
|
||||
if tx then
|
||||
n.translation = T
|
||||
|
||||
@@ -31,7 +31,7 @@ local Usda = {
|
||||
category = "model",
|
||||
extensions = { "usda", "usd", "usdz" },
|
||||
mime = { "model/vnd.usda", "model/vnd.usdz+zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, materials = true, animation = true, cameras = true, lights = true, polygons = true, vertexColors = true, usdz = "read", usdc = false },
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user