Files
TarkovMaps/docs/TODO-gemini-regeneration.md
lucioandClaude Fable 5 472c983287 Commit build outputs via LFS; right-click markers, inline edit box, panel pin
Make a fresh clone servable after `git lfs pull` alone, and rebuildable with
`tools/build.py` without re-rendering unchanged PSDs:

- Track build/ (rendered base PNGs, previews, world composites), web/tiles/,
  web/tiles-trimmed/, web/data/world.json and web/data/markers/. Large PNGs
  and the tile pyramids go through Git LFS (.gitattributes); JSON stays plain.
- build.py: decide PSD re-export from build/<id>/base.stamp.json (PSD size,
  mtime, SHA-256 recorded at export time) instead of file mtimes, which are
  meaningless after an LFS checkout. Same size + new mtime hashes once and
  refreshes the stamp; --stamp-only writes stamps for existing base.pngs.
- Ignore .DS_Store and .env files; track sources/**/*.jpg through LFS.

Viewer:
- Right-click (long-press) the map for a category menu that drops a draft
  marker under the pointer and enters marker edit mode for that map.
- Marker editing moves out of the side panel into a box anchored next to the
  marker: Enter or a left click anywhere else saves, Esc cancels, Delete
  inside the box; drags save position plus the box contents.
- Maps panel expands on hover, header click pins/unpins (status shown in the
  header), unpinned collapses 2 s after the pointer leaves; Overview is the
  first list entry instead of a separate button. Only the panel scrolls; the
  page never grows past the viewport. Hover tooltips render above markers.

Also includes the pending source additions (overall square base, skirt
textures, AI regeneration tooling and notes), service worker, Caddyfile and
map config updates.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-22 09:46:02 -07:00

9.6 KiB
Raw Permalink Blame History

TODO — Regenerate the world with Gemini image-to-image

Status: Implemented. The pipeline tools are complete:

  • tools/ai/gemini.py: Gemini client wrapper for image generation and image-to-image editing.
  • tools/square_base.py: Generates the square base satellite map with corner outpainting.
  • tools/composite.py: Creates multi-resolution world composites and transition masks.
  • tools/regen_world.py: Serpentine sliding window AI patch loop with guardrails and resume support.
  • tools/tile.py: Extended with "mode": "aligned" skirts and unified backdrop handling.

Two goals:

  1. Square base map — the overall satellite is rotated 45°, so the world is a diamond with four empty corner triangles. Fill them so the base is a full square.
  2. One seamless world at a stable resolution — detail maps (native z6–z9) sit on a base that is native z3; edges, palette and sharpness jump at every map boundary. Produce one coherent layer at a single, modest resolution for the whole world, and use it to blend every map into its surroundings.

Why Gemini: unlike MiniMax (image-01, text-to-image only — tested, no inpainting/outpainting, subject_reference is portrait-only and degenerates on terrain), Gemini image models accept input images in contents and return an edited/extended image. That is what both goals need.


0. Ground truth you will be working with

Fact Value Where
Base source sources/overall/overall.jpeg, 1308 × 1296 RGB maps.json → world.image
Base rotation 45° CCW, expand=True → 1841 × 1841 tile.py → load_source, common.rotated_size
World pixels 1841 × overallScale 64 = 117 824 × 117 824 world.json → worldSize
Zoom depth maxZoom 9, tile 256 px; world z n edge = 117 824 / 2^(9−n) px common.Config
Base native level z3 (1841 px edge) — Leaflet upscales it beyond world.json
Detail maps 12, native z6–z9, placed by place in rotated overall-map px maps.json, §3 of DOCUMENTATION
Trim polygons customs, customs-dorms, woods, reserve, shoreline, lighthouse, streets maps.json → trim.points (normalised 0–1)
Viewer stacking base bottom; finer map on top; focused family lifted app.js → focus()
Attribution re3mr.com · CC BY-NC-SA 4.0 — derived imagery inherits it README

World edge per level: z4 3 682 px · z5 7 364 px · z6 14 728 px · z7 29 456 px.


1. Square base map

Keep every placement valid by never changing the rotated frame:

  1. tools/square_base.py: load overall.jpeg, rotate exactly as load_source does (rotate(45, expand=True, BICUBIC)) → 1841² RGBA with transparent corners. Upscale ×2 first (3682², = world z4) so Gemini has pixels to work with.
  2. For each corner triangle, cut overlapping 1024² windows that contain (a) a generous band of real satellite content and (b) the empty wedge. Send [window image, mask image, prompt] to Gemini; prompt: "Extend the satellite photo into the transparent/masked region. Continue terrain, forest, coastline and road network plausibly; match colour, grain, season and lighting exactly; do not alter any existing pixel; output the full window." Low temperature; fixed seed if the model exposes one.
  3. Stitch with feathered overlaps (cosine ramp, ≥128 px). Composite generated pixels only where the original alpha was 0 (hard guarantee that real imagery is untouched — verify with a pixel diff, fail the run if > 0).
  4. Save sources/overall/overall-square.png (LFS). In maps.json set world.image to it and world.rotate to 0. Because place is already expressed in rotated-frame pixels and the new image has the exact rotated size (1841², or 3682² with overallScale halved to 32), no placement changes. Confirm common.rotated_size(...) of the new source equals the old rotated size; retilt.py becomes a no-op at 0°.
  5. python tools/build.py --tiles-only overall → base pyramid gets z0–z4 (was z0–z3) if you kept the ×2 upscale.

