model/gltf: mesh.extras.targetNames from and to glTF mesh extras; ops/MeshOps.merge: targets = true concatenates morph targets by name

This commit is contained in:
4DBug committed 2026-11-02 21:53:22 -06:00
1 parent 835ddde65b
commit 4231149f3e
6 files changed
+148 -2

No files matched your search

+1 -1
View File
@@ -246,7 +246,7 @@ Every function accepts any accessor type and writes `f32` attributes and `u32` i
| Function | Does |
|---|---|
| `merge(scene, opts?)` | bakes every primitive (node transforms applied) into one triangle primitive; `byMaterial = true` returns one per material. Lines and points are skipped; skins / morphs stay in rest pose |
| `merge(scene, opts?)` | bakes every primitive (node transforms applied) into one triangle primitive; `byMaterial = true` returns one per material. Lines and points are skipped; skins / morphs stay in rest pose; `targets = true` keeps the morph targets, merged by name (`mesh.extras.targetNames`, else by index) and zero where a primitive lacks one, named in the result's `extras.targetNames` with default weights in `extras.weights` |
| `bounds(primitive \| scene)` | `min { x, y, z }, max { x, y, z }` (a scene: world space) |
| `transform(prim, matrix)` | in place: positions, normals, tangents (and morph deltas); a mirroring matrix flips the winding |
| `triangulate(prim, opts?)` | a new triangle primitive from strips, fans and polygons (`method = "earclip"` default, or `"fan"`) |
+10
View File
@@ -460,6 +460,16 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1))
end
local mesh: Types.Mesh = { name = str(m.name), primitives = {}, weights = if type(m.weights) == "table" then table.clone(m.weights) else nil, extras = keep(m, nil) }
-- morph target names: the extras.targetNames convention (Blender's exporter, three.js, Babylon)
local tn = type(m.extras) == "table" and m.extras.targetNames
if type(tn) == "table" and #tn > 0 then
local names = {}
for k, v in tn do
names[k] = if type(v) == "string" then v else tostring(v)
end
mesh.extras = mesh.extras or {}
;(mesh.extras :: any).targetNames = names
end
for k, p in arr(m.primitives) do
if type(p) ~= "table" or type(p.attributes) ~= "table" then
return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: no attributes", i - 1, k - 1))
+5
View File
@@ -445,6 +445,11 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?)
end
g.weights = mesh.weights
restore(g, mesh.extras)
local tn = mesh.extras and (mesh.extras :: any).targetNames
if type(tn) == "table" and #tn > 0 and g.extras == nil then
g.extras = object()
g.extras.targetNames = table.clone(tn)
end
table.insert(meshes, g)
end
local cameras: { any } = {}
+89 -1
View File
@@ -732,7 +732,10 @@ local function defaultFor(name: string, k: number): number
return 0
end
function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? }?): (any, Err.Err?)
-- opts.targets: morph targets concatenated by name (mesh.extras.targetNames, else prim.extras.targetNames; unnamed
-- ones by index), zero where a primitive lacks one; the result's extras.targetNames / extras.weights name them and
-- give their default weights (node weights, else mesh weights)
function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?, targets: boolean? }?): (any, Err.Err?)
local placed: { Types.Placed } = {}
if type(input) ~= "table" then
return nil, Err.new(Err.ARG, "merge: expected a Scene or a list of Primitives")
@@ -746,6 +749,9 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?
end
local groups: { [number]: { Primitive } } = {}
local order: { number } = {}
local withTargets = opts ~= nil and opts.targets == true
local keysOf: { [Primitive]: { any } } = {} -- per baked primitive: the key (name or index) of each target
local weightOf: { [any]: number } = {}
local allPoints = #placed > 0
for _, pl in placed do
if pl.primitive.mode ~= "points" then
@@ -765,6 +771,27 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?
for name, a in t.attributes do
copy.attributes[name] = MeshOps.f32(a)
end
if withTargets and t.targets and #t.targets > 0 then
local mesh = if input.kind == "scene" and pl.mesh > 0 then input.meshes[pl.mesh] else nil
local node = if input.kind == "scene" and pl.node > 0 then input.nodes[pl.node] else nil
local names = mesh and mesh.extras and mesh.extras.targetNames or (p.extras and (p.extras :: any).targetNames)
local weights = node and node.weights or (mesh and mesh.weights)
local keys, targets = {}, {}
for k, tg in t.targets do
local key = if type(names) == "table" and type(names[k]) == "string" then names[k] else k
keys[k] = key
if weightOf[key] == nil then
weightOf[key] = weights and weights[k] or 0
end
local own = {}
for name, a in tg do
own[name] = MeshOps.f32(a)
end
targets[k] = own
end
copy.targets = targets
keysOf[copy] = keys
end
if pl.matrix ~= Scene.IDENTITY then
local ok, terr = MeshOps.transform(copy, pl.matrix)
if not ok then
@@ -810,6 +837,56 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?
end
attrs[name] = Scene.accessor(out, "f32", comps, nv)
end
local targets: { { [string]: Accessor } }? = nil
local targetKeys: { any } = {}
if withTargets then
local index: { [any]: number } = {}
for _, p in list do
for _, key in keysOf[p] or {} do
if not index[key] then
table.insert(targetKeys, key)
index[key] = #targetKeys
end
end
end
if #targetKeys > 0 then
local out: { { [string]: Accessor } } = {}
for ti, key in targetKeys do
local comps: { [string]: number } = {}
for _, p in list do
for k, pk in keysOf[p] or {} do
if pk == key then
for name, a in (p.targets :: any)[k] do
comps[name] = math.max(comps[name] or 0, a.components)
end
end
end
end
local tgt: { [string]: Accessor } = {}
for name, nc in comps do
local buf = buffer.create(nv * nc * 4) -- zero where a primitive lacks the target
local base = 0
for _, p in list do
local cnt = vertexCount(p)
for k, pk in keysOf[p] or {} do
local a = if pk == key then (p.targets :: any)[k][name] else nil
if a then
for v = 0, math.min(cnt, a.count) - 1 do
for c = 0, math.min(nc, a.components) - 1 do
writef32(buf, ((base + v) * nc + c) * 4, readf32(a.data, (v * a.components + c) * 4))
end
end
end
end
base += cnt
end
tgt[name] = Scene.accessor(buf, "f32", nc, nv)
end
out[ti] = tgt
end
targets = out
end
end
local idx = buffer.create(ni * 4)
local base, at = 0, 0
for _, p in list do
@@ -820,11 +897,22 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string?
at += n
base += vertexCount(p)
end
local extras: { [string]: any }? = nil
if targets then
local names, weights = {}, {}
for k, key in targetKeys do
names[k] = if type(key) == "string" then key else tostring(key)
weights[k] = weightOf[key] or 0
end
extras = { targetNames = names, weights = weights }
end
return {
attributes = attrs,
indices = if points then nil else Scene.accessor(idx, "u32", 1, ni),
mode = if points then "points" else "triangles",
material = material,
targets = targets,
extras = extras,
}
end
if opts and opts.byMaterial then
+5
View File
@@ -229,6 +229,11 @@ do
t:check(#skin.animations == 1 and skin.animations[1].channels[1].path == "rotation", "skin animation")
local morph = load("SimpleMorph/glTF")
t:check(#morph.meshes[1].primitives[1].targets == 2 and morph.meshes[1].weights[1] == 0.5, "morph targets, weights")
-- mesh.extras.targetNames: written as glTF mesh extras, read back from them
morph.meshes[1].extras = { targetNames = { "first", "second" } }
local named = decode(Gltf.encode(morph, { binary = true }) :: buffer) :: any
local tn = named and named.meshes[1].extras and named.meshes[1].extras.targetNames
t:check(tn and tn[1] == "first" and tn[2] == "second", "targetNames round trip")
local q = load("AnimatedMorphCube/glTF-Quantized")
local qp = q.meshes[1].primitives[1]
t:check(qp.attributes.NORMAL.type == "snorm8" and qp.targets and qp.targets[1].POSITION.type == "i16", "quantized normals / target deltas")
+38
View File
@@ -290,6 +290,44 @@ do
t:check(e and e.code == Err.ARG, "merge junk")
end
-- merge with morph targets: by name across meshes, zero where missing, deltas through the node's linear part only
do
local s = S.new()
local tri = { 0, 0, 0, 1, 0, 0, 0, 1, 0 }
local a = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = {
{ POSITION = f32({ 0, 0, 1, 0, 0, 1, 0, 0, 1 }, 3) }, { POSITION = f32({ 1, 0, 0, 1, 0, 0, 1, 0, 0 }, 3) },
} }
local b = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = {
{ POSITION = f32({ 0, 2, 0, 0, 2, 0, 0, 2, 0 }, 3) }, { POSITION = f32({ 0, 0, 3, 0, 0, 3, 0, 0, 3 }, 3) },
} }
local c = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles" }
s.meshes = {
{ primitives = { a }, weights = { 0.5, 0.25 }, extras = { targetNames = { "smile", "blink" } } },
{ primitives = { b }, weights = { 0.75, 1 }, extras = { targetNames = { "blink", "puff" } } },
{ primitives = { c } },
}
s.nodes = {
{ children = {}, mesh = 1, scale = { 2, 2, 2 } },
{ children = {}, mesh = 2, translation = { 5, 0, 0 } },
{ children = {}, mesh = 3 },
}
s.roots = { 1, 2, 3 }
t:check(MeshOps.merge(s).targets == nil, "merge drops targets by default")
local m = MeshOps.merge(s, { targets = true }) :: any
local names = m.extras and m.extras.targetNames or {}
t:check(#m.targets == 3 and names[1] == "smile" and names[2] == "blink" and names[3] == "puff", "targets merged by name")
t:check(m.extras.weights[1] == 0.5 and m.extras.weights[2] == 0.25 and m.extras.weights[3] == 1, "default weights (first mesh wins)")
local function d(ti: number, v: number, k: number): number
return get(m.targets[ti].POSITION, v, k)
end
t:check(near(d(1, 0, 2), 2) and near(d(1, 3, 2), 0) and near(d(1, 6, 2), 0), "smile: scaled by the node, zero elsewhere")
t:check(near(d(2, 0, 0), 2) and near(d(2, 3, 1), 2) and near(d(2, 6, 0), 0), "blink from both meshes, translation ignored")
t:check(near(d(3, 3, 2), 3) and near(d(3, 0, 2), 0), "puff from the second mesh only")
t:check(get(a.targets[1].POSITION, 0, 2) == 1, "the source targets are untouched")
local list = MeshOps.merge({ a, c }, { targets = true }) :: any
t:check(#list.targets == 2 and list.extras.targetNames[1] == "1", "a list of primitives: targets by index")
end
do
local s = S.fromPrimitive(MeshOps.triangulate(cube()) :: any)
s.nodes[1].translation = { 1, 2, 3 }