merge ws/3d: obj stl ply off vox gltf/glb, ops/MeshOps, codecs Base64/MeshFlatten, api sibling files + texture encoding
This commit is contained in:
commit
91eba2bc24
145 files changed
+16413
-17
No files matched your search
+101
-3
@@ -161,6 +161,26 @@ local function drain(dec: any): any
|
||||
return a
|
||||
end
|
||||
|
||||
-- siblings(path) -> resolve(uri): reads files relative to path's directory (no absolute paths, no "..", no URLs)
|
||||
local function siblings(path: string): (uri: string) -> buffer?
|
||||
local dir = string.match(path, "^(.*[/\\])") or ""
|
||||
return function(uri: string): buffer?
|
||||
if type(uri) ~= "string" or uri == "" or string.find(uri, "^%a[%w+.-]*:") or string.sub(uri, 1, 1) == "/" then
|
||||
return nil
|
||||
end
|
||||
local u = string.gsub(uri, "%%(%x%x)", function(h: string): string
|
||||
return string.char(tonumber(h, 16) :: number)
|
||||
end)
|
||||
for part in string.gmatch(u, "[^/\\]+") do
|
||||
if part == ".." then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local b = host.readFile(dir .. u)
|
||||
return b
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ API
|
||||
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
|
||||
|
||||
@@ -272,6 +292,11 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
if not m then
|
||||
return nil, merr
|
||||
end
|
||||
if o.resolve == nil and name and name == (type(src) == "string" and src or nil) and host.hasFiles then
|
||||
-- files next to the one loaded (OBJ .mtl, glTF .bin / images): opts.resolve(uri) for the decoder
|
||||
o = table.clone(o)
|
||||
o.resolve = siblings(name)
|
||||
end
|
||||
local value, err
|
||||
if m.decode then
|
||||
local ok, v, e = pcall(m.decode, Bytes.reader(buf), withHooks(o))
|
||||
@@ -328,8 +353,10 @@ function F.open(src: any, opts: Options?): (any, Err.Err?)
|
||||
return dec
|
||||
end
|
||||
|
||||
local encodeTextures: (scene: any) -> Err.Err?
|
||||
|
||||
-- the writable format for a value: opts.format, the destination's extension, or the source format
|
||||
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?)
|
||||
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?, string?)
|
||||
local category = o.category
|
||||
if category == nil and type(value) == "table" and type(value.kind) == "string" then
|
||||
category = KIND_CATEGORY[value.kind]
|
||||
@@ -359,16 +386,49 @@ local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entr
|
||||
end
|
||||
return nil, nil, Err.wrap(Err.new(Err.UNSUPPORTED, "read-only format" .. (if alt then "; try " .. alt else "")), entry.name)
|
||||
end
|
||||
return entry, m, nil
|
||||
return entry, m, nil, key
|
||||
end
|
||||
|
||||
-- scene textures with only a decoded image get PNG bytes before a model writer sees them (writers never encode images)
|
||||
function encodeTextures(scene: any): Err.Err?
|
||||
if type(scene.textures) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
for _, tex in scene.textures do
|
||||
if type(tex) == "table" and tex.data == nil and type(tex.image) == "table" and tex.uri == nil then
|
||||
local png, err = F.save(tex.image, nil, { format = "png" })
|
||||
if not png then
|
||||
return err
|
||||
end
|
||||
tex.data, tex.mime = png, "image/png"
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err
|
||||
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
||||
local o = opts or {}
|
||||
local entry, m, err = encoderFor(value, dest, o)
|
||||
local entry, m, err, key = encoderFor(value, dest, o)
|
||||
if not entry then
|
||||
return nil, err
|
||||
end
|
||||
-- the encoder sees the format key it was picked by ("glb" vs "gltf"), the destination's name, and (writing a file)
|
||||
-- an opts.files table for the extra files it writes (OBJ .mtl, glTF .bin)
|
||||
o = table.clone(o)
|
||||
o.format = o.format or key
|
||||
if dest then
|
||||
o.name = o.name or string.match(dest, "([^/\\]+)$")
|
||||
if o.files == nil then
|
||||
o.files = {}
|
||||
end
|
||||
end
|
||||
if type(value) == "table" and value.kind == "scene" then
|
||||
local terr = encodeTextures(value)
|
||||
if terr then
|
||||
return nil, terr
|
||||
end
|
||||
end
|
||||
local ok, buf, e = pcall(m.encode, value, withHooks(o))
|
||||
if not ok then
|
||||
return nil, Err.wrap(Err.new(Err.ARG, "encoder threw: " .. tostring(buf)), entry.name)
|
||||
@@ -381,6 +441,15 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
||||
if not wok then
|
||||
return nil, werr
|
||||
end
|
||||
if type(o.files) == "table" and o.files ~= (opts and opts.files) then
|
||||
local dir = string.match(dest, "^(.*[/\\])") or ""
|
||||
for fname, data in o.files do
|
||||
local fok, ferr = host.writeFile(dir .. fname, if type(data) == "string" then buffer.fromstring(data) else data)
|
||||
if not fok then
|
||||
return nil, ferr
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
return buf
|
||||
@@ -470,4 +539,33 @@ for category in Types.CATEGORIES do
|
||||
end
|
||||
F.data.null = Types.null
|
||||
|
||||
-- F.model.loadTextures(scene, opts?) -> scene, { [index]: Err }: decodes each texture's encoded bytes (texture.data, or
|
||||
-- texture.uri through opts.resolve) into texture.image with F.image.load. Model formats never decode images
|
||||
-- themselves (a format does not require another format); this is the lazy step at the api layer. Textures whose
|
||||
-- image format is missing or broken keep image = nil and report an Err.
|
||||
F.model.loadTextures = function(scene: any, opts: Options?): (any, { [number]: Err.Err })
|
||||
local errs: { [number]: Err.Err } = {}
|
||||
if type(scene) ~= "table" or type(scene.textures) ~= "table" then
|
||||
return scene, errs
|
||||
end
|
||||
local resolve = opts and opts.resolve
|
||||
for i, tex in scene.textures do
|
||||
if type(tex) == "table" and tex.image == nil then
|
||||
local data = tex.data
|
||||
if data == nil and tex.uri and resolve then
|
||||
data = resolve(tex.uri)
|
||||
end
|
||||
if data ~= nil then
|
||||
local img, err = F.load(data, { category = "image", name = tex.uri, data = true })
|
||||
if img then
|
||||
tex.image = img
|
||||
elseif err then
|
||||
errs[i] = err
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return scene, errs
|
||||
end
|
||||
|
||||
return F
|
||||
Reference in new issue
Block a user