From 14ad4d14f9fa656e241cb5e28ec36a84af5b6157 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 04:30:29 +0100 Subject: [PATCH] ridge-bake: delete the reveal mask, max_slide_px and SLIDE_PCT (verdict s2) The slide contract (viewport-aspect drift, simultaneous slides) lives in the runtime cut; a baker bound of 5% of the card width was unsound and did not lower the decode total. cards() is again one rectangle of rows with coverage; the schema is regenerated and committed. Co-Authored-By: Claude Opus 5.5 --- scripts/ridge-bake/README.md | 5 ++--- scripts/ridge-bake/export.py | 10 ++-------- scripts/ridge-bake/ridge-bake.schema.json | 5 ----- scripts/ridge-bake/ridge.py | 15 ++------------- 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/scripts/ridge-bake/README.md b/scripts/ridge-bake/README.md index 502156c2..c3c8f4e5 100644 --- a/scripts/ridge-bake/README.md +++ b/scripts/ridge-bake/README.md @@ -33,9 +33,8 @@ Files are `Copernicus_DSM_COG_10_dd_00_ddd_00_DEM.tif`; the list below 2. **Low-pass.** `prefilter` blurs the sampled heights with a Gaussian of `prefilter_m` metres along each ray and across rays at equal distance (sigma = `prefilter_m` / (distance x column width) columns), then recomputes the angles. Roofs go, hills stay. 3. **View.** `set_view` maps angle to row linearly: the highest skyline angle in the window sits at 4% of the frame height, the lowest at 66%, clamped to 1x to 10x vertical exaggeration. 4. **Seams.** `coverage` counts the visible rows of the opaque view per log-distance bin and column. `seams` finds one seam per authored split per column by dynamic programming (seam carving, forward energy): it sits in a gap between depths, moving it costs 2 per bin plus the visible rows it sweeps (so it never hops across a ridge between two columns and splits it side by side), a seam inside visible terrain is cheap to cross-fade, the authored depth only breaks ties. Seam k stays 0.4 nat beyond seam k-1. -5. **Cards.** `cards` makes card i from the first visible hit at or beyond seam i-1 (layered depth: a farther card keeps the terrain a nearer ridge hides), alpha 1 minus a smoothstep over seam i in log-distance (the cross-fade), crest rows with fractional coverage, distance and normal interpolated between samples by angle, albedo from a small height and slope material, reveal-bounded (below), cropped to the rows where alpha is not zero. The alpha-over composite of the cards equals the opaque view; `check_cover` refuses a bake that differs by more than 0.02. -6. **Reveal bound.** The runtime slides each card against the others by at most `MAX_SLIDE_PX` = ceil(5% of the card width) (64 at 1280, the runtime's `DRIFT_FRAC`; the manifest's `max_slide_px`). A card therefore loses every pixel that the nearer cards' opaque pixels hide at every offset within that slide, also when any single card slides alone; the composite at rest is unchanged and no slide within the bound shows a cut pixel. A column whose slide window leaves the frame keeps its pixels. The crop is a rectangle of rows, so the edge strips keep a card's rows even where its interior is cut, and the decode total is the rows that still hold a pixel anywhere. -7. **Normals.** `normals` takes the along-ray slope and the across-ray slope at equal distance (neighbouring columns, same sample), both times the vertical exaggeration. +5. **Cards.** `cards` makes card i from the first visible hit at or beyond seam i-1 (layered depth: a farther card keeps the terrain a nearer ridge hides), alpha 1 minus a smoothstep over seam i in log-distance (the cross-fade), crest rows with fractional coverage, distance and normal interpolated between samples by angle, albedo from a small height and slope material, cropped to the rows where alpha is not zero. The alpha-over composite of the cards equals the opaque view; `check_cover` refuses a bake that differs by more than 0.02. +6. **Normals.** `normals` takes the along-ray slope and the across-ray slope at equal distance (neighbouring columns, same sample), both times the vertical exaggeration. Files per card `i`: `card--colour.webp` (lossy RGB albedo, lossless straight alpha) and `card--geometry.webp` (lossless: R, G = (n + 1) / 2 for the normal's along and across components, B = log-distance normalised between `encoding.log_d_min` and `log_d_max`). The manifest lists the cards nearest first with their row band (`top`, `h`), median seam depths and files; the runtime paints the list last to first. `check_budget` refuses more than `MAX_CARDS` cards, more than 1.5 MB of card files, or more than 20 MB decoded (2 textures x width x band rows x 4 bytes per card); a cap is never raised to fit a bake. diff --git a/scripts/ridge-bake/export.py b/scripts/ridge-bake/export.py index a11f1d86..1ac70028 100644 --- a/scripts/ridge-bake/export.py +++ b/scripts/ridge-bake/export.py @@ -18,7 +18,6 @@ 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 @@ -38,7 +37,6 @@ 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), @@ -98,10 +96,6 @@ 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() @@ -120,7 +114,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), max_slide_px=max_slide_px(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), 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-')) @@ -144,7 +138,7 @@ 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, 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)) + return dict(place=place, view=view, meta=meta, cards=ridge.cards(d, aw, hw, sm, nrm), 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.""" diff --git a/scripts/ridge-bake/ridge-bake.schema.json b/scripts/ridge-bake/ridge-bake.schema.json index f414c283..fbf47731 100644 --- a/scripts/ridge-bake/ridge-bake.schema.json +++ b/scripts/ridge-bake/ridge-bake.schema.json @@ -10,7 +10,6 @@ "bake_id", "place", "view", - "max_slide_px", "curvature", "encoding", "cards", @@ -111,10 +110,6 @@ } } }, - "max_slide_px": { - "type": "integer", - "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": { "type": "object", "required": [ diff --git a/scripts/ridge-bake/ridge.py b/scripts/ridge-bake/ridge.py index 0b941742..62a7a256 100644 --- a/scripts/ridge-bake/ridge.py +++ b/scripts/ridge-bake/ridge.py @@ -234,19 +234,11 @@ 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 _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): +def cards(d, a, h, seams, nrm=None): """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 @@ -263,12 +255,9 @@ def cards(d, a, h, seams, nrm=None, *, slide_px): 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)); solid = []; held = [] # solid: the nearer cards' opaque pixels (after their own loss); held: where each still holds across the slide + out = []; A = np.zeros((H, n)) 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