125 lines
6.8 KiB
Python
125 lines
6.8 KiB
Python
# Doom 3 .proc fixtures (tools/fixtures.py). Doom 3's data is not redistributable, so the files are written here in
|
|
# dmap's layout (the Doom 3 GPL source's WriteOutputFile, read for the text layout; no code taken): two area models
|
|
# made of boxes, an inline entity model, portals, BSP nodes and a shadow model; and a Quake 4 "PROC" "4" file with
|
|
# vertex colours. The reference is this script's own geometry: per model and surface the vertex count, CRC-32 of the
|
|
# positions / texcoords as f32 LE, and of the indices as u32 LE with every triangle turned counter-clockwise.
|
|
# tests/gen/proc.luau { doom3 = { text, models = { { name, surfaces = { { material, verts, pos, uv, idx } } } },
|
|
# portals, nodes, areas, shadow = { name, verts, indices } }, quake4 = { ... } }
|
|
import struct, zlib
|
|
|
|
|
|
def box(rng, x0, y0, z0, x1, y1, z1):
|
|
# 6 quads; triangles clockwise seen from outside (id Tech 4's front faces)
|
|
c = [(x0, y0, z0), (x1, y0, z0), (x1, y1, z0), (x0, y1, z0), (x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1)]
|
|
faces = [((0, 1, 2, 3), (0, 0, -1)), ((4, 7, 6, 5), (0, 0, 1)), ((0, 4, 5, 1), (0, -1, 0)), ((1, 5, 6, 2), (1, 0, 0)),
|
|
((2, 6, 7, 3), (0, 1, 0)), ((3, 7, 4, 0), (-1, 0, 0))]
|
|
verts, idx = [], []
|
|
for quad, n in faces:
|
|
b = len(verts)
|
|
for k, vi in enumerate(quad):
|
|
x, y, z = c[vi]
|
|
verts.append((x, y, z, (k & 1) * 2.5 + rng.randint(0, 3) * 0.25, (k >> 1) * -1.5 + 0.0, n[0], n[1], n[2]))
|
|
# clockwise triangles about n, whichever way the corners run
|
|
(ax, ay, az), (bx, by, bz), (cx, cy, cz) = c[quad[0]], c[quad[1]], c[quad[2]]
|
|
ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
|
|
ccw = (uy * vz - uz * vy) * n[0] + (uz * vx - ux * vz) * n[1] + (ux * vy - uy * vx) * n[2] > 0
|
|
idx += [b, b + 2, b + 1, b, b + 3, b + 2] if ccw else [b, b + 1, b + 2, b, b + 2, b + 3]
|
|
return verts, idx
|
|
|
|
|
|
def fmt(v):
|
|
return ("%d" % v) if float(v).is_integer() else ("%g" % v)
|
|
|
|
|
|
def write(models, portals, nodes, areas, shadow, header, colors):
|
|
out = [header, ""]
|
|
for m in models:
|
|
out.append('model { /* name = */ "%s" /* numSurfaces = */ %d' % (m["name"], len(m["surfaces"])))
|
|
out.append("")
|
|
for si, s in enumerate(m["surfaces"]):
|
|
out.append('/* surface %d */ { "%s" /* numVerts = */ %d /* numIndexes = */ %d' % (si, s["material"], len(s["verts"]), len(s["idx"])))
|
|
line = []
|
|
for v in s["verts"]:
|
|
vals = list(v) + (s["color"] if colors else [])
|
|
line.append("( " + " ".join(fmt(x) for x in vals) + " )")
|
|
if len(line) == 3:
|
|
out.append(" ".join(line))
|
|
line = []
|
|
if line:
|
|
out.append(" ".join(line))
|
|
for k in range(0, len(s["idx"]), 15):
|
|
out.append(" ".join(str(i) for i in s["idx"][k:k + 15]) + " ")
|
|
out.append("}")
|
|
out.append("")
|
|
out.append("}")
|
|
out.append("")
|
|
out.append("shadowModel { /* name = */ \"%s\"" % shadow["name"])
|
|
out.append("/* numVerts = */ %d /* noCaps = */ 0 /* noFrontCaps = */ 0 /* numIndexes = */ %d /* planeBits = */ 59"
|
|
% (len(shadow["verts"]) // 3, len(shadow["indices"])))
|
|
out.append(" ".join("( %s %s %s )" % tuple(fmt(x) for x in shadow["verts"][k:k + 3]) for k in range(0, len(shadow["verts"]), 3)))
|
|
out.append(" ".join(str(i) for i in shadow["indices"]))
|
|
out.append("}")
|
|
out.append("")
|
|
out.append("interAreaPortals { /* numAreas = */ %d /* numIAP = */ %d" % (areas, len(portals)))
|
|
out.append("")
|
|
out.append("/* interAreaPortal format is: numPoints positiveSideArea negativeSideArea ( point) ... */")
|
|
for i, p in enumerate(portals):
|
|
pts = " ".join("( %s %s %s )" % tuple(fmt(x) for x in p["points"][k:k + 3]) for k in range(0, len(p["points"]), 3))
|
|
out.append("/* iap %d */ %d %d %d %s " % (i, len(p["points"]) // 3, p["positive"], p["negative"], pts))
|
|
out.append("}")
|
|
out.append("")
|
|
out.append("nodes { /* numNodes = */ %d" % len(nodes))
|
|
out.append("")
|
|
out.append("/* node format is: ( planeVector ) positiveChild negativeChild */")
|
|
out.append("/* a child number of 0 is an opaque, solid area */")
|
|
out.append("/* negative child numbers are areas: (-1-child) */")
|
|
for i, n in enumerate(nodes):
|
|
out.append("/* node %d */ ( %s ) %d %d " % (i, " ".join(fmt(x) for x in n["plane"]), n["positive"], n["negative"]))
|
|
out.append("}")
|
|
return "\n".join(out) + "\n"
|
|
|
|
|
|
def ref(models):
|
|
res = []
|
|
for m in models:
|
|
ss = []
|
|
for s in m["surfaces"]:
|
|
pos = b"".join(struct.pack("<3f", *v[0:3]) for v in s["verts"])
|
|
uv = b"".join(struct.pack("<2f", *v[3:5]) for v in s["verts"])
|
|
ccw = []
|
|
for k in range(0, len(s["idx"]), 3):
|
|
a, b, c = s["idx"][k:k + 3]
|
|
ccw += [a, c, b]
|
|
ss.append({"material": s["material"], "verts": len(s["verts"]), "pos": zlib.crc32(pos), "uv": zlib.crc32(uv),
|
|
"idx": zlib.crc32(struct.pack("<%dI" % len(ccw), *ccw)), "tris": len(ccw) // 3})
|
|
res.append({"name": m["name"], "surfaces": ss})
|
|
return res
|
|
|
|
|
|
def level(rng, colors):
|
|
models = []
|
|
for a in range(2):
|
|
surfs = []
|
|
for k in range(2 + a):
|
|
v, i = box(rng, a * 512 + k * 64, -128, 0, a * 512 + k * 64 + 48, 128 + rng.randint(0, 4) * 8, 96.5)
|
|
surfs.append({"material": "textures/base_wall/lfwall%d" % (k + 1), "verts": v, "idx": i,
|
|
"color": [255, 128, 0, 255]})
|
|
models.append({"name": "_area%d" % a, "surfaces": surfs})
|
|
v, i = box(rng, 100, 0, 8, 116, 16, 24)
|
|
models.append({"name": "func_static_1", "surfaces": [{"material": "models/mapobjects/crate", "verts": v, "idx": i, "color": [10, 20, 30, 40]}]})
|
|
portals = [{"positive": 1, "negative": 0, "points": [448, -128, 0, 448, 128, 0, 448, 128, 96, 448, -128, 96]}]
|
|
nodes = [{"plane": [1, 0, 0, -448], "positive": 1, "negative": -1}, {"plane": [0, 0, 1, 0], "positive": 0, "negative": -2}]
|
|
shadow = {"name": "_prelight_light_1", "verts": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], "indices": [0, 1, 2, 0, 2, 3]}
|
|
return models, portals, nodes, 2, shadow
|
|
|
|
|
|
def generate(ctx):
|
|
rng = ctx.rng(31)
|
|
out = {}
|
|
for key, header, colors in [("doom3", "mapProcFile003", False), ("quake4", 'PROC "4"', True)]:
|
|
models, portals, nodes, areas, shadow = level(rng, colors)
|
|
out[key] = {"text": write(models, portals, nodes, areas, shadow, header, colors), "models": ref(models),
|
|
"portals": portals, "nodes": nodes, "areas": areas,
|
|
"shadow": {"name": shadow["name"], "verts": len(shadow["verts"]) // 3, "indices": len(shadow["indices"])}}
|
|
ctx.emit("proc", out)
|