106 lines
6.5 KiB
Luau
106 lines
6.5 KiB
Luau
--!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).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Types = require("@rosetta/Types")
|
|
local Read = require("@rosetta/model/ldraw/Read")
|
|
local Colors = require("@rosetta/model/ldraw/Colors")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Ldraw = {
|
|
name = "ldraw",
|
|
category = "model",
|
|
extensions = { "ldr", "mpd", "dat" },
|
|
mime = { "application/x-ldraw", "application/x-multi-part-ldraw" },
|
|
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types" },
|
|
caps = { decode = true, encode = false, materials = true, lines = true, instancing = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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", ...)
|
|
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)
|
|
maxTriangles: number?, -- default 2^24
|
|
maxNodes: number?, -- default 2^20
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Ldraw.sniff(r: Bytes.Reader): number
|
|
local head = Sniff.text(r, 2048, true)
|
|
if string.find(head, "^0%s+FILE%s") then
|
|
return 0.9
|
|
end
|
|
if string.find(head, "\n%s*1%s+%S+" .. string.rep("%s+[-+%deE.]+", 12) .. "%s+[^\n]+%.[dDlL][aAdD][tTrR]%s*\r?\n") then
|
|
return 0.8
|
|
end
|
|
if string.find(head, "^0[ \t]+%a") and string.find(head, "\n%s*[1-5]%s+%S+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]+%s+[-+%d.]") then
|
|
return 0.4
|
|
end
|
|
return 0
|
|
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))
|
|
end
|
|
if not scene then
|
|
return nil, Err.from(msg, nil, kind)
|
|
end
|
|
return scene, nil
|
|
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
|
|
Colors.parse(ldconfig, t)
|
|
end
|
|
return t
|
|
end
|
|
|
|
return Ldraw
|