Files
rosetta/src/Rosetta/model/bsp/Quake3.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

227 lines
7.8 KiB
Luau

--!strict
-- model/bsp/Quake3: Quake III Arena maps (IBSP 46; RTCW 47 has the same layout) -> Scene, from the Unofficial Quake 3
-- Map Specs (Kekoa Proudfoot; no code taken). Lightmaps are 128 x 128 RGB; billboards (flares) are skipped; patches
-- through codecs/model/Patch.
local Types = require("@rosetta/Types")
local Patch = require("@rosetta/codecs/model/Patch")
local Quake3 = {}
local Scene = Types.Scene
local readi32, readf32, readu8 = buffer.readi32, buffer.readf32, buffer.readu8
local writef32, writeu32, writeu8 = buffer.writef32, buffer.writeu32, buffer.writeu8
export type Lump = { at: number, size: number }
export type Options = { patchLevel: number?, maxVertices: number? }
type Batch = { shader: number, lightmap: number, verts: { number } } -- indices into the vertex lump, or -1 - k for patch vertices
local function cstr(b: buffer, at: number, n: number): string
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
end
function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts: Options?): (Types.Scene?, string?)
local T, M, VX, MV, F, LM = lumps.textures, lumps.models, lumps.vertexes, lumps.meshverts, lumps.faces, lumps.lightmaps
local nT, nM, nV, nMV, nF, nLM = T.size // 72, M.size // 40, VX.size // 44, MV.size // 4, F.size // 104, LM.size // 49152
local level = math.clamp(math.floor(opts and opts.patchLevel or 4), 1, 16)
local maxV = opts and opts.maxVertices or 2 ^ 24
-- patch vertices: 12 floats + 4 colour bytes each
local extra: { number } = {}
local function vertex(i: number): { number }
local o = base + VX.at + i * 44
local v = table.create(14, 0)
for k = 0, 9 do
v[k + 1] = readf32(b, o + k * 4)
end
for k = 0, 3 do
v[11 + k] = readu8(b, o + 40 + k)
end
return v
end
local function addExtra(v: { number }): number
for k = 1, 14 do
table.insert(extra, v[k])
end
return -(#extra // 14)
end
local faceModel: { number } = {}
for m = 0, nM - 1 do
local o = base + M.at + m * 40
local first, count = readi32(b, o + 24), readi32(b, o + 28)
if first >= 0 and count >= 0 and first + count <= nF then
for f = first, first + count - 1 do
faceModel[f + 1] = faceModel[f + 1] or (m + 1)
end
end
end
local batches: { [number]: { [string]: Batch } } = {}
local orders: { [number]: { string } } = {}
local counts = { polygons = 0, meshes = 0, patches = 0, billboards = 0, skipped = 0 }
local total = 0
for f = 0, nF - 1 do
local o = base + F.at + f * 104
local shader, kind, first, n, mv, nmv, lm = readi32(b, o), readi32(b, o + 8), readi32(b, o + 12), readi32(b, o + 16), readi32(b, o + 20), readi32(b, o + 24), readi32(b, o + 28)
local model = faceModel[f + 1] or 1
local tris: { number } = {}
if (kind == 1 or kind == 3) and first >= 0 and n >= 0 and first + n <= nV and mv >= 0 and nmv >= 0 and mv + nmv <= nMV then
for k = 0, nmv - 3, 3 do
local ok = true
local tri = {}
for c = 0, 2 do
local idx = readi32(b, base + MV.at + (mv + k + c) * 4)
if idx < 0 or idx >= n then
ok = false
end
tri[c + 1] = first + idx
end
if ok then
table.insert(tris, tri[3])
table.insert(tris, tri[2])
table.insert(tris, tri[1])
end
end
counts[if kind == 1 then "polygons" else "meshes"] += 1
elseif kind == 2 and first >= 0 and n >= 0 and first + n <= nV then
local w, h = readi32(b, o + 96), readi32(b, o + 100)
if Patch.valid(w, h, n) then
counts.patches += 1
local ctrl = table.create(n * 14, 0) :: { number }
for k = 0, n - 1 do
table.move(vertex(first + k), 1, 14, k * 14 + 1, ctrl)
end
local pts, gw, gh = Patch.grid(ctrl, w, h, 14, level, level, maxV)
if not pts then
return nil, "limit: too many patch vertices"
end
Patch.normalize(pts, 14, 7)
local ids = table.create(gw * gh) :: { number }
for k = 0, gw * gh - 1 do
ids[k + 1] = addExtra(table.move(pts, k * 14 + 1, k * 14 + 14, 1, table.create(14, 0)) :: { number })
end
for _, v in Patch.triangles(gw, gh) do
table.insert(tris, ids[v + 1])
end
else
counts.skipped += 1
end
elseif kind == 4 then
counts.billboards += 1
else
counts.skipped += 1
end
total += #tris
if total > maxV then
return nil, "limit: too many vertices"
end
if #tris > 0 then
local key = shader .. ":" .. lm
local bm = batches[model] or {}
batches[model] = bm
local bt = bm[key]
if not bt then
bt = { shader = shader, lightmap = lm, verts = {} }
bm[key] = bt
local ol = orders[model] or {}
orders[model] = ol
table.insert(ol, key)
end
for _, v in tris do
table.insert(bt.verts, v)
end
end
end
local scene = Scene.new()
scene.meta.up = "z"
for i = 0, nT - 1 do
local name = cstr(b, base + T.at + i * 72, 64)
table.insert(scene.textures, { name = name, uri = name })
end
local lmTex: { [number]: number } = {}
for i = 0, nLM - 1 do
local img = Types.Image.new(128, 128)
local o = base + LM.at + i * 49152
for k = 0, 128 * 128 - 1 do
writeu8(img.pixels, k * 4, readu8(b, o + k * 3))
writeu8(img.pixels, k * 4 + 1, readu8(b, o + k * 3 + 1))
writeu8(img.pixels, k * 4 + 2, readu8(b, o + k * 3 + 2))
writeu8(img.pixels, k * 4 + 3, 255)
end
img.meta.source = { format = "bsp", lightmap = i }
table.insert(scene.textures, { name = "lightmap " .. i, image = img })
lmTex[i] = #scene.textures
end
local matOf: { [string]: number } = {}
local root: Types.Node = { name = "bsp", children = {} }
table.insert(scene.nodes, root)
scene.roots[1] = 1
local modelCount = math.max(nM, 1)
for m = 1, modelCount do
local list = orders[m]
if list then
local mesh: Types.Mesh = { name = if m == 1 then "world" else "*" .. (m - 1), primitives = {} }
for _, key in list do
local bt = batches[m][key]
local nv = #bt.verts
local pb, nb, ub, lb, cb, ib = buffer.create(nv * 12), buffer.create(nv * 12), buffer.create(nv * 8), buffer.create(nv * 8), buffer.create(nv * 4), buffer.create(nv * 4)
for k, id in bt.verts do
local v: { number }
if id >= 0 then
v = vertex(id)
else
local e = (-id - 1) * 14
v = table.move(extra, e + 1, e + 14, 1, table.create(14, 0)) :: { number }
end
local o = (k - 1)
writef32(pb, o * 12, v[1])
writef32(pb, o * 12 + 4, v[2])
writef32(pb, o * 12 + 8, v[3])
writef32(ub, o * 8, v[4])
writef32(ub, o * 8 + 4, v[5])
writef32(lb, o * 8, v[6])
writef32(lb, o * 8 + 4, v[7])
writef32(nb, o * 12, v[8])
writef32(nb, o * 12 + 4, v[9])
writef32(nb, o * 12 + 8, v[10])
for c = 0, 3 do
writeu8(cb, o * 4 + c, math.clamp(math.floor(v[11 + c] + 0.5), 0, 255))
end
writeu32(ib, o * 4, o)
end
local mi = matOf[key]
if not mi then
local name = if bt.shader >= 0 and bt.shader < nT then scene.textures[bt.shader + 1].name else "noshader"
local mat = Scene.material(name)
mat.metallic = 0
if bt.shader >= 0 and bt.shader < nT then
mat.baseColorTexture = { texture = bt.shader + 1 }
end
local lt = lmTex[bt.lightmap]
mat.extras = { bsp = { lightmap = lt, lightmapTexCoord = if lt then 1 else nil } }
table.insert(scene.materials, mat)
mi = #scene.materials
matOf[key] = mi
end
table.insert(mesh.primitives, {
attributes = {
POSITION = Scene.accessor(pb, "f32", 3, nv),
NORMAL = Scene.accessor(nb, "f32", 3, nv),
TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv),
TEXCOORD_1 = Scene.accessor(lb, "f32", 2, nv),
COLOR_0 = Scene.accessor(cb, "unorm8", 4, nv),
},
indices = Scene.accessor(ib, "u32", 1, nv),
mode = "triangles" :: any,
material = mi,
})
end
table.insert(scene.meshes, mesh)
table.insert(scene.nodes, { name = mesh.name, children = {}, mesh = #scene.meshes, extras = { bsp = { model = m - 1 } } })
table.insert(root.children, #scene.nodes)
end
end
scene.meta.faces = counts
return scene, nil
end
return Quake3