model/bsp: BSP2 / 2PSB, Quake 1 / 2 / Half-Life lightmap atlas (TEXCOORD_1, .lit), tree / hulls / visibility in meta.bsp
Bsp.leafAt / contents / pvs point queries; tested on ericw-tools qbsp + vis output in all three Quake 1 layouts.
This commit is contained in:
1 parent
2704653a3e
commit
67e116a6c6
13 files changed
+2556
-375
No files matched your search
+50
-1
@@ -5,7 +5,7 @@
|
||||
# miptex(name, w, h, rng, palette=None) -> bytes (four mip levels of random indices; WAD3 / BSP 30 add the palette)
|
||||
# compile(ctx, text, files, qbsp_args, light_args=None, vis=False) -> { bsp, obj?, log }
|
||||
# read_bsp(data) -> dict of the parsed lumps
|
||||
import os, shutil, struct, subprocess, tempfile
|
||||
import math, os, shutil, struct, subprocess, tempfile
|
||||
|
||||
Q1_LUMPS = ["entities", "planes", "miptex", "vertices", "visibility", "nodes", "texinfo", "faces", "lighting",
|
||||
"clipnodes", "leaves", "marksurfaces", "edges", "surfedges", "models"]
|
||||
@@ -151,3 +151,52 @@ def face_plane(b, f):
|
||||
if f["side"]:
|
||||
return (-nx, -ny, -nz, -d)
|
||||
return (nx, ny, nz, d)
|
||||
|
||||
|
||||
def write_bsp(b, lumps):
|
||||
# reassembles a Quake 1 family BSP (b from read_bsp: its variant decides the header) from raw lumps
|
||||
head = {"BSP2": b"BSP2", "2PSB": b"2PSB"}.get(b["variant"]) or struct.pack("<i", int(b["variant"][3:]))
|
||||
off = len(head) + 8 * len(Q1_LUMPS)
|
||||
body, dirs = b"", []
|
||||
for n in Q1_LUMPS:
|
||||
d = lumps[n]
|
||||
dirs.append(struct.pack("<ii", off + len(body), len(d)))
|
||||
body += d + b"\0" * ((4 - len(d) % 4) % 4)
|
||||
return head + b"".join(dirs) + body
|
||||
|
||||
|
||||
def extents(b, f):
|
||||
# CalcSurfaceExtents: lightmap size in luxels (16 texels each) and the texture mins
|
||||
ti = b["texinfo"][f["texinfo"]]
|
||||
pts = face_points(b, f)
|
||||
out = []
|
||||
for j in range(2):
|
||||
vals = [p[0] * ti[j * 4] + p[1] * ti[j * 4 + 1] + p[2] * ti[j * 4 + 2] + ti[j * 4 + 3] for p in pts]
|
||||
lo, hi = math.floor(min(vals) / 16), math.ceil(max(vals) / 16)
|
||||
out.append(hi - lo + 1)
|
||||
return out
|
||||
|
||||
|
||||
def light(b, rng, rgb=False):
|
||||
# fills the lighting lump with random luxels at the extents of every face that is not TEX_SPECIAL (the nix light
|
||||
# has no ray tracing backend); returns the new BSP bytes and { face: (w, h, ofs) }
|
||||
big = b["variant"] in ("BSP2", "2PSB")
|
||||
faces = bytearray(b["lumps"]["faces"])
|
||||
size = 28 if big else 20
|
||||
data = b""
|
||||
out = {}
|
||||
for i, f in enumerate(b["faces"]):
|
||||
ti = b["texinfo"][f["texinfo"]]
|
||||
o = i * size + (20 if big else 12)
|
||||
if ti[9] & 1:
|
||||
faces[o:o + 8] = bytes([255, 255, 255, 255]) + struct.pack("<i", -1)
|
||||
continue
|
||||
w, h = extents(b, f)
|
||||
ofs = len(data)
|
||||
data += bytes(rng.randrange(256) for _ in range(w * h * (3 if rgb else 1)))
|
||||
faces[o:o + 8] = bytes([0, 255, 255, 255]) + struct.pack("<i", ofs)
|
||||
out[i] = (w, h, ofs)
|
||||
lumps = dict(b["lumps"])
|
||||
lumps["faces"] = bytes(faces)
|
||||
lumps["lighting"] = data
|
||||
return write_bsp(b, lumps), out
|
||||
Reference in new issue
Block a user