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.
149 lines
6.0 KiB
Python
149 lines
6.0 KiB
Python
# codecs/model/Brush fixtures (tools/fixtures.py): random convex brushes (boxes on a 16-unit grid, some with corners cut
|
|
# by planes through three integer points; overlapping, touching and coplanar ones) written as Quake standard, Valve 220
|
|
# and brush-primitive .map files, compiled with ericw-tools' qbsp -nofill (no outside fill: every CSG face survives)
|
|
# against a WAD2 of textures with their sizes. Reference: qbsp's faces (plane, corners, texinfo vectors and texture).
|
|
# tests/gen/brush.luau { [format] = { brushes = { { { pts = { 9 numbers }, tex, kind, args } } },
|
|
# sizes = { [tex] = { w, h } }, faces = { { plane = { 4 }, pts = { 3n }, tex, vecs = { 8 } } } } }
|
|
import importlib.util, os
|
|
|
|
INPUTS = ["tools/fixtures/_qbsp.py"]
|
|
|
|
TEXTURES = [("brick", 64, 32), ("metal", 32, 128), ("wood", 128, 64), ("stone", 16, 16)]
|
|
|
|
|
|
def helper(ctx):
|
|
spec = importlib.util.spec_from_file_location("_qbsp", os.path.join(ctx.root, "tools", "fixtures", "_qbsp.py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
def sub(a, b):
|
|
return (a[0] - b[0], a[1] - b[1], a[2] - b[2])
|
|
|
|
|
|
def cross(u, v):
|
|
return (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0])
|
|
|
|
|
|
def dot(u, v):
|
|
return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]
|
|
|
|
|
|
def oriented(p1, p2, p3, center):
|
|
# qbsp: normal = (p1 - p2) x (p3 - p2) points out of the brush
|
|
n = cross(sub(p1, p2), sub(p3, p2))
|
|
if dot(n, sub(center, p2)) > 0:
|
|
return (p3, p2, p1)
|
|
return (p1, p2, p3)
|
|
|
|
|
|
def box_faces(lo, hi):
|
|
(x0, y0, z0), (x1, y1, z1) = lo, hi
|
|
return [
|
|
((x0, y0, z0), (x0, y0 + 1, z0), (x0, y0, z0 + 1)),
|
|
((x1, y1, z1), (x1, y1, z1 + 1), (x1, y1 + 1, z1)),
|
|
((x0, y0, z0), (x0, y0, z0 + 1), (x0 + 1, y0, z0)),
|
|
((x1, y1, z1), (x1 + 1, y1, z1), (x1, y1, z1 + 1)),
|
|
((x0, y0, z0), (x0 + 1, y0, z0), (x0, y0 + 1, z0)),
|
|
((x1, y1, z1), (x1, y1 + 1, z1), (x1 + 1, y1, z1)),
|
|
]
|
|
|
|
|
|
def brushes(rng):
|
|
out = []
|
|
for i in range(14):
|
|
lo = tuple(rng.randrange(-8, 6) * 16 for _ in range(3))
|
|
hi = tuple(lo[k] + rng.randrange(2, 7) * 16 for k in range(3))
|
|
faces = box_faces(lo, hi)
|
|
center = tuple((lo[k] + hi[k]) / 2 for k in range(3))
|
|
for _ in range(rng.randrange(0, 3)):
|
|
corner = tuple(rng.choice((lo[k], hi[k])) for k in range(3))
|
|
pts = []
|
|
for k in range(3):
|
|
p = list(corner)
|
|
size = hi[k] - lo[k]
|
|
step = rng.randrange(4, size // 2 + 1)
|
|
p[k] += step if corner[k] == lo[k] else -step
|
|
pts.append(tuple(p))
|
|
faces.append(oriented(pts[0], pts[1], pts[2], center))
|
|
out.append(faces)
|
|
return out
|
|
|
|
|
|
def num(x):
|
|
s = ("%.6f" % x).rstrip("0").rstrip(".")
|
|
return "0" if s in ("-0", "") else s
|
|
|
|
|
|
def projection(rng, kind):
|
|
if kind == "standard":
|
|
return [rng.randrange(-64, 65), rng.randrange(-64, 65), rng.choice([0, 90, 180, 270, 15, 30, 45, 33.5]),
|
|
rng.choice([1, 0.5, 2, -1, 0.25, 1.5]), rng.choice([1, 0.5, 2, -1, 0.75])]
|
|
if kind == "valve":
|
|
u = [round(rng.uniform(-1, 1), 4) for _ in range(3)]
|
|
v = [round(rng.uniform(-1, 1), 4) for _ in range(3)]
|
|
return u + [rng.randrange(-32, 33)] + v + [rng.randrange(-32, 33), 0, rng.choice([1, 0.5, 2, -1]), rng.choice([1, 0.25, 2])]
|
|
while True: # qbsp replaces a singular matrix by the default projection
|
|
m = [rng.randrange(-8, 9) / 256, rng.randrange(-8, 9) / 256, rng.randrange(-16, 17) / 16,
|
|
rng.randrange(-8, 9) / 256, rng.randrange(-8, 9) / 256, rng.randrange(-16, 17) / 16]
|
|
if m[0] * m[4] - m[1] * m[3] != 0:
|
|
return m
|
|
|
|
|
|
def face_line(kind, pts, tex, a):
|
|
p = " ".join("( %s )" % " ".join(num(c) for c in q) for q in pts)
|
|
if kind == "standard":
|
|
return "%s %s %s" % (p, tex, " ".join(num(x) for x in a))
|
|
if kind == "valve":
|
|
return "%s %s [ %s ] [ %s ] %s" % (p, tex, " ".join(num(x) for x in a[0:4]), " ".join(num(x) for x in a[4:8]),
|
|
" ".join(num(x) for x in a[8:11]))
|
|
return "%s ( ( %s ) ( %s ) ) %s 0 0 0" % (p, " ".join(num(x) for x in a[0:3]), " ".join(num(x) for x in a[3:6]), tex)
|
|
|
|
|
|
def write_map(kind, bl, rng):
|
|
lines = ["// Game: Quake", "{", '"classname" "worldspawn"', '"wad" "tex.wad"']
|
|
if kind == "valve":
|
|
lines.append('"mapversion" "220"')
|
|
recs = []
|
|
for faces in bl:
|
|
lines.append("{")
|
|
if kind == "bp":
|
|
lines.append("brushDef")
|
|
lines.append("{")
|
|
rec = []
|
|
for pts in faces:
|
|
tex = rng.choice(TEXTURES)[0]
|
|
a = projection(rng, kind)
|
|
lines.append(face_line(kind, pts, tex, a))
|
|
rec.append({"pts": [float(c) for q in pts for c in q], "tex": tex, "kind": kind, "args": [float(x) for x in a]})
|
|
if kind == "bp":
|
|
lines.append("}")
|
|
lines.append("}")
|
|
recs.append(rec)
|
|
lines += ["}", "{", '"classname" "info_player_start"', '"origin" "0 0 0"', "}", ""]
|
|
return "\n".join(lines), recs
|
|
|
|
|
|
def generate(ctx):
|
|
H = helper(ctx)
|
|
rng = ctx.rng(777)
|
|
lumps = [(n, 0x44, H.miptex(n, w, h, rng)) for n, w, h in TEXTURES]
|
|
w2 = H.wad(lumps)
|
|
out = {}
|
|
for kind in ("standard", "valve", "bp"):
|
|
bl = brushes(rng)
|
|
text, recs = write_map(kind, bl, rng)
|
|
res = H.compile(ctx, text, {"tex.wad": w2}, ["-nofill", "-noclip", "-nopercent"])
|
|
b = H.read_bsp(res["bsp"])
|
|
world = b["models"][0]
|
|
first, count = world[14], world[15]
|
|
faces = []
|
|
for f in b["faces"][first:first + count]:
|
|
ti = b["texinfo"][f["texinfo"]]
|
|
tex = b["textures"][ti[8]][0]
|
|
faces.append({"plane": list(H.face_plane(b, f)), "pts": [c for p in H.face_points(b, f) for c in p],
|
|
"tex": tex, "vecs": list(ti[0:8])})
|
|
out[kind] = {"brushes": recs, "sizes": {n: [w, h] for n, w, h in TEXTURES}, "faces": faces, "map": text}
|
|
ctx.emit("brush", out)
|