Files

222 lines
8.1 KiB
Luau

-- model/vbsp tests: maps written by tools/fixtures/vbsp.py and saved by srctools (v19 / v20 / v21, L4D2 header, LZMA
-- lumps, static props v4 .. v11) against what srctools reads back: face polygons and texture projections per
-- (model, material), the displacement grid, static props, entities, the pak lump; errors and the fuzz pass.
local T = require("@tests/T")
local Vbsp = require("@rosetta/model/vbsp")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Fx = require("@tests/gen/vbsp")
local t = T.suite("vbsp")
t:eq(Vbsp.name, "vbsp", "name")
t:check(Vbsp.caps.decode and not Vbsp.caps.encode, "caps")
local function f2(x: number): string
local s = string.format("%.2f", x)
return if s == "-0.00" then "0.00" else s
end
local function lines(prim: Types.Primitive): (string, number)
local P, U = prim.attributes.POSITION, prim.attributes.TEXCOORD_0
local out = table.create(P.count)
for i = 0, P.count - 1 do
out[i + 1] = table.concat({
f2(buffer.readf32(P.data, i * 12)), f2(buffer.readf32(P.data, i * 12 + 4)), f2(buffer.readf32(P.data, i * 12 + 8)),
f2(buffer.readf32(U.data, i * 8)), f2(buffer.readf32(U.data, i * 8 + 4)),
}, " ")
end
table.sort(out)
return table.concat(out, "\n"), (prim.indices :: Types.Accessor).count // 3
end
local function fixLines(s: string): string
local out = {}
for l in string.gmatch(s, "[^\n]+") do
table.insert(out, (string.gsub(l, "%-0%.00", "0.00")))
end
table.sort(out)
return table.concat(out, "\n")
end
-- every triangle faces its vertices' normals (front faces are counter-clockwise)
local function facing(prim: Types.Primitive): boolean
local P, N, I = prim.attributes.POSITION, prim.attributes.NORMAL, prim.indices :: Types.Accessor
for k = 0, I.count // 3 - 1 do
local a, b, c = buffer.readu32(I.data, k * 12), buffer.readu32(I.data, k * 12 + 4), buffer.readu32(I.data, k * 12 + 8)
local function p(i: number, j: number): number
return buffer.readf32(P.data, i * 12 + j * 4)
end
local ux, uy, uz = p(b, 0) - p(a, 0), p(b, 1) - p(a, 1), p(b, 2) - p(a, 2)
local vx, vy, vz = p(c, 0) - p(a, 0), p(c, 1) - p(a, 1), p(c, 2) - p(a, 2)
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
local d = nx * buffer.readf32(N.data, a * 12) + ny * buffer.readf32(N.data, a * 12 + 4) + nz * buffer.readf32(N.data, a * 12 + 8)
if d <= 0 then
return false
end
end
return true
end
-- Source AngleVectors(pitch, yaw, roll): forward, left (= -right), up
local function basis(p: number, y: number, r: number): { number }
local sp, cp = math.sin(math.rad(p)), math.cos(math.rad(p))
local sy, cy = math.sin(math.rad(y)), math.cos(math.rad(y))
local sr, cr = math.sin(math.rad(r)), math.cos(math.rad(r))
return {
cp * cy, cp * sy, -sp,
sr * sp * cy - cr * sy, sr * sp * sy + cr * cy, sr * cp,
cr * sp * cy + sr * sy, cr * sp * sy - sr * cy, cr * cp,
}
end
local first: Types.Scene? = nil
for _, c in Fx.cases do
local scene, err = Vbsp.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
if not scene then
continue
end
first = first or scene
local src = scene.meta.source :: any
t:eq(src.version, c.version, c.name .. ": version")
t:eq(src.variant == "l4d2", c.l4d2, c.name .. ": L4D2 header")
t:eq(src.compressed, c.compressed, c.name .. ": LZMA lumps")
t:eq(src.staticPropVersion, c.spversion, c.name .. ": static prop version")
-- faces
local byKey: { [string]: Types.Primitive } = {}
for _, n in scene.nodes do
if n.mesh then
local model = (n.extras :: any).vbsp.model
for _, prim in scene.meshes[n.mesh].primitives do
byKey[model .. "|" .. (scene.materials[prim.material :: number].name :: string)] = prim
end
end
end
local ngroups = 0
for _ in byKey do
ngroups += 1
end
t:eq(ngroups, #c.groups, c.name .. ": one primitive per (model, material), nodraw skipped")
for _, g in c.groups do
local prim = byKey[g.model .. "|" .. g.material]
t:check(prim ~= nil, c.name .. ": group " .. g.model .. " " .. g.material)
if prim then
local got, tris = lines(prim)
t:eq(tris, g.tris, c.name .. ": " .. g.material .. " triangles")
t:eq(got, fixLines(g.verts), c.name .. ": " .. g.material .. " positions and UVs as srctools")
t:check(facing(prim), c.name .. ": " .. g.material .. " counter-clockwise fronts")
if string.find(g.material, "BLEND", 1, true) then
t:check(prim.attributes._BLEND ~= nil, c.name .. ": displacement alpha")
end
end
end
-- static props
local props = {}
for _, n in scene.nodes do
if n.extras and (n.extras :: any).model then
table.insert(props, n)
end
end
t:eq(#props, #c.props, c.name .. ": static props")
for i, want in c.props do
local n = props[i]
if n then
t:eq((n.extras :: any).model, want.model, c.name .. ": prop " .. i .. " model")
t:eq((n.extras :: any).vbsp.skin, want.skin, c.name .. ": prop " .. i .. " skin")
local tr = n.translation :: { number }
t:check(math.abs(tr[1] - want.origin[1]) + math.abs(tr[2] - want.origin[2]) + math.abs(tr[3] - want.origin[3]) < 1e-4, c.name .. ": prop " .. i .. " origin")
local m = Mat4.fromQuat(n.rotation :: { number })
local bv = basis(want.angles[1], want.angles[2], want.angles[3])
local d = 0
for col = 0, 2 do
for row = 0, 2 do
d = math.max(d, math.abs(m[col * 4 + row + 1] - bv[col * 3 + row + 1]))
end
end
t:check(d < 1e-5, c.name .. ": prop " .. i .. " rotation is AngleVectors")
end
end
-- entities
local ents = scene.meta.entities :: { any }
t:eq(#ents, c.entities, c.name .. ": entities")
local outputs, classes = 0, {}
for _, e in ents do
table.insert(classes, e.classname)
if type(e.OnTrigger) == "table" then
outputs += #e.OnTrigger
elseif e.OnTrigger then
outputs += 1
end
end
table.sort(classes)
t:eq(table.concat(classes, ","), table.concat(c.classnames, ","), c.name .. ": classnames")
t:eq(outputs, c.outputs, c.name .. ": repeated outputs")
-- pak
local pak = scene.meta.pak
t:check(pak ~= nil and pak.kind == "archive", c.name .. ": pak archive")
if pak then
local names = {}
for _, e in pak:list() do
table.insert(names, e.name)
end
table.sort(names)
t:eq(table.concat(names, ","), table.concat(c.pak, ","), c.name .. ": pak names")
t:eq(buffer.tostring(pak:read("materials/custom/note.txt") :: buffer), "hello from the pak lump\n", c.name .. ": pak read")
end
end
if first then
local s = first :: Types.Scene
t:eq(s.meta.up, "z", "z up")
local door, light
for _, n in s.nodes do
if n.name == "door" then
door = n
end
if n.light then
light = n
end
end
t:check(door and door.mesh and door.translation and door.translation[3] == 64, "func_brush placed at its origin")
t:check(light and s.lights[light.light].type == "point" and math.abs(s.lights[light.light].intensity - 300) < 1e-9, "light entity")
local disp = nil
for _, m in s.meshes do
for _, p in m.primitives do
if p.attributes._BLEND then
disp = p
end
end
end
t:check(disp ~= nil and disp.attributes.POSITION.count == 81, "power 3: 9 x 9 grid")
local all = Vbsp.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].data)), { tools = true }) :: Types.Scene
t:check(#all.materials == #s.materials + 1, "tools = true keeps the nodraw face")
end
do
local plain = buffer.fromstring(Fx.plain)
t:eq(Vbsp.sniff(Bytes.reader(plain)), 1, "sniff")
t:eq(Vbsp.sniff(Bytes.reader(buffer.fromstring("IBSP" .. string.rep("\0", 2000)))), 0, "not Quake")
local bad = buffer.fromstring(Fx.plain)
buffer.writei32(bad, 4, 42)
local _, e1 = Vbsp.decode(Bytes.reader(bad))
t:eq(e1 and e1.code, Err.UNSUPPORTED, "version 42")
local _, e2 = Vbsp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.plain, 1, 600))))
t:eq(e2 and e2.code, Err.TRUNCATED, "short header")
local cut = buffer.fromstring(string.sub(Fx.plain, 1, 3000))
local _, e3 = Vbsp.decode(Bytes.reader(cut))
t:eq(e3 and e3.code, Err.TRUNCATED, "lumps past the end")
end
local samples = {}
for i, c in Fx.cases do
if i <= 4 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Vbsp.decode(Bytes.reader(b))
end, samples, { keepHeader = 8 })
t:done()