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,6 +1,5 @@
|
||||
--!strict
|
||||
-- model/dae/Math: the Collada-specific transform elements (<lookat>, <skew>); everything else (matrices,
|
||||
-- quaternions, decomposition) is codecs/model/Mat4. Matrices are flat arrays of 16 in column-major order.
|
||||
-- model/dae/Math: the Collada transform elements <lookat> and <skew>; matrices are flat column-major arrays of 16.
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
|
||||
local DaeMath = {}
|
||||
@@ -18,7 +17,7 @@ local function norm(a: { number }): { number }
|
||||
return { a[1] / l, a[2] / l, a[3] / l }
|
||||
end
|
||||
|
||||
-- lookat(eye, interest, up) -> the placement of an object at eye whose -Z looks at interest
|
||||
-- <lookat>: an object at eye whose -Z looks at interest
|
||||
function DaeMath.lookat(v: { number }): { number }
|
||||
local ex, ey, ez = v[1] or 0, v[2] or 0, v[3] or 0
|
||||
local z = norm({ ex - (v[4] or 0), ey - (v[5] or 0), ez - (v[6] or 0) })
|
||||
@@ -27,14 +26,14 @@ function DaeMath.lookat(v: { number }): { number }
|
||||
return { x[1], x[2], x[3], 0, y[1], y[2], y[3], 0, z[1], z[2], z[3], 0, ex, ey, ez, 1 }
|
||||
end
|
||||
|
||||
-- skew(angle, rotation axis, translation axis) (RenderMan's skew)
|
||||
-- <skew> (RenderMan's skew)
|
||||
function DaeMath.skew(v: { number }): { number }
|
||||
local a = rad(v[1] or 0)
|
||||
local r = norm({ v[2] or 0, v[3] or 0, v[4] or 0 })
|
||||
local t = norm({ v[5] or 0, v[6] or 0, v[7] or 0 })
|
||||
local s = math.tan(a)
|
||||
local m = Mat4.identity()
|
||||
-- M = I + s * t * r^T (column-major: m[col*4 + row])
|
||||
-- M = I + s * t * r^T
|
||||
for row = 0, 2 do
|
||||
for col = 0, 2 do
|
||||
m[col * 4 + row + 1] += s * t[row + 1] * r[col + 1]
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
--!strict
|
||||
-- model/dae/Read: a parsed Collada document (codecs/text/Xml DOM) -> Scene. Geometry (triangles / polylist / polygons /
|
||||
-- lines / linestrips / tristrips / trifans with shared input offsets, de-indexed per index tuple), common-profile
|
||||
-- materials (phong / blinn / lambert / constant -> PBR + extras.phong) with their sampler2D / surface / image chains,
|
||||
-- images (kept encoded through the fetch callback, or embedded hex <data>), the visual scene's node tree (matrix /
|
||||
-- translate / rotate / scale / lookat / skew, instance_node, several instances per node as child nodes), skin
|
||||
-- controllers (bind shape applied, inverse bind matrices, the 4 strongest weights), morph controllers (targets as
|
||||
-- deltas), cameras, lights (ambient lights as node extras) and animations (every channel on a node's transform
|
||||
-- elements, resampled at the union of the key times (plus subdivisions of large rotations) into glTF-style TRS
|
||||
-- channels; one animation per animation clip, else one for the file).
|
||||
-- model/dae/Read: a parsed Collada document (codecs/text/Xml DOM) -> Scene.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Xml = require("@rosetta/codecs/text/Xml")
|
||||
@@ -100,8 +92,8 @@ type Ctx = {
|
||||
meshes: { [string]: number },
|
||||
skins: { [string]: number },
|
||||
posIndex: { [any]: { number } },
|
||||
nodeOf: { [string]: { number } }, -- collada node id -> scene nodes
|
||||
info: { [number]: any }, -- scene node -> { el, xf, sid, cid }
|
||||
nodeOf: { [string]: { number } },
|
||||
info: { [number]: any },
|
||||
pendingSkins: { any },
|
||||
skipped: { [string]: number },
|
||||
err: string?,
|
||||
@@ -113,7 +105,6 @@ local function skip(C: Ctx, what: string)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sources
|
||||
-- source(el) -> { data = flat numbers or strings, comps, count } (only the accessor's named params)
|
||||
local function readSource(C: Ctx, el: any): any
|
||||
local cached = C.sources[el]
|
||||
if cached ~= nil then
|
||||
@@ -279,7 +270,6 @@ local function image(C: Ctx, id: string): any
|
||||
return img
|
||||
end
|
||||
|
||||
-- the effect's newparams by sid (this level and the profile's)
|
||||
local function newparams(effect: any, profile: any, technique: any): { [string]: any }
|
||||
local out = {}
|
||||
for _, scope in { effect, profile, technique } do
|
||||
@@ -295,7 +285,6 @@ local function newparams(effect: any, profile: any, technique: any): { [string]:
|
||||
return out
|
||||
end
|
||||
|
||||
-- texture(effect params, sampler sid) -> scene texture index
|
||||
local function texture(C: Ctx, params: { [string]: any }, samplerSid: string): number?
|
||||
local np = params[samplerSid]
|
||||
local imageId: string? = nil
|
||||
@@ -364,7 +353,6 @@ local function texture(C: Ctx, params: { [string]: any }, samplerSid: string): n
|
||||
return idx
|
||||
end
|
||||
|
||||
-- one colour-or-texture parameter of a common technique
|
||||
local function param(C: Ctx, params: { [string]: any }, el: any, uvsets: { [string]: number }): (any, any)
|
||||
if not el then
|
||||
return nil, nil
|
||||
@@ -474,7 +462,7 @@ local function material(C: Ctx, id: string, uvsets: { [string]: number }): numbe
|
||||
if type(shininess) == "number" then
|
||||
mat.roughness = math.clamp(math.sqrt(2 / (math.max(shininess, 0) + 2)), 0, 1)
|
||||
end
|
||||
-- opacity (Collada 1.4.1 spec section 7: A_ONE (default) uses transparent.a, RGB_ZERO the luminance)
|
||||
-- Collada 1.4.1 section 7: A_ONE (default) uses transparent.a, RGB_ZERO the luminance
|
||||
if transparentEl and (type(transparent) == "table" or type(transparency) == "number") then
|
||||
local mode = attr(transparentEl, "opaque") or "A_ONE"
|
||||
local tv = if type(transparency) == "number" then transparency else 1
|
||||
@@ -531,7 +519,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ geometry
|
||||
type Input = { semantic: string, source: any, offset: number, set: number }
|
||||
|
||||
-- the inputs of a primitive element (VERTEX expanded into the <vertices> inputs)
|
||||
local function inputs(C: Ctx, prim: any, verticesEl: any): ({ Input }, number)
|
||||
local out: { Input } = {}
|
||||
local stride = 0
|
||||
@@ -563,7 +550,6 @@ local function inputs(C: Ctx, prim: any, verticesEl: any): ({ Input }, number)
|
||||
return out, math.max(stride, 1)
|
||||
end
|
||||
|
||||
-- faces(primEl, kind, stride) -> corner list (tuple starts into the concatenated p), face sizes, mode
|
||||
local function corners(C: Ctx, prim: any, kind: string, stride: number): ({ number }?, { number }?, { number }?, string)
|
||||
local p: { number } = {}
|
||||
local faces: { number } = {}
|
||||
@@ -670,7 +656,6 @@ local PRIMS = { triangles = true, polylist = true, polygons = true, lines = true
|
||||
|
||||
type Built = { prim: Types.Primitive, symbol: string?, texSets: { number }, posIndex: { number } }
|
||||
|
||||
-- one primitive element -> Primitive (+ the source position index of each output vertex, for skins / morphs)
|
||||
local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Built?
|
||||
local ins, stride = inputs(C, prim, verticesEl)
|
||||
local p, faces, starts, mode = corners(C, prim, kind, stride)
|
||||
@@ -688,7 +673,6 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
|
||||
skip(C, "primitives without positions")
|
||||
return nil
|
||||
end
|
||||
-- attribute names
|
||||
local named: { { input: Input, name: string, comps: number } } = {}
|
||||
local texSets: { number } = {}
|
||||
local texInputs: { Input } = {}
|
||||
@@ -725,10 +709,9 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
|
||||
table.insert(named, { input = i, name = "_" .. s .. (if i.set ~= 0 then tostring(i.set) else ""), comps = math.max(comps, 1) })
|
||||
end
|
||||
end
|
||||
-- de-index
|
||||
local np = #p
|
||||
local keyOf: { [any]: number } = {}
|
||||
local tuples: { number } = {} -- the first p index of each output vertex's tuple
|
||||
local tuples: { number } = {}
|
||||
local corner = table.create(#starts) :: { number }
|
||||
local count = 0
|
||||
for ci, s in starts do
|
||||
@@ -803,7 +786,6 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
|
||||
if allTri then
|
||||
mode = "triangles"
|
||||
elseif C.triangulate then
|
||||
-- fan triangulation
|
||||
local tri: { number } = {}
|
||||
local at = 0
|
||||
for _, f in faces do
|
||||
@@ -832,7 +814,6 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
|
||||
return { prim = out, symbol = attr(prim, "material"), texSets = texSets, posIndex = posIndex }
|
||||
end
|
||||
|
||||
-- geometry(el) -> the built primitives (cached per geometry element)
|
||||
local function geometry(C: Ctx, el: any): { Built }?
|
||||
local cached = C.posIndex[el]
|
||||
if cached then
|
||||
@@ -863,7 +844,6 @@ local function geometry(C: Ctx, el: any): { Built }?
|
||||
return out
|
||||
end
|
||||
|
||||
-- the material bindings of an instance: symbol -> material id, and semantic -> texcoord set
|
||||
local function bindings(node: any): ({ [string]: string }, { [string]: { [string]: number } })
|
||||
local syms: { [string]: string } = {}
|
||||
local sets: { [string]: { [string]: number } } = {}
|
||||
@@ -894,7 +874,6 @@ local function copyAccessor(a: Types.Accessor): Types.Accessor
|
||||
return Scene.accessor(b, a.type, a.components, a.count)
|
||||
end
|
||||
|
||||
-- mesh(geometry element, instance element, skin?) -> scene mesh index
|
||||
local function meshFor(C: Ctx, geomEl: any, inst: any, key: string, bindShape: { number }?): (number?, { Built }?)
|
||||
local syms, sets = bindings(inst)
|
||||
local parts = { key }
|
||||
@@ -915,7 +894,6 @@ local function meshFor(C: Ctx, geomEl: any, inst: any, key: string, bindShape: {
|
||||
for _, b in built do
|
||||
local prim = b.prim
|
||||
if bindShape then
|
||||
-- skinned: the bind shape matrix applied to positions and normals (a copy: the geometry may be shared)
|
||||
local attrs = table.clone(prim.attributes)
|
||||
local pos = copyAccessor(attrs.POSITION)
|
||||
for v = 0, pos.count - 1 do
|
||||
@@ -961,7 +939,6 @@ local function meshFor(C: Ctx, geomEl: any, inst: any, key: string, bindShape: {
|
||||
end
|
||||
end
|
||||
if target then
|
||||
-- texcoord semantic -> TEXCOORD_n index among this primitive's sets
|
||||
local uv: { [string]: number } = {}
|
||||
for sem, set in (sym and sets[sym]) or {} do
|
||||
uv[sem] = (table.find(b.texSets, set) or 1) - 1
|
||||
@@ -976,7 +953,6 @@ local function meshFor(C: Ctx, geomEl: any, inst: any, key: string, bindShape: {
|
||||
return idx, built
|
||||
end
|
||||
|
||||
-- morph controller -> mesh with targets (deltas)
|
||||
local function morphMesh(C: Ctx, ctrl: any, morph: any, inst: any): number?
|
||||
local base = C.ids[urlId(attr(morph, "source")) or ""]
|
||||
if not base or lname(base) ~= "geometry" then
|
||||
@@ -1082,7 +1058,6 @@ local function camera(C: Ctx, el: any): number?
|
||||
return #C.scene.cameras
|
||||
end
|
||||
|
||||
-- light(el) -> light index, or nil and the ambient colour
|
||||
local function light(C: Ctx, el: any): (number?, { number }?)
|
||||
local tc = child(el, "technique_common")
|
||||
if not tc then
|
||||
@@ -1124,7 +1099,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ nodes
|
||||
local XFORMS = { matrix = 16, translate = 3, rotate = 4, scale = 3, lookat = 9, skew = 7 }
|
||||
|
||||
-- the matrix of one transform element's values
|
||||
local function xfMatrix(kind: string, v: { number }): { number }
|
||||
if kind == "matrix" then
|
||||
return Mat4.fromRows(v)
|
||||
@@ -1182,7 +1156,7 @@ local function addChild(C: Ctx, parent: Types.Node, node: Types.Node): number
|
||||
return idx
|
||||
end
|
||||
|
||||
-- the instances of a node: the first of each kind on the node itself, the rest on child nodes
|
||||
-- extra instances of a node go on child nodes
|
||||
local function instances(C: Ctx, el: any, node: Types.Node, idx: number)
|
||||
local function slot(kind: string, name: string?): (Types.Node, number)
|
||||
local n: any = node
|
||||
@@ -1322,7 +1296,7 @@ visitNode = function(C: Ctx, el: any): number?
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ skins
|
||||
-- the scene node for a joint name: by sid below the skeleton roots, else by sid / id / name anywhere
|
||||
-- joints: by sid below the skeleton roots, else by sid / id / name anywhere
|
||||
local function findJoint(C: Ctx, name: string, roots: { number }, byId: boolean): number?
|
||||
local nodes = C.scene.nodes
|
||||
local function search(i: number, depth: number): number?
|
||||
@@ -1410,7 +1384,7 @@ local function resolveSkins(C: Ctx)
|
||||
local skin: Types.Skin = { name = attr(ps.ctrl, "name") or attr(ps.ctrl, "id"), joints = jointNodes }
|
||||
if ps.bind then
|
||||
local sk: any = skin
|
||||
sk.extras = { dae = { bindShapeMatrix = ps.bind } } -- already applied to the mesh's positions / normals
|
||||
sk.extras = { dae = { bindShapeMatrix = ps.bind } }
|
||||
end
|
||||
if ibm and ibm.comps == 16 and ibm.count >= #names then
|
||||
local b = buffer.create(#names * 64)
|
||||
@@ -1432,7 +1406,6 @@ local function resolveSkins(C: Ctx)
|
||||
cache[key] = si
|
||||
end
|
||||
C.scene.nodes[ps.node].skin = si
|
||||
-- vertex weights -> JOINTS_0 / WEIGHTS_0 (once per mesh)
|
||||
local mesh = C.scene.meshes[ps.mesh]
|
||||
if mesh.primitives[1] and mesh.primitives[1].attributes.JOINTS_0 then
|
||||
continue
|
||||
@@ -1457,7 +1430,7 @@ local function resolveSkins(C: Ctx)
|
||||
local vcount = numbers(text(child(vw, "vcount") or ""), C.limit) or {}
|
||||
local v = numbers(text(child(vw, "v") or ""), C.limit * 8) or {}
|
||||
local wdata = wSrc and wSrc.data or {}
|
||||
local per: { { number } } = {} -- position index -> { j1, w1, j2, w2, ... } strongest 4
|
||||
local per: { { number } } = {}
|
||||
local at = 0
|
||||
for pi, c in vcount do
|
||||
local list = {}
|
||||
@@ -1555,9 +1528,9 @@ local function sample(c: Curve, t: number): { number }
|
||||
return a
|
||||
end
|
||||
|
||||
-- resolve "id/sid/sid.member" or "id/sid(i)(j)" to a node's transform element
|
||||
-- "id/sid/sid.member" or "id/sid(i)(j)"
|
||||
local function target(C: Ctx, s: string): ({ number }?, any, number?)
|
||||
-- the id may contain dots (assimp writes "Bone.001/matrix"); sids cannot, so the member starts after the last "/"
|
||||
-- ids may contain dots (assimp writes "Bone.001/matrix"); sids cannot, so the member starts after the last "/"
|
||||
local id, sids = match(s, "^([^/]*)/(.*)$")
|
||||
if not id then
|
||||
return nil, nil, nil
|
||||
@@ -1607,7 +1580,6 @@ local function target(C: Ctx, s: string): ({ number }?, any, number?)
|
||||
return nodes, xf, member
|
||||
end
|
||||
|
||||
-- the channels of one <animation> (recursively) -> curves grouped by node
|
||||
local function collect(C: Ctx, anim: any, byNode: { [number]: { Curve } }, order: { number }, depth: number)
|
||||
if depth > 64 then
|
||||
return
|
||||
@@ -1697,7 +1669,6 @@ local function bake(C: Ctx, name: string?, byNode: { [number]: { Curve } }, orde
|
||||
local tb, rb, sb = buffer.create(n * 12), buffer.create(n * 16), buffer.create(n * 12)
|
||||
local prev: { number }? = nil
|
||||
for i, t in times do
|
||||
-- this time's values of each animated element
|
||||
local xf = {}
|
||||
for k, x in info.xf do
|
||||
xf[k] = { kind = x.kind, values = table.clone(x.values) }
|
||||
@@ -1738,7 +1709,6 @@ local function bake(C: Ctx, name: string?, byNode: { [number]: { Curve } }, orde
|
||||
table.insert(channels, { node = ni, path = "translation", input = input, output = Scene.accessor(tb, "f32", 3, n), interpolation = "LINEAR" } :: Types.Channel)
|
||||
table.insert(channels, { node = ni, path = "rotation", input = input, output = Scene.accessor(rb, "f32", 4, n), interpolation = "LINEAR" } :: Types.Channel)
|
||||
table.insert(channels, { node = ni, path = "scale", input = input, output = Scene.accessor(sb, "f32", 3, n), interpolation = "LINEAR" } :: Types.Channel)
|
||||
-- the node is now TRS-driven
|
||||
local node = C.scene.nodes[ni]
|
||||
if node.matrix then
|
||||
setTransform(node, node.matrix)
|
||||
@@ -1821,12 +1791,11 @@ function Read.scene(doc: any, fetch: Fetch, opts: any?): (Types.Scene?, Err.Err?
|
||||
}
|
||||
for _, el in Xml.descendants(root) do
|
||||
local id = el.attrs.id
|
||||
-- duplicate ids: the first one, unless it is empty (some exporters write <light id="x"/> before the real one)
|
||||
-- duplicate ids: the first, unless empty (some exporters write <light id="x"/> before the real one)
|
||||
if type(id) == "string" and (C.ids[id] == nil or #C.ids[id].children == 0) then
|
||||
C.ids[id] = el
|
||||
end
|
||||
end
|
||||
-- asset: up axis, unit
|
||||
local asset = child(root, "asset")
|
||||
local up = asset and child(asset, "up_axis")
|
||||
local upAxis = if up then upper(match(text(up), "[%w_]+") or "Y_UP") else "Y_UP"
|
||||
@@ -1834,7 +1803,6 @@ function Read.scene(doc: any, fetch: Fetch, opts: any?): (Types.Scene?, Err.Err?
|
||||
local unit = asset and child(asset, "unit")
|
||||
local meter = unit and attr(unit, "meter")
|
||||
scene.meta.unit = if meter then tonumber(meter) or tonumber((string.gsub(meter, ",", "."))) or 1 else 1
|
||||
-- the instanced visual scene (else the first one)
|
||||
local vsId: string? = nil
|
||||
local sceneEl = child(root, "scene")
|
||||
local ivs = sceneEl and child(sceneEl, "instance_visual_scene")
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
--!strict
|
||||
-- model/dae/Write: Scene -> Collada 1.4.1 text. One <geometry> per mesh primitive (triangles, lines or polylist; strips
|
||||
-- and fans unrolled into triangles, points skipped) with POSITION / NORMAL / TEXCOORD_n (V flipped back) / COLOR_n
|
||||
-- sources sharing one index; common-profile phong (or lambert / constant) effects from the PBR material and its
|
||||
-- extras.phong; images as files (opts.files), their uri, or data: URIs; the node tree as row-major <matrix>; cameras;
|
||||
-- lights. Not written: skins, morph targets, animations.
|
||||
-- model/dae/Write: Scene -> Collada 1.4.1 text; strips and fans unrolled, points skipped, nodes as row-major <matrix>.
|
||||
-- Not written: skins, morph targets, animations.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Xml = require("@rosetta/codecs/text/Xml")
|
||||
local Base64 = require("@rosetta/core/Base64")
|
||||
@@ -38,7 +35,6 @@ local function nameAttr(s: string?): string
|
||||
return if s then ' name="' .. esc(s) .. '"' else ""
|
||||
end
|
||||
|
||||
-- triangle / line corner lists of a primitive (0-based vertex indices) and the element kind
|
||||
local function corners(prim: Types.Primitive): ({ number }?, string?, { number }?)
|
||||
local n = prim.attributes.POSITION.count
|
||||
local idx: { number } = {}
|
||||
@@ -160,7 +156,6 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
local meta: { [string]: any } = scene.meta or {}
|
||||
local up = if meta.up == "z" then "Z_UP" elseif meta.up == "x" then "X_UP" else "Y_UP"
|
||||
w(format('<asset><contributor><authoring_tool>rosetta</authoring_tool></contributor><created>1970-01-01T00:00:00Z</created><modified>1970-01-01T00:00:00Z</modified><unit meter="%s" name="unit"/><up_axis>%s</up_axis></asset>\n', f(meta.unit or 1), up))
|
||||
-- images
|
||||
local imageId: { [number]: string } = {}
|
||||
if #scene.textures > 0 then
|
||||
w("<library_images>\n")
|
||||
@@ -184,12 +179,11 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
end
|
||||
w("</library_images>\n")
|
||||
end
|
||||
-- effects + materials
|
||||
if #scene.materials > 0 then
|
||||
w("<library_effects>\n")
|
||||
for i, mat in scene.materials do
|
||||
local ph: any = mat.extras and mat.extras.phong or {}
|
||||
local params: { [any]: any } = {} -- the newparam strings, and their sids as seen-keys
|
||||
local params: { [any]: any } = {}
|
||||
local function texParam(ref: Types.TextureRef?): (string?, number?)
|
||||
if not ref or not imageId[ref.texture] then
|
||||
return nil, nil
|
||||
@@ -236,7 +230,6 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
end
|
||||
w("</library_materials>\n")
|
||||
end
|
||||
-- geometries: one per primitive
|
||||
local geoms: { [number]: { { id: string, material: number?, sets: number } } } = {}
|
||||
w("<library_geometries>\n")
|
||||
for mi, mesh in scene.meshes do
|
||||
@@ -289,7 +282,6 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
end
|
||||
end
|
||||
w("</library_geometries>\n")
|
||||
-- cameras / lights
|
||||
if #scene.cameras > 0 then
|
||||
w("<library_cameras>\n")
|
||||
for i, cam in scene.cameras do
|
||||
@@ -316,7 +308,6 @@ function Write.build(scene: Types.Scene, opts: any?): string?
|
||||
end
|
||||
w("</library_lights>\n")
|
||||
end
|
||||
-- the node tree
|
||||
w('<library_visual_scenes><visual_scene id="scene" name="scene">\n')
|
||||
local seen: { [number]: boolean } = {}
|
||||
local function node(i: number, depth: number)
|
||||
|
||||
@@ -1,26 +1,8 @@
|
||||
--!strict
|
||||
-- model/dae: COLLADA 1.4.1 / 1.5.0 (Khronos COLLADA specifications; .dae XML, and .zae: a zip holding the .dae, its
|
||||
-- manifest.xml and the images). New code.
|
||||
-- Reads: geometry (<mesh> triangles / polylist / polygons / lines / linestrips / tristrips / trifans with shared input
|
||||
-- offsets, de-indexed per index tuple; n-gons fan-triangulated unless opts.triangulate = false keeps "polygons"),
|
||||
-- common-profile materials (phong / blinn / lambert / constant -> PBR baseColor / emissive / roughness from shininess /
|
||||
-- alpha from transparent + transparency; every Phong term kept in material.extras.phong; FCOLLADA / MAX3D bump ->
|
||||
-- normalTexture, double_sided), images through newparam sampler2D -> surface -> image (1.4) or instance_image (1.5),
|
||||
-- kept encoded (opts.resolve / opts.files, embedded hex <data>, data: URIs), the instanced visual scene (matrix /
|
||||
-- translate / rotate / scale / lookat / skew; instance_geometry / controller / camera / light / node; extra instances
|
||||
-- of a node become child nodes), skin controllers (bind_shape_matrix applied to the mesh, joints by sid under
|
||||
-- <skeleton> or by id / name, INV_BIND_MATRIX, JOINTS_0 / WEIGHTS_0 from the 4 strongest weights, normalized), morph
|
||||
-- controllers (targets as POSITION / NORMAL deltas, weights), cameras (perspective: yfov from xfov / aspect;
|
||||
-- orthographic), lights (directional / point / spot; attenuation in light.extras.dae; ambient lights as
|
||||
-- node.extras.dae.ambientLight), animations (channels on node transform elements: whole elements, .X / .ANGLE
|
||||
-- members, matrix (i)(j) elements; resampled at the union of their key times, rotations over 90 degrees subdivided,
|
||||
-- into glTF TRS channels; one animation per animation_clip, else one for the file). meta.up = the up_axis ("y" / "z"
|
||||
-- / "x"), meta.unit = unit meter; the data is never rotated or scaled. TEXCOORD_n V is flipped to glTF's top-left
|
||||
-- origin (opts.flipV = false keeps Collada's).
|
||||
-- Writes: Collada 1.4.1 (see model/dae/Write: meshes, materials, images, nodes, cameras, lights), or a .zae.
|
||||
-- Not supported: <spline> / <brep> geometry, polygon holes (<ph>: the outer loop only), Bezier / Hermite curves
|
||||
-- (sampled linearly at their keys), animations of non-transform targets, physics / kinematics / FX profiles other than
|
||||
-- COMMON (counted in meta.source.skipped). Writing skins, morphs and animations.
|
||||
-- model/dae: COLLADA 1.4.1 / 1.5.0 (Khronos specifications; .dae XML, .zae zip with manifest.xml). Supports: geometry,
|
||||
-- common-profile materials, images, the visual scene, skin / morph controllers, cameras, lights, transform animations.
|
||||
-- Not supported: <spline> / <brep>, polygon holes (<ph>), Bezier / Hermite curves (sampled linearly), non-transform
|
||||
-- animation targets, physics / kinematics / non-COMMON FX profiles; writing skins, morphs and animations.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -43,17 +25,17 @@ local Dae = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
resolve: ((uri: string) -> any)?, -- image files (buffer or string); the api sets it for paths
|
||||
files: { [string]: any }?, -- or: the files by uri / base name
|
||||
triangulate: boolean?, -- fan-triangulate n-gons (default true); false keeps "polygons" primitives
|
||||
flipV: boolean?, -- TEXCOORD_n v = 1 - v (default true: glTF's convention)
|
||||
maxVertices: number?, -- refuse more output vertices / array values (default 2^25)
|
||||
resolve: ((uri: string) -> any)?, -- image files (buffer or string)
|
||||
files: { [string]: any }?,
|
||||
triangulate: boolean?, -- default true
|
||||
flipV: boolean?, -- default true
|
||||
maxVertices: number?, -- default 2^25
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
files: { [string]: any }?, -- receives the texture images (else they are written as data: URIs or their uri)
|
||||
name: string?, -- the destination's file name (names the image files)
|
||||
flipV: boolean?, -- default true (the inverse of decode)
|
||||
format: string?, -- "zae" writes a zip (manifest.xml + model.dae + images); the api passes the extension
|
||||
files: { [string]: any }?, -- receives the texture images (else data: URIs or their uri)
|
||||
name: string?,
|
||||
flipV: boolean?, -- default true
|
||||
format: string?, -- "zae" writes a zip (manifest.xml + model.dae + images)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -107,7 +89,7 @@ local function fetcher(opts: DecodeOptions?, zip: any): Read.Fetch
|
||||
end
|
||||
end
|
||||
|
||||
-- the main .dae of a .zae: manifest.xml's <dae_root>, else the first .dae entry
|
||||
-- a .zae's main .dae: manifest.xml's <dae_root>, else the first .dae entry
|
||||
local function zae(r: Bytes.Reader): (any, string?, Err.Err?)
|
||||
local zip, err = Zip.open(r)
|
||||
if not zip then
|
||||
@@ -174,7 +156,6 @@ function Dae.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
return nil, Err.new(Err.ARG, "needs a Scene")
|
||||
end
|
||||
if opts and opts.format == "zae" then
|
||||
-- a zip: manifest.xml, the .dae and its images
|
||||
local files = {}
|
||||
local o = table.clone(opts)
|
||||
o.files = files
|
||||
|
||||
Reference in new issue
Block a user