merge ws/3d3-a (model/3ds, model/ms3d)

This commit is contained in:
4DBug committed 2026-10-31 14:55:36 -05:00
commit feb8d18cfb
16 files changed
+4539 -4

No files matched your search

+66 -4
View File
@@ -1,8 +1,8 @@
# Scene (3D models) and Voxels
Every model format (glTF / GLB, OBJ, STL, PLY, OFF, FBX, Collada, 3MF, USDA, VOX, B3D, LDraw) loads as one `Scene`, a structure
shaped like glTF 2.0: a node tree, meshes made of primitives, flat attribute buffers (accessors), PBR materials,
textures, cameras, lights, skins and animations. Mesh-only formats (STL, PLY, OFF) give a one-node scene. MagicaVoxel
Every model format (glTF / GLB, OBJ, STL, PLY, OFF, FBX, Collada, 3MF, USDA, VOX, 3DS, MS3D, B3D, LDraw) loads as one
`Scene`, a structure shaped like glTF 2.0: a node tree, meshes made of primitives, flat attribute buffers (accessors),
PBR materials, textures, cameras, lights, skins and animations. Mesh-only formats (STL, PLY, OFF) give a one-node scene. MagicaVoxel
files also carry voxel grids (`scene.voxels`). Editing goes through `F.ops.mesh`.
```luau
@@ -309,7 +309,7 @@ the binary container.
| glTF | `external = true` writes `<name>.bin` and the images into `opts.files` (default: embedded `data:` URIs) | `binary`, `external`, `name` |
| OBJ | `<name>.mtl` and texture files into `opts.files` | `name`, `mtlName`, `normals`, `texcoords`, `colors`, `flipV` |
| STL | | `ascii = true` (default binary), `normals = "attribute"` |
| PLY, OFF, FBX, DAE, 3MF, USDA, VOX | see [FORMATS.md](../FORMATS.md) and the format's options | |
| PLY, OFF, FBX, DAE, 3MF, USDA, VOX, 3DS, MS3D | see [FORMATS.md](../FORMATS.md) and the format's options | |
Mesh-only writers (STL, PLY, OFF, OBJ) bake the node transforms into the vertices. Extra files go into the
`opts.files` table you pass, named after `opts.name`:
@@ -469,6 +469,66 @@ assert(#flat.meshes == 1) -- one mesh, one primitiv
`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").
## 3D Studio (3DS)
`.3ds` files load with their keyframer: the node hierarchy, pivots, instances of one object, the first key of each
track as the node's TRS and, when tracks have several keys, one animation (frames / `opts.fps`, default 30).
Normals come from the smoothing groups (`normals = false` keeps the file's shared vertices); the groups stay in
`prim.extras["3ds"].smoothing` and are written back. Materials are Phong (the raw values in `extras["3ds"]`), texture
maps become textures with a `uri` only (`F.model.loadTextures` reads them). Cameras get `yfov` from the lens at a
4:3 aspect and look at their target; omni lights are `point`, spotlights `spot`. The writer puts one object per mesh
(split past 65535 vertices), its first instance's world matrix as the mesh matrix, and the scene's first animation
into the keyframer.
```luau
local S = F.Types.Scene
local pos = S.accessor(nil, "f32", 3, 3)
for i, v in { 0, 0, 0, 1, 0, 0, 0, 1, 0 } do
buffer.writef32(pos.data, (i - 1) * 4, v)
end
local scene = S.fromPrimitive({ attributes = { POSITION = pos }, mode = "triangles", material = 1 }, "tri")
scene.materials[1] = S.material("red")
scene.materials[1].baseColor = { 1, 0, 0, 1 }
scene.nodes[1].translation = { 0, 0, 2 }
local bytes = assert(F.save(scene, nil, { format = "3ds" }))
local back = assert(F.load(bytes)) -- detected by its 0x4D4D main chunk
assert(back.meta.source.format == "3ds" and back.meta.up == "z")
local prim = back.meshes[1].primitives[1]
assert(prim.attributes.NORMAL and prim.extras["3ds"].smoothing[1] == 1)
assert(back.materials[prim.material].baseColor[1] == 1)
assert(back.nodes[1].translation[3] == 2) -- the keyframer keeps the node transform
```
## MilkShape 3D (MS3D)
Each group loads as a node with one mesh (per-corner normals and uvs, so vertices are split per corner); joints become
nodes with a skin (`JOINTS_0` / `WEIGHTS_0`, up to four bones from the 1.8 vertex extras) and their keys one
animation. Comments, joint colours and the other MilkShape fields stay in `extras.ms3d` / `meta.source`. The writer
puts every placed primitive in a group, the first skin's joints plus every node the first animation moves (meshes
under them are bound rigidly) into the skeleton, and the weights in percent.
```luau
local S = F.Types.Scene
local pos = S.accessor(nil, "f32", 3, 3)
for i, v in { 0, 0, 0, 1, 0, 0, 0, 1, 0 } do
buffer.writef32(pos.data, (i - 1) * 4, v)
end
local scene = S.fromPrimitive({ attributes = { POSITION = pos }, mode = "triangles" }, "tri")
-- spin the node a quarter turn about z in one second
local times = S.accessor(nil, "f32", 1, 2)
buffer.writef32(times.data, 4, 1)
local turns = S.accessor(nil, "f32", 4, 2)
for i, v in { 0, 0, 0, 1, 0, 0, math.sqrt(0.5), math.sqrt(0.5) } do
buffer.writef32(turns.data, (i - 1) * 4, v)
end
scene.animations[1] = { channels = { { node = 1, path = "rotation", input = times, output = turns, interpolation = "LINEAR" } } }
local back = assert(F.load(assert(F.save(scene, nil, { format = "ms3d" }))))
assert(back.meta.source.format == "ms3d" and #back.skins == 1) -- the animated node became a joint
local prim = back.meshes[1].primitives[1]
assert(prim.attributes.JOINTS_0 and prim.attributes.NORMAL)
assert(back.animations[1].channels[1].path == "rotation")
```
## Formats
| Format | Read | Write | Notes |
@@ -485,6 +545,8 @@ text of another LDConfig.ldr, and `color` the colour of code 16 at the top (defa
| 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 |
| 3DS | yes | yes | keyframer hierarchy and animation, Phong materials, texture names, cameras, lights; Z up |
| MS3D (MilkShape 3D) | yes | yes | groups, materials, joints -> skin + animation, comments |
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
Capabilities per format: [FORMATS.md](../FORMATS.md).