model/vmf: Hammer VMF read and write (Brush solids with Valve 220 axes, displacements, entities, cameras, visgroups, byte-exact write back, document builders); KeyValues: escapes = "quotes", bare / flat block layout for writing

This commit is contained in:
4DBug committed 2026-11-01 19:15:54 -06:00
1 parent c30e8618fc
commit e4840b719b
17 files changed
+4075 -1443

No files matched your search

+1
View File
@@ -92,6 +92,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| model | stl | .stl | yes | yes | ascii: true, binary: true, colors: true, indexed: false | `tests/model/stl.luau` |
| model | usda | .usda .usd .usdz | yes | yes | materials: true, animation: true, cameras: true, lights: true, polygons: true, vertexColors: true, usdz: "read", usdc: false | `tests/model/usda.luau` |
| model | vbsp | .bsp | yes | - | versions: 19, 20, 21, displacements: true, staticProps: true, pak: true | `tests/model/vbsp.luau` |
| model | vmf | .vmf .vmx | yes | yes | displacements: true, roundTrip: true | `tests/model/vmf.luau` |
| model | vox | .vox | yes | yes | voxels: true, sceneGraph: true, materials: true | `tests/model/vox.luau` |
| model | vrml | .wrl .wrz .vrml | yes | yes | materials: true, polygons: true, animation: true, cameras: true, lights: true, lines: true, points: true | `tests/model/vrml.luau` |
| model | x | .x | yes | - | text: true, binary: true, compressed: true, materials: true, skins: true, animation: true, polygons: true, vertexColors: true | `tests/model/x.luau` |
+27
View File
@@ -471,6 +471,7 @@ Game model formats load as Scenes too, with what games need: skins (joints + inv
| SMD (Valve, read + write) | skeleton, weight links, a primitive per material | the skeleton frames |
| BSP (Quake 1 / 2 / 3, Half-Life) | each model a mesh, a primitive per texture (and Quake III lightmap); entities in `meta.entities` | - |
| VBSP (Source 19 / 20 / 21) | each model a mesh, a primitive per material, displacements; static props as nodes; lights; entities; the pak lump | - |
| VMF (Hammer, read + write) | a mesh per entity with solids, displacements; point entities as nodes; cameras; visgroups | - |
```luau
local tank = assert(F.model.load(md2Bytes))
@@ -533,6 +534,31 @@ assert(room.meta.entities[1].classname == "worldspawn")
assert(room.meta.pak:read("materials/custom/note.txt"))
```
### Hammer maps (VMF)
`.vmf` sources load like `.map` files: a mesh per entity with solids (Valve 220 texture axes through
`codecs/model/Brush`), displacements tessellated as the compiler does (a brush with a displaced side keeps only its
displaced sides), point entities as nodes (`extras.model` for props), Hammer cameras as cameras. `tools/*` materials
are left out unless `tools = true`; texture sizes come from `textureSizes`, or `materials/<name>.vtf` through
`resolve` and `decodeImage`, else 512 x 512. `meta.entities` holds the keys (outputs under `connections`),
`meta.visgroups`, `meta.groups` and `meta.solids` the editor data, and `meta.vmf` the parsed document, which `F.save`
writes back exactly as Hammer lays it out. `F.format("vmf")` builds documents: `document`, `world`, `box`, `solid`,
`entity`.
```luau
local hammer = assert(F.model.load(vmfText, { format = "vmf" }))
assert(hammer.meta.entities[1].classname == "worldspawn" and hammer.meta.visgroups[1].name == "Walls")
assert(#hammer.cameras == 2)
assert(F.saveString(hammer, { format = "vmf" }) == vmfText) -- unchanged documents write back byte for byte
local Vmf = F.format("vmf")
local doc = Vmf.document({ skyname = "sky_day01_01" })
table.insert(Vmf.world(doc), Vmf.box({ -128, -128, -16 }, { 128, 128, 0 }, "DEV/DEV_MEASUREGENERIC01B"))
Vmf.entity(doc, { { "classname", "info_player_start" }, { "origin", "0 0 8" } })
local built = assert(F.model.load(assert(F.saveString(doc, { format = "vmf" })), { format = "vmf" }))
assert(#built.meshes[1].primitives == 1 and built.meta.entities[2].classname == "info_player_start")
```
## Blitz3D (B3D)
`.b3d` files (Blitz3D, also written by many old game tools) load read-only. The node tree keeps its names and TRS;
@@ -771,6 +797,7 @@ assert(back.meshes[1].primitives[1].indices.count == 3)
| 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) |
| MD2, MD3, MDL, MD5, BSP, VBSP | yes | - | game models and maps: see "Game models" above |
| VMF | yes | yes (round trip, builders) | Hammer maps: see "Hammer maps (VMF)" above |
| SMD | yes | yes | Valve studiomdl data |
| Roblox `.mesh` (robloxmesh) | yes | yes | see [roblox.md](roblox.md) |
| Roblox `.rbxm` / `.rbxl`, `.rbxmx` / `.rbxlx` | as an `RbxTree` | yes | instance trees with typed properties: see [roblox.md](roblox.md) |