model/godot + data/tres + codecs/text/GodotVariant: Godot text scenes (.tscn / .escn) read + write against Godot 4's loader, .tres resources as data

docs/STYLE.md: the layout block lists codecs/text/GodotVariant (DOCS lint).
This commit is contained in:
4DBug committed 2026-11-01 19:25:15 -06:00
1 parent 9c02ebdee5
commit 8f26e32a71
24 files changed
+3649 -5

No files matched your search

+35
View File
@@ -739,6 +739,40 @@ local again = assert(F.load(F.save(scene, nil, { format = "ase" })))
assert(#again.meshes[again.nodes[2].mesh].primitives == 2)
```
## Godot scenes (TSCN / ESCN)
Godot text scenes load as a Y-up Scene (one node per `[node]`, the scene root first), checked against Godot 4's own
loader. Node3D transforms come from `transform = Transform3D(...)` (or `position` / `rotation` / `scale`); a
MeshInstance3D gets its ArrayMesh sub-resource (Godot 4.2+ surfaces, and the `"arrays"` surfaces of Godot 3 / Blender
`.escn` files) or its primitive mesh (Box, Plane, Quad, Sphere and Cylinder are built as Godot builds them), with
`surface_material_override/N` applied. StandardMaterial3D / SpatialMaterial become PBR materials (Godot's sRGB
albedo and emission converted to linear; the raw values in `material.extras.godot`); textures are `ExtResource`
paths (`res://` dropped) with their bytes through `opts.resolve`. Camera3D (`fov` is vertical) and Omni / Spot /
Directional lights map to cameras and lights (`intensity` = `light_energy`). Instanced scenes
(`instance=ExtResource(...)`) are read through `opts.resolve` and grafted under their node. Godot's front faces are
clockwise: indices are reversed into glTF's counter-clockwise winding (and back on save). Every node keeps its Godot
type and raw properties in `node.extras.godot`, so 2D trees (Node2D, Sprite2D, Control) are kept there too.
```luau
local scene = assert(F.load(tscnText, { resolve = function(path) return nil end }))
assert(scene.meta.source.format == "godot" and scene.meta.up == "y")
local box = scene.nodes[2]
assert(box.name == "Box" and box.extras.godot.type == "MeshInstance3D")
assert(box.matrix[13] == 2) -- Transform3D origin x
local mat = scene.materials[scene.meshes[box.mesh].primitives[1].material]
assert(mat.extras.godot.albedo[1] == 1 and mat.metallic == 0.5)
assert(scene.cameras[1].type == "perspective" and math.abs(math.deg(scene.cameras[1].yfov) - 60) < 1e-9)
local hud = scene.nodes[4]
assert(hud.extras.godot.type == "Node2D" and hud.extras.godot.properties.position.args[1] == 10)
local tscn = assert(F.saveString(scene, { format = "godot" })) -- Godot 4 text, format 3
assert(string.find(tscn, '[node name="Box" type="MeshInstance3D" parent="."]', 1, true))
```
The writer makes Node3D / MeshInstance3D / Camera3D / light nodes, ArrayMesh sub-resources (uncompressed format 2
surfaces with positions, normals, tangents, colours, two UV sets) and StandardMaterial3D, plus a `Texture2D`
`ext_resource` per texture with a `uri`. Skins, animations and morph targets are not written. Godot resources
(`.tres`) are data: see [data.md](data.md).
## Formats
| Format | Read | Write | Notes |
@@ -769,6 +803,7 @@ assert(#again.meshes[again.nodes[2].mesh].primitives == 2)
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
| SMD | yes | yes | Valve studiomdl data |
| Godot `.tscn` / `.escn` | yes | yes (`.tscn`) | Node3D trees, ArrayMesh and primitive meshes, StandardMaterial3D, cameras, lights, instances; 2D nodes in extras |
| ASE (3ds Max ASCII) | yes | yes | Multi/Sub-Object materials, texture channels, vertex colours, normals, helpers, cameras, lights; no animation; Z up |
| 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) |