ws-3d3: model/gltf: KHR_draco_mesh_compression through codecs/model/Draco
Draco primitives decode on load (attributes as their accessors declare them, u32 indices), written back uncompressed without the extension; meshopt stays UNSUPPORTED. Tests: transcoded Khronos samples (two quantizations), Draco's and assimp's Draco glTF files, each Draco primitive byte for byte against libdraco, against the uncompressed sources, round trips. Draco bench line; scene.md section.
This commit is contained in:
1 parent
9a6ddd5c16
commit
c3711a7ab1
10 files changed
+455
-14
No files matched your search
@@ -404,8 +404,8 @@ local list: { Entry } = {
|
||||
path = "model/gltf",
|
||||
extensions = { "gltf", "glb" },
|
||||
mime = { "model/gltf+json", "model/gltf-binary" },
|
||||
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 },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Json", "core/Base64", "core/Sniff", "codecs/model/Draco" },
|
||||
caps = { decode = true, encode = true, binary = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, draco = true },
|
||||
load = function()
|
||||
return require("@formats/model/gltf")
|
||||
end,
|
||||
|
||||
@@ -9,6 +9,7 @@ local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Pack = require("@formats/model/gltf/Pack")
|
||||
local Draco = require("@formats/codecs/model/Draco")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -50,6 +51,7 @@ local KNOWN = {
|
||||
EXT_texture_avif = true,
|
||||
KHR_node_visibility = true,
|
||||
KHR_xmp_json_ld = true,
|
||||
KHR_draco_mesh_compression = true,
|
||||
}
|
||||
Read.KNOWN = KNOWN
|
||||
|
||||
@@ -100,6 +102,57 @@ local function keep(obj: any, handled: { [string]: boolean }?): { [string]: any
|
||||
return { gltf = { extras = extras, extensions = ext } }
|
||||
end
|
||||
|
||||
-- a Draco attribute as the type its glTF accessor declares (the bytes as they are when the layouts agree, else value
|
||||
-- by value: normalized integers <-> floats, ints widened or narrowed)
|
||||
local DRACO_HANDLED = { KHR_draco_mesh_compression = true }
|
||||
local DRACO_SIZE = { i8 = 1, u8 = 1, i16 = 2, u16 = 2, i32 = 4, u32 = 4, f32 = 4 }
|
||||
local DRACO_SAME: { [string]: string } = { i8 = "i8", u8 = "u8", i16 = "i16", u16 = "u16", u32 = "u32", f32 = "f32", snorm8 = "i8", unorm8 = "u8", snorm16 = "i16", unorm16 = "u16", i32 = "i32" }
|
||||
local DRACO_WRITE: { [string]: (buffer, number, number) -> () } = { i8 = buffer.writei8, u8 = buffer.writeu8, i16 = buffer.writei16, u16 = buffer.writeu16, i32 = buffer.writei32, u32 = buffer.writeu32, f32 = buffer.writef32 }
|
||||
local NORM_MAX: { [string]: number } = { u8 = 255, i8 = 127, u16 = 65535, i16 = 32767, u32 = 4294967295, i32 = 2147483647 }
|
||||
local function dracoAccessor(da: Draco.Attribute, acc: Types.Accessor, n: number): Types.Accessor?
|
||||
if da.components ~= acc.components or not DRACO_SIZE[da.type] then
|
||||
return nil
|
||||
end
|
||||
local out = Types.Scene.accessor(nil, acc.type, acc.components, n)
|
||||
out.name = acc.name
|
||||
if DRACO_SAME[acc.type] == da.type then
|
||||
out.data = da.data
|
||||
return out
|
||||
end
|
||||
local reader = Types.Scene.reader({ data = da.data, type = da.type :: any, components = da.components, count = n })
|
||||
local size = Types.COMPONENT_SIZE[acc.type]
|
||||
local norm = string.match(acc.type, "^[us]norm") ~= nil
|
||||
local base: string = DRACO_SAME[acc.type] or acc.type
|
||||
local wr = DRACO_WRITE[base]
|
||||
local dmax = NORM_MAX[base] or 1
|
||||
if not reader or not wr or not size then
|
||||
return nil
|
||||
end
|
||||
local get = reader :: (number, number) -> number
|
||||
local w = wr :: (buffer, number, number) -> ()
|
||||
local stride = size * acc.components
|
||||
local function put(v: number, c: number, x: number)
|
||||
if base ~= "f32" then
|
||||
if norm then
|
||||
x = math.clamp(x, if base == "u8" or base == "u16" then 0 else -1, 1) * dmax
|
||||
end
|
||||
x = math.floor(x + 0.5)
|
||||
end
|
||||
w(out.data, v * stride + c * size, x)
|
||||
end
|
||||
local srcMax = if da.normalized then NORM_MAX[da.type] else nil
|
||||
for v = 0, n - 1 do
|
||||
for c = 0, da.components - 1 do
|
||||
local x = get(v, c)
|
||||
if srcMax then
|
||||
x = math.max(x / srcMax, -1)
|
||||
end
|
||||
put(v, c, x)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Scene?, Err.Err?)
|
||||
local maxBytes = opts and opts.maxBytes or 2 ^ 30
|
||||
if type(doc) ~= "table" then
|
||||
@@ -422,13 +475,39 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
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))
|
||||
end
|
||||
if type(p.extensions) == "table" and (p.extensions.KHR_draco_mesh_compression or p.extensions.EXT_meshopt_compression) then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "compressed primitives (Draco / meshopt)")
|
||||
local pext = if type(p.extensions) == "table" then p.extensions else nil
|
||||
if pext and pext.EXT_meshopt_compression then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "meshopt compressed primitives (EXT_meshopt_compression)")
|
||||
end
|
||||
-- KHR_draco_mesh_compression: the attributes it lists and the indices come from the Draco data
|
||||
local dext = pext and pext.KHR_draco_mesh_compression
|
||||
local draco: Draco.Geometry? = nil
|
||||
local dmap: { [string]: number } = {}
|
||||
if dext ~= nil then
|
||||
local view = type(dext) == "table" and int(dext.bufferView) and views[dext.bufferView + 1]
|
||||
if not view then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("mesh %d primitive %d: draco: bad bufferView", i - 1, k - 1))
|
||||
end
|
||||
local g, derr = Draco.decode(view.buf, view.offset, view.length, { maxPoints = maxBytes // 4, maxFaces = maxBytes // 12 })
|
||||
if not g then
|
||||
local e = derr :: Err.Err
|
||||
return nil, Err.new(e.code, string.format("mesh %d primitive %d: %s", i - 1, k - 1, e.message))
|
||||
end
|
||||
draco = g
|
||||
for name, id in (if type(dext.attributes) == "table" then dext.attributes else {}) do
|
||||
if type(name) == "string" and int(id) then
|
||||
dmap[name] = id
|
||||
end
|
||||
end
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = {}
|
||||
local count = -1
|
||||
for name, ai in p.attributes do
|
||||
local a = accessor(ai)
|
||||
if a and draco and dmap[name] then
|
||||
local da = Draco.attribute(draco, dmap[name])
|
||||
a = if da then dracoAccessor(da, a, draco.numPoints) else nil
|
||||
end
|
||||
if not a then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("mesh %d: attribute %s: bad accessor", i - 1, tostring(name)))
|
||||
end
|
||||
@@ -443,7 +522,14 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
return nil, Err.new(Err.CORRUPT, "bad primitive mode")
|
||||
end
|
||||
local prim: Types.Primitive = { attributes = attrs, mode = mode :: any, material = if int(p.material) and p.material < #scene.materials then p.material + 1 else nil }
|
||||
if p.indices ~= nil then
|
||||
if draco then
|
||||
if draco.kind == "mesh" then
|
||||
prim.indices = Types.Scene.accessor(draco.indices, "u32", 1, draco.numFaces * 3)
|
||||
prim.mode = "triangles"
|
||||
else
|
||||
prim.mode = "points"
|
||||
end
|
||||
elseif p.indices ~= nil then
|
||||
local ind = accessor(p.indices)
|
||||
if not ind or ind.components ~= 1 or (ind.type ~= "u8" and ind.type ~= "u16" and ind.type ~= "u32") then
|
||||
return nil, Err.new(Err.CORRUPT, "bad indices accessor")
|
||||
@@ -472,7 +558,7 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
prim.targets = targets
|
||||
end
|
||||
prim.extras = keep(p, nil)
|
||||
prim.extras = keep(p, DRACO_HANDLED)
|
||||
table.insert(mesh.primitives, prim)
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
|
||||
@@ -12,8 +12,11 @@
|
||||
-- External .bin / image files come from opts.resolve(uri) or opts.files[uri] (the api sets resolve for paths).
|
||||
-- Writes .gltf (buffers and images embedded as data: URIs by default; opts.external = true writes <name>.bin and the
|
||||
-- images into opts.files) or .glb (opts.binary, or opts.format == "glb": the api passes the destination's extension).
|
||||
-- Not supported: Draco (KHR_draco_mesh_compression) and meshopt (EXT_meshopt_compression) compressed primitives
|
||||
-- (UNSUPPORTED), KHR_animation_pointer channels (skipped and counted in the animation's extras).
|
||||
-- Draco compressed primitives (KHR_draco_mesh_compression) decode through codecs/model/Draco: the attributes the
|
||||
-- extension lists and the indices come from the Draco data, as the types their accessors declare. Written back
|
||||
-- uncompressed (the extension is dropped).
|
||||
-- Not supported: meshopt (EXT_meshopt_compression) compressed primitives (UNSUPPORTED), KHR_animation_pointer channels
|
||||
-- (skipped and counted in the animation's extras).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -31,8 +34,8 @@ local Gltf = {
|
||||
category = "model",
|
||||
extensions = { "gltf", "glb" },
|
||||
mime = { "model/gltf+json", "model/gltf-binary" },
|
||||
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 },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Json", "core/Base64", "core/Sniff", "codecs/model/Draco" },
|
||||
caps = { decode = true, encode = true, binary = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true, draco = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
|
||||
Reference in new issue
Block a user