|
| 1 | +"""Terrain LOD demo — distance-based level of detail for large terrains. |
| 2 | +
|
| 3 | +Generates a synthetic 2048x2048 terrain and launches the interactive |
| 4 | +viewer. Press Shift+A to toggle terrain LOD, which splits the terrain |
| 5 | +into tiles and assigns each tile a resolution based on camera distance. |
| 6 | +
|
| 7 | +Controls: |
| 8 | + Shift+A Toggle terrain LOD on/off |
| 9 | + R / Shift+R Manual resolution down / up (applies globally) |
| 10 | + Z / Shift+Z Vertical exaggeration |
| 11 | + WASD / arrows Move camera |
| 12 | +
|
| 13 | +The LOD system is most useful with large terrains where full-resolution |
| 14 | +rendering everywhere would exceed GPU memory or hurt frame rate. Nearby |
| 15 | +tiles render at full detail while distant tiles are progressively |
| 16 | +subsampled (2x, 4x, 8x). |
| 17 | +
|
| 18 | +Usage: |
| 19 | + python terrain_lod_demo.py |
| 20 | + python terrain_lod_demo.py --size 4096 |
| 21 | +""" |
| 22 | + |
| 23 | +import argparse |
| 24 | + |
| 25 | +import numpy as np |
| 26 | +import xarray as xr |
| 27 | + |
| 28 | +import rtxpy # noqa: F401 — registers .rtx accessor |
| 29 | + |
| 30 | + |
| 31 | +def make_terrain(size, seed=42): |
| 32 | + """Generate a synthetic island terrain using multi-octave Perlin noise. |
| 33 | +
|
| 34 | + Falls back to simple sine-based terrain if xarray-spatial is not |
| 35 | + installed. |
| 36 | + """ |
| 37 | + try: |
| 38 | + import cupy as cp |
| 39 | + from xrspatial import generate_terrain |
| 40 | + |
| 41 | + template = xr.DataArray( |
| 42 | + cp.zeros((size, size), dtype=cp.float32), dims=['y', 'x'], |
| 43 | + ) |
| 44 | + terrain = generate_terrain( |
| 45 | + template, |
| 46 | + x_range=(-5000, 5000), |
| 47 | + y_range=(-5000, 5000), |
| 48 | + seed=seed, |
| 49 | + zfactor=3000, |
| 50 | + full_extent=(-5000, -5000, 5000, 5000), |
| 51 | + noise_mode='ridged', |
| 52 | + warp_strength=0.3, |
| 53 | + octaves=5, |
| 54 | + ) |
| 55 | + # Make it an island |
| 56 | + sea_level = float(cp.nanmedian(terrain.data)) * 0.8 |
| 57 | + terrain.data[:] = cp.maximum(terrain.data - sea_level, 0) |
| 58 | + return terrain |
| 59 | + except ImportError: |
| 60 | + pass |
| 61 | + |
| 62 | + # Fallback: sin/cos-based terrain (no xarray-spatial needed) |
| 63 | + rng = np.random.default_rng(seed) |
| 64 | + y = np.linspace(0, 4 * np.pi, size, dtype=np.float32) |
| 65 | + x = np.linspace(0, 4 * np.pi, size, dtype=np.float32) |
| 66 | + yy, xx = np.meshgrid(y, x, indexing='ij') |
| 67 | + z = (np.sin(xx) * np.cos(yy * 0.7) * 500 |
| 68 | + + np.sin(xx * 2.3 + 1) * np.cos(yy * 1.7 + 0.5) * 200 |
| 69 | + + rng.normal(0, 10, (size, size)).astype(np.float32)) |
| 70 | + z = np.maximum(z, 0) |
| 71 | + return xr.DataArray(z, dims=['y', 'x']) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + parser = argparse.ArgumentParser( |
| 76 | + description="Terrain LOD demo — press Shift+A in viewer to toggle", |
| 77 | + ) |
| 78 | + parser.add_argument("--size", type=int, default=2048, |
| 79 | + help="Grid size in pixels (default: 2048)") |
| 80 | + parser.add_argument("--seed", type=int, default=42, |
| 81 | + help="Random seed (default: 42)") |
| 82 | + args = parser.parse_args() |
| 83 | + |
| 84 | + print(f"Generating {args.size}x{args.size} terrain...") |
| 85 | + terrain = make_terrain(args.size, seed=args.seed) |
| 86 | + |
| 87 | + elev = terrain.data |
| 88 | + if hasattr(elev, 'get'): |
| 89 | + elev = elev.get() |
| 90 | + print(f"Elevation range: {np.nanmin(elev):.0f} - {np.nanmax(elev):.0f} m") |
| 91 | + print(f"\nPress Shift+A to toggle terrain LOD") |
| 92 | + print(f"Press H for help overlay\n") |
| 93 | + |
| 94 | + terrain.rtx.explore( |
| 95 | + width=1920, |
| 96 | + height=1080, |
| 97 | + render_scale=0.5, |
| 98 | + subsample=1, |
| 99 | + title="Terrain LOD Demo", |
| 100 | + ) |
0 commit comments