merge ws/engines

This commit is contained in:
4DBug committed 2026-11-01 20:14:34 -06:00
commit 8bd15840d8
66 files changed
+31751 -1331

No files matched your search

+256
View File
@@ -0,0 +1,256 @@
-- model/ase tests: assimp's ASE models and a hand-written file against assimp (meshes, faces, corners, cameras, lights,
-- world bounds); the hand file's values; round trips; assimp reading our output. Fixtures: tools/fixtures/ase.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ase = require("@rosetta/model/ase")
local MeshOps = require("@rosetta/ops/MeshOps")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Cases = require("@tests/gen/ase_cases")
local Xcheck = require("@tests/gen/ase_xcheck")
local t = T.suite("ase")
local S = Types.Scene
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Ase.decode(Bytes.reader(b), opts)
end
t:eq(Ase.name, "ase", "name")
t:eq(Ase.category, "model", "category")
t:check(Ase.caps.decode and Ase.caps.encode, "contract")
local function near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
end
local function counts(scene: any): (number, number, number)
local prims, faces, corners = 0, 0, 0
for _, pl in S.flatten(scene) do
prims += 1
faces += pl.primitive.indices.count // 3
corners += pl.primitive.indices.count
end
return prims, faces, corners
end
-- assimp's ASE root rotates Z-up to Y-up: (x, z, -y)
local function boundsMatch(lo: { number }, hi: { number }, want: any, eps: number): boolean
local a, b = { lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] }
for k = 1, 3 do
if not near(a[k], want.min[k], eps) or not near(b[k], want.max[k], eps) then
return false
end
end
return true
end
local function bstr(lo: any, hi: any): string
if not lo then
return "-"
end
return string.format("(%.4g %.4g %.4g) (%.4g %.4g %.4g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
end
-- assimp mis-reads the UTF-16 copies of ThreeCubesGreen (one mesh of 8 faces, a bone): they are compared with the
-- UTF-8 file instead
local UTF16 = { ["assimp/models/ThreeCubesGreen_UTF16BE.ASE"] = true, ["assimp/models/ThreeCubesGreen_UTF16LE.ASE"] = true }
local scenes: { [number]: any } = {}
local byName: { [string]: any } = {}
local samples = {}
for i, c in Cases do
local scene, err = decode(c.data)
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(Ase.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
scenes[i] = scene
byName[c.name] = scene
if #c.data < 60000 then
table.insert(samples, c.data)
end
local a = c.assimp
if scene and not a.failed and not UTF16[c.name] then
local prims, faces, corners = counts(scene)
t:eq(prims, a.meshes, c.name .. ": meshes (assimp)")
t:eq(faces, a.faces, c.name .. ": faces (assimp)")
t:eq(corners, a.vertices, c.name .. ": corners (assimp)")
t:eq(#scene.cameras, a.cameras, c.name .. ": cameras (assimp)")
t:eq(#scene.lights, a.lights, c.name .. ": lights (assimp)")
if a.min then
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": world bounds (assimp) " .. bstr(lo, hi) .. " want " .. bstr(a.min, a.max))
end
end
end
do
local u8 = byName["assimp/models/ThreeCubesGreen.ASE"]
for name in UTF16 do
local s = byName[name]
if s and u8 then
local p1, f1 = counts(s)
local p2, f2 = counts(u8)
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(s))
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(u8))
t:check(p1 == p2 and f1 == f2 and bstr(lo1, hi1) == bstr(lo2, hi2), name .. ": same scene as the UTF-8 file")
end
end
end
-- the hand-written file
do
local s = byName["hand.ase"]
t:eq(s.meta.up, "z", "hand: up")
t:eq(s.meta.source.frameSpeed, 25, "hand: frame speed")
t:eq(#s.materials, 2, "hand: sub-materials")
local brick, glass = s.materials[1], s.materials[2]
t:eq(brick.name, "Brick", "hand: material name")
t:check(near(brick.baseColor[1], 0.8, 1e-6) and near(brick.baseColor[3], 0.2, 1e-6), "hand: diffuse")
t:eq(s.textures[brick.baseColorTexture.texture].uri, "textures\\brick.png", "hand: bitmap path")
t:check(brick.baseColorTexture.transform and brick.baseColorTexture.transform.scale[2] == 3, "hand: UVW tiling")
t:check(glass.alphaMode == "blend" and near(glass.baseColor[4], 0.5, 1e-6) and glass.doubleSided, "hand: transparency, two-sided")
local quad
for _, n in s.nodes do
if n.name == "Quad" then
quad = n
end
end
local pivot = s.nodes[s.roots[1]]
t:eq(pivot.name, "Pivot", "hand: helper root")
t:check(table.find(pivot.children, table.find(s.nodes, quad)) ~= nil, "hand: NODE_PARENT")
-- local frame: Quad's world TM rotates 90 degrees about z at (10, 5, 0); relative to Pivot at (10, 0, 0)
t:check(near(quad.matrix[13], 0, 1e-6) and near(quad.matrix[14], 5, 1e-6) and near(quad.matrix[2], 1, 1e-6), "hand: matrix relative to the parent")
local mesh = s.meshes[quad.mesh]
t:eq(#mesh.primitives, 2, "hand: one primitive per sub-material")
local p1 = mesh.primitives[1]
t:eq(p1.material, 1, "hand: MESH_MTLID 0")
local P = S.reader(p1.attributes.POSITION) :: any
-- vertex 1 (10, 7, 0) world -> local: inverse of (rot z 90, t (10, 5, 0)) = (2, 0, 0)
t:check(near(P(1, 0), 2, 1e-6) and near(P(1, 1), 0, 1e-6), "hand: vertices in the node frame")
local UV = S.reader(p1.attributes.TEXCOORD_0) :: any
t:check(near(UV(1, 0), 1, 1e-6) and near(UV(1, 1), 1, 1e-6) and near(UV(2, 1), 0, 1e-6), "hand: UVs (v flipped)")
local UV2 = S.reader(p1.attributes.TEXCOORD_1) :: any
t:check(near(UV2(0, 0), 0.25, 1e-6) and near(UV2(0, 1), 0.25, 1e-6), "hand: mapping channel 2")
local C = S.reader(p1.attributes.COLOR_0) :: any
t:check(C(0, 0) == 1 and C(1, 1) == 1 and C(2, 0) == 1, "hand: vertex colours")
local N = S.reader(p1.attributes.NORMAL) :: any
-- (0, 0.6, 0.8) world under the inverse rotation: (0.6, 0, 0.8)
t:check(near(N(2, 0), 0.6, 1e-6) and near(N(2, 2), 0.8, 1e-6), "hand: explicit vertex normals")
t:eq(s.cameras[1].zfar, 500, "hand: camera far")
t:check(s.lights[1].type == "spot" and s.lights[1].intensity == 2 and near(s.lights[1].outerCone, math.rad(30), 1e-9), "hand: spot light")
end
-- round trips: ours -> ours keeps counts, bounds, materials, UVs
local function sameScene(a: any, b: any, label: string)
local p1, f1, c1 = counts(a)
local p2, f2, c2 = counts(b)
t:check(p1 == p2 and f1 == f2 and c1 == c2, label .. ": counts " .. p1 .. "/" .. f1 .. " vs " .. p2 .. "/" .. f2)
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(a))
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(b))
if lo1 and lo2 then
local ok = true
for k = 1, 3 do
ok = ok and near(lo1[k], lo2[k], 1e-5) and near(hi1[k], hi2[k], 1e-5)
end
t:check(ok, label .. ": bounds " .. bstr(lo1, hi1) .. " vs " .. bstr(lo2, hi2))
end
t:eq(#b.cameras, #a.cameras, label .. ": cameras")
t:eq(#b.lights, #a.lights, label .. ": lights")
end
for i, c in Cases do
local s = scenes[i]
if s then
local out, err = Ase.encode(s)
t:check(out, c.name .. ": encode: " .. tostring(err))
if out then
local back, berr = decode(out)
t:check(back, c.name .. ": re-decode: " .. tostring(berr))
if back then
sameScene(s, back, c.name .. ": round trip")
end
end
end
end
do
local s = byName["hand.ase"]
local back = decode(assert(Ase.encode(s)))
local m = back.materials[1]
t:eq(m.name, "Brick", "hand round trip: material")
t:eq(back.textures[m.baseColorTexture.texture].uri, "textures\\brick.png", "hand round trip: bitmap")
local q
for _, n in back.nodes do
if n.name == "Quad" then
q = n
end
end
local p = back.meshes[q.mesh].primitives[1]
t:check(p.attributes.TEXCOORD_1 and p.attributes.COLOR_0 and p.attributes.NORMAL, "hand round trip: attributes")
end
-- glTF -> ASE (any Scene): a mesh with no materials, an n-gon, a strip
do
local s = S.new()
local P = S.accessor(nil, "f32", 3, 5)
for i, v in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0.5, 1.5, 0 }, { 0, 1, 0 } } do
for k = 1, 3 do
buffer.writef32(P.data, ((i - 1) * 3 + k - 1) * 4, v[k])
end
end
local faces = S.accessor(nil, "u32", 1, 1)
buffer.writeu32(faces.data, 0, 5)
s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "polygons", faces = faces } } }
s.meshes[2] = { primitives = { { attributes = { POSITION = P }, mode = "triangle_strip" } } }
s.nodes = { { name = "ngon", children = { 2 }, mesh = 1, translation = { 0, 0, 3 } }, { name = "ngon", children = {}, mesh = 2, scale = { 2, 2, 2 } } }
s.roots = { 1 }
local out = assert(Ase.encode(s))
local back = assert(decode(out))
local _, faces2 = counts(back)
t:eq(faces2, 6, "n-gon (3) + strip (3) as triangles")
t:eq(back.nodes[2].name, "ngon_2", "duplicate names made unique")
local lo, hi = MeshOps.bounds(MeshOps.merge(back))
t:check(near(lo[3], 3, 1e-6) and near(hi[1], 2, 1e-6), "child world transform " .. bstr(lo, hi))
local via = F.save(s, nil, { format = "ase" })
t:check(type(via) == "buffer" and F.detect(via) == "ase", "F.save / F.detect")
end
-- assimp reads what we write
for _, x in Xcheck do
local c = Cases[x.case]
local s = scenes[x.case]
local a = x.assimp
t:check(not a.failed, x.name .. " (" .. c.name .. "): assimp reads it")
if s and not a.failed then
local prims, faces = counts(s)
t:eq(a.faces, faces, c.name .. ": assimp faces of ours")
t:eq(a.meshes, prims, c.name .. ": assimp meshes of ours")
t:eq(a.cameras, #s.cameras, c.name .. ": assimp cameras of ours")
t:eq(a.lights, #s.lights, c.name .. ": assimp lights of ours")
if a.min then
local lo, hi = MeshOps.bounds(MeshOps.merge(s))
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": assimp bounds of ours " .. bstr(lo, hi) .. " vs " .. bstr(a.min, a.max))
end
end
end
-- errors
do
local _, e = decode("*3DSMAX_ASCIIEXPORT 200\n*GEOMOBJECT { *MESH { *MESH_NUMVERTEX 1 *MESH_NUMFACES 1 *MESH_FACE_LIST { *MESH_FACE 0: A: 0 B: 5 C: 0 } } }")
t:check(e and e.code == Err.CORRUPT, "face index out of range: " .. tostring(e))
local _, e2 = decode("*3DSMAX_ASCIIEXPORT 200\n}")
t:check(e2 and e2.code == Err.CORRUPT, "unbalanced brace")
local _, e3 = decode("hello")
t:check(e3 and e3.code == Err.CORRUPT, "not ASE")
local _, e4 = decode(Cases[1].data, { maxItems = 10 })
t:check(e4 and e4.code == Err.LIMIT, "item limit")
local _, e5 = Ase.encode({} :: any)
t:check(e5 and e5.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return Ase.decode(Bytes.reader(b))
end, samples)
t:done()
+356
View File
@@ -0,0 +1,356 @@
-- model/godot tests: scenes saved by Godot 4 against Godot's own loader (node transforms, surface arrays, materials,
-- cameras, lights, primitive meshes, instanced scenes, 2D nodes); a format-2 .escn; our .tscn writer's output loaded by
-- Godot and compared with the original; codecs/text/GodotVariant values; errors; fuzz. Fixtures: tools/fixtures/godot.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Godot = require("@rosetta/model/godot")
local GodotVariant = require("@rosetta/codecs/text/GodotVariant")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/godot")
local Ours = require("@tests/gen/godot_ours")
local t = T.suite("godot")
local S = Types.Scene
local function decode(s: string | buffer, files: any?, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
local o = opts or {}
o.resolve = function(p: string): buffer?
local f = files and files[p]
return if f then buffer.fromstring(f) else nil
end
return Godot.decode(Bytes.reader(b), o)
end
t:eq(Godot.name, "godot", "name")
t:eq(Godot.category, "model", "category")
t:check(Godot.caps.decode and Godot.caps.encode, "caps")
local function list(acc: any): { number }
if not acc then
return {}
end
local rd = S.reader(acc) :: any
local out = {}
for i = 0, acc.count - 1 do
for c = 0, acc.components - 1 do
table.insert(out, rd(i, c))
end
end
return out
end
-- our Scene in the dump's shape (tools/fixtures/_godot_dump.gd): paths, local transforms as basis rows + origin,
-- surface arrays with Godot's clockwise indices
local function asDump(scene: any): { [string]: any }
local out = {}
local function visit(i: number, path: string)
local n = scene.nodes[i]
local g = n.extras and n.extras.godot or {}
local e: any = { path = path, class = g.type }
local m = S.nodeMatrix(n)
e.xf = { m[1], m[5], m[9], m[2], m[6], m[10], m[3], m[7], m[11], m[13], m[14], m[15] }
e.visible = g.visible ~= false
if n.mesh then
local surfs = {}
for _, p in scene.meshes[n.mesh].primitives do
local idx = list(p.indices)
for k = 1, #idx - 2, 3 do
idx[k + 1], idx[k + 2] = idx[k + 2], idx[k + 1]
end
local mat = p.material and scene.materials[p.material]
local md = nil
if mat then
local x = mat.extras.godot
local tex = mat.baseColorTexture and scene.textures[mat.baseColorTexture.texture]
md = {
albedo = x.albedo,
metallic = mat.metallic,
roughness = mat.roughness,
emission = if x.emission then { x.emission[1], x.emission[2], x.emission[3] } else nil,
texture = if tex then "res://" .. tex.uri else nil,
transparency = if mat.alphaMode == "blend" then 1 else 0,
cull = if mat.doubleSided then 2 else 0,
}
end
local colors = {}
for k, v in list(p.attributes.COLOR_0) do
colors[k] = v
end
table.insert(surfs, {
vertex = list(p.attributes.POSITION),
normal = list(p.attributes.NORMAL),
tangent = list(p.attributes.TANGENT),
uv = list(p.attributes.TEXCOORD_0),
uv2 = list(p.attributes.TEXCOORD_1),
color = colors,
index = idx,
material = md,
})
end
e.surfaces = surfs
end
if n.camera then
local c = scene.cameras[n.camera]
e.camera = if c.type == "orthographic" then { projection = 1, size = c.ymag * 2, near = c.znear, far = c.zfar } else { projection = 0, fov = math.deg(c.yfov), near = c.znear, far = c.zfar }
end
if n.light then
local l = scene.lights[n.light]
local raw = g.lightColor or { 1, 1, 1 }
e.light = { color = { raw[1], raw[2], raw[3] }, energy = l.intensity, range = l.range, angle = if l.outerCone then math.deg(l.outerCone) else nil }
end
out[path] = e
for _, c in n.children do
local cn = scene.nodes[c].name
visit(c, if path == "." then cn else path .. "/" .. cn)
end
end
for _, r in scene.roots do
visit(r, ".")
end
return out
end
local function close(a: { number }?, b: { number }?, eps: number): (boolean, string)
a, b = a or {}, b or {}
if #a ~= #b then
return false, "length " .. #a .. " vs " .. #b
end
for i = 1, #a do
if math.abs(a[i] - b[i]) > eps * math.max(1, math.abs(b[i])) then
return false, "[" .. i .. "] " .. a[i] .. " vs " .. b[i]
end
end
return true, ""
end
-- the 3D facts of a dump node `got` against the reference `want`
local function compare(got: any, want: any, label: string, eps: number)
if want.xf then
local ok, why = close(got.xf, want.xf, 1e-5)
t:check(ok, label .. ": transform " .. why)
t:eq(got.visible, want.visible, label .. ": visible")
end
if want.surfaces then
t:eq(#(got.surfaces or {}), #want.surfaces, label .. ": surfaces")
for k, ws in want.surfaces do
local gs = (got.surfaces or {})[k]
if gs then
for _, f in { "vertex", "uv", "uv2" } do
local ok, why = close(gs[f], ws[f], 1e-5)
t:check(ok, label .. ": surface " .. k .. " " .. f .. " " .. why)
end
for _, f in { "normal", "tangent" } do
local ok, why = close(gs[f], ws[f], eps)
t:check(ok, label .. ": surface " .. k .. " " .. f .. " " .. why)
end
local ok, why = close(gs.color, ws.color, 1 / 255 + 1e-6)
t:check(ok, label .. ": surface " .. k .. " color " .. why)
local wi = ws.index
if #wi == 0 then
for i = 0, #ws.vertex // 3 - 1 do
wi[i + 1] = i
end
end
local gi = gs.index
if #gi == 0 then
for i = 0, #gs.vertex // 3 - 1 do
gi[i + 1] = i
end
end
local iok, iwhy = close(gi, wi, 0)
t:check(iok, label .. ": surface " .. k .. " index " .. iwhy)
local wm, gm = ws.material, gs.material
if wm then
t:check(gm ~= nil, label .. ": surface " .. k .. " material")
if gm then
local aok, awhy = close(gm.albedo, wm.albedo, 1e-5)
t:check(aok, label .. ": albedo " .. awhy)
t:check(math.abs(gm.metallic - wm.metallic) < 1e-5 and math.abs(gm.roughness - wm.roughness) < 1e-5, label .. ": metallic / roughness")
local eok, ewhy = close(gm.emission, wm.emission, 1e-5)
t:check(eok, label .. ": emission " .. ewhy)
t:eq(gm.texture, wm.texture, label .. ": albedo texture")
t:eq(gm.transparency, wm.transparency, label .. ": transparency")
t:eq(gm.cull, wm.cull, label .. ": cull")
end
end
end
end
end
if want.camera then
local gc, wc = got.camera or {}, want.camera
t:eq(gc.projection, wc.projection, label .. ": projection")
if wc.projection == 1 then
t:check(math.abs(gc.size - wc.size) < 1e-5, label .. ": ortho size")
else
t:check(math.abs(gc.fov - wc.fov) < 1e-5, label .. ": fov")
end
t:check(math.abs(gc.near - wc.near) < 1e-6 and math.abs(gc.far - wc.far) < 1e-3, label .. ": near / far")
end
if want.light then
local gl, wl = got.light or {}, want.light
local ok, why = close(gl.color, wl.color, 1e-5)
t:check(ok, label .. ": light colour " .. why)
t:check(math.abs((gl.energy or -1) - wl.energy) < 1e-5, label .. ": light energy")
if wl.range then
t:check(math.abs((gl.range or -1) - wl.range) < 1e-5, label .. ": light range")
end
if wl.angle then
t:check(math.abs((gl.angle or -1) - wl.angle) < 1e-4, label .. ": spot angle")
end
end
end
local scenes = {}
local samples = {}
for _, c in Fx.cases do
local scene, err = decode(c.data, c.files)
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(Godot.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
scenes[c.name] = scene
table.insert(samples, c.data)
if scene and c.dump then
t:check(c.dump.ok, c.name .. ": Godot loads it")
local mine = asDump(scene)
local n = 0
for _, want in c.dump.nodes do
local got = mine[want.path]
t:check(got, c.name .. ": node " .. want.path)
if got then
t:eq(got.class, want.class, c.name .. ": " .. want.path .. " class")
compare(got, want, c.name .. ": " .. want.path, 3e-4)
n += 1
end
end
t:note("%s: %d nodes against Godot", c.name, n)
end
end
-- Godot reads what we write
for _, o in Ours do
if not o.dump then
continue
end
t:check(o.dump.ok, o.name .. ": Godot loads our .tscn")
local base = string.match(o.name, "^ours_(.*)%.tscn$")
local ref
for _, c in Fx.cases do
if string.gsub(c.name, "%.%w+$", "") == base then
ref = c
end
end
if ref and ref.dump and o.dump.ok then
local byPath = {}
for _, nd in o.dump.nodes do
byPath[nd.path] = nd
end
local n = 0
for _, want in ref.dump.nodes do
if want.xf then
local got = byPath[want.path]
t:check(got, o.name .. ": node " .. want.path)
if got then
compare(got, want, o.name .. ": " .. want.path, 5e-4)
n += 1
end
end
end
t:note("%s: %d 3D nodes after our writer", o.name, n)
elseif o.dump.ok and base == "hand" then
-- the escn has no Godot reference: compare Godot's reading of ours with our reading of the escn
local mine = asDump(scenes["hand.escn"])
for _, got in o.dump.nodes do
local want = mine[got.path]
t:check(want, o.name .. ": node " .. got.path)
if want and want.xf then
compare(got, want, o.name .. ": " .. got.path, 5e-4)
end
end
end
end
-- details
do
local s = scenes["main.tscn"]
t:eq(s.meta.source.version, 4, "header format")
t:eq(s.meta.up, "y", "up axis")
local shape
for _, n in s.nodes do
if n.name == "Shape" then
shape = n
end
end
local m = s.materials[s.meshes[shape.mesh].primitives[1].material]
t:eq(m.name, "Painted", "material name")
t:check(math.abs(m.baseColor[1] - ((0.8 + 0.055) / 1.055) ^ 2.4) < 1e-6, "albedo sRGB -> linear")
t:eq(m.extensions.KHR_materials_emissive_strength.emissiveStrength, 2, "emission energy")
t:eq(s.textures[m.baseColorTexture.texture].uri, "checker.png", "texture path")
t:check(s.textures[m.baseColorTexture.texture].data ~= nil, "texture bytes through resolve")
local hud
for _, n in s.nodes do
if n.name == "Hud" then
hud = n
end
end
t:check(hud.extras.godot.is2D and hud.extras.godot.properties.rotation == 0.5, "2D node kept in extras")
local pos = hud.extras.godot.properties.position
t:check(GodotVariant.isCtor(pos) and pos.type == "Vector2" and pos.args[2] == 20, "2D position as a Vector2")
-- without resolve: the instance stays an empty node
local bare = assert(decode(Fx.cases[2].data))
local inst
for _, n in bare.nodes do
if n.name == "PartInstance" then
inst = n
end
end
t:check(inst and #inst.children == 0, "unresolved instance")
local e = scenes["hand.escn"]
t:eq(e.meta.source.version, 2, "escn format 2")
t:eq(#e.cameras, 1, "escn camera")
t:check(math.abs(math.deg(e.cameras[1].yfov) - 60) < 1e-9 and e.cameras[1].zfar == 250, "escn camera fov / far")
t:eq(e.lights[1].range, 9, "escn light range")
local cube = e.nodes[2]
local p = e.meshes[cube.mesh].primitives[1]
t:eq(p.indices.count, 6, "escn arrays surface")
t:check(e.materials[1].doubleSided and e.materials[1].metallic == 0.5, "SpatialMaterial")
local strip = e.meshes[e.nodes[3].mesh].primitives[1]
t:check(strip.mode == "triangles" and strip.indices.count == 6 and strip.attributes.COLOR_0 ~= nil, "escn triangle strip -> triangles")
t:check(cube.matrix[14] == 3 and cube.matrix[7] == 1, "Transform (format 2) rows")
end
-- any Scene: through F.save
do
local s = S.new()
local P = S.accessor(nil, "f32", 3, 3)
for i, v in { 0, 0, 0, 1, 0, 0, 0, 1, 0 } do
buffer.writef32(P.data, (i - 1) * 4, v)
end
s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "triangles" } } }
s.nodes = { { name = "a/b", children = {}, mesh = 1, translation = { 1, 2, 3 } }, { name = "a/b", children = {} } }
s.roots = { 1, 2 }
local out = assert(F.save(s, nil, { format = "godot" }))
local back = assert(decode(out))
t:eq(back.nodes[1].name, "Root", "two roots under Root")
t:check(back.nodes[2].name == "a_b" and back.nodes[3].name == "a_b2", "names sanitised and unique")
t:eq(back.nodes[2].matrix[14], 2, "transform")
t:eq(back.meshes[1].primitives[1].indices.count, 3, "triangle")
t:eq(F.detect(out), "godot", "F.detect")
end
do
local _, e = decode("[gd_resource type=\"Resource\" format=3]\n[resource]\nx = 1\n")
t:check(e and e.code == Err.UNSUPPORTED, "a .tres is not a scene")
local _, e2 = decode("[gd_scene format=3]\n[node name=\"A\" parent=\"missing\"]\n")
t:check(e2 and e2.code == Err.CORRUPT, "missing parent")
local _, e3 = decode("[gd_scene format=3]\nx = Vector3(1,\n")
t:check(e3 ~= nil, "truncated value")
local _, e4 = Godot.encode({} :: any)
t:check(e4 and e4.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return Godot.decode(Bytes.reader(b))
end, samples, { mutations = 80, garbage = 20 })
t:done()
+165
View File
@@ -0,0 +1,165 @@
-- model/t3d tests: UE1-3 and UE4 T3D maps against reference values computed by tools/fixtures/t3d.py (brush world
-- vertices through PrePivot / MainScale / FRotationMatrix / PostScale / Location, polygon areas per texture, bounds,
-- texel probes), actors, lights, components, static mesh references; errors; fuzz.
local T = require("@tests/T")
local F = require("@rosetta")
local T3d = require("@rosetta/model/t3d")
local MeshOps = require("@rosetta/ops/MeshOps")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/t3d")
local t = T.suite("t3d")
local S = Types.Scene
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return T3d.decode(Bytes.reader(b), opts)
end
t:eq(T3d.name, "t3d", "name")
t:eq(T3d.category, "model", "category")
t:check(T3d.caps.decode and not T3d.caps.encode, "caps")
local function near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(b))
end
local function triArea(prim: any, m: { number }): number
local P = S.reader(prim.attributes.POSITION) :: any
local I = S.reader(prim.indices) :: any
local a = 0
local function w(i: number): (number, number, number)
local x, y, z = P(i, 0), P(i, 1), P(i, 2)
return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
end
for k = 0, prim.indices.count // 3 - 1 do
local ax, ay, az = w(I(k * 3, 0))
local bx, by, bz = w(I(k * 3 + 1, 0))
local cx, cy, cz = w(I(k * 3 + 2, 0))
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
a += math.sqrt(nx * nx + ny * ny + nz * nz) / 2
end
return a
end
local samples = {}
for _, c in Fx.cases do
local scene, err = decode(c.data, { textureSize = c.sizes })
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(T3d.sniff(Bytes.reader(buffer.fromstring(c.data))), 0.9, c.name .. ": sniff")
table.insert(samples, c.data)
if scene then
t:eq(scene.meta.up, "z", c.name .. ": up")
local byName = {}
for _, n in scene.nodes do
byName[n.name] = n
end
for _, a in c.actors do
local n = byName[a.name]
t:check(n, c.name .. ": actor " .. a.name)
if n then
t:eq(n.extras.t3d.class, a.class, c.name .. ": " .. a.name .. " class")
local m = S.nodeMatrix(n)
if a.origin then
t:check(near(m[13], a.origin[1], 1e-9) and near(m[14], a.origin[2], 1e-9) and near(m[15], a.origin[3], 1e-9), c.name .. ": " .. a.name .. " location")
end
if a.staticMesh then
t:eq(n.extras.t3d.staticMesh, a.staticMesh, c.name .. ": static mesh reference")
end
t:eq(n.mesh ~= nil, a.mesh == true, c.name .. ": " .. a.name .. " has geometry")
if a.mesh and n.mesh then
local mesh = scene.meshes[n.mesh]
local areas, tris = {}, 0
for _, p in mesh.primitives do
local name = scene.materials[p.material].name or ""
areas[name] = (areas[name] or 0) + triArea(p, m)
tris += p.indices.count // 3
end
t:eq(tris, a.triangles, c.name .. ": " .. a.name .. " triangles")
for tex, want in a.area do
t:check(areas[tex] and near(areas[tex], want, 1e-6), c.name .. ": " .. a.name .. " area of " .. tex .. ": " .. tostring(areas[tex]) .. " vs " .. want)
end
local one = Types.Scene.new()
one.meshes = { mesh }
one.nodes = { { children = {}, mesh = 1, matrix = m } }
one.roots = { 1 }
local lo, hi = MeshOps.bounds(MeshOps.merge(one))
local ok = true
for k = 1, 3 do
ok = ok and near(lo[k], a.min[k], 1e-6) and near(hi[k], a.max[k], 1e-6)
end
t:check(ok, c.name .. ": " .. a.name .. " world bounds")
-- the probe vertex (local) has the reference texel coordinates
local pr = a.probe
local found = false
for _, p in mesh.primitives do
local P = S.reader(p.attributes.POSITION) :: any
local U = S.reader(p.attributes.TEXCOORD_0) :: any
for i = 0, p.attributes.POSITION.count - 1 do
if near(P(i, 0), pr[1], 1e-6) and near(P(i, 1), pr[2], 1e-6) and near(P(i, 2), pr[3], 1e-6) and near(U(i, 0), pr[4], 1e-5) and near(U(i, 1), pr[5], 1e-5) then
found = true
end
end
end
t:check(found, c.name .. ": " .. a.name .. " texel probe")
end
end
end
end
end
do
local s = assert(decode(Fx.cases[1].data, { textureSize = Fx.cases[1].sizes }))
local room, light
for _, n in s.nodes do
if n.name == "Brush0" then
room = n
elseif n.name == "Light0" then
light = n
end
end
t:eq(room.extras.t3d.csg, "CSG_Subtract", "csg operation")
-- a subtracted room faces inwards: the floor's normal points up (+z) in the brush frame
local prim
for _, p in s.meshes[room.mesh].primitives do
if s.materials[p.material].name == "Floor" then
prim = p
end
end
local N = S.reader(prim.attributes.NORMAL) :: any
local P = S.reader(prim.attributes.POSITION) :: any
t:check(P(0, 2) < 0 and N(0, 2) > 0.99, "subtractive brush faces inwards")
t:check(s.lights[s.nodes[table.find(s.nodes, light) :: number].light].type == "point", "UE1 light")
t:eq(s.nodes[1].extras.t3d.properties.Title, "Test Level", "actor properties")
local withBuilder = assert(decode(Fx.cases[1].data, { builder = true }))
t:eq(#withBuilder.meshes, #s.meshes + 1, "builder brush on request")
local u4 = assert(decode(Fx.cases[2].data))
t:eq(u4.meta.source.engine, "ue4", "UE4 text")
local pl
for _, n in u4.nodes do
if n.light then
pl = u4.lights[n.light]
end
end
t:check(pl and pl.intensity == 3000 and pl.range == 500 and near(pl.color[1], 1, 1e-9), "UE4 point light component")
local bare = assert(decode("Begin PolyList\nBegin Polygon Texture=X\nVertex 0,0,0\nVertex 0,1,0\nVertex 1,0,0\nEnd Polygon\nEnd PolyList\n"))
t:eq(#bare.meshes, 1, "a bare polylist")
t:eq(F.detect(Fx.cases[1].data), "t3d", "F.detect")
end
do
local _, e = decode("Begin Map\nBegin Actor Class=Brush Name=B\n")
t:check(e and e.code == Err.TRUNCATED, "unclosed blocks: " .. tostring(e))
local _, e2 = decode("hello")
t:check(e2 and e2.code == Err.CORRUPT, "no blocks")
local _, e3 = decode(Fx.cases[1].data, { maxBlocks = 3 })
t:check(e3 and e3.code == Err.LIMIT, "block limit")
end
t:fuzz("decode", function(b: buffer)
return T3d.decode(Bytes.reader(b))
end, samples)
t:done()