diff --git a/scripts/ridge-bake/export.py b/scripts/ridge-bake/export.py index cea56499..a11f1d86 100644 --- a/scripts/ridge-bake/export.py +++ b/scripts/ridge-bake/export.py @@ -18,6 +18,7 @@ PLANE_W_MAX = 4096 MAX_CARDS = 6 # per place; a full-frame card decodes to 2 textures x 1280 x 600 x 4 B = 6.1 MB, the decode budget below holds 3 of them and cropping to the row band is what lets a livelier place have more TOTAL_BYTES_MAX = 1_500_000 # every card file of a place together DECODE_BYTES_MAX = 20_000_000 # the cards decoded: sum over cards of 2 textures x w x band h x 4 bytes +SLIDE_PCT = 5 # the runtime's DRIFT_FRAC (src/app/features/scene/ridge/view.ts) in percent of the card width: the most one card may slide against another COVER_TOL = 0.02 # the composite of the cards may differ from the opaque view by this much of a pixel, nowhere more WEBP_QUALITY = 80 SHEET_SKY = (0.80, 0.86, 0.94) # the flat sky of the contact sheet @@ -37,6 +38,7 @@ SCHEMA = {'$schema': 'https://json-schema.org/draft/2020-12/schema', 'title': 'r 'manifest': _obj(schema={'const': 2}, baker_version=_int, bake_id=_str, place=_obj(id=_str, name=_str, lat=_num, lon=_num, altitude_km=_num, eye_m=_num), view=_obj(look_az_deg=_num, window_deg={'const': WINDOW_DEG}, plane_w={'type': 'integer', 'maximum': PLANE_W_MAX}, plane_h=_int, px_per_deg_h=_num, px_per_deg_v=_num, exaggeration=_num, horizon_row=_num, sky_row=_num, near_row=_num), + max_slide_px=dict(_int, description='the largest slide of one card against another the runtime may use, in card pixels; a card holds no pixel that the nearer cards hide at every offset within it'), curvature=_obj(r_earth_m=_num, k_ref=_num), encoding=_obj(log_d_min=_num, log_d_max=_num, normal={'const': 'rt8'}), cards={'type': 'array', 'minItems': 1, 'maxItems': MAX_CARDS, 'items': dict(_obj(top=_int, h=_int, near_m=_numnull, far_m=_numnull, median_m=_num, colour=_file, geometry=_file), @@ -96,6 +98,10 @@ def check_budget(card_bytes, w, bands): decoded = sum(2 * w * h * 4 for h in bands) if decoded > DECODE_BYTES_MAX: raise ValueError(f'the cards decode to {decoded} bytes, above {DECODE_BYTES_MAX}') +def max_slide_px(w): + """MAX_SLIDE_PX: ceil(SLIDE_PCT / 100 x card width) in whole pixels (64 at 1280); the baker cuts from each card what the nearer cards hide at every offset within it.""" + return -(-(w * SLIDE_PCT) // 100) + def bake_id(cards): """First 12 hex of sha256 over the card files in index order, colour then geometry. cards: [(colour bytes, geometry bytes)].""" h = hashlib.sha256() @@ -114,7 +120,7 @@ def write_bake(out, place, view, meta, cards, opaque, default=None): sizes = [len(c['colour']) + len(c['geometry']) for c in cards]; check_budget(sizes, w, bands) check_cover([(c['top'], col[..., 3] / 255.0) for c, (col, _) in zip(cards, dec)], opaque) bid = bake_id([(c['colour'], c['geometry']) for c in cards]); name = f"{place['id']}-{bid}"; out = Path(out); dest = out / name - manifest = dict(schema=2, baker_version=BAKER_VERSION, bake_id=bid, place=place, view=dict(view, window_deg=WINDOW_DEG, plane_w=w), curvature=meta['curvature'], encoding=dict(meta['encoding'], normal='rt8'), + manifest = dict(schema=2, baker_version=BAKER_VERSION, bake_id=bid, place=place, view=dict(view, window_deg=WINDOW_DEG, plane_w=w), max_slide_px=max_slide_px(w), curvature=meta['curvature'], encoding=dict(meta['encoding'], normal='rt8'), cards=[dict(top=c['top'], h=band, near_m=c['near_m'], far_m=c['far_m'], median_m=c['median_m'], colour=dict(file=f'card-{i}-colour.webp', bytes=len(c['colour'])), geometry=dict(file=f'card-{i}-geometry.webp', bytes=len(c['geometry']))) for i, (c, band) in enumerate(zip(cards, bands))], w=w, h=h, bytes=sum(sizes), attribution=ATTRIBUTION) validate(manifest, SCHEMA['$defs']['manifest']); out.mkdir(parents=True, exist_ok=True); tmp = Path(tempfile.mkdtemp(dir=out, prefix='.bake-')) @@ -138,11 +144,13 @@ def bake_place(pid): place = dict(id=p['id'], name=p['name'], lat=p['lat'], lon=p['lon'], altitude_km=p['ground_m'] / 1000.0, eye_m=p['eye_m']) view = dict(look_az_deg=p['az_center'], plane_h=ridge.H, px_per_deg_h=ridge.HPX_DEG, px_per_deg_v=v['px_per_deg'], exaggeration=v['exaggeration'], horizon_row=v['horizon_row'], sky_row=v['sky_row'] * ridge.H, near_row=v['near_row'] * ridge.H) meta = dict(curvature=dict(r_earth_m=ridge.R_EARTH, k_ref=ridge.KREF), encoding=dict(log_d_min=math.log(d[0]), log_d_max=math.log(d[-1]))) - return dict(place=place, view=view, meta=meta, cards=ridge.cards(d, aw, hw, sm, nrm), seams=sm, cov=cov, opaque=ridge.opaque(aw), edges=ridge.log_edges(d)) + return dict(place=place, view=view, meta=meta, cards=ridge.cards(d, aw, hw, sm, nrm, slide_px=max_slide_px(ridge.WIN)), splits_km=list(p['card_split_km']), seams=sm, cov=cov, opaque=ridge.opaque(aw), edges=ridge.log_edges(d)) def pack(bake): """The cards of a bake as bytes plus their manifest facts: median seam depths either side, median distance of the card's solid pixels.""" - enc = bake['meta']['encoding']; sm = bake['seams']; out = [] + enc = bake['meta']['encoding']; sm = bake['seams']; out = []; sp = bake['splits_km'] + for i, c in enumerate(bake['cards']): + if c['alpha'].shape[0] == 0: raise ValueError(f"card {i} is empty: it lies between the splits {' and '.join(f'{sp[j]:g} km' for j in (i - 1, i) if 0 <= j < len(sp))} (card_split_km {sp}) and holds no terrain nothing nearer hides; move or remove a split") for i, c in enumerate(bake['cards']): solid = c['alpha'] >= 0.5; out.append(dict(top=c['top'], colour=encode_colour(c['albedo'], c['alpha']), geometry=encode_geometry(c['n_r'], c['n_t'], c['logd'], enc), near_m=float(np.median(sm[i - 1])) if i else None, far_m=float(np.median(sm[i])) if i < len(sm) else None, median_m=float(np.exp(np.median(c['logd'][solid]))) if solid.any() else 0.0)) diff --git a/scripts/ridge-bake/ridge.py b/scripts/ridge-bake/ridge.py index 3eaa8cc1..0b941742 100644 --- a/scripts/ridge-bake/ridge.py +++ b/scripts/ridge-bake/ridge.py @@ -189,7 +189,7 @@ def set_view(skyline, sky_row=0.04, near_row=0.66): COV_BINS = 160 # log-distance bins between the first and the last sample SIG_B = 1.5 # blur of the coverage along depth (bins): seams settle in gaps SEAM_LAM, SEAM_UNARY = 2.0, 0.25 # forward energy: a seam that moves a bin between neighbouring columns pays SEAM_LAM per bin plus every visible row it sweeps; one that sits in visible terrain pays SEAM_UNARY x those rows (cross-fadable) -SEAM_PRIOR = 0.1 # the authored depth is a weak pull, rows per nat^2: a stronger one makes a seam hop across a ridge it cannot follow (2.0 hopped a 4 to 15 km ridge between two columns, a tear); a weaker one lets a seam without the swept term follow it (the test pins both sides) +SEAM_PRIOR = 0.02 # the authored depth is a faint pull, rows per nat^2 (the test pins the window: six synthetic receding ridges tear at 0.1, and without the swept-rows term at 0.02 they tear again) SEAM_SEP = 0.4 # seams at least this far apart in log-distance (nat) XF = 0.07 # half-width of a seam's cross-fade, nat (about two bins) @@ -200,11 +200,10 @@ def opaque(a): p = 1.0 / PXDEG_V; lo = rowang - 0.5 * p; return np.clip((np.asarray(a, np.float64).max(0)[None, :] - lo[:, None]) / p, 0, 1) def coverage(d, a, nb=COV_BINS): - """(nb, n): rows of the opaque view first revealed by the samples of each log-distance bin, per column. The rows below the first sample (nearer than it) count in bin 0. Needs set_view.""" + """(nb, n): rows of the opaque view first revealed by the samples of each log-distance bin, per column. The first sample reveals every row from the frame bottom up to its angle, so the rows below it (nearer than it) count in bin 0. Needs set_view.""" a = np.asarray(a, np.float64); edges = log_edges(d, nb); kb = np.clip(np.searchsorted(edges, np.log(d), 'right') - 1, 0, nb - 1) runmax = np.maximum.accumulate(a, axis=0); prev = np.vstack([np.full((1, a.shape[1]), BOTTOM_DEG), runmax[:-1]]) C = np.zeros((nb, a.shape[1])); np.add.at(C, kb, np.clip(np.minimum(a, TOP_DEG) - np.maximum(prev, BOTTOM_DEG), 0, None) * PXDEG_V) - C[0] += np.clip(np.minimum(a[0], TOP_DEG) - BOTTOM_DEG, 0, None) * PXDEG_V return C def _dt(f, u): @@ -235,11 +234,19 @@ def albedo(h, n_up): t = lambda a, b: np.clip((h - a) / (b - a), 0, 1)[..., None]; c = np.asarray(GRASS) + (np.asarray(FOREST) - GRASS) * t(150, 900); c = c + (np.asarray(ROCK) - c) * t(1300, 2000) c = c + (np.asarray(ROCK) - c) * np.clip((0.93 - n_up) / 0.25, 0, 1)[..., None]; return c + (np.asarray(SNOW) - c) * (t(1900, 2300) * np.clip((n_up - 0.55) / 0.3, 0, 1)[..., None]) -def cards(d, a, h, seams, nrm=None): +def _erode(b, m): + """b (rows, n) bool: True where b holds at every column within +-m; a column whose window leaves the frame is not held (the slide would show what is beyond).""" + n = b.shape[1]; c = np.concatenate([np.zeros((b.shape[0], 1), int), np.cumsum(b, axis=1)], axis=1); x = np.arange(n); lo = x - m; hi = x + m + 1; ok = (lo >= 0) & (hi <= n) + return ok[None, :] & ((c[:, np.clip(hi, 0, n)] - c[:, np.clip(lo, 0, n)]) == 2 * m + 1) + +def cards(d, a, h, seams, nrm=None, *, slide_px): """K+1 cards, nearest first, from the angles a and heights h (samples, n) of n columns and the (K, n) seams (m). Card i holds per pixel the FIRST VISIBLE hit at or beyond seam i-1 (less XF): layered depth, so a farther card keeps the terrain a nearer ridge hides. Its alpha is that pixel's coverage (a crest row holds the share of its angular span under the card's running maximum) times 1 - smoothstep over seam i +- XF in log-distance of the hit; card 0 owns the rows below the first sample, the last card has no far edge. Alphas are then made nested so the alpha-over composite of all cards is exactly the opaque coverage (a row two cards both hold partly is not counted twice). Distance and normal are interpolated between sample k-1 and k by angle. Each card is cropped to the rows where its 8-bit alpha is not zero. + Reveal-bounded: card k then loses every pixel that the opaque pixels of the nearer cards (after their own loss) hide at every horizontal offset within +-slide_px, even if any single card slides by that much (covered by at least two + nearer cards, or by one whose own coverage holds across the slide, and by the union of nearer cards at every offset of its own slide), so no slide the runtime may use reveals what was cut; the composite at rest is unchanged. + slide_px None cuts nothing (the reference for tests). nrm is normals(d, h) of the same columns (default: computed from h as given, so pass it for a window cut from the ring). Each card: dict(top, alpha (h,n), albedo (h,n,3), n_r, n_t, logd (ln metres) (h,n)).""" d = np.asarray(d, np.float64); a = np.asarray(a, np.float64); h = np.asarray(h, np.float64); seams = np.asarray(seams, np.float64); nk, n = a.shape; nc = len(seams) + 1 n_r, n_t, n_u = nrm if nrm is not None else normals(d, h); ld = np.broadcast_to(np.log(d)[:, None], (nk, n)); p = 1.0 / PXDEG_V; lo = rowang - 0.5 * p @@ -256,9 +263,12 @@ def cards(d, a, h, seams, nrm=None): F[i, :, x] = np.where(hit, np.clip((rm[-1] - lo) / p, 0, 1), 0); lh = mix(ld) G[i, :, :, x] = np.where(hit, np.stack([lh, mix(h), mix(n_r), mix(n_t), mix(n_u)]), 0) if i < nc - 1: s = np.clip((lh - (math.log(seams[i, x]) - XF)) / (2 * XF), 0, 1); X[i, :, x] = 1 - s * s * (3 - 2 * s) - out = []; A = np.zeros((H, n)) + out = []; A = np.zeros((H, n)); solid = []; held = [] # solid: the nearer cards' opaque pixels (after their own loss); held: where each still holds across the slide for i in range(nc): part = np.where(F[i] < 1, np.clip((np.maximum(A, F[i]) - A) / np.maximum(1 - A, 1e-6), 0, 1), F[i]); al = np.where(np.rint(X[i] * part * 255) > 0, X[i] * part, 0.0); A = 1 - (1 - A) * (1 - al) + if slide_px is not None and solid: + cnt = np.sum(solid, axis=0); alone = np.any([s & e for s, e in zip(solid, held)], axis=0); lost = _erode(cnt > 0, slide_px) & ((cnt >= 2) | alone); al = np.where(lost, 0.0, al); G[i] = np.where(lost[None], 0.0, G[i]) + sd = np.rint(al * 255) == 255; solid.append(sd); held.append(_erode(sd, slide_px or 0)) rows = np.flatnonzero(al.max(1) > 0) if not len(rows): out.append(dict(top=0, alpha=np.zeros((0, n), np.float32), albedo=np.zeros((0, n, 3), np.float32), n_r=np.zeros((0, n), np.float32), n_t=np.zeros((0, n), np.float32), logd=np.zeros((0, n), np.float32))); continue sl = slice(int(rows[0]), int(rows[-1]) + 1); g = G[i, :, sl]; nz = np.sqrt(g[2] ** 2 + g[3] ** 2 + g[4] ** 2) + 1e-12