-- 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()