ws-3d3-d: model/ldraw (LDR / DAT / MPD read: parts as shared meshes per colour, sub-models as nodes, BFC, LDConfig colours, library paths through files / resolve)

Built-in colour table from LDConfig.ldr (CC BY 2.0 / 4.0, licenses/LDRAW_COLOURS.txt + NOTICE.md row).
Tests: independent Python expansion + LeoCAD OBJ export of library parts / car.ldr / pyramid.ldr (read from the nix store).
This commit is contained in:
4DBug committed 2026-10-31 14:28:56 -05:00
1 parent 07dc6f7e84
commit e3ac2f8b36
10 files changed
+1737

No files matched your search

+1
View File
@@ -39,6 +39,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` |
| model | fbx | .fbx | yes | yes | binary: true, ascii: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true, polygons: true | `tests/model/fbx.luau` |
| model | gltf | .gltf .glb | yes | yes | binary: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true | `tests/model/gltf.luau` |
| model | ldraw | .ldr .mpd .dat | yes | - | materials: true, lines: true, instancing: true | `tests/model/ldraw.luau` |
| model | obj | .obj | yes | yes | materials: true, polygons: true, vertexColors: true | `tests/model/obj.luau` |
| model | off | .off | yes | yes | polygons: true, points: true, colors: true, binary: false | `tests/model/off.luau` |
| model | ply | .ply | yes | yes | ascii: true, binary: true, polygons: true, points: true, colors: true | `tests/model/ply.luau` |
+60
View File
@@ -391,6 +391,65 @@ Blitz3D is **left-handed**: the reader mirrors z (and turns the triangle winding
is glTF's right-handed Y-up with the model looking as it did in Blitz3D. `meta.source.handedness = "left"` records
the origin, `meta.source.fps` / `frames` the `ANIM` chunk.
## LDraw
LDraw files (`.ldr` models, `.dat` parts, `.mpd` multi-part files) load read-only. Every reference to a **part**
becomes a node with the reference's matrix, and each part is baked once per colour into a mesh those nodes share;
**sub-models** (`.ldr` / `.mpd` files, MPD sections that are not parts) become nodes with children. Colour 16 takes the
colour of the reference above it, 24 its edge colour; codes come from the LDConfig.ldr table built in (one material per
colour, `extras.ldraw.code` / `name`), `0x2RRGGBB` is a direct colour. BFC (`CERTIFY`, `CW` / `CCW`, `INVERTNEXT`,
`NOCLIP`) turns every face counter-clockwise; faces of uncertified parts get double-sided materials. Edge lines (type 2)
are `"lines"` primitives (`lines = false` drops them); optional lines only on request (`optionalLines = true`).
```luau
local mpd = [[
0 FILE house.ldr
1 4 0 0 0 1 0 0 0 1 0 0 0 1 wall.dat
1 4 50 0 0 1 0 0 0 1 0 0 0 1 wall.dat
1 1 0 -30 0 1 0 0 0 1 0 0 0 1 roof.dat
0 FILE wall.dat
0 !LDRAW_ORG Unofficial_Part
0 BFC CERTIFY CCW
4 16 -10 0 -10 10 0 -10 10 0 10 -10 0 10
2 24 -10 0 -10 10 0 -10
0 FILE roof.dat
0 BFC CERTIFY CCW
3 16 0 -10 0 10 0 0 -10 0 0
]]
local scene = assert(F.model.load(mpd, { format = "ldraw" }))
print(#scene.meshes) -- 2: wall.dat in red (both walls share it), roof.dat in blue
local root = scene.nodes[scene.roots[1]] -- "LDraw": turns LDraw's -Y up into Y up
local a, b = scene.nodes[root.children[1]], scene.nodes[root.children[2]]
assert(a.mesh == b.mesh and b.matrix[13] == 50)
local red = scene.materials[scene.meshes[a.mesh].primitives[1].material]
print(red.name, red.extras.ldraw.code) -- Red 4
assert(scene.meta.up == "y" and scene.meta.unit == 0.0004) -- 1 LDU = 0.4 mm
```
Parts and primitives that are not in the file come through `files` or `resolve`, asked for by their LDraw library
path: the name as written (backslashes turned into slashes), then in lower case, under `""`, `parts/`, `p/`,
`parts/s/` and `models/`. Point `resolve` at an LDraw library folder to load real models; missing files are skipped and
listed in `meta.source.missing`:
```luau
local library = {
["p/box.dat"] = "0 BFC CERTIFY CCW\n4 16 -1 -1 -1 1 -1 -1 1 -1 1 -1 -1 1\n4 16 -1 1 -1 -1 1 1 1 1 1 1 1 -1\n",
["parts/plate.dat"] = "0 !LDRAW_ORG Part\n1 16 0 4 0 20 0 0 0 4 0 0 0 10 box.dat\n",
}
local model = "0 two plates\n1 14 0 0 0 1 0 0 0 1 0 0 0 1 plate.dat\n1 0x2FF8000 0 -8 0 1 0 0 0 1 0 0 0 1 plate.dat\n"
.. "1 4 0 -16 0 1 0 0 0 1 0 0 0 1 3001.dat\n"
local scene = assert(F.model.load(model, { format = "ldraw", files = library }))
assert(#scene.meshes == 2 and scene.meta.source.missing[1] == "3001.dat")
local same = assert(F.model.load(model, { format = "ldraw", resolve = function(path)
return library[path] -- or: read ldrawFolder .. "/" .. path
end }))
local flat = assert(F.model.load(model, { format = "ldraw", files = library, flatten = true }))
assert(#flat.meshes == 1) -- one mesh, one primitive per colour
```
`upright = false` leaves out the root node (LDraw's axes as stored, `meta.source.up = "-y"`); `ldconfig` takes the
text of another LDConfig.ldr, and `color` the colour of code 16 at the top (default 16, "Main_Colour").
## Formats
| Format | Read | Write | Notes |
@@ -406,6 +465,7 @@ the origin, `meta.source.fps` / `frames` the `ANIM` chunk.
| USDA (+ USDZ read) | yes | yes | |
| VOX | yes | yes | voxel grids, scene graph, materials |
| B3D (Blitz3D) | yes | | brushes, textures, skins, keyframe animation; left-handed source mirrored to glTF's frame |
| LDraw (LDR / DAT / MPD) | yes | | parts as shared meshes per colour, sub-models as nodes, BFC, LDConfig colours, `files` / `resolve` for the library |
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
Capabilities per format: [FORMATS.md](../FORMATS.md).