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:
1 parent
835ddde65b
commit
4231149f3e
6 files changed
+148
-2
No files matched your search
@@ -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))
|
||||
|
||||
@@ -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 } = {}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user