Files
rosetta/tests/codecs/patch.luau
T
4DBug 5ad934565d codecs/model/Brush, codecs/model/Patch, codecs/image/Miptex; Pixels palette helpers
Brush: plane brushes -> polygons (qbsp-style clipping), Quake standard / Valve 220 / brush-primitive projection,
optional coplanar merge and CSG hull; tested against ericw-tools qbsp -nofill (area per plane, texels per corner).
Patch: Bezier control grids moved out of model/bsp/Quake3, per-axis subdivisions (patchDef2 / patchDef3), normals.
Miptex: Quake / GoldSrc miptex decode / encode and the WAD2 / WAD3 directory (model/bsp uses it now).
Pixels: grey, keyed, colorList, colormap, paletteBytes. STYLE: layout block and 2a table list the new modules.
2026-11-01 18:40:29 -06:00

86 lines
3.1 KiB
Luau

-- codecs/model/Patch tests: biquadratic Bezier evaluation against the closed form, shared sub-patch edges, triangle
-- winding and normals, explicit (patchDef3-style) subdivisions per axis, the fuzz pass.
local T = require("@tests/T")
local Patch = require("@rosetta/codecs/model/Patch")
local t = T.suite("patch")
-- a 5 x 3 grid (two sub-patches along u): x = column * 8, y = row * 8, z = a bump on the middle control row
local ctrl = {}
for j = 0, 2 do
for i = 0, 4 do
table.insert(ctrl, i * 8)
table.insert(ctrl, j * 8)
table.insert(ctrl, if j == 1 then 16 else 0)
end
end
local pts, gw, gh = Patch.grid(ctrl, 5, 3, 3, 4, 2)
t:check(pts ~= nil, "grid")
t:eq(gw, 9, "width: 2 sub-patches x 4 segments + 1")
t:eq(gh, 3, "height: 2 segments + 1")
local p = pts :: { number }
-- z(v) = 2 v (1 - v) * 16 on every column (the bump is constant along u); x is linear in u
local worst = 0
for gy = 0, gh - 1 do
local v = gy / 2
for gx = 0, gw - 1 do
local o = (gy * gw + gx) * 3
worst = math.max(worst, math.abs(p[o + 1] - gx * 4), math.abs(p[o + 2] - v * 16), math.abs(p[o + 3] - 32 * v * (1 - v)))
end
end
t:check(worst < 1e-12, "closed form (worst " .. worst .. ")")
-- corners interpolate the control points
t:eq(p[1] .. " " .. p[2] .. " " .. p[3], "0 0 0", "first corner")
local last = (gw * gh - 1) * 3
t:eq(p[last + 1] .. " " .. p[last + 2] .. " " .. p[last + 3], "32 16 0", "last corner")
local tris = Patch.triangles(gw, gh)
t:eq(#tris, (gw - 1) * (gh - 1) * 6, "two triangles per cell")
local nrm = Patch.normals(p, 3, gw, gh)
local bad = 0
for k = 1, #tris, 3 do
local a, b, c = tris[k] * 3, tris[k + 1] * 3, tris[k + 2] * 3
local ux, uy, uz = p[b + 1] - p[a + 1], p[b + 2] - p[a + 2], p[b + 3] - p[a + 3]
local vx, vy, vz = p[c + 1] - p[a + 1], p[c + 2] - p[a + 2], p[c + 3] - p[a + 3]
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
if cx * nrm[a + 1] + cy * nrm[a + 2] + cz * nrm[a + 3] <= 0 then
bad += 1
end
end
t:eq(bad, 0, "normals face the front of the triangles")
-- extra components (texture coordinates) interpolate too; normalize renormalizes a vec3
local flat = {}
for j = 0, 2 do
for i = 0, 2 do
for _, v in { i, j, 0, i / 2, j / 2, 0, 0, 2 } do
table.insert(flat, v)
end
end
end
local q, qw, qh = Patch.grid(flat, 3, 3, 8, 3, 5)
t:eq(qw * qh, 4 * 6, "explicit subdivisions per axis")
Patch.normalize(q :: { number }, 8, 5)
t:eq((q :: { number })[8], 1, "renormalized normal")
t:eq((q :: { number })[(qw * qh - 1) * 8 + 4], 1, "texture coordinate at the far corner")
t:check(Patch.grid({ 1, 2, 3 }, 2, 2, 3, 4, 4) == nil, "even grid: nil")
t:check(Patch.grid(ctrl, 5, 3, 3, 1000, 1000, 1000) == nil, "point limit: nil")
t:fuzz("grid", function(b: buffer)
local n = buffer.len(b)
local w = 1 + 2 * (if n > 0 then buffer.readu8(b, 0) % 4 else 1)
local h = 1 + 2 * (if n > 1 then buffer.readu8(b, 1) % 4 else 1)
local c = {}
for k = 0, n - 1 do
table.insert(c, buffer.readu8(b, k) - 128)
end
local g, gw2, gh2 = Patch.grid(c, w, h, 3, 1 + n % 5, 1 + n % 3)
if g then
Patch.normals(g, 3, gw2, gh2)
Patch.triangles(gw2, gh2)
end
return g
end, { buffer.create(27), buffer.create(45), buffer.create(75) })
t:done()