{
 "id": "hydrology-terrain-synthesis",
 "title": "Terrain from Hydrology (River-First Terrain Synthesis)",
 "aliases": [
  "procedural terrain from river networks"
 ],
 "domain": "procgen",
 "subdomain": "terrain generation",
 "stage": "procedural",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "desktop-gpu",
  "python",
  "houdini"
 ],
 "year_introduced": 2013,
 "inputs": [
  "graph"
 ],
 "outputs": [
  "heightfield"
 ],
 "style": "photoreal",
 "dynamic": "static",
 "summary": "Build a river network first as a tree with drainage-area-based constraints, then synthesise a heightfield that is consistent with it by interpolating around the rivers and adding noise detail.",
 "how_it_works": "The user (or a generator) provides a coarse drainage structure: river segments, branching topology and a few elevation constraints. The algorithm expands this into a full tree where each segment's flow grows downstream, and elevations are assigned so rivers always flow downhill. Terrain between rivers is built by interpolating from the river elevations upward with a profile that depends on distance to the nearest channel and on drainage area, then adding procedural noise as detail. Since rivers are an input rather than an outcome of simulation, results are controllable and fast to compute, and the river layout is guaranteed to be hydrologically plausible. Landscape features like valley width and ridgelines follow from the tree.",
 "when_to_use": "Choose it when you need to control where rivers and watersheds go (game maps, scenic composition) and want terrain consistent with them without running a long erosion simulation. Prefer `stream-power-erosion` when you want the river network to emerge from uplift rather than be drawn, and `hydraulic-erosion-stava` for fine erosion detail on top.",
 "cost": {
  "class": "medium",
  "notes": "Graph construction plus an interpolation pass over the grid; far cheaper than iterating full erosion."
 },
 "failure_modes": [
  "Terrain between rivers is generic noise-modulated interpolation, lacking tributary gullies unless a post-erosion pass is added",
  "Constraints that force uphill flow need explicit repair or the tree rejects them",
  "Large drainage-area contrast gives unrealistically wide valleys for small rivers",
  "River sketches without enough branching produce flat, featureless basins"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/2461912.2461996",
   "type": "paper",
   "title": "Terrain generation using procedural models based on hydrology",
   "authors": [
    "Jean-David Génevaux",
    "Éric Galin",
    "Eric Guérin",
    "Adrien Peytavie",
    "Bedrich Benes"
   ],
   "year": 2013,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/2461912.2461996",
   "ids": {
    "doi": "10.1145/2461912.2461996"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2461912.2461996",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2461912.2461996",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2461912.2461996",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2461912.2461996",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/2461912.2461996",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "gnevaux2013terrain"
  }
 ],
 "presets": [
  {
   "id": "hydrology-terrain-synthesis-live",
   "title": "Hydrology Terrain Synthesis — Strahler river-tree & watershed map (left) vs uplifted alpine valleys (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_UPLIFT": 0.92,
    "P_RIVER_WIDTH": 0.85,
    "P_CAM_SWAY": 0.42,
    "P_SPLIT": 0.46
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_UPLIFT = 0.92; // P Ridge Uplift [0.3, 1.5, 0.04]\nconst float P_RIVER_WIDTH = 0.85; // P River Network [0.3, 1.5, 0.04]\nconst float P_CAM_SWAY = 0.42; // P Camera Sway [0.0, 2.0, 0.05]\nconst float P_SPLIT = 0.46; // P River Tree / 3D [0.0, 1.0, 0.02]\n\nfloat sdSeg(vec2 p, vec2 a, vec2 b, out float h) {\n  vec2 pa = p - a, ba = b - a;\n  h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);\n  return length(pa - ba * h);\n}\n\n// River-first hierarchical dendritic drainage tree (Genevaux et al. 2013):\n// Returns vec3(distToNearestRiver, riverElevation, strahlerOrder)\nvec3 evalRiverTree(vec2 p) {\n  // Subtle natural meander warp\n  vec2 q = p + vec2(0.12 * sin(p.y * 2.2), 0.08 * cos(p.x * 2.4));\n\n  float h0, h1, h2, h3, h4;\n  // Order-3 main trunk river flowing from (0, -1.6) to (0, 1.8)\n  float dMain = sdSeg(q, vec2(0.0, -1.6), vec2(0.0, 1.8), h0);\n  float zMain = mix(0.22, -0.30, h0);\n\n  // Order-2 primary tributaries branching left and right\n  float dT1 = sdSeg(q, vec2(-1.45, -1.15), vec2(0.0, -0.25), h1);\n  float zT1 = mix(0.46, -0.02, h1);\n\n  float dT2 = sdSeg(q, vec2(1.45, -0.85), vec2(0.0, 0.15), h2);\n  float zT2 = mix(0.44, -0.08, h2);\n\n  // Order-1 headwater streams feeding tributaries\n  float dH1 = sdSeg(q, vec2(-1.35, 0.35), vec2(-0.62, -0.62), h3);\n  float zH1 = mix(0.56, 0.20, h3);\n\n  float dH2 = sdSeg(q, vec2(1.25, 0.72), vec2(0.68, -0.32), h4);\n  float zH2 = mix(0.54, 0.16, h4);\n\n  // Pick closest drainage segment & its Strahler order\n  float d = dMain; float zR = zMain; float ord = 3.0;\n  if (dT1 < d) { d = dT1; zR = zT1; ord = 2.0; }\n  if (dT2 < d) { d = dT2; zR = zT2; ord = 2.0; }\n  if (dH1 < d) { d = dH1; zR = zH1; ord = 1.0; }\n  if (dH2 < d) { d = dH2; zR = zH2; ord = 1.0; }\n  return vec3(d, zR, ord);\n}\n\n// Uplift watershed ridges proportionally to distance from the river network!\nfloat evalHydroTerrain(vec2 xz, float uplift) {\n  vec3 rt = evalRiverTree(xz);\n  // Concave valley cross-section rising to sharp alpine watershed divides between rivers\n  float ridge = pow(clamp(rt.x * 0.95, 0.0, 1.4), 1.15) * 0.68 * uplift;\n  float detail = 0.08 * sin(xz.x * 5.2 + cos(xz.y * 4.7)) + 0.035 * cos(xz.x * 11.0 - xz.y * 9.5);\n  return rt.y + ridge + detail * smoothstep(0.05, 0.35, rt.x);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isMap = screenX < split;\n  float uplift = clamp(P_UPLIFT, 0.25, 1.6);\n  float rw = clamp(P_RIVER_WIDTH, 0.25, 1.6);\n  float sway = sin(iTime * 0.45 * max(P_CAM_SWAY, 0.0)) * 0.35;\n\n  vec3 ro = vec3(sway, 1.45, 2.55);\n  vec3 rd = normalize(vec3(uv.x - sway * 0.12, uv.y - 0.42, -1.32));\n  vec3 sky = mix(vec3(0.84, 0.88, 0.94), vec3(0.18, 0.42, 0.76), smoothstep(-0.08, 0.52, rd.y));\n  vec3 col = sky;\n\n  float t = 0.2;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    vec3 p = ro + rd * t;\n    float h = evalHydroTerrain(p.xz, uplift);\n    float d = p.y - h;\n    if (d < 0.008) { hit = true; break; }\n    t += max(d * 0.45, 0.025);\n    if (t > 12.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    float e = 0.016;\n    float hC = evalHydroTerrain(p.xz, uplift);\n    float hX = evalHydroTerrain(p.xz + vec2(e, 0.0), uplift);\n    float hZ = evalHydroTerrain(p.xz + vec2(0.0, e), uplift);\n    vec3 n = normalize(vec3((hC - hX) / e, 1.0, (hC - hZ) / e));\n\n    vec3 rt = evalRiverTree(p.xz);\n    float riverW = (0.028 + 0.024 * rt.z) * rw;\n    float inRiver = smoothstep(riverW * 1.4, riverW * 0.5, rt.x);\n\n    vec3 sunDir = normalize(vec3(0.62, 0.68, 0.45));\n    float diff = max(dot(n, sunDir), 0.0);\n\n    if (isMap) {\n      // Left: Topographic watershed map showing Strahler river tree orders & elevation iso-contours!\n      float contour = smoothstep(0.06, 0.012, abs(fract(hC * 8.0) - 0.5));\n      vec3 topo = mix(vec3(0.07, 0.14, 0.22), vec3(0.28, 0.52, 0.36), smoothstep(-0.25, 0.35, hC));\n      topo = mix(topo, vec3(0.86, 0.72, 0.42), smoothstep(0.35, 0.85, hC));\n      topo = mix(topo, vec3(0.95, 0.96, 1.0), contour * 0.38);\n      // Color river branches by Strahler order (1 = mint, 2 = cyan, 3 = bright sky-blue trunk)\n      vec3 ordCol = (rt.z > 2.5) ? vec3(0.16, 0.88, 1.0)\n                  : (rt.z > 1.5 ? vec3(0.20, 0.96, 0.72) : vec3(0.98, 0.84, 0.26));\n      col = mix(topo * (0.45 + 0.65 * diff), ordCol * 1.35, inRiver);\n    } else {\n      // Right: Full 3D alpine valley landscape synthesized from the hydrology tree\n      float slope = 1.0 - n.y;\n      vec3 forest = vec3(0.14, 0.36, 0.18);\n      vec3 rock = vec3(0.42, 0.40, 0.38);\n      vec3 snow = vec3(0.92, 0.95, 0.98);\n      vec3 alb = mix(forest, rock, smoothstep(0.16, 0.42, slope));\n      alb = mix(alb, snow, smoothstep(0.38, 0.68, hC) * smoothstep(0.48, 0.18, slope));\n      vec3 riverCol = vec3(0.08, 0.52, 0.72);\n      alb = mix(alb, riverCol, inRiver);\n      float spec = pow(max(dot(n, normalize(sunDir - rd)), 0.0), 40.0) * inRiver;\n      col = alb * (0.24 + 0.88 * diff) + vec3(0.95, 0.98, 1.0) * spec * 0.85;\n    }\n    col = mix(col, sky, 1.0 - exp(-t * 0.07));\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "hydrology-terrain-synthesis",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:50.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.8161,
      "std": 0.0713,
      "min": 0.5849,
      "max": 0.918
     },
     "image": "/specimens/hydrology-terrain-synthesis-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "stream-power-erosion",
  "hydraulic-erosion-stava",
  "fractal-brownian-motion"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "hydraulic-erosion-stava",
   "why": "The river-consistent heightfield gets small-scale gullies and deposits from a short erosion pass."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/procgen/hydrology-terrain-synthesis.json"
 },
 "solves": [
  "need to place rivers exactly where i want them",
  "terrain should be consistent with a designed watershed",
  "erosion simulation takes too long for map design",
  "game map needs believable drainage and valleys"
 ],
 "authority": "Génevaux et al., 2013",
 "url": "https://texelatlas.vercel.app/t/hydrology-terrain-synthesis/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 40.86,
  "tier": "heavy",
  "p50_ms": 1.3444,
  "p95_ms": 1.4474,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "hydrology-terrain-synthesis-live",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 34.88,
   "tier": "heavy",
   "p50_ms": 0.5546,
   "p95_ms": 0.5575,
   "speedup_x": 2.42,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 27.2,
   "tier": "heavy",
   "p50_ms": 0.7834,
   "p95_ms": 0.7905,
   "speedup_x": 1.72,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}