ws-3d3-b: docs/guide/scene.md: LWO, .x and IQM (sections with tested snippets, Formats table, intro list)

This commit is contained in:
4DBug committed 2026-10-31 14:41:36 -05:00
1 parent 51daa00a67
commit 1d45c66108
1 file changed
+81 -1
+81 -1
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, LightWave LWO, DirectX .x, IQM) 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`.
@@ -378,6 +378,83 @@ local vox = assert(F.save(grid, nil, { format = "vox" })) -- a grid alone saves
assert(F.load(vox).voxels[1].size[1] == 4)
```
## LightWave, DirectX and Inter-Quake models
**LightWave objects** (`.lwo`: LWO2 and the older LWOB; Modo's `.lxo`) give one node per layer (its pivot as the
translation, parents by layer index), one mesh per layer and one primitive per surface. LightWave is left-handed: z is
negated and the winding reversed on load, so the scene is glTF's right-handed y up. UV maps become `TEXCOORD_n` (one
set per map name), the colour map a surface names becomes `COLOR_0`, weight maps `_WEIGHT_n` and morph maps morph
targets (names in `prim.extras.lwo.maps` / `mesh.extras.targetNames`). Surface values and texture blocks are in
`material.extras.lwo`; image textures keep only the file name as `uri` (`F.model.loadTextures` reads them). Subdivision
cages (`PTCH` / `SUBD`) stay polygons with `prim.extras.lwo.subdivision = true`.
```luau
-- a one-triangle LWO2 file, built by hand (IFF chunks, big-endian)
local function be(n, bytes)
local s = ""
for i = bytes - 1, 0, -1 do
s ..= string.char(bit32.band(bit32.rshift(n, i * 8), 255))
end
return s
end
local function f4(x)
local b = buffer.create(4)
buffer.writef32(b, 0, x)
return be(buffer.readu32(b, 0), 4)
end
local function chunk(id, body)
return id .. be(#body, 4) .. body
end
local body = "LWO2" .. chunk("TAGS", "Red\0") .. chunk("LAYR", be(0, 4) .. f4(0) .. f4(0) .. f4(0) .. "Body\0\0")
.. chunk("PNTS", f4(0) .. f4(0) .. f4(0) .. f4(1) .. f4(0) .. f4(0) .. f4(0) .. f4(1) .. f4(0))
.. chunk("POLS", "FACE" .. be(3, 2) .. be(0, 2) .. be(1, 2) .. be(2, 2))
.. chunk("PTAG", "SURF" .. be(0, 4))
.. chunk("SURF", "Red\0\0\0" .. "COLR" .. be(14, 2) .. f4(1) .. f4(0) .. f4(0) .. be(0, 2))
local scene = assert(F.load("FORM" .. be(#body, 4) .. body))
assert(scene.nodes[1].name == "Body" and scene.meta.source.version == "LWO2")
assert(scene.materials[1].name == "Red" and scene.materials[1].baseColor[1] == 1)
assert(scene.meshes[1].primitives[1].indices.count == 3)
```
**DirectX `.x`** files (text, binary and MSZIP-compressed, 32- or 64-bit floats) give the frame hierarchy, one primitive
per material, materials with their texture file names, skins (`SkinWeights`) and animation sets. Direct3D is
left-handed too: z is negated and the winding reversed. Key times are in seconds (`AnimTicksPerSecond`, default 4800).
```luau
local text = [[xof 0303txt 0032
Material Wood { 0.8; 0.6; 0.4; 1.0;; 20.0; 0.1; 0.1; 0.1;; 0.0; 0.0; 0.0;; TextureFilename { "wood.png"; } }
Frame Root {
FrameTransformMatrix { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,5,1;; }
Mesh Tri { 3; 0;0;0;, 1;0;0;, 0;1;0;; 1; 3;0,1,2;; MeshMaterialList { 1; 1; 0;; { Wood } } }
}
]]
local scene = assert(F.load(text, { format = "x" }))
local root = scene.nodes[1]
assert(root.name == "Root" and root.translation[3] == -5) -- z negated
assert(scene.materials[1].name == "Wood")
assert(scene.textures[scene.materials[1].baseColorTexture.texture].uri == "wood.png")
local glb = assert(F.save(scene, nil, { format = "glb" }))
```
**IQM** (Inter-Quake Model, version 2) reads and writes meshes, every vertex array, the skeleton (a node per joint and
one skin), animations (dequantized frames as translation / rotation / scale channels, loop flags and frame ranges in
`meta.source.animations`) and per-frame bounds. IQM is z-up (`meta.up = "z"`); nothing is rotated either way. The
writer turns any Scene's triangles into IQM meshes, merges the skins into one skeleton and samples each animation at
its key spacing when uniform (else `opts.framerate`, default 30 frames per second).
```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")
local iqm = assert(F.save(scene, nil, { format = "iqm" }))
local back = assert(F.load(iqm))
assert(back.meta.source.format == "iqm" and back.meshes[1].name == "tri")
assert(back.meshes[1].primitives[1].indices.count == 3)
```
## Formats
| Format | Read | Write | Notes |
@@ -392,6 +469,9 @@ 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 |
| LightWave LWO (LWO2, LWOB, Modo LXOB) | yes | | layers, vertex maps, surfaces; see below |
| DirectX .x (text, binary, MSZIP) | yes | | frames, materials, skins, animation |
| IQM (Inter-Quake Model) | yes | yes | skeleton, skin, sampled animations, bounds |
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
Capabilities per format: [FORMATS.md](../FORMATS.md).