model formats (3ds..lwo): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
3bddd582bc
35 files changed
+233
-697
No files matched your search
@@ -1,15 +1,6 @@
|
||||
--!strict
|
||||
-- model/fbx/Read: an FBX 7.x node tree (model/fbx/Binary or model/fbx/Ascii) -> Scene. Objects + Connections ->
|
||||
-- nodes (Model, with the full FBX transform: pre / post rotation, pivots, offsets, rotation order, geometric
|
||||
-- transform baked into the mesh data), meshes (polygons, layer elements for normals / uvs / colours / materials in
|
||||
-- every mapping (ByPolygonVertex, ByVertice, ByPolygon, AllSame) and reference (Direct, IndexToDirect) mode, one
|
||||
-- primitive per material, vertices unwelded per polygon corner as assimp does), materials (Phong / Lambert and the
|
||||
-- 3ds Max Physical / PBR compound -> PBR fields + extras.fbx), textures (embedded Video content kept encoded, or file
|
||||
-- references through the fetch callback), skins (Skin + Cluster deformers -> JOINTS_0 / WEIGHTS_0, inverse bind
|
||||
-- matrices), blend shapes (BlendShape / BlendShapeChannel / Shape -> morph targets), animation stacks (every layer's
|
||||
-- curve nodes on Lcl Translation / Rotation / Scaling and DeformPercent, sampled at the union of key times (or
|
||||
-- opts.sampleRate) and turned into glTF channels through the node transform), cameras and lights (on a child node
|
||||
-- turning glTF's -Z forward into FBX's +X camera / -Y light axis). Property templates from Definitions supply defaults.
|
||||
-- model/fbx/Read: an FBX 7.x node tree -> Scene. Layer elements in every mapping (ByPolygonVertex, ByVertice,
|
||||
-- ByPolygon, AllSame) and reference (Direct, IndexToDirect) mode; vertices unwelded per polygon corner as assimp does.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Base64 = require("@rosetta/core/Base64")
|
||||
@@ -50,7 +41,7 @@ local function isArray(v: any): boolean
|
||||
return type(v) == "table" and v.array == true
|
||||
end
|
||||
|
||||
-- the array under n.<name> (or inline numbers: old ASCII files)
|
||||
-- old ASCII files have inline numbers
|
||||
local function array(n: Node?, name: string): Array?
|
||||
local c = child(n, name)
|
||||
if not c then
|
||||
@@ -87,7 +78,7 @@ local function at(a: Array, i: number): number
|
||||
return buffer.readf64(a.data, i * 8)
|
||||
end
|
||||
|
||||
-- "Name\0\1Class" (binary) / "Class::Name" (ASCII) -> name
|
||||
-- "Name\0\1Class" (binary) / "Class::Name" (ASCII)
|
||||
local function objectName(s: any): string
|
||||
if type(s) ~= "string" then
|
||||
return ""
|
||||
@@ -104,7 +95,7 @@ local function objectName(s: any): string
|
||||
end
|
||||
Read.objectName = objectName
|
||||
|
||||
-- Properties70 (P: name, type, label, flags, values...) -> { [name] = { type, v1, v2, ... } }
|
||||
-- Properties70: P: name, type, label, flags, values...
|
||||
local function properties(n: Node?, into: { [string]: { any } }?): { [string]: { any } }
|
||||
local out = into or {}
|
||||
local p70 = child(n, "Properties70")
|
||||
@@ -155,7 +146,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
local maxVertices = opts and opts.maxVertices or 2 ^ 25
|
||||
local scene = Types.Scene.new()
|
||||
|
||||
-- property templates (Definitions/ObjectType/PropertyTemplate) by object class
|
||||
-- Definitions/ObjectType/PropertyTemplate
|
||||
local templates: { [string]: { [string]: { any } } } = {}
|
||||
local defs = child(root, "Definitions")
|
||||
if defs then
|
||||
@@ -169,7 +160,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
|
||||
-- objects
|
||||
local objects: { [any]: Obj } = {}
|
||||
local order: { Obj } = {}
|
||||
local objs = child(root, "Objects")
|
||||
@@ -187,7 +177,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
|
||||
-- connections: children (sources) of each parent, parents of each child, in file order
|
||||
local kids: { [any]: { Conn } } = {}
|
||||
local parents: { [any]: { Conn } } = {}
|
||||
local conns = child(root, "Connections")
|
||||
@@ -236,7 +225,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
return out
|
||||
end
|
||||
|
||||
-- global settings
|
||||
local gs = properties(child(root, "GlobalSettings"))
|
||||
local upAxis = pnum(gs, "UpAxis", 1)
|
||||
local unitScale = pnum(gs, "UnitScaleFactor", 1)
|
||||
@@ -280,7 +268,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
return p
|
||||
end
|
||||
end
|
||||
-- ASCII: base64 strings (possibly split into several)
|
||||
-- ASCII: base64, possibly split into several strings
|
||||
local parts = {}
|
||||
for _, p in c.props do
|
||||
if type(p) == "string" then
|
||||
@@ -384,7 +372,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
if opacity < 1 then
|
||||
m.alphaMode = "blend"
|
||||
end
|
||||
-- 3ds Max Physical / PBR materials
|
||||
local pbr = p["3dsMax|main|basecolor"] or p["3dsMax|Parameters|base_color"]
|
||||
if pbr then
|
||||
local prefix = if p["3dsMax|main|basecolor"] then "3dsMax|main|" else "3dsMax|Parameters|"
|
||||
@@ -504,7 +491,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
nd.scale = s
|
||||
end
|
||||
nd.extras = { fbx = { type = o.sub, id = o.id } }
|
||||
-- parent
|
||||
local parentModel: Obj? = nil
|
||||
for _, t2 in targets(o.id, "Model") do
|
||||
parentModel = t2.obj
|
||||
@@ -517,7 +503,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
table.insert(scene.roots, ni)
|
||||
end
|
||||
end
|
||||
-- break parent cycles (a node reachable from itself is not a valid tree): cut children already placed
|
||||
-- break parent cycles: cut children already placed
|
||||
do
|
||||
local placed: { [number]: boolean } = {}
|
||||
local function visit(i: number)
|
||||
@@ -537,13 +523,11 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
for i in scene.nodes do
|
||||
if not placed[i] then
|
||||
-- part of a parent cycle: make it a root
|
||||
table.insert(scene.roots, i)
|
||||
visit(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- world matrices (rest pose)
|
||||
local worldM: { [number]: Mat4.M4 } = {}
|
||||
local function world(i: number, parent: Mat4.M4)
|
||||
worldM[i] = Mat4.mul(parent, localM[i] or Mat4.identity())
|
||||
@@ -559,7 +543,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
|
||||
-- ------------------------------------------------------------------------------------------ meshes
|
||||
local corners = 0
|
||||
-- per geometry: skin (control point -> { joint node, weight }) and blend shape channels
|
||||
local function skinOf(geo: Obj): ({ { { number } } }?, Types.Skin?)
|
||||
local skin = sources(geo.id, "Deformer", "Skin")[1]
|
||||
if not skin then
|
||||
@@ -645,7 +628,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
|
||||
local meshCache: { [string]: number } = {}
|
||||
local shapeChannels: { [number]: { Obj } } = {} -- mesh index -> blend shape channel objects (for weights curves)
|
||||
local shapeChannels: { [number]: { Obj } } = {}
|
||||
local nodeSkin: { [number]: number } = {}
|
||||
local skinCache: { [any]: number } = {}
|
||||
|
||||
@@ -662,7 +645,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
if corners > maxVertices or ncp > maxVertices then
|
||||
return nil, Err.new(Err.LIMIT, "over " .. maxVertices .. " vertices")
|
||||
end
|
||||
-- polygons: corner -> control point, polygon of each corner, polygon starts
|
||||
local cp = table.create(nc, 0)
|
||||
local polyOf = table.create(nc, 0)
|
||||
local starts: { number } = { 0 }
|
||||
@@ -688,7 +670,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
np += 1
|
||||
starts[np + 1] = nc
|
||||
end
|
||||
-- layer elements
|
||||
type Elem = { data: Array, index: Array?, mapping: string, comps: number, name: string?, order: number }
|
||||
local function elems(kind: string, dataName: string, indexName: string, comps: number): { Elem }
|
||||
local out: { Elem } = {}
|
||||
@@ -711,7 +692,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end)
|
||||
return out
|
||||
end
|
||||
-- the data element of a corner (c 0-based), or -1
|
||||
local function lookup(e: Elem, c: number): number
|
||||
local m = e.mapping
|
||||
local i
|
||||
@@ -742,7 +722,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
local colors = elems("LayerElementColor", "Colors", "ColorIndex", 4)
|
||||
local tangents = elems("LayerElementTangent", "Tangents", "TangentsIndex", 3)
|
||||
local matEl = elems("LayerElementMaterial", "Materials", "Materials", 1)[1]
|
||||
-- the normals index name varies ("NormalsIndex" / "NormalIndex")
|
||||
-- "NormalsIndex" / "NormalIndex"
|
||||
for _, e in normals do
|
||||
if not e.index then
|
||||
local c = nil
|
||||
@@ -758,7 +738,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
end
|
||||
-- polygons by material slot
|
||||
local groups: { [number]: { number } } = {}
|
||||
local slots: { number } = {}
|
||||
for p = 0, np - 1 do
|
||||
@@ -785,7 +764,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
table.sort(slots)
|
||||
local perCp, skin = skinOf(geo)
|
||||
local shapes = shapesOf(geo)
|
||||
local N3: Mat4.M4? = nil -- normal matrix (inverse transpose of G)
|
||||
local N3: Mat4.M4? = nil -- inverse transpose of G
|
||||
if G then
|
||||
local inv = Mat4.inverse(G)
|
||||
if inv then
|
||||
@@ -1004,12 +983,11 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
for _, o in models do
|
||||
local ni = nodeOf[o.id]
|
||||
local nd = scene.nodes[ni]
|
||||
-- materials of the model, in connection order
|
||||
local mats: { number } = {}
|
||||
for _, m in sources(o.id, "Material") do
|
||||
table.insert(mats, material(m))
|
||||
end
|
||||
-- geometric transform (not inherited: baked into the mesh)
|
||||
-- geometric transform: not inherited, baked into the mesh
|
||||
local gt = pvec(o.props, "GeometricTranslation", { 0, 0, 0 })
|
||||
local gr = pvec(o.props, "GeometricRotation", { 0, 0, 0 })
|
||||
local gsc = pvec(o.props, "GeometricScaling", { 1, 1, 1 })
|
||||
@@ -1047,7 +1025,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
end
|
||||
-- cameras and lights: on a child node rotated from glTF's -Z forward to FBX's axis
|
||||
-- cameras / lights: a child node rotated from glTF's -Z forward to FBX's +X camera / -Y light axis
|
||||
for _, a in sources(o.id, "NodeAttribute") do
|
||||
local p = a.props
|
||||
if a.sub == "Camera" then
|
||||
@@ -1120,7 +1098,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
times[k + 1] = at(kt, k) / TICKS
|
||||
values[k + 1] = at(kv, k)
|
||||
end
|
||||
-- per-key attribute flags (expanded through KeyAttrRefCount)
|
||||
-- expanded through KeyAttrRefCount
|
||||
local fl, rc, df = array(o.node, "KeyAttrFlags"), array(o.node, "KeyAttrRefCount"), array(o.node, "KeyAttrDataFloat")
|
||||
local flags = table.create(n, 4)
|
||||
local slopes: { { number } } = {}
|
||||
@@ -1223,11 +1201,10 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
for _, stack in order do
|
||||
if stack.class == "AnimationStack" then
|
||||
local anim: Types.Animation = { name = stack.name, channels = {} }
|
||||
-- model id -> { T/R/S -> { axis -> curve } , defaults }
|
||||
type Track = { curves: { [string]: { [number]: Curve } }, defaults: { [string]: { number } } }
|
||||
local tracks: { [any]: Track } = {}
|
||||
local trackOrder: { any } = {}
|
||||
local morph: { [any]: Curve } = {} -- blend shape channel id -> DeformPercent curve
|
||||
local morph: { [any]: Curve } = {}
|
||||
local layers = sources(stack.id, "AnimationLayer")
|
||||
for _, layer in layers do
|
||||
for _, cn in sources(layer.id, "AnimationCurveNode") do
|
||||
@@ -1328,7 +1305,6 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
|
||||
end
|
||||
end
|
||||
end
|
||||
-- morph target weights: every node whose mesh has an animated blend shape channel
|
||||
if next(morph) then
|
||||
for ni, nd in scene.nodes do
|
||||
local chans = nd.mesh and shapeChannels[nd.mesh]
|
||||
|
||||
Reference in new issue
Block a user