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,7 +1,6 @@
|
||||
--!strict
|
||||
-- model/ldraw/Colors: the LDraw colour table: code -> { name, rgb, edge, alpha, luminance, finish }, parsed from
|
||||
-- !COLOUR lines. The built-in table is the colour list of LDraw.org's LDConfig.ldr (UPDATE 2025-05-20; CC BY 2.0, see
|
||||
-- licenses/LDRAW_COLOURS.txt), one line per colour without the "0 !COLOUR " prefix and with the MATERIAL parameters cut
|
||||
-- model/ldraw/Colors: the LDraw colour table, parsed from !COLOUR lines. The built-in table is LDraw.org's LDConfig.ldr
|
||||
-- (UPDATE 2025-05-20; CC BY 2.0, see licenses/LDRAW_COLOURS.txt), without the "0 !COLOUR " prefix and with MATERIAL cut
|
||||
-- after the material's type. Colours are 0xRRGGBB (sRGB); an edge is a colour code or a direct colour 0x2RRGGBB.
|
||||
local Colors = {}
|
||||
|
||||
@@ -10,7 +9,7 @@ export type Table = { [number]: Colour }
|
||||
|
||||
local FINISHES = { CHROME = true, PEARLESCENT = true, RUBBER = true, MATTE_METALLIC = true, METAL = true }
|
||||
|
||||
-- entry(rest) -> code, Colour | nil: one "Name CODE n VALUE #RRGGBB EDGE #RRGGBB|code [ALPHA a] [LUMINANCE l] ..."
|
||||
-- "Name CODE n VALUE #RRGGBB EDGE #RRGGBB|code [ALPHA a] [LUMINANCE l] ..."
|
||||
function Colors.entry(rest: string): (number?, Colour?)
|
||||
local words: { string } = {}
|
||||
for w in string.gmatch(rest, "%S+") do
|
||||
@@ -51,7 +50,6 @@ function Colors.entry(rest: string): (number?, Colour?)
|
||||
return code, { name = name, rgb = rgb :: number, edge = edge or 0x2333333, alpha = alpha, luminance = lum, finish = finish }
|
||||
end
|
||||
|
||||
-- parse(text, into?) -> into (a new table by default) with every "0 !COLOUR" line of text added
|
||||
function Colors.parse(text: string, into: Table?): Table
|
||||
local t: Table = into or {}
|
||||
for line in string.gmatch(text, "[^\n]+") do
|
||||
@@ -297,7 +295,6 @@ Trans_Sticker CODE 10047 VALUE #FFFFFF EDGE #FFFFFF ALPHA 16
|
||||
]]
|
||||
|
||||
local cache: Table? = nil
|
||||
-- builtin() -> a fresh copy of the LDConfig.ldr table (callers may add to it)
|
||||
function Colors.builtin(): Table
|
||||
if not cache then
|
||||
local t: Table = {}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
--!strict
|
||||
-- model/ldraw/Read: LDraw text -> Types.Scene (see model/ldraw for the mapping). Files are parsed once into command
|
||||
-- lists (MPD sections, then sub-files through opts.files / opts.resolve with the library paths); parts are baked into
|
||||
-- one mesh per (part, colour, inversion), with the BFC rules applied inside the part; model files become nodes.
|
||||
-- Returns nil, message[, kind] on failure (the contract turns it into an Err with Err.from).
|
||||
-- model/ldraw/Read: LDraw text -> Types.Scene. Sub-files are tried as written (backslashes as slashes), then lower
|
||||
-- case, under "", "parts/", "p/", "parts/s/" and "models/"; faces are written counter-clockwise after the matrices'
|
||||
-- mirroring.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Colors = require("@rosetta/model/ldraw/Colors")
|
||||
|
||||
@@ -22,7 +21,7 @@ export type Options = {
|
||||
maxNodes: number?,
|
||||
}
|
||||
|
||||
-- a parsed line: k = 0 (a BFC state change: cw / clip), 1 (a reference: v = x y z a b c d e f g h i), 2..5
|
||||
-- k = 0 (a BFC state change), 1 (a reference: v = x y z a b c d e f g h i), 2..5
|
||||
type Cmd = { k: number, c: number, v: { number }, name: string?, inv: boolean?, step: number?, cw: boolean?, clip: boolean? }
|
||||
type File = {
|
||||
name: string,
|
||||
@@ -52,7 +51,6 @@ local function srgbToLinear(v: number): number
|
||||
return if v <= 0.04045 then v / 12.92 else ((v + 0.055) / 1.055) ^ 2.4
|
||||
end
|
||||
|
||||
-- parse(text, name) -> File (colour definitions go into colours as they are met)
|
||||
local function parse(text: string, name: string, colours: Colors.Table, stats: { bad: number, lines: number }): File
|
||||
local f: File = { name = name, key = norm(name), cmds = {}, certified = false, refs = 0, library = false, main = false }
|
||||
local cmds = f.cmds
|
||||
@@ -96,7 +94,6 @@ local function parse(text: string, name: string, colours: Colors.Table, stats: {
|
||||
elseif w1 == "Author:" then
|
||||
f.author = rest
|
||||
elseif w1 == "Name:" or w1 == "FILE" or w1 == "NOFILE" or w1 == "!HISTORY" or w1 == "!LICENSE" then
|
||||
-- (headers)
|
||||
elseif first and w1 and string.sub(w1, 1, 1) ~= "!" and w1 ~= "//" then
|
||||
f.title = (string.match(line, "^%s*0%s+(.-)%s*$"))
|
||||
end
|
||||
@@ -152,7 +149,6 @@ local function parse(text: string, name: string, colours: Colors.Table, stats: {
|
||||
return f
|
||||
end
|
||||
|
||||
-- scene(text, opts) -> Scene | nil, message, kind
|
||||
function Read.scene(text: string, opts: Options?): (Types.Scene?, string?, string?)
|
||||
local o: Options = opts or {}
|
||||
local colours = Colors.builtin()
|
||||
@@ -329,7 +325,7 @@ function Read.scene(text: string, opts: Options?): (Types.Scene?, string?, strin
|
||||
end
|
||||
end
|
||||
|
||||
-- bake(file, m (3x4 row-major), colour, inv, certified, depth, stack, groups, order, direct)
|
||||
-- m is a 3x4 row-major matrix
|
||||
local function bake(f: File, m: { number }, colour: number, inv: boolean, certified: boolean, depth: number, stack: { [File]: boolean }, groups: { [string]: Group }, order: { Group }, direct: boolean)
|
||||
if failure then
|
||||
return
|
||||
@@ -439,7 +435,6 @@ function Read.scene(text: string, opts: Options?): (Types.Scene?, string?, strin
|
||||
return Types.Scene.accessor(b, "f32", 3, count)
|
||||
end
|
||||
|
||||
-- the mesh of a file baked at the identity (nil when it has no geometry)
|
||||
local meshOf: { [string]: number | false } = {}
|
||||
local function meshFor(f: File, colour: number, inv: boolean, direct: boolean): number?
|
||||
local key = f.key .. "|" .. colour .. "|" .. (if inv then "i" else "") .. (if direct then "d" else "")
|
||||
|
||||
@@ -1,33 +1,7 @@
|
||||
--!strict
|
||||
-- model/ldraw: LDraw (.ldr / .dat / .mpd), read. Written from the LDraw File Format specification 1.0.2, the MPD and
|
||||
-- BFC (back face culling) language extensions and the colour definition (!COLOUR) extension (ldraw.org); checked
|
||||
-- against an independent expansion in the fixture generator and LeoCAD's OBJ export of library parts and models.
|
||||
-- Reads: line types 0 (meta: Name / Author / !LDRAW_ORG / STEP / !COLOUR / BFC), 1 (sub-file references: colour +
|
||||
-- 3x4 matrix), 2 (lines), 3 (triangles), 4 (quads, split in two), 5 (optional lines: skipped, or opts.optionalLines
|
||||
-- -> "lines" primitives with _CONTROL_0 / _CONTROL_1 attributes and prim.extras.ldraw.optional). MPD files split at
|
||||
-- `0 FILE` / `0 NOFILE` (the first file is the model); other sub-files come through opts.files / opts.resolve, tried
|
||||
-- as written (backslashes as slashes), then lower case, under "", "parts/", "p/", "parts/s/" and "models/". Missing
|
||||
-- sub-files are skipped and listed (meta.source.missing); a file that places itself is skipped (meta.source.cycles).
|
||||
-- Scene: parts (files whose !LDRAW_ORG type is not a model, library files, .dat sub-files, a main file without
|
||||
-- references) are baked into one mesh per (part, colour, INVERTNEXT), shared by every placement; models (the main
|
||||
-- file, .ldr / .mpd sub-models) become nodes: one node per reference with the reference's matrix (node.extras.ldraw =
|
||||
-- { file, color, step, invert? }), a model's own geometry on an extra node. opts.flatten bakes the whole model into one
|
||||
-- mesh. Primitives: one "triangles" primitive per colour (and per one- / double-sided), "lines" for type 2 (opts.lines
|
||||
-- = false drops them); no normals, no indices (each triangle has its own three vertices).
|
||||
-- BFC, inside a part: CERTIFY / NOCERTIFY, CW / CCW, CLIP / NOCLIP and INVERTNEXT; faces are written counter-clockwise
|
||||
-- (glTF's front faces) after the reference matrices' mirroring; faces of uncertified files (and their sub-files) or
|
||||
-- under NOCLIP get double-sided materials. Model files do not limit the certification of the parts they place.
|
||||
-- Colours: 16 (the parent's colour; opts.color at the top, default 16 "Main_Colour"), 24 (the parent colour's edge
|
||||
-- colour), codes from the built-in LDConfig.ldr table (model/ldraw/Colors; !COLOUR lines in the files and opts.ldconfig
|
||||
-- text add or replace entries), direct colours 0x2RRGGBB (opaque) / 0x3RRGGBB (transparent) and the old dithered
|
||||
-- 0x4-0x7 codes. One material per colour (baseColor linear, ALPHA -> blend, CHROME / METAL / PEARLESCENT / RUBBER ->
|
||||
-- metallic / roughness; extras.ldraw = { code, name, edge, finish?, luminance? }).
|
||||
-- Axes: LDraw is right-handed with -Y up, 1 LDU = 0.4 mm. The vertices and matrices keep LDraw's values; a root node
|
||||
-- "LDraw" turns the model 180 degrees about X so the scene is Y up (meta.up = "y", meta.unit = 0.0004); opts.upright =
|
||||
-- false leaves it out (meta.up = nil, meta.source.up = "-y").
|
||||
-- meta.source = { format, mpd, name?, title?, author?, files, missing, cycles, badLines, up? }.
|
||||
-- Not supported: writing; !TEXMAP and !DATA (texture mapping, embedded images: ignored); the LDCad / MLCad / LPub meta
|
||||
-- commands (ignored); BFC in model files' references (models place parts as certified).
|
||||
-- model/ldraw: LDraw (.ldr / .dat / .mpd), read; File Format spec 1.0.2 with the MPD, BFC and !COLOUR extensions
|
||||
-- (ldraw.org). Supports: line types 0-5, sub-files, BFC, LDConfig colours. -Y up, 1 LDU = 0.4 mm (a root node turns
|
||||
-- it). Not supported: writing; !TEXMAP and !DATA; LDCad / MLCad / LPub meta commands; BFC in model files' references.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -49,15 +23,15 @@ local Ldraw = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
resolve: ((uri: string) -> any)?, -- sub-files (parts, primitives) by library path (the api sets it for paths)
|
||||
files: { [string]: any }?, -- or: the files by library path ("parts/3001.dat", "p/stud.dat", ...)
|
||||
resolve: ((uri: string) -> any)?, -- sub-files by library path
|
||||
files: { [string]: any }?, -- or by library path ("parts/3001.dat", "p/stud.dat", ...)
|
||||
ldconfig: string?, -- LDConfig.ldr text: its !COLOUR lines add to / replace the built-in colours
|
||||
color: number?, -- the colour of code 16 at the top (default 16, "Main_Colour")
|
||||
upright: boolean?, -- add the root node turning -Y up into Y up (default true)
|
||||
flatten: boolean?, -- one mesh for the whole model instead of a node per reference (default false)
|
||||
lines: boolean?, -- type 2 edge lines as "lines" primitives (default true)
|
||||
optionalLines: boolean?, -- type 5 optional lines too (default false)
|
||||
maxDepth: number?, -- sub-file nesting (default 64)
|
||||
color: number?, -- default 16 ("Main_Colour")
|
||||
upright: boolean?, -- default true
|
||||
flatten: boolean?, -- default false
|
||||
lines: boolean?, -- default true
|
||||
optionalLines: boolean?, -- default false
|
||||
maxDepth: number?, -- default 64
|
||||
maxTriangles: number?, -- default 2^24
|
||||
maxNodes: number?, -- default 2^20
|
||||
}
|
||||
@@ -80,7 +54,6 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Ldraw.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local text = Sniff.dropBom(buffer.readstring(r.buf, r.base, r.limit - r.base))
|
||||
-- the scene builder is plain (not native) code: a bug there must not escape as an error
|
||||
local ok, scene, msg, kind = pcall(Read.scene, text, opts)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "bad model: " .. tostring(scene))
|
||||
@@ -92,8 +65,6 @@ function Ldraw.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- colors(ldconfig?) -> { [code] = { name, rgb (0xRRGGBB sRGB), edge (a code or 0x2RRGGBB), alpha (0..255), luminance,
|
||||
-- finish? } }: the built-in LDConfig.ldr table, with the !COLOUR lines of ldconfig added
|
||||
function Ldraw.colors(ldconfig: string?): { [number]: any }
|
||||
local t = Colors.builtin()
|
||||
if ldconfig then
|
||||
|
||||
Reference in new issue
Block a user