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,11 +1,9 @@
|
||||
--!strict
|
||||
-- model/3ds/Build: model/3ds/Read's chunk tables -> Types.Scene. The 3DS model: mesh vertices are stored in world
|
||||
-- space and the mesh matrix (0x4160) is the object's world transform; the keyframer places node(t) = parent(t) *
|
||||
-- T(pos) * R(rot) * S(scale) and draws a mesh at node(t) * T(-pivot) * inverse(mesh matrix) (a mirroring mesh matrix
|
||||
-- also flips x, as 3ds Max writes it and assimp reads it). Rotation keys are axis / angle deltas from the previous key
|
||||
-- (the angle measured clockwise). Cameras and spotlights look at their target node's position, with the camera's
|
||||
-- roll about the view axis. Smoothing groups give the normals (corners of faces that share a group and a position
|
||||
-- are averaged; group 0 is flat). Times are frame / fps.
|
||||
-- model/3ds/Build: Read's chunk tables -> Types.Scene.
|
||||
-- 3DS transforms: mesh vertices are in world space and the mesh matrix (0x4160) is the object's world transform; a
|
||||
-- keyframer node is parent * T(pos) * R(rot) * S(scale) and draws its mesh at node * T(-pivot) * inverse(mesh matrix)
|
||||
-- (a mirroring mesh matrix also flips x, as 3ds Max writes and assimp reads). Rotation keys are clockwise axis / angle
|
||||
-- deltas from the previous key. Smoothing group 0 is flat.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
local Read = require("@rosetta/model/3ds/Read")
|
||||
@@ -20,7 +18,6 @@ local sqrt, abs = math.sqrt, math.abs
|
||||
export type Options = { normals: boolean?, flipV: boolean?, fps: number? }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ math
|
||||
-- the mesh matrix (X, Y, Z axes, origin) as a column-major 4x4
|
||||
local function meshMatrix(m: { number }?): { number }
|
||||
if not m then
|
||||
return Mat4.identity()
|
||||
@@ -34,7 +31,6 @@ local function det3(m: { number }): number
|
||||
end
|
||||
Build.det3 = det3
|
||||
|
||||
-- q(a) * q(b)
|
||||
local function qmul(a: { number }, b: { number }): { number }
|
||||
local ax, ay, az, aw = a[1], a[2], a[3], a[4]
|
||||
local bx, by, bz, bw = b[1], b[2], b[3], b[4]
|
||||
@@ -47,7 +43,7 @@ local function qmul(a: { number }, b: { number }): { number }
|
||||
end
|
||||
Build.qmul = qmul
|
||||
|
||||
-- a rotation key (angle in radians, axis) -> its quaternion (3DS angles turn clockwise)
|
||||
-- 3DS angles turn clockwise
|
||||
local function keyQuat(angle: number, x: number, y: number, z: number): { number }
|
||||
if x == 0 and y == 0 and z == 0 then
|
||||
return { 0, 0, 0, 1 }
|
||||
@@ -63,7 +59,7 @@ local function normalize(x: number, y: number, z: number): (number, number, numb
|
||||
return x / l, y / l, z / l, l
|
||||
end
|
||||
|
||||
-- lookAt(pos, target, rollDegrees) -> the rotation whose -Z looks from pos to target, +Y up (world up +Z)
|
||||
-- -Z looks from pos to target, world up +Z
|
||||
function Build.lookAt(p: { number }, t: { number }, roll: number): { number }
|
||||
local fx, fy, fz, l = normalize(t[1] - p[1], t[2] - p[2], t[3] - p[3])
|
||||
if l == 0 then
|
||||
@@ -73,7 +69,6 @@ function Build.lookAt(p: { number }, t: { number }, roll: number): { number }
|
||||
if abs(fz) > 0.999999 then
|
||||
ux, uy, uz = 0, 1, 0
|
||||
end
|
||||
-- right = forward x up, up = right x forward
|
||||
local rx, ry, rz = normalize(fy * uz - fz * uy, fz * ux - fx * uz, fx * uy - fy * ux)
|
||||
ux, uy, uz = ry * fz - rz * fy, rz * fx - rx * fz, rx * fy - ry * fx
|
||||
local a = math.rad(roll)
|
||||
@@ -163,18 +158,14 @@ local function materials(raw: Read.Raw, scene: Types.Scene): { [string]: number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ meshes
|
||||
-- mesh(obj, xf, matByName, opts) -> a Types.Mesh of the object's faces (one primitive per material), vertices moved
|
||||
-- by xf (world -> the node's local space)
|
||||
local function mesh(name: string, m: Read.Mesh, xf: { number }, matByName: { [string]: number }, opts: Options): Types.Mesh
|
||||
local nv, nf = m.nv, m.nf
|
||||
local pos, faces = m.positions, m.faces
|
||||
-- local positions
|
||||
local lp = table.create(nv * 3, 0)
|
||||
for v = 0, nv - 1 do
|
||||
local x, y, z = Mat4.point(xf, readf32(pos, v * 12), readf32(pos, v * 12 + 4), readf32(pos, v * 12 + 8))
|
||||
lp[v * 3 + 1], lp[v * 3 + 2], lp[v * 3 + 3] = x, y, z
|
||||
end
|
||||
-- faces with valid corners, their materials
|
||||
local fa, fb, fc = table.create(nf, 0), table.create(nf, 0), table.create(nf, 0)
|
||||
local valid = table.create(nf, false)
|
||||
for f = 0, nf - 1 do
|
||||
@@ -193,7 +184,6 @@ local function mesh(name: string, m: Read.Mesh, xf: { number }, matByName: { [st
|
||||
end
|
||||
local smooth = m.smoothing
|
||||
local doNormals = opts.normals ~= false
|
||||
-- face normals (area weighted) and the faces around each distinct position
|
||||
local fn = table.create(nf * 3, 0)
|
||||
local around: { [number]: { number } } = {}
|
||||
local group = table.create(nv, 0)
|
||||
@@ -248,7 +238,6 @@ local function mesh(name: string, m: Read.Mesh, xf: { number }, matByName: { [st
|
||||
local nx, ny, nz = normalize(x, y, z)
|
||||
return nx, ny, nz
|
||||
end
|
||||
-- primitives in the order their material first appears
|
||||
local order: { number } = {}
|
||||
local byMat: { [number]: { number } } = {}
|
||||
for f = 1, nf do
|
||||
@@ -321,7 +310,6 @@ local function mesh(name: string, m: Read.Mesh, xf: { number }, matByName: { [st
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ tracks
|
||||
-- the value of a track at a frame (linear between keys, clamped at the ends)
|
||||
local function evalTrack(t: Read.Track?, frame: number, default: { number }): { number }
|
||||
if not t or #t.frames == 0 then
|
||||
return default
|
||||
@@ -359,7 +347,6 @@ local function scaleKey(v: { number }): { number }
|
||||
return { if v[1] == 0 then 1 else v[1], if v[2] == 0 then 1 else v[2], if v[3] == 0 then 1 else v[3] }
|
||||
end
|
||||
|
||||
-- absolute rotations from a rotation track: times (frames), quaternions; deltas over ~180 degrees get extra keys
|
||||
local function rotations(t: Read.Track): ({ number }, { { number } })
|
||||
local frames, quats = {}, {}
|
||||
local acc: { number }? = nil
|
||||
@@ -396,7 +383,6 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
local fps = o.fps or 30
|
||||
local scene = S.new()
|
||||
local matByName = materials(raw, scene)
|
||||
-- objects by name
|
||||
local objByName: { [string]: Read.Object } = {}
|
||||
for _, obj in raw.objects do
|
||||
if objByName[obj.name] == nil then
|
||||
@@ -478,7 +464,6 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
|
||||
local kf = raw.keyframer
|
||||
local kfNodes = if kf then kf.nodes else {}
|
||||
-- targets of cameras / spotlights by name
|
||||
local targets: { [string]: Read.Node } = {}
|
||||
for _, n in kfNodes do
|
||||
if n.kind == "target" or n.kind == "lightTarget" then
|
||||
@@ -486,7 +471,7 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
end
|
||||
end
|
||||
local placedObj: { [Read.Object]: boolean } = {}
|
||||
local sceneOf: { [number]: number } = {} -- kf id -> scene node
|
||||
local sceneOf: { [number]: number } = {}
|
||||
local channels: { Types.Channel } = {}
|
||||
local function channel(node: number, path: any, times: { number }, values: { number }, comps: number)
|
||||
for i = 1, #times do
|
||||
@@ -551,7 +536,6 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
channel(ni, "scale", table.clone(tr.scl.frames), vals, 3)
|
||||
end
|
||||
else
|
||||
-- cameras and lights: position, target, roll
|
||||
local isCam = kind == "camera"
|
||||
local cam = if obj and isCam then obj.camera else nil
|
||||
local lit = if obj and not isCam then obj.light else nil
|
||||
@@ -581,7 +565,6 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
end
|
||||
local ni = addNode(node, parent)
|
||||
sceneOf[id] = ni
|
||||
-- every key time of the position, roll and target tracks
|
||||
local set: { [number]: boolean } = {}
|
||||
local times: { number } = {}
|
||||
for _, t in { tr.pos, tr.roll, if aims and tgt then tgt.tracks.pos else nil } do
|
||||
@@ -612,7 +595,6 @@ function Build.scene(raw: Read.Raw, opts: Options?): Types.Scene
|
||||
end
|
||||
end
|
||||
end
|
||||
-- objects the keyframer does not place (or files without one): at their own matrix / position
|
||||
for _, obj in raw.objects do
|
||||
if placedObj[obj] then
|
||||
continue
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
--!strict
|
||||
-- model/3ds/Read: the 3DS chunk tree (a u16 id and a u32 size that counts the 6-byte header, little endian) read
|
||||
-- into plain tables: materials, named objects (triangle meshes, cameras, lights) and the keyframer (nodes with their
|
||||
-- tracks). Chunk ids and layouts from the 3D Studio file format as documented by Autodesk's 3DS File Toolkit notes
|
||||
-- and read by assimp's 3DS importer (BSD-3, used as the reference) and lib3ds (read for the layout only, no code).
|
||||
-- Unknown chunks are skipped. A chunk running past the end of the data is an error; one running past its parent is
|
||||
-- clipped to the parent. Never throws on bad data: returns nil, message, offset.
|
||||
-- model/3ds/Read: the 3DS chunk tree (u16 id, u32 size including the 6-byte header, little endian) -> plain tables.
|
||||
-- Unknown chunks are skipped; a chunk past the end of the data is an error, one past its parent is clipped to it.
|
||||
local Read = {}
|
||||
|
||||
local readu8, readu16, readu32, readi16, readf32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi16, buffer.readf32
|
||||
@@ -28,17 +24,14 @@ local C = {
|
||||
}
|
||||
Read.C = C
|
||||
|
||||
-- material map chunks -> their name in Raw materials
|
||||
Read.MAPS = {
|
||||
[0xA200] = "diffuse", [0xA33A] = "diffuse2", [0xA210] = "opacity", [0xA230] = "bump", [0xA204] = "specular",
|
||||
[0xA33C] = "shininess", [0xA33D] = "selfIllum", [0xA220] = "reflection",
|
||||
}
|
||||
-- keyframer node chunks -> kind
|
||||
local NODE_KINDS = {
|
||||
[0xB001] = "ambient", [0xB002] = "object", [0xB003] = "camera", [0xB004] = "target", [0xB005] = "light",
|
||||
[0xB006] = "lightTarget", [0xB007] = "spot",
|
||||
}
|
||||
-- track chunks -> (field, values per key)
|
||||
local TRACKS: { [number]: { any } } = {
|
||||
[0xB020] = { "pos", 3 }, [0xB021] = { "rot", 4 }, [0xB022] = { "scl", 3 }, [0xB023] = { "fov", 1 },
|
||||
[0xB024] = { "roll", 1 }, [0xB025] = { "color", 3 }, [0xB027] = { "hotspot", 1 }, [0xB028] = { "falloff", 1 },
|
||||
@@ -59,7 +52,7 @@ export type Mesh = {
|
||||
nf: number, faces: buffer?, -- nf * 4 u16 (a, b, c, flags)
|
||||
faceMats: { { name: string, faces: { number } } }, -- 0-based face numbers
|
||||
smoothing: buffer?, -- nf u32
|
||||
matrix: { number }?, -- 12 floats: the X, Y, Z axes and the origin (world space)
|
||||
matrix: { number }?, -- 12 floats: X, Y, Z axes, origin (world space)
|
||||
}
|
||||
export type Camera = { pos: { number }, target: { number }, roll: number, lens: number, near: number?, far: number? }
|
||||
export type Light = {
|
||||
@@ -82,13 +75,12 @@ export type Raw = {
|
||||
-- ------------------------------------------------------------------------------------------------ helpers
|
||||
type Ctx = { b: buffer, limit: number, maxVertices: number, raw: Raw }
|
||||
|
||||
-- each(ctx, p, stop, fn): calls fn(id, body, chunkEnd) for the chunks of [p, stop); returns fn's error or a size error
|
||||
local function each(ctx: Ctx, p: number, stop: number, fn: (number, number, number) -> (string?, number?)): (string?, number?)
|
||||
local b = ctx.b
|
||||
while p + 6 <= stop do
|
||||
local id, size = readu16(b, p), readu32(b, p + 2)
|
||||
if size < 6 then
|
||||
return nil, nil -- padding or garbage after the last chunk
|
||||
return nil, nil
|
||||
end
|
||||
local e = p + size
|
||||
if e > ctx.limit then
|
||||
@@ -103,7 +95,6 @@ local function each(ctx: Ctx, p: number, stop: number, fn: (number, number, numb
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- cstring(b, p, stop) -> the NUL-terminated string at p, the offset after it | nil
|
||||
local function cstring(b: buffer, p: number, stop: number): (string?, number)
|
||||
for q = p, stop - 1 do
|
||||
if readu8(b, q) == 0 then
|
||||
@@ -117,7 +108,7 @@ local function vec3(b: buffer, p: number): { number }
|
||||
return { readf32(b, p), readf32(b, p + 4), readf32(b, p + 8) }
|
||||
end
|
||||
|
||||
-- a colour chunk list: the first float or byte colour (gamma-corrected "linear" variants only when nothing else)
|
||||
-- colour chunks: the first float or byte colour (the gamma-corrected variants only when nothing else)
|
||||
local function color(ctx: Ctx, p: number, stop: number): { number }?
|
||||
local b = ctx.b
|
||||
local main: { number }?, lin: { number }? = nil, nil
|
||||
@@ -142,7 +133,6 @@ local function color(ctx: Ctx, p: number, stop: number): { number }?
|
||||
return main or lin
|
||||
end
|
||||
|
||||
-- a percentage chunk list -> a fraction 0..1
|
||||
local function percent(ctx: Ctx, p: number, stop: number): number?
|
||||
local b = ctx.b
|
||||
local v: number? = nil
|
||||
@@ -472,7 +462,6 @@ local function editor(ctx: Ctx, p: number, stop: number): (string?, number?)
|
||||
end)
|
||||
end
|
||||
|
||||
-- parse(b, base, limit, opts) -> Raw | nil, message, offset
|
||||
function Read.parse(b: buffer, base: number, limit: number, opts: any?): (Raw?, string?, number?)
|
||||
if limit - base < 6 or readu16(b, base) ~= C.MAIN then
|
||||
return nil, "not a 3DS file (no 0x4D4D main chunk)", base
|
||||
@@ -483,7 +472,6 @@ function Read.parse(b: buffer, base: number, limit: number, opts: any?): (Raw?,
|
||||
if size < 6 then
|
||||
return nil, "bad main chunk size", base
|
||||
end
|
||||
-- a main chunk declared longer than the data: truncated
|
||||
if base + size > limit then
|
||||
return nil, "truncated: the main chunk runs past the end of the data", base
|
||||
end
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
--!strict
|
||||
-- model/3ds/Write: Types.Scene -> 3DS. One named object per mesh (split at 65535 vertices / faces), vertices in world
|
||||
-- space at the first node that shows the mesh, its world matrix as the mesh matrix, uvs (v flipped back to 3DS's
|
||||
-- bottom-left origin), smoothing groups (extras["3ds"].smoothing from model/3ds, else 0 for flat-shaded faces and 1
|
||||
-- for the others), per-face materials; Phong materials (extras["3ds"] when present, else from the PBR fields) with
|
||||
-- texture maps (file names: texture.uri, or a name for texture.data put into opts.files); cameras (lens from yfov and
|
||||
-- aspect, target along -Z) and lights (omni / spot; directional lights become spots); the keyframer: every node in
|
||||
-- tree order (object nodes, $$$DUMMY nodes for empty ones, camera / light nodes with their targets), pivot 0, one key
|
||||
-- per track or the keys of the first animation (frames = round(time * fps)), rotation keys as axis / angle deltas.
|
||||
-- Mirroring nodes are written the way 3ds Max does (positive keyframer transform, mirror in the mesh matrix).
|
||||
-- model/3ds/Write: Types.Scene -> 3DS. Objects split at 65535 vertices / faces; mirroring nodes written as 3ds Max does
|
||||
-- (positive keyframer transform, mirror in the mesh matrix).
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Sampler = require("@rosetta/codecs/model/Sampler")
|
||||
@@ -108,7 +101,6 @@ local function unit(x: number, y: number, z: number): (number, number, number)
|
||||
return x / l, y / l, z / l
|
||||
end
|
||||
|
||||
-- a quaternion -> the 3DS (clockwise angle, axis)
|
||||
local function axisAngle(q: { number }): (number, number, number, number)
|
||||
local x, y, z, w = q[1], q[2], q[3], q[4]
|
||||
local n = sqrt(x * x + y * y + z * z + w * w)
|
||||
@@ -128,7 +120,6 @@ local function conj(q: { number }): { number }
|
||||
return { -q[1], -q[2], -q[3], q[4] }
|
||||
end
|
||||
|
||||
-- a channel's value at a time (codecs/model/Sampler: STEP, LINEAR with slerp, CUBICSPLINE)
|
||||
local function sample(ch: Types.Channel, t: number): { number }
|
||||
local f = Sampler.channel(ch)
|
||||
if not f then
|
||||
@@ -191,7 +182,6 @@ local function writeMaterial(w: W, m: Types.Material, mname: string, files: { [n
|
||||
if x.wire then
|
||||
leaf(w, C.MAT_WIRE, function() end)
|
||||
end
|
||||
-- maps: the PBR texture refs first, then the other kinds model/3ds read
|
||||
local maps: { [string]: any } = {}
|
||||
local function fromRef(ref: Types.TextureRef?, saved: any): any
|
||||
if not ref or not files[ref.texture] then
|
||||
@@ -246,7 +236,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ meshes
|
||||
type Part = { name: string, positions: { number }, uvs: { number }?, faces: { number }, smooth: { number }, mats: { [number]: { number } } }
|
||||
|
||||
-- the mesh as 3DS parts (local space), or nil, message
|
||||
local function parts(mesh: Types.Mesh, base: string, opts: Options): ({ Part }?, string?)
|
||||
local merged, err = Flatten.merge({ kind = "scene", nodes = {}, roots = {}, meshes = { mesh } }, function(n: string): boolean
|
||||
return n == "TEXCOORD_0" or n == "NORMAL"
|
||||
@@ -260,7 +249,6 @@ local function parts(mesh: Types.Mesh, base: string, opts: Options): ({ Part }?,
|
||||
local tv, tc = if T then T.values else nil, if T then T.components else 0
|
||||
local flipV = opts.flipV ~= false
|
||||
local corners = merged.corners
|
||||
-- per face: material, smoothing group
|
||||
local nf = #corners // 3
|
||||
local fmat, fsg = table.create(nf, 0), table.create(nf, 1)
|
||||
for _, part in merged.parts do
|
||||
@@ -272,7 +260,6 @@ local function parts(mesh: Types.Mesh, base: string, opts: Options): ({ Part }?,
|
||||
if savedOk then
|
||||
fsg[f] = saved[f - part.firstFace]
|
||||
elseif N then
|
||||
-- flat when every corner normal is the face normal
|
||||
local a, b, c = corners[f * 3 - 2], corners[f * 3 - 1], corners[f * 3]
|
||||
local e1 = { P[b * 3 + 1] - P[a * 3 + 1], P[b * 3 + 2] - P[a * 3 + 2], P[b * 3 + 3] - P[a * 3 + 3] }
|
||||
local e2 = { P[c * 3 + 1] - P[a * 3 + 1], P[c * 3 + 2] - P[a * 3 + 2], P[c * 3 + 3] - P[a * 3 + 3] }
|
||||
@@ -398,7 +385,6 @@ local function writeMesh(w: W, part: Part, M: { number }, matNames: { [number]:
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ cameras / lights
|
||||
-- the eye, target and roll of a node's world matrix (looking down -Z, Y up)
|
||||
local function aim(m: { number }, dist: number): ({ number }, { number }, number)
|
||||
local p = { m[13], m[14], m[15] }
|
||||
local fx, fy, fz = unit(-m[9], -m[10], -m[11])
|
||||
@@ -406,7 +392,6 @@ local function aim(m: { number }, dist: number): ({ number }, { number }, number
|
||||
fx, fy, fz = 0, 0, -1
|
||||
end
|
||||
local t = { p[1] + fx * dist, p[2] + fy * dist, p[3] + fz * dist }
|
||||
-- the roll: the angle from the unrolled up vector (Build.lookAt) to the node's Y axis
|
||||
local q0 = Build.lookAt(p, t, 0)
|
||||
local m0 = Mat4.fromQuat(q0)
|
||||
local ux, uy, uz = unit(m[5], m[6], m[7])
|
||||
@@ -490,11 +475,10 @@ local function writeTrack(w: W, id: number, frames: { number }, values: { number
|
||||
end
|
||||
|
||||
type KfNode = {
|
||||
chunk: number, name: string, instance: string?, parent: number, -- parent: kf index or -1
|
||||
chunk: number, name: string, instance: string?, parent: number, -- kf index or -1
|
||||
frames: { [string]: { number } }, values: { [string]: { number } },
|
||||
}
|
||||
|
||||
-- frames (rounded, strictly increasing) and the indices of the channel keys they keep
|
||||
local function frameKeys(ch: Types.Channel, fps: number): ({ number }, { number })
|
||||
local IN = S.reader(ch.input) :: any
|
||||
local frames, keep = {}, {}
|
||||
@@ -558,7 +542,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
leaf(w, C.MESH_VERSION, function()
|
||||
w:u32le(3)
|
||||
end)
|
||||
-- materials (unique names)
|
||||
local files = textureFiles(scene, o)
|
||||
local matNames: { [number]: string } = {}
|
||||
local used: { [string]: boolean } = {}
|
||||
@@ -578,7 +561,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
leaf(w, C.MASTER_SCALE, function()
|
||||
w:f32le(1)
|
||||
end)
|
||||
-- meshes: at the first node showing them; their kf world (mirrors moved out of the keyframer)
|
||||
used = {}
|
||||
local firstNode: { [number]: number } = {}
|
||||
for _, i in order do
|
||||
@@ -608,7 +590,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
writeMesh(w, part, M, matNames)
|
||||
end
|
||||
end
|
||||
-- the keyframer world of each node: W, or W * flip-x where the node's mesh matrix mirrors
|
||||
local KW: { [number]: { number } } = {}
|
||||
for _, i in order do
|
||||
local n = scene.nodes[i]
|
||||
@@ -645,7 +626,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
local kn: KfNode = { chunk = kind, name = objName, parent = -1, frames = { pos = { 0 }, roll = { 0 } }, values = { pos = pos, roll = { roll } } }
|
||||
table.insert(kfs, kn)
|
||||
local idx = #kfs
|
||||
-- animated root cameras / lights: position, target and roll at the key times
|
||||
local chs: any = if p == 0 then byNode[i] else nil
|
||||
local targetFrames, targetVals = { 0 }, tgt
|
||||
if chs and (chs.translation or chs.rotation) then
|
||||
@@ -707,7 +687,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
end
|
||||
if onlyThis and idx then
|
||||
kfOf[i] = idx
|
||||
-- the node's frame in the keyframer: the camera / light pose
|
||||
local kn = kfs[idx]
|
||||
local pos = kn.values.pos
|
||||
local m: { number } = W[i]
|
||||
@@ -724,7 +703,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
camLight(true)
|
||||
continue
|
||||
end
|
||||
-- an object node (or a dummy) with the node's transform relative to its keyframer parent
|
||||
local pkw = if p ~= 0 and kfOf[p] then KW[p] else S.IDENTITY
|
||||
local inv = Mat4.inverse(pkw) or Mat4.identity()
|
||||
local L = S.mul(inv, KW[i])
|
||||
@@ -733,7 +711,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
local objName = if plist then plist[1].name else "$$$DUMMY"
|
||||
local instance = if plist and n.name and n.name ~= objName then name(n.name, objName) elseif not plist then name(n.name, "Dummy" .. i) else nil
|
||||
local kn: KfNode = { chunk = 0xB002, name = objName, instance = instance, parent = if pk then pk - 1 else -1, frames = { pos = { 0 }, rot = { 0 }, scl = { 0 } }, values = { pos = single(t), rot = rotTrack({ r }), scl = single(s) } }
|
||||
-- animation keys, when the keyframer transform is the node's own (no mirror fix-up up the tree)
|
||||
local chs = byNode[i]
|
||||
if chs then
|
||||
local own = S.nodeMatrix(n)
|
||||
@@ -774,7 +751,6 @@ function Write.encode(scene: Types.Scene, opts: Options?): (buffer?, string?)
|
||||
camLight(false)
|
||||
end
|
||||
close(w, ed)
|
||||
-- the keyframer
|
||||
local kf = open(w, C.KEYFRAMER)
|
||||
leaf(w, C.KF_HDR, function()
|
||||
w:u16le(5)
|
||||
|
||||
@@ -1,25 +1,7 @@
|
||||
--!strict
|
||||
-- model/3ds: Autodesk 3D Studio (.3ds, the chunked binary of 3D Studio R4 / 3ds Max's exporter). New code, from the
|
||||
-- chunk layout as documented by the 3DS File Toolkit notes and read by assimp's 3DS importer (BSD-3: the reference
|
||||
-- for transforms and conventions; no code ported) and lib3ds (layouts only).
|
||||
-- Reads: triangle meshes (vertices, faces, uvs (TEXCOORD_0, flipped to glTF's top-left origin), the mesh matrix,
|
||||
-- per-face materials -> one primitive per material, smoothing groups -> NORMAL: corners of faces sharing a group and
|
||||
-- a position are averaged, group 0 is flat; extras["3ds"].smoothing keeps the groups per triangle), materials
|
||||
-- (ambient / diffuse / specular colours, shininess, shininess strength, transparency, self-illumination, two-sided,
|
||||
-- shading, wire; texture maps -> Textures with `uri` (never loaded: F.model.loadTextures resolves them), diffuse as
|
||||
-- baseColorTexture, self-illumination as emissiveTexture, the others in extras["3ds"].maps; uv scale / offset /
|
||||
-- angle as a texture transform), cameras (lens -> yfov at a 4:3 aspect, near / far ranges; -Z looking at the
|
||||
-- target, rolled), lights (omni -> point, spot -> spot with hotspot / falloff cones, colour, multiplier, outer range),
|
||||
-- the keyframer (node hierarchy by node id, $$$DUMMY nodes with their instance names, instances of one object,
|
||||
-- pivots, the first key of every track as the node's TRS, tracks with several keys -> one animation: position /
|
||||
-- scale keys linear, rotation keys accumulated from their axis / angle deltas, cameras / spotlights sampled from
|
||||
-- their position, target and roll tracks; TCB spline parameters are ignored, times are frame / opts.fps). Objects
|
||||
-- without a keyframer node stand at their mesh matrix. meta.up = "z"; meta.source has version, masterScale (not
|
||||
-- applied), the keyframer frame range.
|
||||
-- Writes: see model/3ds/Write (meshes, materials, texture names, cameras, lights, the keyframer and the first
|
||||
-- animation). Not supported: 3DS project / material library files (.prj, .mli), morph / hide tracks, procedural
|
||||
-- and shadow parameters, background / atmosphere settings, mesh limits beyond 65535 vertices per object (split on
|
||||
-- write), vertex colours / normals (3DS has neither; normals come from smoothing groups).
|
||||
-- model/3ds: Autodesk 3D Studio chunked binary; layouts from the 3DS File Toolkit notes, assimp's importer (BSD-3,
|
||||
-- reference only) and lib3ds. Supports: meshes, smoothing groups, Phong materials and maps, cameras, lights, keyframer.
|
||||
-- Not supported: .prj / .mli, morph / hide tracks, TCB spline parameters, vertex colours / normals (3DS has none).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -41,16 +23,16 @@ local ThreeDs = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
normals: boolean?, -- compute NORMAL from the smoothing groups (default true; false keeps the file's vertices)
|
||||
flipV: boolean?, -- TEXCOORD_0.v = 1 - v (default true: glTF's convention)
|
||||
fps: number?, -- keyframer frames per second (default 30, 3ds Max's default)
|
||||
maxVertices: number?, -- refuse more vertices in total (default 2^24)
|
||||
normals: boolean?, -- default true
|
||||
flipV: boolean?, -- default true
|
||||
fps: number?, -- default 30 (3ds Max's default)
|
||||
maxVertices: number?, -- default 2^24
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
flipV: boolean?, -- default true (the inverse of decode)
|
||||
fps: number?, -- seconds -> keyframer frames (default 30)
|
||||
name: string?, -- the destination's file name (names textures that have no uri)
|
||||
files: { [string]: any }?, -- receives the bytes of textures that have data but no uri
|
||||
flipV: boolean?, -- default true
|
||||
fps: number?, -- default 30
|
||||
name: string?,
|
||||
files: { [string]: any }?, -- receives the bytes of textures with data but no uri
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -74,7 +56,6 @@ function ThreeDs.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, E
|
||||
if not raw then
|
||||
return nil, Err.from(msg, at)
|
||||
end
|
||||
-- the scene builder is plain (not native) code: a bug there must not escape as an error
|
||||
local ok, scene = pcall(Build.scene, raw, opts :: any)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "bad object data: " .. tostring(scene))
|
||||
|
||||
Reference in new issue
Block a user