ws-3d3: model/3ds (Autodesk 3D Studio) read + write

Meshes (smoothing-group normals, uvs, mesh matrix, per-face materials), Phong
materials with texture maps (uri), cameras, lights, the keyframer (hierarchy,
pivots, instances, dummies, position / rotation / scale / camera tracks ->
one animation). Writer: objects, materials, cameras, lights, keyframer.
Tested against assimp (test models, its 3DS exporter, and reading our files).
This commit is contained in:
4DBug committed 2026-10-31 14:19:08 -05:00
1 parent de4f14a343
commit a43d81538e
10 files changed
+2921 -2

No files matched your search

+33 -2
View File
@@ -1,6 +1,6 @@
# Scene (3D models) and Voxels
Every model format (glTF / GLB, OBJ, STL, PLY, OFF, FBX, Collada, 3MF, USDA, VOX) loads as one `Scene`, a structure
Every model format (glTF / GLB, OBJ, STL, PLY, OFF, FBX, Collada, 3MF, USDA, VOX, 3DS) 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`.
@@ -290,7 +290,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 | 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`:
@@ -378,6 +378,36 @@ local vox = assert(F.save(grid, nil, { format = "vox" })) -- a grid alone saves
assert(F.load(vox).voxels[1].size[1] == 4)
```
## 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
```
## Formats
| Format | Read | Write | Notes |
@@ -392,6 +422,7 @@ assert(F.load(vox).voxels[1].size[1] == 4)
| 3MF | yes | yes | |
| USDA (+ USDZ read) | yes | yes | |
| VOX | yes | yes | voxel grids, scene graph, materials |
| 3DS | yes | yes | keyframer hierarchy and animation, Phong materials, texture names, cameras, lights; Z up |
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
Capabilities per format: [FORMATS.md](../FORMATS.md).