Files
rosetta/tests/model/d3map.luau
T

162 lines
7.1 KiB
Luau

-- model/d3map tests: an editor-layout Doom 3 / Quake 4 map written by the fixture: brushDef3 side areas against qhull
-- (scipy HalfspaceIntersection), texture coordinates against Doom 3's ComputeAxisBase + matrix, the patchDef3 grid
-- against a direct Bezier evaluation, entities. Fixtures: tools/fixtures/d3map.py.
local T = require("@tests/T")
local F = require("@rosetta")
local D3map = require("@rosetta/model/d3map")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/d3map")
local t = T.suite("d3map")
t:eq(D3map.name, "d3map", "name")
t:eq(F.detect(Fx.doom3, { name = "test.map" }), "d3map", "F.detect: Version 2 is not a Quake map")
t:eq(F.detect(Fx.quake4, { name = "test.map" }), "d3map", "F.detect: Version 3")
local s, err = D3map.decode(Bytes.reader(buffer.fromstring(Fx.doom3)))
t:check(s, "doom3: " .. tostring(err))
local sc = s :: any
t:eq(sc.meta.source.variant, "doom3", "variant")
t:eq((F.load(Fx.quake4, { format = "d3map" }) :: any).meta.source.variant, "quake4", "Quake 4")
t:eq(sc.meta.counts.brushes, 3, "brushes")
t:eq(sc.meta.counts.patches, 2, "patches")
t:eq(sc.meta.counts.skippedPrimitives, 1, "brushDef skipped")
local byMaterial: { [string]: any } = {}
for _, mesh in sc.meshes do
for _, p in mesh.primitives do
byMaterial[sc.materials[p.material].name] = p
end
end
local function tris(p: any, fn: (ax: number, ay: number, az: number, bx: number, by: number, bz: number, cx: number, cy: number, cz: number, a: number, b: number, c: number) -> ())
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local idx = Types.Scene.reader(p.indices) :: any
for k = 0, p.indices.count - 1, 3 do
local a, b, c = idx(k, 0), idx(k + 1, 0), idx(k + 2, 0)
fn(pos(a, 0), pos(a, 1), pos(a, 2), pos(b, 0), pos(b, 1), pos(b, 2), pos(c, 0), pos(c, 1), pos(c, 2), a, b, c)
end
end
do
local bad, uvBad, windBad, faces = {}, 0, 0, 0
for _, w in Fx.sides do
local p = byMaterial[w.material]
if w.area < 1e-6 then
t:check(p == nil, w.material .. ": a redundant side has no polygon")
elseif not p then
table.insert(bad, w.material .. " missing")
else
faces += 1
local area = 0
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
tris(p, function(ax, ay, az, bx, by, bz, cx, cy, cz, a)
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
area += math.sqrt(nx * nx + ny * ny + nz * nz) / 2
if nx * nrm(a, 0) + ny * nrm(a, 1) + nz * nrm(a, 2) <= 0 then
windBad += 1
end
end)
if math.abs(area - w.area) > 1e-3 * math.max(1, w.area) then
table.insert(bad, string.format("%s: %.4f vs %.4f", w.material, area, w.area))
end
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local uv = Types.Scene.reader(p.attributes.TEXCOORD_0) :: any
local v = w.vecs
for i = 0, p.attributes.POSITION.count - 1 do
local x, y, z = pos(i, 0), pos(i, 1), pos(i, 2)
local su = x * v[1] + y * v[2] + z * v[3] + v[4]
local tv = x * v[5] + y * v[6] + z * v[7] + v[8]
if math.abs(uv(i, 0) - su) > 1e-3 or math.abs(uv(i, 1) - tv) > 1e-3 then
uvBad += 1
end
end
end
end
t:eq(#bad, 0, "side areas against qhull: " .. table.concat(bad, "; ", 1, math.min(#bad, 4)))
t:eq(uvBad, 0, "texture coordinates: ComputeAxisBase + the texture matrix")
t:eq(windBad, 0, "brush triangles counter-clockwise about the side normal")
t:note("%d brush sides with polygons match qhull; volumes %s", faces, table.concat(Fx.volumes :: any, ", "))
end
do
local p = byMaterial["textures/test/patchDef3"]
t:check(p, "patchDef3 primitive")
local w = Fx.patch3
t:eq(p.attributes.POSITION.count, w.width * w.height, "patchDef3: explicit subdivisions")
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
local worst = 0
for i = 0, w.width * w.height - 1 do
for c = 0, 2 do
worst = math.max(worst, math.abs(pos(i, c) - w.points[i * 3 + c + 1]))
end
end
t:check(worst < 1e-3, string.format("patchDef3 grid against the Bezier evaluation (worst %.2g)", worst))
local p2 = byMaterial["textures/test/patchDef2"]
t:check(p2, "patchDef2 primitive")
local pos2 = Types.Scene.reader(p2.attributes.POSITION) :: any
local c = Fx.patch2.corners
t:check(pos2(0, 0) == c[1] and pos2(0, 1) == c[2] and math.abs(pos2(0, 2) - c[3]) < 1e-4, "patchDef2 first corner")
local n2 = p2.attributes.POSITION.count - 1
t:check(pos2(n2, 0) == c[4] and pos2(n2, 1) == c[5] and math.abs(pos2(n2, 2) - c[6]) < 1e-4, "patchDef2 last corner")
local nrm = Types.Scene.reader(p2.attributes.NORMAL) :: any
local agree = 0
local total = 0
tris(p2, function(ax, ay, az, bx, by, bz, cx, cy, cz, a)
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
local d = (uy * vz - uz * vy) * nrm(a, 0) + (uz * vx - ux * vz) * nrm(a, 1) + (ux * vy - uy * vx) * nrm(a, 2)
total += 1
if d > 0 then
agree += 1
end
end)
t:eq(agree, total, "patch normals face the triangle winding")
end
do
local named: { [string]: any } = {}
for _, n in sc.nodes do
named[n.name] = n
end
t:eq(#sc.nodes, 1 + 4, "root + 4 entities")
t:check(named.worldspawn and named.worldspawn.mesh, "worldspawn mesh")
t:check(named.crate and named.crate.mesh and named.crate.extras.keys.model == "crate", "func_static: mesh and keys")
t:check(named.crate.translation == nil, "brush entities stay in world coordinates")
local l = named.light_1
t:check(l.translation[1] == 10 and l.translation[3] == 30, "light origin")
local q = l.rotation
t:check(math.abs(q[3] - math.sqrt(0.5)) < 1e-6 and math.abs(q[4] - math.sqrt(0.5)) < 1e-6, "rotation matrix rows: yaw 90")
local ps = named.info_player_start_1
t:check(math.abs(ps.rotation[3] - math.sqrt(0.5)) < 1e-6, "angle 90")
t:eq(ps.extras.classname, "info_player_start", "classname")
t:eq(sc.meta.entities[1].classname, "worldspawn", "meta.entities")
local lean = D3map.decode(Bytes.reader(buffer.fromstring(Fx.doom3)), { skip = { "textures/test/box_0", "TEXTURES/TEST/PATCHDEF2" } }) :: any
local seen = {}
for _, mesh in lean.meshes do
for _, p in mesh.primitives do
seen[lean.materials[p.material].name] = true
end
end
t:check(not seen["textures/test/box_0"] and not seen["textures/test/patchDef2"] and seen["textures/test/box_1"], "skip")
end
do
local function code(src: string, opts: any?): any
local _, e = D3map.decode(Bytes.reader(buffer.fromstring(src)), opts)
return e and e.code
end
t:eq(code("{ }"), Err.CORRUPT, "no Version")
t:eq(code("Version 2\n{ \"classname\" \"worldspawn\"\n{ brushDef3 { ( 0 0 1 0 ) ( ( 1 0 0 ) ( 0 1 0 ) ) \"x\" 0 0 0 }"), Err.CORRUPT, "truncated")
t:eq(code("Version 2\n{ \"classname\" \"worldspawn\" { patchDef2 { \"x\" ( 4 3 0 0 0 ) ( ) } } }"), Err.CORRUPT, "short patch")
t:eq(code(Fx.doom3, { maxPrimitives = 2 }), Err.LIMIT, "maxPrimitives")
t:eq(code("Version 2\n"), Err.CORRUPT, "no entities")
t:check(F.detect("Version 2\n{\n\"classname\" \"worldspawn\"\n}\n") == "d3map", "an empty map sniffs")
end
t:fuzz("decode", function(b: buffer)
return D3map.decode(Bytes.reader(b))
end, { buffer.fromstring(Fx.doom3) }, { mutations = 200 })
t:done()