model/d3map: Doom 3 / Quake 4 editor .map (Version 2 / 3): brushDef3 through codecs/model/Brush, patchDef2 / 3 through codecs/model/Patch, entities as nodes; side areas checked against qhull

This commit is contained in:
4DBug committed 2026-11-01 19:06:29 -06:00
1 parent 4bd59a30c8
commit 0f0da6e8ea
13 files changed
+2945 -1101

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
# Doom 3 / Quake 4 .map fixtures (tools/fixtures.py). Doom 3's maps are not redistributable, so the map is written here
# in the editor's layout (the Doom 3 GPL source docs' idMapFile text, read for the layout; no code taken): a box and a
# random convex brush in worldspawn (every side its own material), a func_static with a box, a patchDef3 and a
# patchDef2, a light with a rotation matrix, a player start with an angle, and a Quake III brushDef (skipped). The
# reference: scipy's HalfspaceIntersection (qhull) per brush -> the area of every side; the side's texture vectors by
# Doom 3's ComputeAxisBase + texture matrix; the patchDef3 grid by evaluating the biquadratic Bezier sub-patches.
# tests/gen/d3map.luau { doom3 = <text>, quake4 = <text>, sides = { { material, area, vecs = { 8 } } },
# patch3 = { width, height, points = { x, y, z, ... } }, ... }
import json, math
REF = r"""
import json, sys
import numpy as np
from scipy.spatial import HalfspaceIntersection, ConvexHull
brushes = json.loads(sys.stdin.read())
out = []
for b in brushes:
hs = np.array([s["plane"] for s in b["sides"]], dtype=float)
hi = HalfspaceIntersection(hs, np.array(b["inside"], dtype=float))
pts = hi.intersections
vol = ConvexHull(pts).volume
for s in b["sides"]:
n, d = np.array(s["plane"][:3], dtype=float), s["plane"][3]
on = pts[np.abs(pts @ n + d) < 1e-6]
area = 0.0
if len(on) >= 3:
u = np.cross(n, [1, 0, 0] if abs(n[0]) < 0.9 else [0, 1, 0]); u /= np.linalg.norm(u)
v = np.cross(n, u)
q = np.stack([on @ u, on @ v], axis=1)
q = np.unique(np.round(q, 9), axis=0)
if len(q) >= 3:
area = ConvexHull(q).volume
out.append({"material": s["material"], "area": area})
out.append({"volume": vol})
print(json.dumps(out))
"""
def axis_base(n):
rot_y = -math.atan2(n[2], math.sqrt(n[1] * n[1] + n[0] * n[0]))
rot_z = math.atan2(n[1], n[0])
s = (-math.sin(rot_z), math.cos(rot_z), 0.0)
t = (-math.sin(rot_y) * math.cos(rot_z), -math.sin(rot_y) * math.sin(rot_z), -math.cos(rot_y))
return s, t
def vecs(n, m):
s, t = axis_base(n)
out = []
for i in range(2):
out += [s[k] * m[i][0] + t[k] * m[i][1] for k in range(3)] + [m[i][2]]
return out
def box(x0, y0, z0, x1, y1, z1):
return [((-1, 0, 0), x0), ((1, 0, 0), -x1), ((0, -1, 0), y0), ((0, 1, 0), -y1), ((0, 0, -1), z0), ((0, 0, 1), -z1)]
def g(v):
return repr(round(v, 9)).rstrip("0").rstrip(".") if isinstance(v, float) else str(v)
def bezier(ctrl, w, h, su, sv):
# ctrl[row][col] = (x, y, z, s, t); sub-patches of 3 x 3; su / sv segments per sub-patch
def b(t):
return ((1 - t) ** 2, 2 * t * (1 - t), t * t)
gw, gh = (w - 1) // 2 * su + 1, (h - 1) // 2 * sv + 1
pts = []
for gy in range(gh):
py, ty = min(gy // sv, (h - 1) // 2 - 1), None
ty = gy / sv - py
for gx in range(gw):
px = min(gx // su, (w - 1) // 2 - 1)
tx = gx / su - px
bu, bv = b(tx), b(ty)
p = [0.0] * 3
for j in range(3):
for i in range(3):
c = ctrl[py * 2 + j][px * 2 + i]
for k in range(3):
p[k] += bv[j] * bu[i] * c[k]
pts += p
return gw, gh, pts
def generate(ctx):
rng = ctx.rng(3)
brushes = []
sides = []
def brush(planes, inside, tag):
b = {"inside": inside, "sides": []}
for k, (n, d) in enumerate(planes):
m = [[rng.choice([1 / 64, 1 / 128, -1 / 128]), rng.uniform(-0.01, 0.01), rng.uniform(-1, 1)],
[rng.uniform(-0.01, 0.01), rng.choice([1 / 64, 1 / 128]), rng.uniform(-1, 1)]]
mat = "textures/test/%s_%d" % (tag, k)
b["sides"].append({"plane": [n[0], n[1], n[2], d], "material": mat, "m": m, "vecs": vecs(n, m)})
brushes.append(b)
return b
world = [brush(box(-128, -128, -16, 128, 128, 0), [0, 0, -8], "box")]
planes = []
cx, cy, cz = 200.0, 0.0, 64.0
for _ in range(12):
z = rng.uniform(-1, 1)
a = rng.uniform(0, 2 * math.pi)
n = (math.sqrt(1 - z * z) * math.cos(a), math.sqrt(1 - z * z) * math.sin(a), z)
r = rng.uniform(40, 64)
planes.append((n, -(n[0] * cx + n[1] * cy + n[2] * cz) - r))
for n, d in box(cx - 100, cy - 100, cz - 100, cx + 100, cy + 100, cz + 100):
planes.append((n, d))
world.append(brush(planes, [cx, cy, cz], "rnd"))
crate = brush(box(-16, 284, 0, 16, 316, 32), [0, 300, 16], "crate")
def side_text(s):
p = s["plane"]
m = s["m"]
return ' ( %s %s %s %s ) ( ( %s %s %s ) ( %s %s %s ) ) "%s" 0 0 0' % tuple(
[g(float(x)) for x in p] + [g(float(x)) for x in m[0]] + [g(float(x)) for x in m[1]] + [s["material"]])
def brush_text(i, b):
return "// primitive %d\n{\n brushDef3\n {\n%s\n }\n}" % (i, "\n".join(side_text(s) for s in b["sides"]))
# patchDef3: 3 x 3, a bump; patchDef2: 5 wide x 3 high, a half pipe
p3 = [[(x * 32.0, 300 + y * 32.0, 40 + (16.0 if x == 0 and y == 0 else 0.0), (x + 1) / 2, (y + 1) / 2) for x in (-1, 0, 1)] for y in (-1, 0, 1)]
p2 = [[(-64 + x * 32.0, 400 + y * 64.0, 64 * abs(math.cos(x * math.pi / 4)), x / 4, y / 2) for x in range(5)] for y in range(3)]
def patch_text(kind, ctrl, info):
h, w = len(ctrl), len(ctrl[0])
cols = []
for i in range(w):
cols.append(" ( " + " ".join("( %s )" % " ".join(g(float(v)) for v in ctrl[j][i]) for j in range(h)) + " )")
return '{\n %s\n {\n "textures/test/%s"\n ( %s )\n (\n%s\n )\n }\n}' % (kind, kind, " ".join(str(x) for x in info), "\n".join(cols))
def text(version):
out = ["Version %d" % version, "// entity 0", "{", '"classname" "worldspawn"']
for i, b in enumerate(world):
out.append(brush_text(i, b))
out.append("// primitive 2\n{\nbrushDef\n{\n( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) common/caulk 0 0 0\n}\n}")
out.append("}")
out += ["// entity 1", "{", '"classname" "func_static"', '"name" "crate"', '"origin" "0 300 16"', '"model" "crate"',
brush_text(0, crate), patch_text("patchDef3", p3, [3, 3, 4, 4, 0, 0, 0]),
patch_text("patchDef2", p2, [5, 3, 0, 0, 0]), "}"]
out += ["// entity 2", "{", '"classname" "light"', '"name" "light_1"', '"origin" "10 20 30"',
'"rotation" "0 1 0 -1 0 0 0 0 1"', '"light_radius" "300 300 300"', "}"]
out += ["// entity 3", "{", '"classname" "info_player_start"', '"name" "info_player_start_1"', '"origin" "-64 0 1"',
'"angle" "90"', "}"]
return "\n".join(out) + "\n"
ref = json.loads(ctx.python(["numpy", "scipy"], REF, input=json.dumps(
[{"inside": b["inside"], "sides": [{"plane": s["plane"], "material": s["material"]} for s in b["sides"]]} for b in brushes]).encode()))
vols = [r["volume"] for r in ref if "volume" in r]
areas = [r for r in ref if "volume" not in r]
k = 0
for b in brushes:
for s in b["sides"]:
assert areas[k]["material"] == s["material"]
sides.append({"material": s["material"], "area": areas[k]["area"], "vecs": s["vecs"]})
k += 1
gw, gh, pts = bezier([[c for c in row] for row in p3], 3, 3, 4, 4)
ctx.emit("d3map", {"doom3": text(2), "quake4": text(3), "sides": sides, "volumes": vols,
"patch3": {"width": gw, "height": gh, "points": pts},
"patch2": {"corners": list(p2[0][0][:3]) + list(p2[2][4][:3])}})
+30 -2
View File
@@ -1,5 +1,5 @@
# Docs inputs for the id Tech sections of the scene / tilemap / archive pages (tools/fixtures.py; tools/docs_check.py
# explains the inputs), made by the format fixtures' writers: the hand-written UDMF room (udmf.py), a dmap-layout
# explains the inputs), a hand-written Doom 3 .map, and inputs made by the format fixtures' writers: the hand-written UDMF room (udmf.py), a dmap-layout
# .proc (proc.py), a VSWAP and a one-map GAMEMAPS + MAPHEAD with a random palette (wolf3d.py).
# tests/gen/docs_idtech.luau { files = { [name] = bytes }, vars = { udmfText = "room.udmf", ... } }
import importlib.util, os, struct
@@ -7,6 +7,33 @@ import importlib.util, os, struct
INPUTS = ["tools/docs_check.py", "tools/fixtures/udmf.py", "tools/fixtures/proc.py", "tools/fixtures/wolf3d.py"]
D3MAP = """Version 2
// entity 0
{
"classname" "worldspawn"
// primitive 0
{
brushDef3
{
( 0 0 -1 -16 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/base_floor/a_sofloor1_d" 0 0 0
( 0 0 1 0 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/base_floor/a_sofloor1_d" 0 0 0
( 0 -1 0 -128 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/common/caulk" 0 0 0
( 1 0 0 -128 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/common/caulk" 0 0 0
( 0 1 0 -128 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/common/caulk" 0 0 0
( -1 0 0 -128 ) ( ( 0.0078125 0 0 ) ( 0 0.0078125 0 ) ) "textures/common/caulk" 0 0 0
}
}
}
// entity 1
{
"classname" "info_player_start"
"name" "info_player_start_1"
"origin" "0 0 1"
"angle" "90"
}
"""
def mod(ctx, name):
spec = importlib.util.spec_from_file_location("fx_" + name, os.path.join(ctx.root, "tools", name + ".py") if name == "docs_check"
else os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
@@ -37,6 +64,7 @@ def generate(ctx):
body += struct.pack("<iiiHHHHH16s", *starts, *(len(pl) for pl in planes), 16, 16, b"Tiny map") + b"!ID!"
files["GAMEMAPS.WL6"] = bytes(body)
files["MAPHEAD.WL6"] = struct.pack("<H100i", W.TAG, head, *([0] * 99))
vars = {"udmfText": "room.udmf", "procText": "level.proc", "vswapBytes": "VSWAP.WL6", "wolfPaletteBytes": "wolf.pal",
files["room.map"] = D3MAP.encode()
vars = {"d3mapText": "room.map", "udmfText": "room.udmf", "procText": "level.proc", "vswapBytes": "VSWAP.WL6", "wolfPaletteBytes": "wolf.pal",
"gamemapsBytes": "GAMEMAPS.WL6", "mapheadBytes": "MAPHEAD.WL6"}
check.emit_inputs(ctx, files, vars)