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

+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 }