Alternative (rejected): generate a brand-new square satellite — loses real geography the placements were aligned to.

Acceptance: overview shows a square; corner content has no visible seam at z≤4; git diff maps.json touches only world.image/world.rotate/overallScale.


2. Seamless world at a stable resolution

2.1 Pick the resolution

Target world z6 (14 728² px) — about 8 px per overall-map pixel, where the coarsest detail maps (Shoreline, Ground Zero, native z6) are at 1:1 and the sharpest (z9) are downscaled 8×. Enough to read roads and buildings at overview/mid zooms; the native high-res tiles keep serving deep zooms. z5 (7 364²) is a 4× cheaper first pass to validate the pipeline.

2.2 Build the input composite (no AI yet)

tools/composite.py --level 6:

  • Reuse common.Config.placement() / tile.py maths to paint, onto a z6 canvas: the square base (upscaled), then every detail map's trimmed raster (build/<id>/trimmed.png, skirts off) scaled to its world rect, in the viewer's stacking order. Paint tile-wise or in strips — 14 728² RGBA is ~870 MB; PIL copes, but write build/world/composite-z6.png in chunks.
  • Also write build/world/mask-z6.png: 0 = real art inside a trim polygon (protect), 128 = transition band (art edge ± N px and every art-on-satellite overlap), 255 = satellite-only (free to re-render).

2.3 Patch loop with Gemini

tools/regen_world.py --level 6 --window 1024 --overlap 256:

  1. Serpentine order over the canvas so each window sees already-processed neighbours (left/top) in its overlap → long-range coherence.
  2. Per window send: current composite crop (with processed neighbours baked in), the mask crop, and the prompt: "Re-render this top-down map patch at one uniform resolution and style. Keep every road, building, shoreline, rail and clearing exactly where it is (white = free to re-render, grey = blend both sides seamlessly, black = copy unchanged). Match the style of the black regions. No text, no labels."
  3. Post-process: paste back only where mask > 0; cosine-feather the overlaps; for protected pixels, copy the original regardless of the output.
  4. Guardrails per patch: edge-map IoU between input and output inside the grey band ≥ 0.6 (no invented geometry), mean colour shift < 12/255, no text detected (run mmx vision describe or Gemini itself as a cheap judge). Retry with a new seed up to 3×, then flag the window in build/world/report.json.
  5. Resume support (skip windows already done), dry-run that writes the window grid as an overlay for inspection before spending money.

Cost ballpark: ~370 windows at z6 whole-world / ~150 for bands only; at ~$0.04 per 1K image that is $6–15 per full pass. z5 is 4× cheaper.

2.4 Use it in the viewer — two integration modes

  • A. Unified backdrop (simple): tile build/world/unified-z6.png as a new base layer world-unified (z0–z6) replacing overall under the detail maps. Detail maps still paint above it at their native resolution; since the unified layer was derived from the same art, the hand-off at their polygon is subtle. Keep overall as a fallback layer behind a flag.
  • B. Aligned skirts (best seams): extend skirt in tile.py with "mode": "aligned" — instead of tiling a texture, cut the ring around each map's rectangle out of the unified composite at world coordinates, scale to the map's raster and use it as the skirt. Pixel-exact continuity with both the art and the backdrop; all existing knobs (featherIn, fadeOut, soften) still apply; the stamp must include the unified image's hash. This is the mode that makes "seamless" true at every zoom.

2.5 Acceptance

  • Pan Woods → Customs → Interchange at z4–z6: no frame edges, no resolution jumps, palette continuous; roads continue across polygons.
  • Deep zooms unchanged (native tiles); editors unaffected (original pyramid).
  • Bytes: report total tile MB before/after; keep the Detail-mode/soften budgets in mind (skirts doubled bytes until softened).

3. Plumbing / hygiene

  • API key: tools/gemini_image.py currently has a key pasted into GOOGLE_API_KEY. Move it to GEMINI_API_KEY in the environment (the client reads it when the constant is empty) before committing — the file is tracked. Consider a .env + python-dotenv and add .env to .gitignore.
  • Refactor tools/gemini_image.py into tools/ai/gemini.py with edit_image(images: list[PIL.Image], prompt, *, size, seed) -> PIL.Image and generate(prompt, ...); skirt.py gets --backend minimax|gemini.
  • Check current Gemini image model names/limits (gemini-3.1-flash-image vs gemini-3-pro-image-preview), max input images, output sizes (1K/2K/4K), whether seed/temperature apply via GenerateContentConfig, rate limits.
  • Put all generated sources under sources/ai/ (LFS, *.png|*.jpg already matched) with a JSON sidecar per file: model, prompt, seed, windows, input hashes — so a pass is reproducible and attributable.
  • Licence: generated imagery derived from re3mr art stays CC BY-NC-SA; keep the attribution control and note AI-assisted regions in the README.

4. Order of work

  1. square_base.py (small, self-contained, immediately visible win).
  2. composite.py at z5 + regen_world.py dry-run grid → one real pass on the Woods/Customs/Reserve cluster only → evaluate.
  3. Integration mode A (backdrop) → if seams still show at z5–z6, mode B.
  4. Full-world pass at z6; byte budget check; docs.