model formats (3ds..lwo): comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:53:16 -05:00
1 parent 0bd7b87d2b
commit 3bddd582bc
35 files changed
+233 -697

No files matched your search

+7 -16
View File
@@ -1,11 +1,6 @@
--!strict
-- model/lwo/Build: a parsed LightWave object (model/lwo/Parse) -> Types.Scene. LightWave is left-handed (y up, z
-- away from the viewer, clockwise front faces): z is negated and the winding reversed, so the scene is glTF's
-- right-handed y-up with counter-clockwise front faces (assimp does the same). One node per layer (translation = its
-- pivot relative to the parent layer's pivot, geometry relative to the pivot, so world positions are the file's),
-- one mesh per layer, one primitive per surface (and per kind: polygons, lines, points; subdivision patches apart,
-- flagged extras.lwo.subdivision). Vertices are shared per point, split where a VMAD gives a corner its own value.
-- Plain code (the caller wraps it in pcall).
-- model/lwo/Build: a parsed LightWave object -> Types.Scene. LightWave is left-handed (y up, clockwise front faces): z
-- is negated and the winding reversed, as assimp does. Vertices are split where a VMAD gives a corner its own value.
local Types = require("@rosetta/Types")
local Parse = require("@rosetta/model/lwo/Parse")
@@ -14,7 +9,7 @@ local Build = {}
local Scene = Types.Scene
local writef32, writeu32 = buffer.writef32, buffer.writeu32
type Group = { material: number, class: string, polys: { number } } -- class: "poly" / "patch" / "line" / "point"
type Group = { material: number, class: string, polys: { number } } -- "poly" / "patch" / "line" / "point"
local MIME: { [string]: string } = { png = "image/png", jpg = "image/jpeg", jpeg = "image/jpeg", bmp = "image/bmp", tga = "image/x-tga", gif = "image/gif", tif = "image/tiff", tiff = "image/tiff", iff = "image/x-iff" }
@@ -118,7 +113,6 @@ local function groupLayer(layer: Parse.Layer, surfaceOf: (number?) -> number): {
return groups
end
-- the corners of a face in output order (winding reversed for the handedness change, first corner kept)
local function ordered(corners: { number }): { number }
local n = #corners
local out = table.create(n) :: { number }
@@ -134,8 +128,8 @@ type Attr = { map: Parse.Map, name: string, comps: number }
local function primitive(layer: Parse.Layer, g: Group, attrs: { Attr }, morphs: { Parse.Map }, triangulate: boolean): Types.Primitive
local pts, pivot = layer.pts, layer.pivot
local vertOf: { [number]: number } = {}
local srcPoint: { number } = {} -- vertex (1-based) -> point
local srcPoly: { number } = {} -- vertex -> the polygon whose VMAD values it carries (-1: none)
local srcPoint: { number } = {}
local srcPoly: { number } = {} -- -1: none
local indices: { number } = {}
local faces: { number } = {}
local madMaps: { Parse.Map } = {}
@@ -204,7 +198,6 @@ local function primitive(layer: Parse.Layer, g: Group, attrs: { Attr }, morphs:
writef32(pos, o + 8, -(pts[p + 3] - pivot[3]))
end
local attributes: { [string]: Types.Accessor } = { POSITION = Scene.accessor(pos, "f32", 3, nv) }
-- the value of map m for vertex v (1-based), or nil
local function value(m: Parse.Map, v: number): ({ number }?, number?)
local f = srcPoly[v]
if f >= 0 then
@@ -306,7 +299,7 @@ function Build.scene(m: Parse.Model, opts: { triangulate: boolean? }?): (Types.S
local scene = Scene.new()
scene.meta.up = "y"
scene.meta.source = { format = "lwo", version = m.version, skipped = m.skipped }
-- texture coordinate sets: every TXUV map name, in order of first appearance, gets one TEXCOORD_n index
-- every TXUV map name, in order of first appearance, gets one TEXCOORD_n index
local uvSets: { [string]: number } = {}
local nuv = 0
for _, layer in m.layers do
@@ -317,7 +310,6 @@ function Build.scene(m: Parse.Model, opts: { triangulate: boolean? }?): (Types.S
end
end
end
-- materials: one per surface, then one per tag without a surface, then a default one
local texCache: { [string]: number } = {}
local bySurfName: { [string]: number } = {}
for i, surf in m.surfaces do
@@ -359,7 +351,6 @@ function Build.scene(m: Parse.Model, opts: { triangulate: boolean? }?): (Types.S
end
return default :: number
end
-- nodes and meshes
local nodeOf: { [number]: number } = {}
local layerOfNode: { Parse.Layer } = {}
for _, layer in m.layers do
@@ -434,7 +425,7 @@ function Build.scene(m: Parse.Model, opts: { triangulate: boolean? }?): (Types.S
end
end
end
-- the layer hierarchy (parents by layer index; a link that would close a cycle is dropped)
-- a parent link that would close a cycle is dropped
local parentOf: { [number]: number } = {}
for ni, layer in layerOfNode do
local pi = if layer.parent ~= nil then nodeOf[layer.parent] else nil
+13 -23
View File
@@ -1,15 +1,7 @@
--!strict
-- model/lwo/Parse: the LightWave object chunks -> layers, tags, surfaces and clips (plain tables; model/lwo/Build
-- makes the Scene). From the LWO2 specification (LightWave 6 "Object Files", Ernie Wright) and the LWOB one (LightWave
-- 5.x), checked against what assimp's LWO importer reads (no code taken).
-- LWO2 / LXOB: TAGS, LAYR (index, pivot, name, parent), PNTS (more than one per layer: later POLS / VMAP indices are
-- relative to the latest), VMAP / VMAD (TXUV, RGB, RGBA, WGHT, MNVW, NORM (Modo), MORF, SPOT; others skipped), POLS
-- (FACE, PTCH, SUBD kept; CURV, BONE, MBAL and unknown types counted and skipped), PTAG (SURF, and MATR in Modo
-- files), SURF (COLR, DIFF, LUMI, SPEC, REFL, TRAN, TRNL, GLOS, BUMP, SIDE, SMAN, RIND, VCOL, BLOK image / procedural
-- / gradient / shader blocks with their header and IMAP sub-chunks; derived surfaces), CLIP (STIL, ISEQ, XREF).
-- LWOB: SRFS, PNTS, POLS (detail polygons flattened), SURF (COLR, FLAG, the U2 and V* float values, GLOS, SMAN,
-- CTEX ... TTEX with TIMG / TFLG / TVAL). Not read: envelopes (ENVL), LWO3, Modo ITEM / ACTN chunks.
-- Plain code (the caller wraps it in pcall); bad data returns nil, message.
-- model/lwo/Parse: LightWave object chunks -> plain tables. From the LWO2 specification (LightWave 6 "Object Files",
-- Ernie Wright) and the LWOB one (LightWave 5.x), checked against assimp (no code taken). Several PNTS per layer: later
-- POLS / VMAP indices are relative to the latest. Not read: envelopes (ENVL), LWO3, Modo ITEM / ACTN chunks.
local Bytes = require("@rosetta/core/Bytes")
local Chunks = require("@rosetta/containers/Chunks")
@@ -19,17 +11,17 @@ export type Map = {
kind: string, -- "uv" | "color" | "normal" | "weight" | "morph" | "spot"
name: string,
dims: number,
vals: { number }, -- per point: vals[point * dims + c + 1]
vals: { number }, -- vals[point * dims + c + 1]
has: { [number]: boolean },
mad: { [number]: { [number]: { number } } }, -- VMAD: mad[poly][point] = values
}
export type Poly = { kind: string, surf: number?, corners: { number } } -- corners: 0-based point indices
export type Poly = { kind: string, surf: number?, corners: { number } } -- 0-based point indices
export type Layer = {
index: number,
name: string?,
parent: number?,
pivot: { number },
pts: { number }, -- x, y, z flat
pts: { number },
polys: { Poly },
maps: { Map },
implicit: boolean?, -- the layer before any LAYR
@@ -50,7 +42,7 @@ export type Block = {
}
export type Surface = {
name: string,
values: { [string]: number }, -- COLR is separate; DIFF, LUMI, SPEC, REFL, TRAN, TRNL, GLOS, BUMP, SMAN, RIND
values: { [string]: number },
color: { number },
side: number,
vcol: { name: string, kind: string, intensity: number }?,
@@ -62,7 +54,7 @@ export type Model = {
tags: { string },
layers: { Layer },
surfaces: { Surface },
clips: { [number]: string }, -- clip index -> path
clips: { [number]: string },
skipped: { [string]: number },
corners: number,
}
@@ -79,7 +71,7 @@ local MAP_KINDS: { [string]: string } = {
}
-- ------------------------------------------------------------------------------------------------ primitives
-- S0: a NUL-terminated string padded to an even length
-- S0: NUL-terminated, padded to an even length
local function s0(r: Bytes.Reader): string
local start = r.pos
local s = r:cstring()
@@ -89,7 +81,7 @@ local function s0(r: Bytes.Reader): string
return s
end
-- VX: a variable-length index (2 bytes, or 4 bytes after an 0xFF byte)
-- VX: 2 bytes, or 4 bytes after an 0xFF byte
local function vx(r: Bytes.Reader): number
if r:peekU8() == 0xFF then
return r:u32be() - 0xFF000000
@@ -102,7 +94,7 @@ local function id4(r: Bytes.Reader): string
return (string.gsub(s, " +$", ""))
end
-- sub-chunks (LWO2: 4-byte id, 2-byte size, padded to even) of [p, e): calls f(id, reader over the body)
-- LWO2 sub-chunks: 4-byte id, 2-byte size, padded to even
local function subchunks(buf: buffer, p: number, e: number, f: (string, Bytes.Reader) -> ())
while p + 6 <= e do
local id = buffer.readstring(buf, p, 4)
@@ -133,7 +125,6 @@ local function readPoints(r: Bytes.Reader, layer: Layer, limit: number): string?
return nil
end
-- POLS (LWO2): returns nil or an error
local function readPolys(r: Bytes.Reader, layer: Layer, kind: string, ptBase: number, budget: { number }): string?
local polys = layer.polys
local npts = #layer.pts // 3
@@ -281,7 +272,7 @@ end
local SURF_VALUES = { DIFF = true, LUMI = true, SPEC = true, REFL = true, TRAN = true, TRNL = true, GLOS = true, BUMP = true, SMAN = true, RIND = true }
local function readBlock(r: Bytes.Reader, surf: Surface)
-- the header sub-chunk: IMAP / PROC / GRAD / SHDR, ordinal string, header sub-chunks
-- IMAP / PROC / GRAD / SHDR header: ordinal string, header sub-chunks
if r:remaining() < 6 then
return
end
@@ -464,7 +455,6 @@ local function readClip(r: Bytes.Reader, clips: { [number]: string }, refs: { [n
end
-- ------------------------------------------------------------------------------------------------ the file
-- parse(buf, from, limit, opts) -> Model | nil, message
function Parse.model(buf: buffer, from: number, limit: number, maxCorners: number): (Model?, string?)
local version = buffer.readstring(buf, from + 8, 4)
local declared = bit32.byteswap(buffer.readu32(buf, from + 4))
@@ -516,7 +506,7 @@ function Parse.model(buf: buffer, from: number, limit: number, maxCorners: numbe
if kind == "FACE" or kind == "PTCH" or kind == "SUBD" then
err = readPolys(r, layer, kind, ptBase, budget)
else
-- keep the polygon numbering (PTAG / VMAD refer to it) but skip the geometry
-- PTAG / VMAD refer to the polygon numbering: keep it, skip the geometry
local before = #layer.polys
err = readPolys(r, layer, kind, ptBase, budget)
model.skipped[kind] = (model.skipped[kind] or 0) + #layer.polys - before
+5 -17
View File
@@ -1,19 +1,7 @@
--!strict
-- model/lwo: LightWave objects: LWO2 (LightWave 6+), LWOB (LightWave 5.x) and Modo's LXOB, read only. New code from
-- the LWO2 / LWOB specifications, checked against assimp's LWO importer and test models.
-- Reads layers (one node each: the pivot as translation, parents by layer index), points and polygons (FACE; PTCH /
-- SUBD subdivision cages kept as polygons and flagged extras.lwo.subdivision; CURV, BONE and metaballs skipped and
-- counted in meta.source.skipped; 1- and 2-point polygons as points / lines primitives), vertex maps (VMAP / VMAD:
-- TXUV -> TEXCOORD_n (one set per map name, flipped to glTF's top-left origin), RGB / RGBA -> COLOR_0 for the map the
-- surface's VCOL names and _COLOR_n for the others, Modo NORM -> NORMAL, WGHT / MNVW weight maps -> _WEIGHT_n (LWO has
-- no skeleton to bind them to), MORF / SPOT -> morph targets; the map names are in prim.extras.lwo.maps), surfaces
-- (one material each: COLR -> baseColor, TRAN -> alpha, GLOS / SPEC -> roughness, LUMI -> emissive, SIDE ->
-- doubleSided, every value and texture block in extras.lwo; the first enabled COLR image block -> baseColorTexture,
-- with its UV map as texCoord) and clips (textures keep the file name as uri and the full path in extras.lwo.path:
-- F.model.loadTextures reads them). LightWave's left-handed space becomes glTF's right-handed y up (z negated, winding
-- reversed), as assimp does. Polygons are fan-triangulated unless opts.triangulate = false.
-- Not supported: LWO3, envelopes, procedural / gradient textures (kept as entries in extras.lwo.maps), planar /
-- cylindrical / spherical projections (no UVs are generated), smoothing (no normals are computed).
-- model/lwo: LightWave objects LWO2 (6+), LWOB (5.x) and Modo's LXOB, read; from the LWO2 / LWOB specs, checked against
-- assimp. Supports: layers, polygons, VMAP / VMAD, surfaces, clips. Left-handed: z negated, winding reversed. Not
-- supported: LWO3, envelopes, procedural / gradient textures, planar / cylindrical / spherical projections, smoothing.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -34,8 +22,8 @@ local Lwo = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
triangulate: boolean?, -- fan-triangulate polygons (default true); false keeps "polygons" primitives
maxVertices: number?, -- refuse more points / polygon corners (default 2^25)
triangulate: boolean?, -- default true
maxVertices: number?, -- default 2^25
}
-- ------------------------------------------------------------------------------------------------ constants