rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,221 @@
|
||||
--!strict
|
||||
-- model/usda: USD ASCII layers (.usda, .usd in text form) and USDZ packages holding them. New code, from the Sdf text
|
||||
-- format and the UsdGeom / UsdShade / UsdLux schema documentation (OpenUSD 26.x); checked against OpenUSD's own
|
||||
-- reader / writer (pxr) and Blender's USD exporter.
|
||||
-- Reads: stage metadata (upAxis -> meta.up, metersPerUnit -> meta.unit, defaultPrim, timeCodesPerSecond, start /
|
||||
-- endTimeCode), the prim tree (def / over; class skipped; selected variants merged), xform op stacks (translate, scale,
|
||||
-- rotateX/Y/Z, rotateXYZ ... rotateZYX, orient, transform, !invert!, !resetXformStack!) with time samples -> one
|
||||
-- animation of TRS channels, Mesh (faceVertexCounts / Indices, holes, leftHanded orientation, normals, every
|
||||
-- texCoord2f primvar -> TEXCOORD_n with st first, displayColor / displayOpacity, all interpolations, :indices,
|
||||
-- GeomSubsets -> one primitive each), UsdPreviewSurface materials with UsdUVTexture / UsdPrimvarReader_float2 /
|
||||
-- UsdTransform2d, material:binding (inherited, per subset), Camera, DistantLight / SphereLight (+ ShapingAPI cone ->
|
||||
-- spot) / RectLight / DiskLight (-> spot lights over a hemisphere). Textures stay encoded: bytes from opts.files /
|
||||
-- opts.resolve (or the USDZ package) + mime, else just the uri (F.model.loadTextures decodes them).
|
||||
-- Writes .usda (see model/usda/Write): Xform / Mesh / GeomSubset / Material / Shader / Camera / lights, time samples.
|
||||
-- Not supported: USDC binary crate files (.usdc, and .usd / USDZ holding one: UNSUPPORTED), composition arcs
|
||||
-- (references, payloads, inherits, specializes, subLayers: ignored and listed in meta.source.unsupported), UsdSkel
|
||||
-- skinning, blend shapes, curves / points / NURBS prims, point instancers, MaterialX shading networks, writing USDZ.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Zip = require("@rosetta/containers/Zip")
|
||||
local Parse = require("@rosetta/model/usda/Parse")
|
||||
local Build = require("@rosetta/model/usda/Build")
|
||||
local Write = require("@rosetta/model/usda/Write")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Usda = {
|
||||
name = "usda",
|
||||
category = "model",
|
||||
extensions = { "usda", "usd", "usdz" },
|
||||
mime = { "model/vnd.usda", "model/vnd.usdz+zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, materials = true, animation = true, cameras = true, lights = true, polygons = true, vertexColors = true, usdz = "read", usdc = false },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
triangulate: boolean?, -- fan-triangulate faces (default true); false keeps "polygons" primitives
|
||||
flipV: boolean?, -- TEXCOORD_n.v = 1 - v (default true: glTF's top-left origin)
|
||||
resolve: ((uri: string) -> any)?, -- texture files next to the layer (the api sets it for paths)
|
||||
files: { [string]: any }?, -- or: the files by asset path
|
||||
maxVertices: number?, -- refuse meshes with more points / face corners (default 2^25)
|
||||
maxBytes: number?, -- refuse numeric arrays over this many bytes (default 2^30)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
files: { [string]: any }?, -- receives embedded texture bytes as <name>_<n>.<ext>
|
||||
name: string?, -- the destination's file name (names the texture files)
|
||||
format: string?, -- "usdz" -> UNSUPPORTED (set by the api from the destination's extension)
|
||||
upAxis: string?, -- "Y" / "Z" (default: from scene.meta.up)
|
||||
metersPerUnit: number?, -- default scene.meta.unit or 1
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function isUsdz(r: Bytes.Reader): boolean
|
||||
if r:peek(4) ~= "PK\3\4" then
|
||||
return false
|
||||
end
|
||||
-- the first entry is the root layer
|
||||
local lo, hi = r:peekU8(26), r:peekU8(27)
|
||||
if lo < 0 or hi < 0 then
|
||||
return false
|
||||
end
|
||||
local nameLen = lo + hi * 256
|
||||
local name = string.lower(r:peek(30 + nameLen))
|
||||
name = string.sub(name, 31)
|
||||
return string.find(name, "%.usd[ac]?$") ~= nil
|
||||
end
|
||||
|
||||
function Usda.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(8)
|
||||
if string.sub(head, 1, 5) == "#usda" or string.sub(head, 1, 8) == "\239\187\191#usda" then
|
||||
return 1
|
||||
end
|
||||
if head == "PXR-USDC" then
|
||||
return 0.9 -- recognised so the error says USDC is not supported
|
||||
end
|
||||
if isUsdz(r) then
|
||||
return 0.95
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function fetcher(opts: DecodeOptions?, package: any): (uri: string) -> buffer?
|
||||
return function(uri: string): buffer?
|
||||
local candidates = { uri }
|
||||
local slashed = string.gsub(uri, "\\", "/")
|
||||
local stripped = string.gsub(slashed, "^%./", "")
|
||||
if slashed ~= uri then
|
||||
table.insert(candidates, slashed)
|
||||
end
|
||||
if stripped ~= slashed then
|
||||
table.insert(candidates, stripped)
|
||||
end
|
||||
-- packaged assets: "model.usdz[textures/a.png]"
|
||||
local inner = string.match(uri, "%[(.+)%]$")
|
||||
if inner then
|
||||
table.insert(candidates, inner)
|
||||
end
|
||||
for _, u in candidates do
|
||||
if package then
|
||||
local data = package:read(u)
|
||||
if data then
|
||||
return data
|
||||
end
|
||||
end
|
||||
local v: any = nil
|
||||
if opts and type((opts :: DecodeOptions).files) == "table" then
|
||||
v = ((opts :: DecodeOptions).files :: { [string]: any })[u]
|
||||
end
|
||||
if v == nil and opts and type((opts :: DecodeOptions).resolve) == "function" then
|
||||
local ok, res = pcall((opts :: DecodeOptions).resolve :: (uri: string) -> any, u)
|
||||
v = if ok then res else nil
|
||||
end
|
||||
if type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
elseif type(v) == "buffer" then
|
||||
return v
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
function Usda.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local head = r:peek(8)
|
||||
if head == "PXR-USDC" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "USDC binary crate files (convert with usdcat to .usda)")
|
||||
end
|
||||
local text: string
|
||||
local package: any = nil
|
||||
local usdz = false
|
||||
if string.sub(head, 1, 4) == "PK\3\4" then
|
||||
local archive, zerr = Zip.open(r)
|
||||
if not archive then
|
||||
return nil, zerr or Err.new(Err.CORRUPT, "bad USDZ package")
|
||||
end
|
||||
local entries = archive:list()
|
||||
local root = nil
|
||||
for _, e in entries do
|
||||
if not e.dir and string.find(string.lower(e.name), "%.usd[ac]?$") then
|
||||
root = e
|
||||
break
|
||||
end
|
||||
end
|
||||
if not root then
|
||||
return nil, Err.new(Err.CORRUPT, "the USDZ package holds no USD layer")
|
||||
end
|
||||
local data, rerr = archive:read(root)
|
||||
if not data then
|
||||
return nil, rerr
|
||||
end
|
||||
if buffer.len(data) >= 8 and buffer.readstring(data, 0, 8) == "PXR-USDC" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "USDZ holding a USDC binary crate layer")
|
||||
end
|
||||
text = buffer.tostring(data)
|
||||
package = archive
|
||||
usdz = true
|
||||
else
|
||||
text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
end
|
||||
if not Parse.version(text) then
|
||||
return nil, Err.new(Err.CORRUPT, "no #usda header")
|
||||
end
|
||||
local layer, msg, at = Parse.layer(text, opts and opts.maxBytes)
|
||||
if not layer then
|
||||
if msg == "array over the size limit" then
|
||||
return nil, Err.new(Err.LIMIT, msg :: string, at)
|
||||
end
|
||||
local code = if at and at >= #text - 1 then Err.TRUNCATED else Err.CORRUPT
|
||||
if msg and string.find(msg, "^unterminated") then
|
||||
code = Err.TRUNCATED
|
||||
end
|
||||
return nil, Err.new(code, msg or "parse error", if usdz then nil else at)
|
||||
end
|
||||
local scene, err = Build.scene(layer, {
|
||||
triangulate = opts and opts.triangulate,
|
||||
flipV = opts and opts.flipV,
|
||||
maxVertices = opts and opts.maxVertices,
|
||||
fetch = fetcher(opts, package),
|
||||
})
|
||||
if not scene then
|
||||
if err == "LIMIT" then
|
||||
return nil, Err.new(Err.LIMIT, "a mesh over maxVertices")
|
||||
end
|
||||
return nil, Err.new(Err.CORRUPT, err or "bad scene")
|
||||
end
|
||||
local src: any = scene.meta.source
|
||||
src.version = Parse.version(text)
|
||||
src.usdz = usdz or nil
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Usda.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(scene) ~= "table" or scene.kind ~= "scene" then
|
||||
return nil, Err.new(Err.ARG, "needs a Scene")
|
||||
end
|
||||
if opts and opts.format == "usdz" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "writing USDZ; write .usda")
|
||||
end
|
||||
local ok, text = pcall(Write.text, scene, opts)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(text))
|
||||
end
|
||||
return buffer.fromstring(text :: any), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- parse(text) -> the raw layer (prim specs, properties, metadata) | nil, Err: for prims the Scene does not cover
|
||||
function Usda.parse(text: string): (Parse.Layer?, Err.Err?)
|
||||
local layer, msg, at = Parse.layer(text)
|
||||
if not layer then
|
||||
return nil, Err.new(Err.CORRUPT, msg or "parse error", at)
|
||||
end
|
||||
return layer, nil
|
||||
end
|
||||
|
||||
return Usda
|
||||
Reference in new issue
Block a user