model formats (mca..x3d): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b952c10222
49 files changed
+310
-936
No files matched your search
@@ -1,22 +1,7 @@
|
||||
--!strict
|
||||
-- model/usda/Build: a parsed USDA layer (model/usda/Parse) -> Scene, following the UsdGeom / UsdShade / UsdLux
|
||||
-- schemas (openusd 26.x documentation). Prims: Xform / Scope / untyped def / over -> nodes (class prims are skipped;
|
||||
-- Material, Shader, NodeGraph, GeomSubset and scopes holding only those are not nodes); Mesh -> a node with a mesh
|
||||
-- (one primitive per GeomSubset of the "face" element type + one for the remaining faces), Camera, DistantLight /
|
||||
-- SphereLight / RectLight / DiskLight / CylinderLight. The selected variant of each variant set is merged into its prim.
|
||||
-- Transforms: xformOpOrder over translate / scale / rotateX|Y|Z / rotateXYZ (all six orders) / orient / transform, the
|
||||
-- !invert! prefix and !resetXformStack! (the node becomes a root). Time samples on xform ops -> one animation (the local
|
||||
-- matrix sampled at the union of the ops' time codes, decomposed into TRS channels; time = code / timeCodesPerSecond);
|
||||
-- other attributes use their earliest sample (as UsdTimeCode.EarliestTime()). Primvars: st (and every texCoord2f /
|
||||
-- float2 primvar) -> TEXCOORD_n (v flipped to glTF's top-left origin unless opts.flipV = false), normals, displayColor
|
||||
-- / displayOpacity -> COLOR_0 (a constant displayColor on an unbound mesh becomes a material instead), with every
|
||||
-- interpolation (constant / uniform / vertex / varying / faceVarying) and :indices. Faces are fan-triangulated
|
||||
-- (opts.triangulate = false keeps "polygons"), holeIndices skipped, orientation = "leftHanded" reversed.
|
||||
-- UsdPreviewSurface materials (inputs resolved through connections to Material / NodeGraph interface inputs) with
|
||||
-- UsdUVTexture textures (file, wrapS / wrapT, st through UsdPrimvarReader_float2, UsdTransform2d ->
|
||||
-- KHR_texture_transform), material:binding (inherited down the namespace, also on GeomSubsets).
|
||||
-- Not supported: composition arcs (references, payloads, inherits, specializes, subLayers: listed in
|
||||
-- meta.source.unsupported), UsdSkel, blend shapes, BasisCurves / Points / NURBS, instancing, MaterialX networks.
|
||||
-- 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")
|
||||
@@ -53,7 +38,6 @@ local function str(v: any): string?
|
||||
end
|
||||
return nil
|
||||
end
|
||||
-- a tuple / scalar -> numbers (nil when not numeric)
|
||||
local function nums(v: any): { number }?
|
||||
if type(v) == "number" then
|
||||
return { v }
|
||||
@@ -90,7 +74,7 @@ local function num(v: any): number?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- the value of a property: its default, else its earliest time sample
|
||||
-- a property's default, else its earliest time sample
|
||||
local function propValue(p: Prop?): any
|
||||
if not p then
|
||||
return nil
|
||||
@@ -117,7 +101,6 @@ local function joinPath(parent: string, name: string): string
|
||||
return if parent == "/" then "/" .. name else parent .. "/" .. name
|
||||
end
|
||||
|
||||
-- resolve a (possibly relative) Sdf path against the prim path that holds it
|
||||
local function resolvePath(base: string, p: string): string
|
||||
if string.sub(p, 1, 1) == "/" then
|
||||
return p
|
||||
@@ -138,7 +121,6 @@ local function resolvePath(base: string, p: string): string
|
||||
end
|
||||
return "/" .. table.concat(out, "/")
|
||||
end
|
||||
-- "/a/b.outputs:rgb" -> "/a/b", "outputs:rgb"
|
||||
local function splitProp(p: string): (string, string?)
|
||||
local prim, prop = string.match(p, "^(.-)%.([^/]+)$")
|
||||
if prim and prop then
|
||||
@@ -161,7 +143,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ xform ops
|
||||
type Op = { kind: string, axes: string?, prop: Prop, invert: boolean }
|
||||
|
||||
-- the op stack of a prim: ops in xformOpOrder, resetXformStack
|
||||
local function xformOps(p: Prim): ({ Op }, boolean)
|
||||
local order = p.props.xformOpOrder
|
||||
local list = order and propValue(order)
|
||||
@@ -193,7 +174,6 @@ local function xformOps(p: Prim): ({ Op }, boolean)
|
||||
end
|
||||
|
||||
local AXIS = { X = 1, Y = 2, Z = 3 }
|
||||
-- the matrix of one op for a value
|
||||
local function opMatrix(op: Op, v: any): { number }?
|
||||
local n = nums(v)
|
||||
if not n then
|
||||
@@ -217,7 +197,7 @@ local function opMatrix(op: Op, v: any): { number }?
|
||||
elseif k == "rotate" and op.axes then
|
||||
local axes = op.axes :: string
|
||||
local r: { number } = Types.Scene.IDENTITY :: any
|
||||
-- rotateXZY: X first, then Z, then Y: R = Ry * Rz * Rx (column vectors); the value is always (x, y, z) angles
|
||||
-- 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
|
||||
@@ -240,7 +220,6 @@ local function lerpValue(a: any, b: any, f: number, quat: boolean): any
|
||||
end
|
||||
local out = table.create(#na)
|
||||
if quat and #na == 4 then
|
||||
-- (w, x, y, z): slerp through nlerp with the short path
|
||||
local dot = 0
|
||||
for k = 1, 4 do
|
||||
dot += na[k] * nb[k]
|
||||
@@ -257,7 +236,7 @@ local function lerpValue(a: any, b: any, f: number, quat: boolean): any
|
||||
return { t = "tuple", v = out }
|
||||
end
|
||||
|
||||
-- an op's value at time code t (samples interpolated linearly, held outside their range)
|
||||
-- samples interpolated linearly, held outside their range
|
||||
local function opValueAt(op: Op, t: number?): any
|
||||
local p = op.prop
|
||||
local s = p.samples
|
||||
@@ -309,7 +288,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
local unsupported: { [string]: boolean } = {}
|
||||
local meta = layer.meta
|
||||
|
||||
-- prim index (variants merged), paths
|
||||
local byPath: { [string]: Prim } = {}
|
||||
local parentOf: { [Prim]: string } = {}
|
||||
local pathOf: { [Prim]: string } = {}
|
||||
@@ -360,7 +338,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
parentOf[p] = parent
|
||||
local existing = byPath[path]
|
||||
if existing and existing ~= p then
|
||||
-- over / def of the same path in one layer: merge into the first spec
|
||||
-- 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]
|
||||
@@ -375,7 +353,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
table.insert(existing.children, c)
|
||||
end
|
||||
table.clear(p.children)
|
||||
p.specifier = "class" -- skipped below
|
||||
p.specifier = "class"
|
||||
else
|
||||
byPath[path] = p
|
||||
end
|
||||
@@ -396,7 +374,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
unsupported.subLayers = true
|
||||
end
|
||||
|
||||
-- a property of a prim path ("/a/b.inputs:x")
|
||||
local function propAt(path: string): (Prim?, Prop?)
|
||||
local pp, pn = splitProp(path)
|
||||
local prim = byPath[pp]
|
||||
@@ -406,7 +383,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
return prim, prim.props[pn]
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------------------------- materials
|
||||
local texCoordIndex: { [string]: number } = {}
|
||||
local texCoordCount = 0
|
||||
local function texCoordFor(name: string): number
|
||||
@@ -418,7 +394,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
return k
|
||||
end
|
||||
-- primvar names of every mesh, "st" first, so material readers and meshes agree on TEXCOORD_n
|
||||
-- "st" first, so materials and meshes agree on TEXCOORD_n
|
||||
do
|
||||
local names: { [string]: boolean } = {}
|
||||
for _, p in byPath do
|
||||
@@ -448,7 +424,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
end
|
||||
|
||||
-- an input's resolved source: a value, or { shader = Prim, output = name }
|
||||
type Source = { value: any?, shader: Prim?, output: string? }
|
||||
local function input(prim: Prim, name: string, depth: number?): Source?
|
||||
local d = depth or 0
|
||||
@@ -469,7 +444,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
if outName and tprim.typeName == "Shader" then
|
||||
return { shader = tprim, output = outName }
|
||||
elseif outName then
|
||||
-- a NodeGraph output: follow its connection
|
||||
local op = tprim.props[tn]
|
||||
if op and op.connect and #op.connect > 0 then
|
||||
local t2 = resolvePath(tp, op.connect[1])
|
||||
@@ -499,7 +473,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
|
||||
local textureIndex: { [string]: number } = {}
|
||||
local WRAP: { [string]: string } = { ["repeat"] = "repeat", clamp = "clamp", mirror = "mirror", black = "clamp", useMetadata = "repeat" }
|
||||
-- UsdUVTexture shader -> TextureRef (texture index + texCoord + transform)
|
||||
local function textureRef(shader: Prim): Types.TextureRef?
|
||||
if shaderId(shader) ~= "UsdUVTexture" then
|
||||
return nil
|
||||
@@ -535,7 +508,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
textureIndex[key] = ti
|
||||
end
|
||||
local ref: Types.TextureRef = { texture = ti :: number }
|
||||
-- st: UsdPrimvarReader_float2 (maybe through UsdTransform2d)
|
||||
-- st: UsdPrimvarReader_float2, maybe through UsdTransform2d
|
||||
local st = input(shader, "st")
|
||||
local reader = st and st.shader
|
||||
if reader and shaderId(reader) == "UsdTransform2d" then
|
||||
@@ -587,7 +560,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
mat.metallic = 0
|
||||
mat.roughness = 0.5
|
||||
mat.extras = { usd = { path = path } }
|
||||
-- the surface shader
|
||||
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
|
||||
@@ -695,7 +667,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
return #scene.materials
|
||||
end
|
||||
|
||||
-- the bound material of a prim (material:binding, inherited from ancestors)
|
||||
-- material:binding, inherited from ancestors
|
||||
local function binding(prim: Prim, path: string): number?
|
||||
local p: Prim? = prim
|
||||
local pp = path
|
||||
@@ -733,7 +705,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
return mi :: number
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------------------------- meshes
|
||||
type Primvar = { data: { number }, w: number, interp: string, idx: { number }? }
|
||||
local function primvar(p: Prop?, defaultInterp: string): Primvar?
|
||||
if not p then
|
||||
@@ -791,7 +762,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
faceStart[nfaces + 1] = acc
|
||||
end
|
||||
-- primvars, validated
|
||||
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
|
||||
@@ -848,7 +818,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the value of a primvar for (point, face, corner) (0-based), component c
|
||||
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
|
||||
@@ -871,7 +840,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
end
|
||||
local leftHanded = str(propValue(prim.props.orientation)) == "leftHanded"
|
||||
-- holes
|
||||
local hole: { [number]: boolean } = {}
|
||||
local hv = propValue(prim.props.holeIndices)
|
||||
if isArr(hv) then
|
||||
@@ -879,7 +847,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
hole[f] = true
|
||||
end
|
||||
end
|
||||
-- face sets: GeomSubsets ("face" elements), the rest
|
||||
-- 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
|
||||
@@ -921,9 +889,8 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
local mesh: Types.Mesh = { name = prim.name, primitives = {} }
|
||||
for _, set in sets do
|
||||
-- vertices: one per point (vertex-rate data) or per corner
|
||||
local keyToVert: { [number]: number } = {}
|
||||
local vp, vf, vc = {}, {}, {} -- point, face, corner per new vertex (0-based)
|
||||
local vp, vf, vc = {}, {}, {}
|
||||
local nv = 0
|
||||
local out: { number } = {}
|
||||
local faceSizes: { number } = {}
|
||||
@@ -1001,7 +968,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
end
|
||||
local mi = set.material
|
||||
if color and not constantColor then
|
||||
-- varying colours -> COLOR_0 (rgb, or rgba with displayOpacity)
|
||||
if opacity then
|
||||
local a = Types.Scene.accessor(nil, "f32", 4, nv)
|
||||
for v = 1, nv do
|
||||
@@ -1048,7 +1014,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
return mesh, nil
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------------------------- cameras / lights
|
||||
local function camera(prim: Prim): Types.Camera
|
||||
local function f(name: string, d: number): number
|
||||
return num(propValue(prim.props[name])) or d
|
||||
@@ -1095,7 +1060,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
return l
|
||||
end
|
||||
|
||||
-- --------------------------------------------------------------------------------------------- nodes
|
||||
local tcps = num(meta.timeCodesPerSecond) or num(meta.framesPerSecond) or 24
|
||||
if tcps <= 0 then
|
||||
tcps = 24
|
||||
@@ -1147,7 +1111,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
table.insert(scene.nodes, n)
|
||||
local ni = #scene.nodes
|
||||
local ops, reset = xformOps(p)
|
||||
-- animated ops: the local matrix at the union of their time codes
|
||||
local times: { number } = {}
|
||||
do
|
||||
local seen: { [number]: boolean } = {}
|
||||
@@ -1192,7 +1155,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
local T, R, S = Mat4.decompose(localMatrix(ops, times[1]))
|
||||
n.translation, n.rotation, n.scale = T, R, S
|
||||
elseif #ops > 0 then
|
||||
-- static: TRS when the stack is translate / rotate / scale in that order, else the matrix
|
||||
local simple = true
|
||||
local stage = 0
|
||||
for _, op in ops do
|
||||
@@ -1288,7 +1250,6 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
|
||||
table.insert(scene.animations, animation)
|
||||
end
|
||||
|
||||
-- stage metadata
|
||||
local up = str(meta.upAxis)
|
||||
scene.meta.up = if up == "Z" then "z" else "y"
|
||||
local mpu = num(meta.metersPerUnit)
|
||||
|
||||
Reference in new issue
Block a user