diff --git a/docs/guide/scene.md b/docs/guide/scene.md index 525643e..c44b70a 100644 --- a/docs/guide/scene.md +++ b/docs/guide/scene.md @@ -1,6 +1,7 @@ # 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, PMX / PMD, VRML, X3D) 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`. @@ -291,6 +292,7 @@ the binary container. | OBJ | `.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 | | +| VRML, X3D | texture images into `opts.files` (else `url` = the texture's uri) | `name`, `flipV`; X3D `encoding = "x3dv"` (or a `.x3dv` destination) for the classic encoding | 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`: @@ -317,6 +319,60 @@ assert(F.save(scene, "out/box.obj")) -- box.obj + box.mtl assert(F.convert("assets/Box.glb", "stl", { out = "out/box.stl" })) ``` +## MikuMikuDance (PMX / PMD) + +`.pmx` (PMX 2.0 / 2.1) and `.pmd` files load as one skinned mesh with one primitive per material. MMD is left-handed, +so z is negated and the winding reversed on load; `meta.up` is `"y"` (one unit is about 8 cm). Bones become nodes (a +translation relative to the parent bone) under the model's root node and one `Skin`; every vertex carries `JOINTS_0` / +`WEIGHTS_0` (BDEF1 / 2 / 4, SDEF and QDEF all as linear weights, normalized; the SDEF C / R0 / R1 vectors as +`_SDEF_C` / `_SDEF_R0` / `_SDEF_R1` and the deform type as `_DEFORM` when a file uses them), the additional UVs as +`_UVA1` .. `_UVA4` (vec4) and the edge scale as `_EDGE_SCALE`. Vertex morphs are morph targets (`mesh.extras.targetNames` +holds their names); the other morphs (group, bone, UV, material, flip, impulse) are in `mesh.extras.pmx.morphs`. +Materials take the diffuse colour as `baseColor`, the specular power as roughness and the no-cull flag as +`doubleSided`; the MMD terms (specular, ambient, edge, sphere map and its mode, toon texture or shared toon index, memo) +are in `material.extras.pmx`, bone flags / IK / inherit settings in `node.extras.pmx`, display frames, rigid bodies and +joints in `meta.source`. Textures are referenced by their file names (`opts.files` / `opts.resolve` give the bytes). +`morphs = false` skips the morph targets. + +## VRML and X3D + +VRML97 (`.wrl`, gzipped `.wrz`) and X3D (XML `.x3d`, classic `.x3dv`, gzipped `.x3dz` / `.x3dvz`) share one reader: the +scene graph becomes nodes (a `Transform` with a `center` or `scaleOrientation` adds an inner node), `DEF` / `USE` +shares meshes and materials, `PROTO` / `ProtoInstance` are expanded, Box / Sphere / Cone / Cylinder are tessellated +(`opts.segments`, default 32), polygons are fan-triangulated unless `triangulate = false`, texture coordinates are +flipped to glTF's top-left origin, viewpoints become cameras, lights become lights, and ROUTEs from a `TimeSensor` +through position / orientation interpolators to a `Transform` become animations. `Inline` files and textures come +from `opts.files` / `opts.resolve`. Binary X3D (`.x3db`) is `UNSUPPORTED`. + +```luau +local wrl = [[#VRML V2.0 utf8 +DEF Red Transform { translation 2 0 0 children Shape { + appearance Appearance { material Material { diffuseColor 1 0 0 } } + geometry Box { size 1 2 3 } } } +DEF Cam Viewpoint { position 0 0 10 } +]] +local scene = assert(F.load(wrl)) -- detected by the #VRML header +assert(scene.meta.source.format == "vrml" and #scene.cameras == 1) +assert(scene.nodes[1].name == "Red" and scene.materials[1].baseColor[1] == 1) +local lo, hi = F.ops.mesh.bounds(scene) +assert(lo[1] == 1.5 and hi[1] == 2.5) -- the Box, moved by its Transform +``` + +Both write too: VRML97 text, X3D XML, or the classic X3D encoding. Shared meshes are written once and `USE`d; textures +with bytes go into `opts.files`: + +```luau +local x3d = [[ + +]] +local quad = assert(F.load(x3d, { triangulate = false })) +assert(quad.meshes[1].primitives[1].mode == "polygons" and quad.meshes[1].primitives[1].faces.count == 1) +local out = assert(F.save(quad, nil, { format = "wrl" })) -- VRML97 +assert(buffer.readstring(out, 0, 15) == "#VRML V2.0 utf8") +local classic = assert(F.save(quad, nil, { format = "x3dv" })) -- the classic X3D encoding +assert(F.load(classic, { triangulate = false }).meshes[1].primitives[1].faces.count == 1) +``` + ## Voxels MagicaVoxel `.vox` files load as a Scene with `meta.up = "z"`, units of one voxel, and grids in `scene.voxels`; each @@ -392,6 +448,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 | +| PMX / PMD (MikuMikuDance) | yes | | bones + skin, vertex morphs, MMD materials and physics in extras | +| VRML97 (WRL / WRZ) | yes | yes | primitives, PROTO, Inline, ROUTE animations, cameras, lights | +| X3D (XML / classic) | yes | yes | the VRML reader plus the X3D triangle / quad / line sets; no binary `.x3db` | | STEP | via the CAD plug-in | | see [cad.md](cad.md) | Capabilities per format: [FORMATS.md](../FORMATS.md). diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..06aee19 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -872,6 +872,19 @@ do bench("3mf encode", 2, buffer.len(tmf), function() ThreeMf.encode(scene) end) + local Wrl = require("@formats/model/vrml") + local X3d = require("@formats/model/x3d") + local wrl = assert(Wrl.encode(scene)) + bench("vrml decode (" .. buffer.len(wrl) // 1000 .. " KB)", 2, buffer.len(wrl), function() + Wrl.decode(Bytes.reader(wrl)) + end) + bench("vrml encode", 2, buffer.len(wrl), function() + Wrl.encode(scene) + end) + local x3d = assert(X3d.encode(scene)) + bench("x3d xml decode (" .. buffer.len(x3d) // 1000 .. " KB)", 2, buffer.len(x3d), function() + X3d.decode(Bytes.reader(x3d)) + end) local V = 128 local grid = Types.Voxels.new(V, V, V) for z = 0, V - 1 do