1277 lines
37 KiB
Luau
1277 lines
37 KiB
Luau
--!strict
|
|
-- model/usda/Build: a USDA layer to a Scene, following the UsdGeom / UsdShade / UsdLux schemas (OpenUSD 26.x). Other
|
|
-- attributes than xform ops use their earliest sample (as UsdTimeCode.EarliestTime()). Not supported: composition arcs,
|
|
-- UsdSkel, blend shapes, BasisCurves / Points / NURBS, instancing, MaterialX networks.
|
|
local Types = require("@rosetta/Types")
|
|
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
|
local Parse = require("@rosetta/model/usda/Parse")
|
|
|
|
local Build = {}
|
|
|
|
type Prim = Parse.Prim
|
|
type Prop = Parse.Prop
|
|
|
|
local NONE = Parse.NONE
|
|
local DEG = math.pi / 180
|
|
|
|
local NOT_NODES = { Material = true, Shader = true, NodeGraph = true, GeomSubset = true, Skeleton = true, SkelAnimation = true, BlendShape = true }
|
|
local SKEL = { Skeleton = true, SkelAnimation = true, BlendShape = true }
|
|
local LIGHTS = { DistantLight = true, SphereLight = true, RectLight = true, DiskLight = true, CylinderLight = true, DomeLight = true }
|
|
local MIME = { png = "image/png", jpg = "image/jpeg", jpeg = "image/jpeg", webp = "image/webp", tga = "image/x-tga", bmp = "image/bmp", exr = "image/x-exr", hdr = "image/vnd.radiance", ktx2 = "image/ktx2", gif = "image/gif", tif = "image/tiff", tiff = "image/tiff", avif = "image/avif" }
|
|
|
|
export type Options = {
|
|
triangulate: boolean?,
|
|
flipV: boolean?,
|
|
fetch: ((uri: string) -> buffer?)?,
|
|
maxVertices: number?,
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ helpers
|
|
local function isArr(v: any): boolean
|
|
return type(v) == "table" and v.t == "arr"
|
|
end
|
|
local function str(v: any): string?
|
|
if type(v) == "string" then
|
|
return v
|
|
elseif type(v) == "table" and (v.t == "id" or v.t == "asset" or v.t == "path") then
|
|
return v.v
|
|
end
|
|
return nil
|
|
end
|
|
local function nums(v: any): { number }?
|
|
if type(v) == "number" then
|
|
return { v }
|
|
elseif type(v) == "boolean" then
|
|
return { if v then 1 else 0 }
|
|
elseif type(v) == "table" and v.t == "tuple" then
|
|
local out = {}
|
|
for k, x in v.v do
|
|
if type(x) == "number" then
|
|
out[k] = x
|
|
elseif type(x) == "table" and x.t == "tuple" then
|
|
for _, y in x.v do
|
|
if type(y) ~= "number" then
|
|
return nil
|
|
end
|
|
table.insert(out, y)
|
|
end
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
return out
|
|
elseif type(v) == "table" and v.t == "arr" then
|
|
return v.v
|
|
end
|
|
return nil
|
|
end
|
|
local function num(v: any): number?
|
|
if type(v) == "number" then
|
|
return v
|
|
elseif type(v) == "boolean" then
|
|
return if v then 1 else 0
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- a property's default, else its earliest time sample
|
|
local function propValue(p: Prop?): any
|
|
if not p then
|
|
return nil
|
|
end
|
|
if p.samples and #p.samples.times > 0 then
|
|
local best, bi = math.huge, 0
|
|
for k, t in p.samples.times do
|
|
if t < best then
|
|
best, bi = t, k
|
|
end
|
|
end
|
|
local v = p.samples.values[bi]
|
|
if v ~= NONE then
|
|
return v
|
|
end
|
|
end
|
|
if p.hasValue and p.value ~= NONE then
|
|
return p.value
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function joinPath(parent: string, name: string): string
|
|
return if parent == "/" then "/" .. name else parent .. "/" .. name
|
|
end
|
|
|
|
local function resolvePath(base: string, p: string): string
|
|
if string.sub(p, 1, 1) == "/" then
|
|
return p
|
|
end
|
|
local parts = string.split(base, "/")
|
|
local out: { string } = {}
|
|
for _, x in parts do
|
|
if x ~= "" then
|
|
table.insert(out, x)
|
|
end
|
|
end
|
|
for _, x in string.split(p, "/") do
|
|
if x == ".." then
|
|
table.remove(out)
|
|
elseif x ~= "." and x ~= "" then
|
|
table.insert(out, x)
|
|
end
|
|
end
|
|
return "/" .. table.concat(out, "/")
|
|
end
|
|
local function splitProp(p: string): (string, string?)
|
|
local prim, prop = string.match(p, "^(.-)%.([^/]+)$")
|
|
if prim and prop then
|
|
return prim, prop
|
|
end
|
|
return p, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ matrices
|
|
-- column-major 4x4 (glTF layout = USD's row-major row-vector text order)
|
|
local mul = Types.Scene.mul
|
|
local function quatM(x: number, y: number, z: number, w: number): { number }
|
|
local l = math.sqrt(x * x + y * y + z * z + w * w)
|
|
if l > 0 then
|
|
x, y, z, w = x / l, y / l, z / l, w / l
|
|
end
|
|
return Mat4.fromQuat({ x, y, z, w })
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ xform ops
|
|
type Op = { kind: string, axes: string?, prop: Prop, invert: boolean }
|
|
|
|
local function xformOps(p: Prim): ({ Op }, boolean)
|
|
local order = p.props.xformOpOrder
|
|
local list = order and propValue(order)
|
|
local ops: { Op } = {}
|
|
local reset = false
|
|
if type(list) ~= "table" or list.t ~= "list" then
|
|
return ops, false
|
|
end
|
|
for _, item in list.v do
|
|
local name = str(item)
|
|
if name == "!resetXformStack!" then
|
|
reset = true
|
|
table.clear(ops)
|
|
elseif name then
|
|
local invert = false
|
|
if string.sub(name, 1, 8) == "!invert!" then
|
|
invert = true
|
|
name = string.sub(name, 9)
|
|
end
|
|
local prop = p.props[name]
|
|
local kind = string.match(name, "^xformOp:(%a+)")
|
|
if prop and kind then
|
|
local axes = string.match(kind, "^rotate([XYZ]+)$")
|
|
table.insert(ops, { kind = if axes then "rotate" else kind, axes = axes, prop = prop, invert = invert })
|
|
end
|
|
end
|
|
end
|
|
return ops, reset
|
|
end
|
|
|
|
local AXIS = { X = 1, Y = 2, Z = 3 }
|
|
local function opMatrix(op: Op, v: any): { number }?
|
|
local n = nums(v)
|
|
if not n then
|
|
return nil
|
|
end
|
|
local m: { number }?
|
|
local k = op.kind
|
|
if k == "translate" then
|
|
m = Mat4.translation(n[1] or 0, n[2] or 0, n[3] or 0)
|
|
elseif k == "scale" then
|
|
local s1 = n[1] or 1
|
|
m = Mat4.scaling(s1, n[2] or s1, n[3] or s1)
|
|
elseif k == "orient" then
|
|
-- USD quaternions are written (real, i, j, k)
|
|
m = quatM(n[2] or 0, n[3] or 0, n[4] or 0, n[1] or 1)
|
|
elseif k == "transform" then
|
|
if #n ~= 16 then
|
|
return nil
|
|
end
|
|
m = table.clone(n)
|
|
elseif k == "rotate" and op.axes then
|
|
local axes = op.axes :: string
|
|
local r: { number } = Types.Scene.IDENTITY :: any
|
|
-- rotateXZY: R = Ry * Rz * Rx (column vectors); the value is always (x, y, z) angles
|
|
for c = 1, #axes do
|
|
local a = AXIS[string.sub(axes, c, c)]
|
|
local angle = if #axes == 1 then n[1] or 0 else n[a] or 0
|
|
r = mul(Mat4.rotation(a, angle), r)
|
|
end
|
|
m = r
|
|
else
|
|
return nil
|
|
end
|
|
if op.invert and m then
|
|
m = Mat4.inverseFull(m)
|
|
end
|
|
return m
|
|
end
|
|
|
|
local function lerpValue(a: any, b: any, f: number, quat: boolean): any
|
|
local na, nb = nums(a), nums(b)
|
|
if not na or not nb or #na ~= #nb then
|
|
return a
|
|
end
|
|
local out = table.create(#na)
|
|
if quat and #na == 4 then
|
|
local dot = 0
|
|
for k = 1, 4 do
|
|
dot += na[k] * nb[k]
|
|
end
|
|
local sgn = if dot < 0 then -1 else 1
|
|
for k = 1, 4 do
|
|
out[k] = na[k] + (nb[k] * sgn - na[k]) * f
|
|
end
|
|
return { t = "tuple", v = out }
|
|
end
|
|
for k = 1, #na do
|
|
out[k] = na[k] + (nb[k] - na[k]) * f
|
|
end
|
|
return { t = "tuple", v = out }
|
|
end
|
|
|
|
-- samples interpolated linearly, held outside their range
|
|
local function opValueAt(op: Op, t: number?): any
|
|
local p = op.prop
|
|
local s = p.samples
|
|
if t ~= nil and s and #s.times > 0 then
|
|
local times, values = s.times, s.values
|
|
local lo, hi
|
|
for k = 1, #times do
|
|
local tk = times[k]
|
|
if values[k] ~= NONE then
|
|
if tk <= t and (lo == nil or tk > times[lo]) then
|
|
lo = k
|
|
end
|
|
if tk >= t and (hi == nil or tk < times[hi]) then
|
|
hi = k
|
|
end
|
|
end
|
|
end
|
|
if lo and hi then
|
|
if lo == hi or times[hi] == times[lo] then
|
|
return values[lo]
|
|
end
|
|
local f = (t - times[lo]) / (times[hi] - times[lo])
|
|
return lerpValue(values[lo], values[hi], f, op.kind == "orient")
|
|
end
|
|
return values[lo or hi]
|
|
end
|
|
return propValue(p)
|
|
end
|
|
|
|
local function localMatrix(ops: { Op }, t: number?): { number }
|
|
local m: { number } = Types.Scene.IDENTITY :: any
|
|
for _, op in ops do
|
|
local om = opMatrix(op, opValueAt(op, t))
|
|
if om then
|
|
m = mul(m, om)
|
|
end
|
|
end
|
|
return m
|
|
end
|
|
Build.localMatrix = localMatrix
|
|
|
|
-- ------------------------------------------------------------------------------------------------ build
|
|
function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
|
local scene = Types.Scene.new()
|
|
local flipV = opts.flipV ~= false
|
|
local triangulate = opts.triangulate ~= false
|
|
local maxVertices = opts.maxVertices or 2 ^ 25
|
|
local fetch = opts.fetch
|
|
local unsupported: { [string]: boolean } = {}
|
|
local meta = layer.meta
|
|
|
|
local byPath: { [string]: Prim } = {}
|
|
local parentOf: { [Prim]: string } = {}
|
|
local pathOf: { [Prim]: string } = {}
|
|
local function merge(p: Prim)
|
|
if not p.variantSets then
|
|
return
|
|
end
|
|
local nested: { any }? = nil
|
|
local sel = p.meta.variants
|
|
local chosen = if type(sel) == "table" and sel.t == "dict" then sel.v else {}
|
|
for setName, set in p.variantSets :: any do
|
|
local name = str(chosen[setName])
|
|
local body = name and set[name] -- unselected sets contribute nothing
|
|
if body then
|
|
for _, pn in body.order do
|
|
if not p.props[pn] then
|
|
p.props[pn] = body.props[pn]
|
|
table.insert(p.order, pn)
|
|
end
|
|
end
|
|
for _, c in body.children do
|
|
table.insert(p.children, c)
|
|
end
|
|
for k, v in body.meta do
|
|
if p.meta[k] == nil then
|
|
p.meta[k] = v
|
|
end
|
|
end
|
|
if body.variantSets then
|
|
nested = nested or {}
|
|
table.insert(nested :: any, body.variantSets)
|
|
end
|
|
end
|
|
end
|
|
p.variantSets = nil
|
|
for _, vs in nested or {} do
|
|
p.variantSets = vs
|
|
merge(p)
|
|
end
|
|
end
|
|
local function index(p: Prim, parent: string, depth: number)
|
|
if depth > 200 then
|
|
return
|
|
end
|
|
merge(p)
|
|
local path = joinPath(parent, p.name)
|
|
pathOf[p] = path
|
|
parentOf[p] = parent
|
|
local existing = byPath[path]
|
|
if existing and existing ~= p then
|
|
-- over / def of the same path in one layer: merged into the first spec
|
|
for _, pn in p.order do
|
|
if not existing.props[pn] then
|
|
existing.props[pn] = p.props[pn]
|
|
table.insert(existing.order, pn)
|
|
end
|
|
end
|
|
existing.typeName = existing.typeName or p.typeName
|
|
if p.specifier == "def" then
|
|
existing.specifier = "def"
|
|
end
|
|
for _, c in p.children do
|
|
table.insert(existing.children, c)
|
|
end
|
|
table.clear(p.children)
|
|
p.specifier = "class"
|
|
else
|
|
byPath[path] = p
|
|
end
|
|
for _, key in { "references", "payload", "inherits", "specializes" } do
|
|
local v = p.meta[key]
|
|
if v ~= nil and v ~= NONE then
|
|
unsupported[key] = true
|
|
end
|
|
end
|
|
for _, c in p.children do
|
|
index(c, path, depth + 1)
|
|
end
|
|
end
|
|
for _, p in layer.prims do
|
|
index(p, "/", 1)
|
|
end
|
|
if meta.subLayers ~= nil then
|
|
unsupported.subLayers = true
|
|
end
|
|
|
|
local function propAt(path: string): (Prim?, Prop?)
|
|
local pp, pn = splitProp(path)
|
|
local prim = byPath[pp]
|
|
if not prim or not pn then
|
|
return prim, nil
|
|
end
|
|
return prim, prim.props[pn]
|
|
end
|
|
|
|
local texCoordIndex: { [string]: number } = {}
|
|
local texCoordCount = 0
|
|
local function texCoordFor(name: string): number
|
|
local k = texCoordIndex[name]
|
|
if k == nil then
|
|
k = texCoordCount
|
|
texCoordIndex[name] = k
|
|
texCoordCount += 1
|
|
end
|
|
return k
|
|
end
|
|
-- "st" first, so materials and meshes agree on TEXCOORD_n
|
|
do
|
|
local names: { [string]: boolean } = {}
|
|
for _, p in byPath do
|
|
if p.typeName == "Mesh" then
|
|
for pn, pr in p.props do
|
|
local nm = string.match(pn, "^primvars:([^:]+)$")
|
|
if nm and (pr.typeName == "texCoord2f" or pr.typeName == "float2" or pr.typeName == "texCoord2d" or pr.typeName == "double2" or pr.typeName == "texCoord2h" or pr.typeName == "half2") and pr.array then
|
|
names[nm] = true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local list = {}
|
|
for nm in names do
|
|
table.insert(list, nm)
|
|
end
|
|
table.sort(list, function(a: string, b: string): boolean
|
|
local pa = if a == "st" then 0 elseif string.sub(a, 1, 2) == "st" then 1 else 2
|
|
local pb = if b == "st" then 0 elseif string.sub(b, 1, 2) == "st" then 1 else 2
|
|
if pa ~= pb then
|
|
return pa < pb
|
|
end
|
|
return a < b
|
|
end)
|
|
for _, nm in list do
|
|
texCoordFor(nm)
|
|
end
|
|
end
|
|
|
|
type Source = { value: any?, shader: Prim?, output: string? }
|
|
local function input(prim: Prim, name: string, depth: number?): Source?
|
|
local d = depth or 0
|
|
if d > 32 then
|
|
return nil
|
|
end
|
|
local p = prim.props["inputs:" .. name]
|
|
if not p then
|
|
return nil
|
|
end
|
|
if p.connect and #p.connect > 0 then
|
|
local target = resolvePath(pathOf[prim] or "/", p.connect[1])
|
|
local tp, tn = splitProp(target)
|
|
local tprim = byPath[tp]
|
|
if tprim and tn then
|
|
local outName = string.match(tn, "^outputs:(.+)$")
|
|
local inName = string.match(tn, "^inputs:(.+)$")
|
|
if outName and tprim.typeName == "Shader" then
|
|
return { shader = tprim, output = outName }
|
|
elseif outName then
|
|
local op = tprim.props[tn]
|
|
if op and op.connect and #op.connect > 0 then
|
|
local t2 = resolvePath(tp, op.connect[1])
|
|
local sp, sn = splitProp(t2)
|
|
local sprim = byPath[sp]
|
|
if sprim and sn and string.match(sn, "^outputs:") then
|
|
return { shader = sprim, output = string.sub(sn, 9) }
|
|
end
|
|
end
|
|
elseif inName then
|
|
local r = input(tprim, inName, d + 1)
|
|
if r then
|
|
return r
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local v = propValue(p)
|
|
if v ~= nil then
|
|
return { value = v }
|
|
end
|
|
return nil
|
|
end
|
|
local function shaderId(p: Prim): string?
|
|
return str(propValue(p.props["info:id"]))
|
|
end
|
|
|
|
local textureIndex: { [string]: number } = {}
|
|
local WRAP: { [string]: string } = { ["repeat"] = "repeat", clamp = "clamp", mirror = "mirror", black = "clamp", useMetadata = "repeat" }
|
|
local function textureRef(shader: Prim): Types.TextureRef?
|
|
if shaderId(shader) ~= "UsdUVTexture" then
|
|
return nil
|
|
end
|
|
local f = input(shader, "file")
|
|
local file = f and str(f.value)
|
|
if not file or file == "" then
|
|
return nil
|
|
end
|
|
local ws = input(shader, "wrapS")
|
|
local wt = input(shader, "wrapT")
|
|
local wrapS = WRAP[ws and str(ws.value) or "repeat"] or "repeat"
|
|
local wrapT = WRAP[wt and str(wt.value) or "repeat"] or "repeat"
|
|
local key = file .. "|" .. wrapS .. "|" .. wrapT
|
|
local ti = textureIndex[key]
|
|
if not ti then
|
|
local tex: Types.Texture = { name = shader.name, uri = file, sampler = { wrapS = wrapS :: any, wrapT = wrapT :: any } }
|
|
local ext = string.lower(string.match(file, "%.(%w+)$") or "")
|
|
tex.mime = MIME[ext]
|
|
local data = fetch and fetch(file)
|
|
if not data and string.sub(file, 1, 2) == "./" and fetch then
|
|
data = fetch(string.sub(file, 3))
|
|
end
|
|
tex.data = data
|
|
local extras: { [string]: any } = { path = pathOf[shader] }
|
|
local cs = input(shader, "sourceColorSpace")
|
|
if cs and str(cs.value) then
|
|
extras.sourceColorSpace = str(cs.value)
|
|
end
|
|
tex.extras = { usd = extras }
|
|
table.insert(scene.textures, tex)
|
|
ti = #scene.textures
|
|
textureIndex[key] = ti
|
|
end
|
|
local ref: Types.TextureRef = { texture = ti :: number }
|
|
-- st: UsdPrimvarReader_float2, maybe through UsdTransform2d
|
|
local st = input(shader, "st")
|
|
local reader = st and st.shader
|
|
if reader and shaderId(reader) == "UsdTransform2d" then
|
|
local tr: { [string]: any } = {}
|
|
local rot = input(reader, "rotation")
|
|
local scl = input(reader, "scale")
|
|
local off = input(reader, "translation")
|
|
if rot and num(rot.value) and num(rot.value) ~= 0 then
|
|
tr.rotation = (num(rot.value) :: number) * DEG
|
|
end
|
|
if scl and nums(scl.value) then
|
|
tr.scale = nums(scl.value)
|
|
end
|
|
if off and nums(off.value) then
|
|
tr.offset = nums(off.value)
|
|
end
|
|
if next(tr) then
|
|
ref.transform = tr :: any
|
|
end
|
|
local inn = input(reader, "in")
|
|
reader = inn and inn.shader
|
|
end
|
|
if reader and string.find(shaderId(reader) or "", "^UsdPrimvarReader") then
|
|
local vn = input(reader, "varname")
|
|
local name = vn and str(vn.value)
|
|
if name then
|
|
local k = texCoordFor(name)
|
|
if k ~= 0 then
|
|
ref.texCoord = k
|
|
end
|
|
end
|
|
end
|
|
return ref
|
|
end
|
|
|
|
local materialIndex: { [string]: number } = {}
|
|
local function material(path: string): number?
|
|
local mi = materialIndex[path]
|
|
if mi ~= nil then
|
|
return if mi > 0 then mi else nil
|
|
end
|
|
local mp = byPath[path]
|
|
if not mp or mp.typeName ~= "Material" then
|
|
materialIndex[path] = 0
|
|
return nil
|
|
end
|
|
local mat = Types.Scene.material(mp.name)
|
|
mat.baseColor = { 0.18, 0.18, 0.18, 1 }
|
|
mat.metallic = 0
|
|
mat.roughness = 0.5
|
|
mat.extras = { usd = { path = path } }
|
|
local surf = mp.props["outputs:surface"] or mp.props["outputs:usd:surface"]
|
|
local shader: Prim? = nil
|
|
if surf and surf.connect and #surf.connect > 0 then
|
|
local sp = splitProp(resolvePath(path, surf.connect[1]))
|
|
shader = byPath[sp]
|
|
end
|
|
if shader and shaderId(shader) == "UsdPreviewSurface" then
|
|
local sh = shader :: Prim
|
|
local function value(name: string): any
|
|
local s = input(sh, name)
|
|
return s and s.value
|
|
end
|
|
local function tex(name: string): (Types.TextureRef?, Prim?)
|
|
local s = input(sh, name)
|
|
if s and s.shader then
|
|
local ref = textureRef(s.shader)
|
|
return ref, if ref then s.shader else nil
|
|
end
|
|
return nil, nil
|
|
end
|
|
local dc = nums(value("diffuseColor"))
|
|
if dc and #dc >= 3 then
|
|
mat.baseColor = { dc[1], dc[2], dc[3], 1 }
|
|
end
|
|
local dt, dshader = tex("diffuseColor")
|
|
if dt then
|
|
mat.baseColorTexture = dt
|
|
mat.baseColor = { 1, 1, 1, 1 }
|
|
local sc = input(dshader :: Prim, "scale")
|
|
local s = sc and nums(sc.value)
|
|
if s and #s >= 3 then
|
|
mat.baseColor = { s[1], s[2], s[3], s[4] or 1 }
|
|
end
|
|
end
|
|
local op = num(value("opacity"))
|
|
local ot = tex("opacity")
|
|
if op then
|
|
mat.baseColor[4] = op
|
|
end
|
|
local thr = num(value("opacityThreshold"))
|
|
if thr and thr > 0 then
|
|
mat.alphaMode = "mask"
|
|
mat.alphaCutoff = thr
|
|
elseif (op and op < 1) or ot then
|
|
mat.alphaMode = "blend"
|
|
end
|
|
local m = num(value("metallic"))
|
|
if m then
|
|
mat.metallic = m
|
|
end
|
|
local r = num(value("roughness"))
|
|
if r then
|
|
mat.roughness = r
|
|
end
|
|
local mt = tex("metallic")
|
|
local rt = tex("roughness")
|
|
if rt or mt then
|
|
mat.metallicRoughnessTexture = rt or mt
|
|
if mt then
|
|
mat.metallic = 1
|
|
end
|
|
if rt then
|
|
mat.roughness = 1
|
|
end
|
|
end
|
|
local e = nums(value("emissiveColor"))
|
|
if e and #e >= 3 then
|
|
mat.emissive = { e[1], e[2], e[3] }
|
|
end
|
|
local et = tex("emissiveColor")
|
|
if et then
|
|
mat.emissiveTexture = et
|
|
mat.emissive = { 1, 1, 1 }
|
|
end
|
|
local nt = tex("normal")
|
|
if nt then
|
|
mat.normalTexture = nt
|
|
end
|
|
local oc = tex("occlusion")
|
|
if oc then
|
|
mat.occlusionTexture = oc
|
|
end
|
|
local ext: { [string]: any } = {}
|
|
local ior = num(value("ior"))
|
|
if ior and ior ~= 1.5 then
|
|
ext.KHR_materials_ior = { ior = ior }
|
|
end
|
|
local cc = num(value("clearcoat"))
|
|
if cc and cc > 0 then
|
|
ext.KHR_materials_clearcoat = { clearcoatFactor = cc, clearcoatRoughnessFactor = num(value("clearcoatRoughness")) or 0.01 }
|
|
end
|
|
if next(ext) then
|
|
mat.extensions = ext
|
|
end
|
|
local usd: any = (mat.extras :: any).usd
|
|
if num(value("useSpecularWorkflow")) == 1 then
|
|
usd.useSpecularWorkflow = true
|
|
usd.specularColor = nums(value("specularColor"))
|
|
end
|
|
elseif shader then
|
|
unsupported["shader " .. (shaderId(shader :: Prim) or "?")] = true
|
|
end
|
|
table.insert(scene.materials, mat)
|
|
materialIndex[path] = #scene.materials
|
|
return #scene.materials
|
|
end
|
|
|
|
-- material:binding, inherited from ancestors
|
|
local function binding(prim: Prim, path: string): number?
|
|
local p: Prim? = prim
|
|
local pp = path
|
|
while p do
|
|
local rel = p.props["material:binding"] or p.props["material:binding:preview"] or p.props["material:binding:full"]
|
|
if rel and rel.value and #rel.value > 0 then
|
|
return material(resolvePath(pp, rel.value[1]))
|
|
end
|
|
if pp == "/" then
|
|
break
|
|
end
|
|
pp = parentOf[p :: Prim] or "/"
|
|
p = byPath[pp]
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local colorMaterials: { [string]: number } = {}
|
|
local function colorMaterial(c: { number }): number
|
|
local key = string.format("%.6g,%.6g,%.6g,%.6g", c[1], c[2], c[3], c[4] or 1)
|
|
local mi = colorMaterials[key]
|
|
if not mi then
|
|
local mat = Types.Scene.material("displayColor")
|
|
mat.baseColor = { c[1], c[2], c[3], c[4] or 1 }
|
|
mat.metallic = 0
|
|
mat.roughness = 0.5
|
|
if (c[4] or 1) < 1 then
|
|
mat.alphaMode = "blend"
|
|
end
|
|
mat.extras = { usd = { displayColor = true } }
|
|
table.insert(scene.materials, mat)
|
|
mi = #scene.materials
|
|
colorMaterials[key] = mi
|
|
end
|
|
return mi :: number
|
|
end
|
|
|
|
type Primvar = { data: { number }, w: number, interp: string, idx: { number }? }
|
|
local function primvar(p: Prop?, defaultInterp: string): Primvar?
|
|
if not p then
|
|
return nil
|
|
end
|
|
local v = propValue(p)
|
|
if not isArr(v) then
|
|
return nil
|
|
end
|
|
local interp = defaultInterp
|
|
if p.meta and str(p.meta.interpolation) then
|
|
interp = str(p.meta.interpolation) :: string
|
|
end
|
|
return { data = v.v, w = v.w, interp = interp }
|
|
end
|
|
|
|
local function buildMesh(prim: Prim, path: string): (Types.Mesh?, string?)
|
|
local pts = propValue(prim.props.points)
|
|
local cnt = propValue(prim.props.faceVertexCounts)
|
|
local idx = propValue(prim.props.faceVertexIndices)
|
|
if not isArr(pts) or pts.w ~= 3 then
|
|
return nil, nil
|
|
end
|
|
local points: { number } = pts.v
|
|
local npts = #points // 3
|
|
local counts: { number } = if isArr(cnt) then cnt.v else {}
|
|
local indices: { number } = if isArr(idx) then idx.v else {}
|
|
local nfaces = #counts
|
|
local ncorners = 0
|
|
for f = 1, nfaces do
|
|
local c = counts[f]
|
|
if c < 0 or c ~= c // 1 then
|
|
return nil, "bad faceVertexCounts at " .. path
|
|
end
|
|
ncorners += c
|
|
end
|
|
if ncorners ~= #indices then
|
|
return nil, "faceVertexCounts and faceVertexIndices disagree at " .. path
|
|
end
|
|
if ncorners > maxVertices or npts > maxVertices then
|
|
return nil, "LIMIT"
|
|
end
|
|
for k = 1, ncorners do
|
|
local v = indices[k]
|
|
if v < 0 or v >= npts or v ~= v // 1 then
|
|
return nil, "a face vertex index out of range at " .. path
|
|
end
|
|
end
|
|
local faceStart = table.create(nfaces + 1, 0)
|
|
do
|
|
local acc = 0
|
|
for f = 1, nfaces do
|
|
faceStart[f] = acc
|
|
acc += counts[f]
|
|
end
|
|
faceStart[nfaces + 1] = acc
|
|
end
|
|
local function sizeOk(pv: Primvar): boolean
|
|
local n = (if pv.idx then #pv.idx else #pv.data // pv.w)
|
|
local want = if pv.interp == "constant" then 1
|
|
elseif pv.interp == "uniform" then nfaces
|
|
elseif pv.interp == "faceVarying" then ncorners
|
|
else npts
|
|
if n < want then
|
|
return false
|
|
end
|
|
if pv.idx then
|
|
local m = #pv.data // pv.w
|
|
for _, k in pv.idx :: { number } do
|
|
if k < 0 or k >= m or k ~= k // 1 then
|
|
return false
|
|
end
|
|
end
|
|
end
|
|
return #pv.data % pv.w == 0
|
|
end
|
|
local function withIndices(pv: Primvar?, name: string): Primvar?
|
|
if not pv then
|
|
return nil
|
|
end
|
|
local ip = propValue(prim.props[name .. ":indices"])
|
|
if isArr(ip) and ip.w == 1 then
|
|
pv.idx = ip.v
|
|
end
|
|
if not sizeOk(pv) then
|
|
unsupported["mismatched primvar " .. name] = true
|
|
return nil
|
|
end
|
|
return pv
|
|
end
|
|
local normals = withIndices(primvar(prim.props["primvars:normals"], "vertex"), "primvars:normals")
|
|
or withIndices(primvar(prim.props.normals, "vertex"), "normals")
|
|
if normals and normals.w ~= 3 then
|
|
normals = nil
|
|
end
|
|
local color = withIndices(primvar(prim.props["primvars:displayColor"], "constant"), "primvars:displayColor")
|
|
if color and color.w ~= 3 then
|
|
color = nil
|
|
end
|
|
local opacity = withIndices(primvar(prim.props["primvars:displayOpacity"], "constant"), "primvars:displayOpacity")
|
|
if opacity and opacity.w ~= 1 then
|
|
opacity = nil
|
|
end
|
|
local uvs: { [number]: Primvar } = {}
|
|
for pn, pr in prim.props do
|
|
local nm = string.match(pn, "^primvars:([^:]+)$")
|
|
if nm and pr.array and (pr.typeName == "texCoord2f" or pr.typeName == "float2" or pr.typeName == "texCoord2d" or pr.typeName == "double2" or pr.typeName == "texCoord2h" or pr.typeName == "half2") then
|
|
local pv = withIndices(primvar(pr, "vertex"), pn)
|
|
if pv and pv.w == 2 then
|
|
uvs[texCoordFor(nm)] = pv
|
|
end
|
|
end
|
|
end
|
|
local function get(pv: Primvar, point: number, face: number, corner: number, c: number): number
|
|
local e = if pv.interp == "constant" then 0
|
|
elseif pv.interp == "uniform" then face
|
|
elseif pv.interp == "faceVarying" then corner
|
|
else point
|
|
if pv.idx then
|
|
e = (pv.idx :: { number })[e + 1]
|
|
end
|
|
return pv.data[e * pv.w + c + 1] or 0
|
|
end
|
|
local perCorner = false
|
|
for _, pv in { normals, color, opacity } do
|
|
if pv and (pv.interp == "faceVarying" or pv.interp == "uniform") then
|
|
perCorner = true
|
|
end
|
|
end
|
|
for _, pv in uvs do
|
|
if pv.interp == "faceVarying" or pv.interp == "uniform" then
|
|
perCorner = true
|
|
end
|
|
end
|
|
local leftHanded = str(propValue(prim.props.orientation)) == "leftHanded"
|
|
local hole: { [number]: boolean } = {}
|
|
local hv = propValue(prim.props.holeIndices)
|
|
if isArr(hv) then
|
|
for _, f in hv.v do
|
|
hole[f] = true
|
|
end
|
|
end
|
|
-- face sets: GeomSubsets of "face" elements, then the rest
|
|
local meshMaterial = binding(prim, path)
|
|
local constantColor: { number }? = nil
|
|
if color and color.interp == "constant" then
|
|
constantColor = { get(color, 0, 0, 0, 0), get(color, 0, 0, 0, 1), get(color, 0, 0, 0, 2), if opacity and opacity.interp == "constant" then get(opacity, 0, 0, 0, 0) else 1 }
|
|
end
|
|
type Set = { faces: { number }, material: number?, name: string? }
|
|
local sets: { Set } = {}
|
|
local used: { [number]: boolean } = {}
|
|
for _, c in prim.children do
|
|
if c.typeName == "GeomSubset" and c.specifier ~= "class" then
|
|
local et = str(propValue(c.props.elementType)) or "face"
|
|
local fi = propValue(c.props.indices)
|
|
if et == "face" and isArr(fi) then
|
|
local faces = {}
|
|
for _, f in fi.v do
|
|
if f >= 0 and f < nfaces and f == f // 1 and not used[f] then
|
|
used[f] = true
|
|
table.insert(faces, f)
|
|
end
|
|
end
|
|
table.sort(faces)
|
|
local cpath = joinPath(path, c.name)
|
|
local rel = c.props["material:binding"] or c.props["material:binding:preview"]
|
|
local mi = if rel and rel.value and #rel.value > 0 then material(resolvePath(cpath, rel.value[1])) else meshMaterial
|
|
table.insert(sets, { faces = faces, material = mi, name = c.name })
|
|
end
|
|
end
|
|
end
|
|
do
|
|
local rest = {}
|
|
for f = 0, nfaces - 1 do
|
|
if not used[f] then
|
|
table.insert(rest, f)
|
|
end
|
|
end
|
|
if #rest > 0 or #sets == 0 then
|
|
table.insert(sets, 1, { faces = rest, material = meshMaterial })
|
|
end
|
|
end
|
|
local mesh: Types.Mesh = { name = prim.name, primitives = {} }
|
|
for _, set in sets do
|
|
local keyToVert: { [number]: number } = {}
|
|
local vp, vf, vc = {}, {}, {}
|
|
local nv = 0
|
|
local out: { number } = {}
|
|
local faceSizes: { number } = {}
|
|
for _, f in set.faces do
|
|
local cnt0 = counts[f + 1]
|
|
if cnt0 >= 3 and not hole[f] then
|
|
local base = faceStart[f + 1]
|
|
local verts = table.create(cnt0) :: { number }
|
|
for k = 0, cnt0 - 1 do
|
|
local corner = base + k
|
|
local point = indices[corner + 1]
|
|
local key = if perCorner then corner else point
|
|
local v = keyToVert[key]
|
|
if not v then
|
|
v = nv
|
|
keyToVert[key] = v
|
|
nv += 1
|
|
vp[nv], vf[nv], vc[nv] = point, f, corner
|
|
end
|
|
verts[k + 1] = v
|
|
end
|
|
if leftHanded then
|
|
local r = table.create(cnt0) :: { number }
|
|
for k = 1, cnt0 do
|
|
r[k] = verts[cnt0 - k + 1]
|
|
end
|
|
verts = r
|
|
end
|
|
if triangulate then
|
|
for k = 2, cnt0 - 1 do
|
|
table.insert(out, verts[1])
|
|
table.insert(out, verts[k])
|
|
table.insert(out, verts[k + 1])
|
|
end
|
|
else
|
|
for k = 1, cnt0 do
|
|
table.insert(out, verts[k])
|
|
end
|
|
table.insert(faceSizes, cnt0)
|
|
end
|
|
end
|
|
end
|
|
if nv > 0 then
|
|
local attrs: { [string]: Types.Accessor } = {}
|
|
local pos = Types.Scene.accessor(nil, "f32", 3, nv)
|
|
for v = 1, nv do
|
|
local p0 = vp[v] * 3
|
|
buffer.writef32(pos.data, (v - 1) * 12, points[p0 + 1])
|
|
buffer.writef32(pos.data, (v - 1) * 12 + 4, points[p0 + 2])
|
|
buffer.writef32(pos.data, (v - 1) * 12 + 8, points[p0 + 3])
|
|
end
|
|
attrs.POSITION = pos
|
|
local function fill(pv: Primvar, comps: number, flip: boolean): Types.Accessor
|
|
local a = Types.Scene.accessor(nil, "f32", comps, nv)
|
|
for v = 1, nv do
|
|
for c = 0, comps - 1 do
|
|
local x = get(pv, vp[v], vf[v], vc[v], c)
|
|
if flip and c == 1 then
|
|
x = 1 - x
|
|
end
|
|
buffer.writef32(a.data, ((v - 1) * comps + c) * 4, x)
|
|
end
|
|
end
|
|
return a
|
|
end
|
|
if normals then
|
|
local nrm = fill(normals, 3, false)
|
|
if leftHanded then
|
|
-- normals follow the authored (left-handed) winding: keep them as authored
|
|
end
|
|
attrs.NORMAL = nrm
|
|
end
|
|
for k, pv in uvs do
|
|
attrs["TEXCOORD_" .. k] = fill(pv, 2, flipV)
|
|
end
|
|
local mi = set.material
|
|
if color and not constantColor then
|
|
if opacity then
|
|
local a = Types.Scene.accessor(nil, "f32", 4, nv)
|
|
for v = 1, nv do
|
|
for c = 0, 2 do
|
|
buffer.writef32(a.data, ((v - 1) * 4 + c) * 4, get(color, vp[v], vf[v], vc[v], c))
|
|
end
|
|
buffer.writef32(a.data, ((v - 1) * 4 + 3) * 4, get(opacity, vp[v], vf[v], vc[v], 0))
|
|
end
|
|
attrs.COLOR_0 = a
|
|
else
|
|
attrs.COLOR_0 = fill(color, 3, false)
|
|
end
|
|
elseif constantColor and mi == nil then
|
|
mi = colorMaterial(constantColor)
|
|
end
|
|
local ind = Types.Scene.accessor(nil, "u32", 1, #out)
|
|
for k, v in out do
|
|
buffer.writeu32(ind.data, (k - 1) * 4, v)
|
|
end
|
|
local prm: Types.Primitive = { attributes = attrs, indices = ind, mode = if triangulate then "triangles" else "polygons", material = mi }
|
|
if not triangulate then
|
|
local fa = Types.Scene.accessor(nil, "u32", 1, #faceSizes)
|
|
for k, v in faceSizes do
|
|
buffer.writeu32(fa.data, (k - 1) * 4, v)
|
|
end
|
|
prm.faces = fa
|
|
end
|
|
if set.name then
|
|
prm.extras = { usd = { subset = set.name } }
|
|
end
|
|
local ds = propValue(prim.props.doubleSided)
|
|
if mi and (ds == 1 or ds == true) then
|
|
local m = mi and scene.materials[mi]
|
|
if m then
|
|
m.doubleSided = true
|
|
end
|
|
end
|
|
table.insert(mesh.primitives, prm)
|
|
end
|
|
end
|
|
if #mesh.primitives == 0 then
|
|
return nil, nil
|
|
end
|
|
return mesh, nil
|
|
end
|
|
|
|
local function camera(prim: Prim): Types.Camera
|
|
local function f(name: string, d: number): number
|
|
return num(propValue(prim.props[name])) or d
|
|
end
|
|
local focal = f("focalLength", 50)
|
|
local hap = f("horizontalAperture", 20.955)
|
|
local vap = f("verticalAperture", 15.2908)
|
|
local clip = nums(propValue(prim.props.clippingRange)) or { 1, 1000000 }
|
|
local proj = str(propValue(prim.props.projection)) or "perspective"
|
|
if proj == "orthographic" then
|
|
return { name = prim.name, type = "orthographic", xmag = hap / 20, ymag = vap / 20, znear = clip[1] or 1, zfar = clip[2] or 1000000 }
|
|
end
|
|
return {
|
|
name = prim.name,
|
|
type = "perspective",
|
|
yfov = 2 * math.atan(vap / (2 * focal)),
|
|
aspect = if vap > 0 then hap / vap else nil,
|
|
znear = clip[1] or 1,
|
|
zfar = clip[2],
|
|
}
|
|
end
|
|
local function light(prim: Prim): Types.Light
|
|
local function f(name: string, d: number): number
|
|
return num(propValue(prim.props["inputs:" .. name])) or num(propValue(prim.props[name])) or d
|
|
end
|
|
local col = nums(propValue(prim.props["inputs:color"])) or nums(propValue(prim.props.color)) or { 1, 1, 1 }
|
|
local intensity = f("intensity", 1) * 2 ^ f("exposure", 0)
|
|
local l: Types.Light = { name = prim.name, type = "point", color = { col[1] or 1, col[2] or 1, col[3] or 1 }, intensity = intensity }
|
|
local t = prim.typeName
|
|
local cone = num(propValue(prim.props["inputs:shaping:cone:angle"])) or num(propValue(prim.props["shaping:cone:angle"]))
|
|
if t == "DistantLight" then
|
|
l.type = "directional"
|
|
elseif t == "RectLight" or t == "DiskLight" then
|
|
l.type = "spot"
|
|
l.outerCone = math.pi / 2
|
|
l.innerCone = 0
|
|
end
|
|
if cone and cone < 90 then
|
|
local soft = num(propValue(prim.props["inputs:shaping:cone:softness"])) or 0
|
|
l.type = "spot"
|
|
l.outerCone = cone * DEG
|
|
l.innerCone = math.max(0, cone * DEG * (1 - soft))
|
|
end
|
|
return l
|
|
end
|
|
|
|
local tcps = num(meta.timeCodesPerSecond) or num(meta.framesPerSecond) or 24
|
|
if tcps <= 0 then
|
|
tcps = 24
|
|
end
|
|
local animation: Types.Animation = { name = "usd", channels = {} }
|
|
local failure: string? = nil
|
|
|
|
local function isNode(p: Prim): boolean
|
|
-- class prims are abstract; an over with no def is not defined (UsdStage traversal skips both)
|
|
if p.specifier == "class" or p.specifier == "over" or NOT_NODES[p.typeName or ""] then
|
|
if SKEL[p.typeName or ""] then
|
|
unsupported["UsdSkel (skeletons, skinning, blend shapes)"] = true
|
|
end
|
|
return false
|
|
end
|
|
if p.typeName == "Scope" or p.typeName == nil then
|
|
-- scopes / untyped prims holding only materials are not nodes
|
|
local function hasContent(q: Prim, d: number): boolean
|
|
if d > 100 then
|
|
return false
|
|
end
|
|
for _, c in q.children do
|
|
if c.specifier ~= "class" and not NOT_NODES[c.typeName or ""] then
|
|
if c.typeName ~= nil and c.typeName ~= "Scope" then
|
|
return true
|
|
end
|
|
if hasContent(c, d + 1) then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
if p.typeName == "Scope" or #p.children > 0 then
|
|
return hasContent(p, 0)
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function node(p: Prim, path: string, parent: number?, depth: number)
|
|
if depth > 200 or failure then
|
|
return
|
|
end
|
|
if not isNode(p) then
|
|
return
|
|
end
|
|
local n: Types.Node = { name = p.name, children = {}, extras = { usd = { path = path, type = p.typeName } } }
|
|
table.insert(scene.nodes, n)
|
|
local ni = #scene.nodes
|
|
local ops, reset = xformOps(p)
|
|
local times: { number } = {}
|
|
do
|
|
local seen: { [number]: boolean } = {}
|
|
for _, op in ops do
|
|
local s = op.prop.samples
|
|
if s and #s.times > 1 then
|
|
for _, t in s.times do
|
|
if not seen[t] then
|
|
seen[t] = true
|
|
table.insert(times, t)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
table.sort(times)
|
|
end
|
|
if #times > 1 then
|
|
local count = math.min(#times, 100000)
|
|
local tin = Types.Scene.accessor(nil, "f32", 1, count)
|
|
local tr = Types.Scene.accessor(nil, "f32", 3, count)
|
|
local ro = Types.Scene.accessor(nil, "f32", 4, count)
|
|
local sc = Types.Scene.accessor(nil, "f32", 3, count)
|
|
local prev: { number }? = nil
|
|
for k = 1, count do
|
|
local T, R, S = Mat4.decompose(localMatrix(ops, times[k]))
|
|
if prev and prev[1] * R[1] + prev[2] * R[2] + prev[3] * R[3] + prev[4] * R[4] < 0 then
|
|
R = { -R[1], -R[2], -R[3], -R[4] }
|
|
end
|
|
prev = R
|
|
buffer.writef32(tin.data, (k - 1) * 4, times[k] / tcps)
|
|
for c = 0, 2 do
|
|
buffer.writef32(tr.data, ((k - 1) * 3 + c) * 4, T[c + 1])
|
|
buffer.writef32(sc.data, ((k - 1) * 3 + c) * 4, S[c + 1])
|
|
end
|
|
for c = 0, 3 do
|
|
buffer.writef32(ro.data, ((k - 1) * 4 + c) * 4, R[c + 1])
|
|
end
|
|
end
|
|
table.insert(animation.channels, { node = ni, path = "translation", input = tin, output = tr, interpolation = "LINEAR" } :: Types.Channel)
|
|
table.insert(animation.channels, { node = ni, path = "rotation", input = tin, output = ro, interpolation = "LINEAR" } :: Types.Channel)
|
|
table.insert(animation.channels, { node = ni, path = "scale", input = tin, output = sc, interpolation = "LINEAR" } :: Types.Channel)
|
|
local T, R, S = Mat4.decompose(localMatrix(ops, times[1]))
|
|
n.translation, n.rotation, n.scale = T, R, S
|
|
elseif #ops > 0 then
|
|
local simple = true
|
|
local stage = 0
|
|
for _, op in ops do
|
|
local st = if op.kind == "translate" then 1 elseif op.kind == "rotate" or op.kind == "orient" then 2 elseif op.kind == "scale" then 3 else 9
|
|
if op.invert or st == 9 or st <= stage or string.find(op.prop.name, "^xformOp:%a+:") then
|
|
simple = false
|
|
end
|
|
stage = st
|
|
end
|
|
local m = localMatrix(ops, nil)
|
|
if simple then
|
|
local T, R, S = Mat4.decompose(m)
|
|
local tx = ops[1].kind == "translate"
|
|
if tx then
|
|
n.translation = T
|
|
end
|
|
for _, op in ops do
|
|
if op.kind == "rotate" or op.kind == "orient" then
|
|
n.rotation = R
|
|
elseif op.kind == "scale" then
|
|
n.scale = S
|
|
end
|
|
end
|
|
-- a negative scale with a rotation is not TRS-representable through decompose: keep the matrix
|
|
local back = Types.Scene.nodeMatrix(n)
|
|
for k = 1, 16 do
|
|
if math.abs(back[k] - m[k]) > 1e-5 * math.max(1, math.abs(m[k])) then
|
|
simple = false
|
|
break
|
|
end
|
|
end
|
|
if not simple then
|
|
n.translation, n.rotation, n.scale = nil, nil, nil
|
|
end
|
|
end
|
|
if not simple then
|
|
local identity = true
|
|
for k = 1, 16 do
|
|
if m[k] ~= Types.Scene.IDENTITY[k] then
|
|
identity = false
|
|
end
|
|
end
|
|
if not identity then
|
|
n.matrix = m
|
|
end
|
|
end
|
|
end
|
|
if reset or parent == nil then
|
|
table.insert(scene.roots, ni)
|
|
if reset and parent then
|
|
(n.extras :: any).usd.resetXformStack = true
|
|
end
|
|
else
|
|
table.insert(scene.nodes[parent].children, ni)
|
|
end
|
|
local t = p.typeName
|
|
if t == "Mesh" then
|
|
local mesh, err = buildMesh(p, path)
|
|
if err == "LIMIT" then
|
|
failure = "LIMIT"
|
|
return
|
|
elseif err then
|
|
failure = err
|
|
return
|
|
end
|
|
if mesh then
|
|
table.insert(scene.meshes, mesh)
|
|
n.mesh = #scene.meshes
|
|
end
|
|
elseif t == "Camera" then
|
|
table.insert(scene.cameras, camera(p))
|
|
n.camera = #scene.cameras
|
|
elseif t and LIGHTS[t] and t ~= "DomeLight" then
|
|
table.insert(scene.lights, light(p))
|
|
n.light = #scene.lights
|
|
elseif t and t ~= "Xform" and t ~= "Scope" and t ~= "Mesh" and t ~= "DomeLight" and t ~= "SkelRoot" then
|
|
unsupported["prim type " .. t] = true
|
|
end
|
|
for _, c in p.children do
|
|
node(c, joinPath(path, c.name), ni, depth + 1)
|
|
end
|
|
end
|
|
|
|
for _, p in layer.prims do
|
|
if p.specifier ~= "class" then
|
|
node(p, "/" .. p.name, nil, 1)
|
|
end
|
|
end
|
|
if failure then
|
|
return nil, failure
|
|
end
|
|
if #animation.channels > 0 then
|
|
table.insert(scene.animations, animation)
|
|
end
|
|
|
|
local up = str(meta.upAxis)
|
|
scene.meta.up = if up == "Z" then "z" else "y"
|
|
local mpu = num(meta.metersPerUnit)
|
|
if mpu and mpu > 0 then
|
|
scene.meta.unit = mpu
|
|
end
|
|
local list = {}
|
|
for k in unsupported do
|
|
table.insert(list, k)
|
|
end
|
|
table.sort(list)
|
|
scene.meta.source = {
|
|
format = "usda",
|
|
defaultPrim = str(meta.defaultPrim),
|
|
timeCodesPerSecond = tcps,
|
|
startTimeCode = num(meta.startTimeCode),
|
|
endTimeCode = num(meta.endTimeCode),
|
|
doc = str(meta.doc),
|
|
unsupported = if #list > 0 then list else nil,
|
|
}
|
|
return scene, nil
|
|
end
|
|
|
|
return Build
|