Files
openclaw-dnd-city-visualiza…/scripts/render_plate.py
T
rick 3d50023cfa Toolkit: segment, mass, render plates, batch, gate, matrices, cost, smoke test
Generalized from the render lanes' proven tools: map segmentation, 2.5D massing by
wealth zone, a CPU ray marcher with analytic landmarks and true-scale proxy people,
prompt composition from blocks, a self-calibrating batch submitter with a renderer
adapter point, the plate-vs-frame gates (drift, ECC, landmark region, skyline and
invented structures, figure brackets, corner sweep), matrix job builders, a cost
line, and an end-to-end smoke test on a synthetic town.
2026-10-14 11:39:57 +00:00

621 lines
26 KiB
Python

#!/usr/bin/env python3
"""Template-plate renderer: a CPU ray marcher over the town heightfield + optional terrain + analytic solids.
For each camera in views.json it writes, under --out:
<V>_mass.png the plate (IMAGE 1 for the re-surfacing model)
<V>_depth.npy ray distance per pixel (m)
<V>_oid.png 16-bit solid id + 1 (0 = heightfield/sky); the figure-scale gate uses it
<V>_cls.png class id per pixel (sky 0, street 1, roof 2, wall 3, tree 4, water 5, deck 6, field 7,
forest 8, rock 9, snow 10, solid 12)
<V>_cam.json camera (so the gates and sheets can project world points)
<V>_people.json every proxy person: screen foot/head rows, distance, px per meter
<V>_landmarks.json projected landmark points from the scene file
views.json: {"<V>": {"pos":[x,y,z], "yaw":deg (0 = north, clockwise), "pitch":deg, "hfov":deg, "shift":0..0.2,
"sun":"day"|"sunset"|"morning", "scene":"scene.json" (optional per-view scene)}}
A camera on the ground: z = ground + 1.65. Vertical lens shift keeps verticals vertical on pitched-up views.
usage: render_plate.py --config citykit.json --views views.json --out plates V1 [V2 ...] [--w 2048]
Runtime: a 2048x1152 plate takes 1-15 minutes on a 32-core CPU depending on how much terrain the rays cross.
Render views in parallel processes.
"""
import argparse
import json
import os
import time
import cv2
import numpy as np
from PIL import Image
import scene_solids as SC
from citykit import MapFrame, bil, load_config, resolve, save_json
SUNC = np.array([1.0, 0.93, 0.82])
AMB = np.array([0.78, 0.85, 0.97])
PAL = {
"street": (150, 142, 124), "field": (112, 128, 82), "meadow": (124, 132, 90), "forest": (62, 80, 58),
"rock": (122, 120, 114), "snow": (232, 236, 240), "water": (58, 82, 92), "deck": (112, 92, 70), "tree": (72, 96, 58),
"wall1": (124, 106, 86), "wall2": (150, 136, 116), "wall3": (178, 174, 166), "wall4": (184, 180, 172),
"roof1": (98, 108, 74), "roof2": (92, 88, 86), "roof3": (80, 84, 92), "roof4": (78, 82, 90),
}
CLS_ID = dict(sky=0, street=1, roof=2, wall=3, tree=4, water=5, deck=6, field=7, forest=8, rock=9, snow=10, solid=12)
SUNS = {"day": dict(az=248.0, el=24.0), "sunset": dict(az=297.0, el=1.5), "morning": dict(az=128.0, el=29.0)}
def sun_vec(kind, suns):
s = suns.get(kind, SUNS[kind])
az, el = np.radians(s["az"]), np.radians(s["el"])
return np.array([np.sin(az) * np.cos(el), np.cos(az) * np.cos(el), np.sin(el)])
class World:
"""Heightfield world: optional terrain grid (m above ground plane) + the town block model on top of it."""
def __init__(self, cfg):
self.cfg = cfg
t = cfg.get("terrain")
if t and t.get("path"):
T = np.load(resolve(cfg, t["path"]))
self.tH = T["H"].astype(np.float32)
self.tc, self.tx0, self.ty1 = float(T["cell"]), float(T["x0"]), float(T["y1"])
self.snowline = float(t.get("snowline_m", 1e9))
else:
self.tH = None
self.snowline = 1e9
m = cfg["map"]
Z = np.load(resolve(cfg, cfg["massing"]["town_npz"]))
self.HR = Z["HR"].astype(np.float32)
self.SDF = Z["SDF"].astype(np.float32)
self.EAV = Z["EAV"].astype(np.float32)
self.CLS, self.TINT, self.ZONE, self.DECK = Z["CLS"], Z["TINT"], Z["ZONE"], Z["DECK"]
self.WD = Z["WD"].astype(np.float32)
self.TREE = Z["TREE"].astype(np.float32)
self.win = Z["win"].astype(float)
self.zc = float(Z["cell"])
self.zh, self.zw = self.HR.shape
self.frame = MapFrame(m["meters_per_px"], m.get("origin_px", [0, 0]))
self.ext = cfg.get("world_extent_m", [-20000, 20000, -20000, 20000])
# 20 m bound tiles over the whole extent: max height per tile, so rays skip open air
self.BT = 20.0
x0, x1, y0, y1 = self.ext
nx, ny = int(np.ceil((x1 - x0) / self.BT)), int(np.ceil((y1 - y0) / self.BT))
mm = np.zeros((ny, nx), np.float32)
if self.tH is not None:
k = max(1, int(round(self.BT / self.tc)))
hh, ww = self.tH.shape
Hp = np.pad(self.tH, ((0, (-hh) % k), (0, (-ww) % k)), constant_values=-50)
tm = Hp.reshape(Hp.shape[0] // k, k, Hp.shape[1] // k, k).max(axis=(1, 3))
r0 = int(round((y1 - self.ty1) / self.BT))
c0 = int(round((self.tx0 - x0) / self.BT))
rs, cs = slice(max(r0, 0), min(r0 + tm.shape[0], ny)), slice(max(c0, 0), min(c0 + tm.shape[1], nx))
mm[rs, cs] = np.maximum(mm[rs, cs], tm[rs.start - r0:rs.stop - r0, cs.start - c0:cs.stop - c0])
town_h = np.maximum(np.where(self.SDF < 0.6, self.HR, 0), self.TREE)
town_h = np.maximum(town_h, self.DECK * 1.5)
th = cv2.dilate(town_h, np.ones((3, 3), np.uint8))
bc = max(1, int(np.ceil(self.BT / self.zc)))
Hz = np.pad(th, ((0, (-self.zh) % bc), (0, (-self.zw) % bc)))
tb = Hz.reshape(Hz.shape[0] // bc, bc, Hz.shape[1] // bc, bc).max(axis=(1, 3))
ty, tx = np.mgrid[0:ny, 0:nx]
Xc = x0 + (tx + 0.5) * self.BT
Yc = y1 - (ty + 0.5) * self.BT
r, c = self.town_rc(Xc, Yc)
bi = np.clip((r / bc).astype(int), 0, tb.shape[0] - 1)
bj = np.clip((c / bc).astype(int), 0, tb.shape[1] - 1)
inside = (r > -bc) & (c > -bc) & (r < self.zh + bc) & (c < self.zw + bc)
add = np.where(inside, tb[bi, bj], 0)
base_t = self.terrain(Xc.ravel(), Yc.ravel()).reshape(ny, nx) if self.tH is not None else 0
mm = np.maximum(mm, base_t + cv2.dilate(add.astype(np.float32), np.ones((3, 3), np.uint8)))
self.mm_base = cv2.dilate(mm.astype(np.float32), np.ones((3, 3), np.uint8)) + 0.6
self.mm = self.mm_base.copy()
self.hmax = float(self.mm.max()) + 1
def add_solids_to_bounds(self):
self.mm = self.mm_base.copy()
x0, x1, y0, y1 = self.ext
for S in SC.SOLIDS:
lo, hi = S["lo"], S["hi"]
c0 = int((lo[0] - x0) / self.BT)
c1 = int((hi[0] - x0) / self.BT)
r0 = int((y1 - hi[1]) / self.BT)
r1 = int((y1 - lo[1]) / self.BT)
r0, c0 = max(r0 - 1, 0), max(c0 - 1, 0)
self.mm[r0:r1 + 2, c0:c1 + 2] = np.maximum(self.mm[r0:r1 + 2, c0:c1 + 2], hi[2] + 0.5)
self.hmax = float(self.mm.max()) + 1
def town_rc(self, X, Y):
px, py = self.frame.world_to_px(X, Y)
return (py - self.win[1]) - 0.5, (px - self.win[0]) - 0.5
def in_town(self, r, c):
return (r >= 0) & (c >= 0) & (r < self.zh - 1) & (c < self.zw - 1)
def terrain(self, X, Y):
if self.tH is None:
return np.zeros(np.shape(X), np.float32)
return bil(self.tH, (self.ty1 - Y) / self.tc - 0.5, (X - self.tx0) / self.tc - 0.5)
def town_off(self, r, c):
sdf = bil(self.SDF, r, c)
hr = bil(self.HR, r, c)
tr = bil(self.TREE, r, c)
wd = bil(self.WD, r, c)
hb = np.where(sdf < 0, hr, 0.0)
h = np.maximum(hb, tr)
rn = np.clip(np.round(r).astype(int), 0, self.zh - 1)
cn = np.clip(np.round(c).astype(int), 0, self.zw - 1)
dk = self.DECK[rn, cn] > 0
return np.where(h > 0.05, h, np.where(dk, 1.2, -wd))
def height(self, X, Y):
h = self.terrain(X, Y).astype(np.float64)
r, c = self.town_rc(X, Y)
it = self.in_town(r, c)
if it.any():
h[it] = h[it] + self.town_off(r[it], c[it])
return h
def bound(self, X, Y):
x0, x1, y0, y1 = self.ext
r = ((y1 - Y) / self.BT).astype(np.int32)
c = ((X - x0) / self.BT).astype(np.int32)
ins = (r >= 0) & (c >= 0) & (r < self.mm.shape[0]) & (c < self.mm.shape[1])
return np.where(ins, self.mm[np.clip(r, 0, self.mm.shape[0] - 1), np.clip(c, 0, self.mm.shape[1] - 1)], -1e3)
def camera(v, W=2048, H=1152):
yaw, pitch = np.radians(v["yaw"]), np.radians(v.get("pitch", 0.0))
f = np.array([np.sin(yaw) * np.cos(pitch), np.cos(yaw) * np.cos(pitch), np.sin(pitch)])
r = np.cross(f, [0, 0, 1.0])
r /= np.linalg.norm(r)
u = np.cross(r, f)
F = (W / 2) / np.tan(np.radians(v["hfov"] / 2))
cy = H / 2 + v.get("shift", 0.0) * H
return dict(C=np.array(v["pos"], float), f=f, r=r, u=u, F=F, W=W, H=H, cx=W / 2, cy=cy)
def rays(cam):
W, H = cam["W"], cam["H"]
px, py = np.meshgrid(np.arange(W) + 0.5, np.arange(H) + 0.5)
d = (cam["f"][None, None] + ((px - cam["cx"]) / cam["F"])[..., None] * cam["r"]
- ((py - cam["cy"]) / cam["F"])[..., None] * cam["u"])
d = d.reshape(-1, 3)
return d / np.linalg.norm(d, axis=1, keepdims=True)
def project(cam, P):
q = np.asarray(P, float) - cam["C"]
zc = q @ cam["f"]
x = cam["cx"] + cam["F"] * (q @ cam["r"]) / zc
y = cam["cy"] - cam["F"] * (q @ cam["u"]) / zc
return np.stack([x, y], -1), zc
def march(world, O, d, pix, tmax=None, max_iter=20000, tcap=40000.0):
"""First hit of rays O + t d against the heightfield world. Returns hit, t, iterations."""
N = len(d)
O = np.broadcast_to(np.asarray(O, float), (N, 3))
t = np.zeros(N)
tprev = np.zeros(N)
hit = np.zeros(N, bool)
tm = np.full(N, tcap) if tmax is None else np.minimum(tmax, tcap)
dxy = np.hypot(d[:, 0], d[:, 1]) + 1e-6
x0, x1, y0, y1 = world.ext
ext = (x0 + 30, x1 - 30, y0 + 30, y1 - 30)
it = 0
idx = np.arange(N)
while idx.size and it < max_iter:
P = O[idx] + t[idx, None] * d[idx]
x, y, z = P[:, 0], P[:, 1], P[:, 2]
gone = (((z > world.hmax) & (d[idx, 2] >= 0)) | (t[idx] > tm[idx]) | (x < ext[0]) | (x > ext[1])
| (y < ext[2]) | (y > ext[3]))
keep = ~gone
idx, x, y, z = idx[keep], x[keep], y[keep], z[keep]
if not idx.size:
break
b = world.bound(x, y)
far = z > b
fi = idx[far]
if fi.size:
dz = d[fi, 2]
down = np.where(dz < -1e-6, (z[far] - b[far]) / np.maximum(-dz, 1e-6), 1e9)
step = np.minimum(down, world.BT * 0.7 / dxy[fi])
tprev[fi] = t[fi]
t[fi] += np.maximum(step, 0.3)
ni = idx[~far]
keep2 = np.ones(idx.size, bool)
if ni.size:
hz = world.height(x[~far], y[~far])
hh = z[~far] <= hz
hit[ni[hh]] = True
mi = ni[~hh]
if mi.size:
fine = np.maximum(0.12, t[mi] * pix * 0.9)
gap = z[~far][~hh] - hz[~hh]
step = np.maximum(fine, np.minimum(gap * 0.4, 4.0 + t[mi] * pix * 2))
tprev[mi] = t[mi]
t[mi] += step
kk = np.ones(idx.size, bool)
kk[np.nonzero(~far)[0][hh]] = False
keep2 = kk
idx = idx[keep2]
it += 1
hi = np.nonzero(hit)[0]
a, bb = tprev[hi].copy(), t[hi].copy()
for _ in range(12):
m = (a + bb) / 2
P = O[hi] + m[:, None] * d[hi]
below = P[:, 2] <= world.height(P[:, 0], P[:, 1])
bb = np.where(below, m, bb)
a = np.where(below, a, m)
t[hi] = bb
t[~hit] = np.inf
return hit, t, it
def convex_interval(planes, O, d):
n = planes[:, :3]
dd = planes[:, 3]
den = d @ n.T
num = dd[None] - (O @ n.T if O.ndim == 2 else (O @ n.T)[None])
with np.errstate(divide="ignore", invalid="ignore"):
t = num / den
ent = np.where(den < -1e-12, t, -np.inf)
ext = np.where(den > 1e-12, t, np.inf)
bad = (np.abs(den) <= 1e-12) & (num < 0)
te = ent.max(1)
ie = ent.argmax(1)
tx = ext.min(1)
te[bad.any(1)] = np.inf
return te, tx, ie
def solid_hit(S, O, d, eps=1e-4):
if S["kind"] == "sphere":
oc = (O - S["c"]) if O.ndim == 2 else (O - S["c"])[None]
b = (oc * d).sum(1)
cc = (oc * oc).sum(1) - S["r"] ** 2
disc = b * b - cc
sq = np.sqrt(np.maximum(disc, 0))
te = np.where(disc >= 0, -b - sq, np.inf)
P = (O[None] if O.ndim == 1 else O) + np.where(np.isfinite(te), te, 0)[:, None] * d
return np.where(te > eps, te, np.inf), (P - S["c"]) / S["r"]
te, tx, ie = convex_interval(S["planes"], O, d)
ok = (te <= tx) & (te > eps)
return np.where(ok, te, np.inf), S["planes"][ie, :3]
def screen_rect(cam, S, pad=2):
lo, hi = S["lo"], S["hi"]
P = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])])
p, zc = project(cam, P)
if (zc <= 0.05).any():
if (zc <= 0.05).all():
return None
return 0, 0, cam["W"], cam["H"]
x0, y0 = np.floor(p.min(0)).astype(int) - pad
x1, y1 = np.ceil(p.max(0)).astype(int) + pad
x0, y0 = max(0, x0), max(0, y0)
x1, y1 = min(cam["W"], x1), min(cam["H"], y1)
if x1 <= x0 or y1 <= y0:
return None
return x0, y0, x1, y1
def trace_solids(cam, d):
W, H = cam["W"], cam["H"]
N = W * H
tb = np.full(N, np.inf)
nb = np.zeros((N, 3))
ob = np.full(N, -1, np.int32)
grid = np.arange(N).reshape(H, W)
for S in SC.SOLIDS:
rc = screen_rect(cam, S)
if rc is None:
continue
x0, y0, x1, y1 = rc
idx = grid[y0:y1, x0:x1].ravel()
t, nrm = solid_hit(S, cam["C"], d[idx])
better = t < tb[idx]
if better.any():
j = idx[better]
tb[j] = t[better]
nb[j] = nrm[better]
ob[j] = S["oid"]
return tb, nb, ob
def shadow_solids(P, Ld, eps=0.03):
N = P.shape[0]
e1 = np.cross(Ld, [0, 0, 1.0])
e1 /= np.linalg.norm(e1)
e2 = np.cross(Ld, e1)
u, v, w = P @ e1, P @ e2, P @ Ld
blocked = np.zeros(N, bool)
for S in SC.SOLIDS:
lo, hi = S["lo"], S["hi"]
C8 = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])])
cu, cv, cw = C8 @ e1, C8 @ e2, C8 @ Ld
sel = (~blocked) & (u >= cu.min()) & (u <= cu.max()) & (v >= cv.min()) & (v <= cv.max()) & (w <= cw.max())
if not sel.any():
continue
j = np.nonzero(sel)[0]
t, _ = solid_hit(S, P[j], np.ascontiguousarray(np.broadcast_to(Ld, (j.size, 3))), eps=eps)
blocked[j[np.isfinite(t)]] = True
return blocked
def sky(d, L, kind):
el = np.degrees(np.arcsin(np.clip(d[:, 2], -1, 1)))
t = np.clip(el / 45.0, 0, 1)[:, None]
if kind in ("day", "morning"):
zen, hor = np.array([118, 154, 200], float), np.array([206, 214, 222], float)
else:
zen, hor = np.array([96, 120, 168], float), np.array([232, 200, 170], float)
col = hor * (1 - t ** 0.55) + zen * t ** 0.55
cs = d @ L
col += np.clip(cs, 0, 1)[:, None] ** 12 * (np.array([60, 40, 10]) if kind != "sunset" else np.array([90, 50, 10]))
return col
def hn(a, bb, s):
return np.sin(a * s + 1.9 * np.sin(bb * s * 0.63)) * np.sin(bb * s * 1.21 + 1.4 * np.sin(a * s * 0.49))
def shade(world, cam, d, kind, suns):
W, H = cam["W"], cam["H"]
N = W * H
C = cam["C"]
L = sun_vec(kind, suns)
pix = 1.0 / cam["F"]
t0 = time.time()
ts, ns, os_ = trace_solids(cam, d)
t1 = time.time()
hit_h, th, iters = march(world, C, d, pix, tmax=np.where(np.isfinite(ts), ts + 0.01, 40000.0))
t2 = time.time()
use_h = hit_h & (th < ts)
t = np.where(use_h, th, ts)
hit = np.isfinite(t)
col = sky(d, L, kind)
cls = np.zeros(N, np.uint8)
idx = np.nonzero(hit)[0]
P = C + t[idx, None] * d[idx]
nrm = np.zeros((idx.size, 3))
base = np.zeros((idx.size, 3))
ccls = np.zeros(idx.size, np.uint8)
hh = use_h[idx]
if hh.any():
Ph = P[hh]
x, y, z = Ph[:, 0], Ph[:, 1], Ph[:, 2]
tt = t[idx][hh]
e = np.maximum(0.06, tt * pix * 0.8)
hx = (world.height(x + e, y) - world.height(x - e, y)) / (2 * e)
hy = (world.height(x, y + e) - world.height(x, y - e)) / (2 * e)
n = np.stack([-hx, -hy, np.ones_like(hx)], 1)
n /= np.linalg.norm(n, axis=1, keepdims=True)
zt = world.terrain(x, y)
rel = z - zt
r, c = world.town_rc(x, y)
it = world.in_town(r, c)
b = np.zeros((hh.sum(), 3))
cl = np.zeros(hh.sum(), np.uint8)
# terrain classes by elevation and slope
slope = np.degrees(np.arctan(np.hypot(hx, hy)))
rn = 0.6 * hn(x, y, 0.012) + 0.4 * hn(y, x, 0.031)
fz = 0.5 + 0.5 * hn(x, y, 0.0021)
tcl = np.full(len(x), CLS_ID["field"], np.uint8)
tcl[(zt > 40) | (slope > 8)] = CLS_ID["field"]
tcl[(slope > 10) & (slope < 38) & (zt > 15) & (zt < 780) & (fz > 0.46)] = CLS_ID["forest"]
tcl[(slope > 36 + 8 * rn)] = CLS_ID["rock"]
tcl[(zt > world.snowline + 180 * rn) & (slope < 42)] = CLS_ID["snow"]
for k, v in ((CLS_ID["field"], PAL["meadow"]), (CLS_ID["forest"], PAL["forest"]), (CLS_ID["rock"], PAL["rock"]),
(CLS_ID["snow"], PAL["snow"])):
b[tcl == k] = v
cl[:] = tcl
tex = 0.5 * hn(x, y + z * 0.7, 0.21) + 0.3 * hn(y, x - z, 0.53) + 0.2 * hn(x + z, y, 1.37)
fo = tcl == CLS_ID["forest"]
b[fo] = b[fo] * (0.78 + 0.3 * tex[fo])[:, None]
rk = tcl == CLS_ID["rock"]
b[rk] = b[rk] * (0.8 + 0.25 * tex[rk])[:, None]
if it.any():
ii = np.nonzero(it)[0]
ri, ci = r[ii], c[ii]
sdf = bil(world.SDF, ri, ci)
rn_ = np.clip(np.round(ri).astype(int), 0, world.zh - 1)
cn_ = np.clip(np.round(ci).astype(int), 0, world.zw - 1)
zcls = world.CLS[rn_, cn_]
zone = world.ZONE[rn_, cn_]
tint = world.TINT[rn_, cn_] / 255.0
eav = bil(world.EAV, ri, ci)
trh = bil(world.TREE, ri, ci)
rl = rel[ii]
house = (sdf < 0.35) & (rl > 0.25) & (trh < rl - 0.3)
treep = (trh > 0.3) & (rl > 0.25) & ~house
wall = house & ((rl < eav - 0.05) | (n[ii, 2] < 0.40))
roofp = house & ~wall
ground = ~house & ~treep
bb = b[ii].copy()
cc = cl[ii].copy()
bb[ground] = PAL["street"]
cc[ground] = CLS_ID["street"]
g_open = ground & (zcls == 8)
bb[g_open] = PAL["field"]
cc[g_open] = CLS_ID["field"]
wat = ground & (rl < -0.6)
bb[wat] = PAL["water"]
cc[wat] = CLS_ID["water"]
dk = ground & (world.DECK[rn_, cn_] > 0) & (rl > 0.5)
bb[dk] = PAL["deck"]
cc[dk] = CLS_ID["deck"]
slopey = ground & (zt[ii] > 1.0)
bb[slopey] = b[ii][slopey]
cc[slopey] = cl[ii][slopey]
zk = np.clip(zone, 1, 4)
for zz in (1, 2, 3, 4):
m = wall & (zk == zz)
bb[m] = np.array(PAL["wall%d" % zz]) * (0.92 + 0.16 * tint[m, None])
m = roofp & (zk == zz)
bb[m] = np.array(PAL["roof%d" % zz]) * (0.92 + 0.16 * tint[m, None])
cc[wall] = CLS_ID["wall"]
cc[roofp] = CLS_ID["roof"]
bb[treep] = PAL["tree"]
cc[treep] = CLS_ID["tree"]
if wall.any():
# a window/door rhythm on the walls: the scale cue the re-surfacing model builds real houses from
wi = np.nonzero(wall)[0]
e2 = 0.4
gx_ = bil(world.SDF, ri[wi], ci[wi] + e2) - bil(world.SDF, ri[wi], ci[wi] - e2)
gy_ = bil(world.SDF, ri[wi] + e2, ci[wi]) - bil(world.SDF, ri[wi] - e2, ci[wi])
tv = np.stack([gy_, gx_], 1)
tv /= (np.linalg.norm(tv, axis=1, keepdims=True) + 1e-9)
Pw = Ph[ii][wi]
s_ = Pw[:, 0] * tv[:, 0] - Pw[:, 1] * tv[:, 1]
zz_ = rl[wi]
ev = eav[wi]
st_ = np.floor((zz_ - 0.3) / 2.8)
zin = (zz_ - 0.3) - st_ * 2.8
fw = (s_ / 3.4) % 1.0
win = (fw > 0.38) & (fw < 0.6) & (zin > 1.0) & (zin < 2.0) & (zz_ < ev - 0.6) & (st_ >= 1)
fd = (s_ / 9.3) % 1.0
door = (fd > 0.45) & (fd < 0.56) & (zz_ < 2.2)
gwin = (fw > 0.4) & (fw < 0.58) & (st_ == 0) & (zin > 1.0) & (zin < 1.8) & ~door
cw = bb[wi]
cw[win | gwin] = (54, 54, 58)
cw[door] = (72, 54, 40)
bb[wi] = cw
wn = np.stack([gx_, -gy_, np.zeros_like(gx_)], 1)
wl = np.linalg.norm(wn, axis=1)
good = wl > 1e-4
nn_ = n[ii].copy()
sub = nn_[wi]
sub[good] = wn[good] / wl[good, None]
nn_[wi] = sub
n[ii] = nn_
b[ii] = bb
cl[ii] = cc
nrm[hh] = n
base[hh] = b
ccls[hh] = cl
sh_ = ~hh
if sh_.any():
j = idx[sh_]
nrm[sh_] = ns[j]
oid = os_[j]
bb = np.zeros((j.size, 3))
for o in np.unique(oid):
bb[oid == o] = SC.MAT[SC.SOLIDS[o]["mat"]]
leaf = np.array([SC.SOLIDS[o]["mat"] == "leaf" for o in oid])
if leaf.any():
Pl = P[sh_][leaf]
hx = ((np.floor(Pl[:, 0] * 2.2).astype(np.int64) * 73856093) ^ (np.floor(Pl[:, 1] * 2.2).astype(np.int64) * 19349663)
^ (np.floor(Pl[:, 2] * 2.2).astype(np.int64) * 83492791))
rr = (hx % 1000) / 1000.0
sub = bb[leaf]
sub *= (0.62 + 0.6 * rr)[:, None]
bb[leaf] = sub
base[sh_] = bb
ccls[sh_] = CLS_ID["solid"]
flip = (nrm * d[idx]).sum(1) > 0
nrm[flip] *= -1
t3 = time.time()
lam = np.clip(nrm @ L, 0, 1)
Q = P + nrm * 0.05 + L * 0.02
li = np.nonzero(lam > 0)[0]
shadow = np.ones(idx.size, bool)
if li.size:
bs = shadow_solids(Q[li], L)
rem = li[~bs]
hs, _, _ = march(world, Q[rem], np.ascontiguousarray(np.broadcast_to(L, (rem.size, 3))), pix * 1.5, tcap=12000.0)
sh = np.ones(li.size, bool)
sh[~bs] = hs
shadow[li] = sh
t4 = time.time()
sky_occ = 0.55 + 0.45 * np.clip(nrm[:, 2] * 0.5 + 0.5, 0, 1)
if kind in ("day", "morning"):
amb = AMB[None] * (0.50 * sky_occ)[:, None]
sun = SUNC[None] * (0.80 * lam * ~shadow)[:, None]
else:
amb = np.array([0.66, 0.70, 0.92])[None] * (0.42 * sky_occ)[:, None]
sun = np.array([1.0, 0.70, 0.45])[None] * (0.95 * lam * ~shadow)[:, None]
cc = base * (amb + sun)
wat = ccls == CLS_ID["water"]
if wat.any():
cc[wat] = cc[wat] * 0.7 + sky(np.c_[d[idx][wat][:, :2], -d[idx][wat][:, 2]], L, kind) * 0.3
dist = t[idx]
fog = 1 - np.exp(-dist / (16000.0 if kind != "sunset" else 12000.0))
hz = np.array([200, 208, 218]) if kind != "sunset" else np.array([206, 182, 168])
cc = cc * (1 - fog[:, None]) + hz * fog[:, None]
col[idx] = cc
cls[idx] = ccls
img = col.reshape(H, W, 3).clip(0, 255).astype(np.uint8)
depth = np.where(hit, t, np.inf).reshape(H, W).astype(np.float32)
oid = np.where(hit & ~use_h, os_ + 1, 0).reshape(H, W).astype(np.uint16)
print(" solids %.0fs march %.0fs (%d it) shade %.0fs shadows %.0fs" % (t1 - t0, t2 - t1, iters, t3 - t2, t4 - t3), flush=True)
return img, depth, oid, cls.reshape(H, W)
def people_table(cam):
out = []
for p in SC.PEOPLE:
base = np.array([p["x"], p["y"], p["z0"]])
top = base + [0, 0, p["h"]]
(pb, pt), zc = project(cam, np.stack([base, top]))
if (zc <= 0.3).any():
continue
x = (pb[0] + pt[0]) / 2
if not (0 <= x < cam["W"] and min(pb[1], pt[1]) < cam["H"] and max(pb[1], pt[1]) > 0):
continue
hpx = pb[1] - pt[1]
out.append(dict(label=p["label"], h_m=round(p["h"], 3), dist_m=round(float(zc[0]), 1), x=round(float(x), 1),
y_foot=round(float(pb[1]), 1), y_head=round(float(pt[1]), 1), px_height=round(float(hpx), 1),
px_per_m=round(float(hpx / p["h"]), 2)))
return out
def landmarks(cam, scene):
L = {}
for lm in scene.get("landmarks", []):
p, zc = project(cam, np.array([lm["xyz"]]))
if zc[0] > 0:
L[lm["name"]] = [round(float(p[0, 0]), 1), round(float(p[0, 1]), 1), round(float(zc[0]), 1)]
return L
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("views", nargs="+")
ap.add_argument("--config", required=True)
ap.add_argument("--views-file", default=None, help="defaults to config.views")
ap.add_argument("--out", default=None, help="defaults to config.plates_dir")
ap.add_argument("--w", type=int, default=2048)
ap.add_argument("--sun", default=None, help="override the view's sun: day|sunset|morning")
a = ap.parse_args()
cfg = load_config(a.config)
VIEWS = json.load(open(resolve(cfg, a.views_file or cfg["views"])))
outdir = resolve(cfg, a.out or cfg.get("plates_dir", "plates"))
os.makedirs(outdir, exist_ok=True)
world = World(cfg)
Hpx = a.w * 9 // 16
suns = cfg.get("suns", {})
for k in a.views:
v = VIEWS[k]
t0 = time.time()
SC.reset()
scene_path = resolve(cfg, v.get("scene", cfg.get("scene")))
scene = json.load(open(scene_path)) if scene_path and os.path.exists(scene_path) else {}
SC.build_from_json(scene, ground_fn=world.terrain)
world.add_solids_to_bounds()
cam = camera(v, a.w, Hpx)
d = rays(cam)
kv = a.sun or v.get("sun", "day")
img, depth, oid, cls = shade(world, cam, d, kv, suns)
sfx = "" if a.w == 2048 else "_%d" % a.w
Image.fromarray(img).save(os.path.join(outdir, "%s_mass%s.png" % (k, sfx)))
np.save(os.path.join(outdir, "%s_depth%s.npy" % (k, sfx)), depth)
cv2.imwrite(os.path.join(outdir, "%s_oid%s.png" % (k, sfx)), oid)
Image.fromarray(cls).save(os.path.join(outdir, "%s_cls%s.png" % (k, sfx)))
save_json({kk: vv for kk, vv in cam.items()}, os.path.join(outdir, "%s_cam%s.json" % (k, sfx)))
save_json(people_table(cam), os.path.join(outdir, "%s_people%s.json" % (k, sfx)))
save_json(landmarks(cam, scene), os.path.join(outdir, "%s_landmarks%s.json" % (k, sfx)))
print(k, kv, "%dx%d" % (a.w, Hpx), "done %.0fs" % (time.time() - t0), flush=True)