GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Solar and storage design / globalgrid2050 / solar-design-studio/202609270402-site-world-v3/world

Raycast and 2 more files

A JavaScript module in the world folder of GlobalGrid2050 that builds pyramid (`buildPyramid`), calculates planar distances and renders with WebGL. It builds pyramid (`buildPyramid`) and creates raycaster (`createRaycaster`).

553 lines of JavaScript from globalgrid2050, in 3 files. Topic: Solar and storage design. Section 151 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Lines render · globalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/lines.mjs lines 1–164
  2. Plant Ground geodesy · globalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/plant-ground.mjs lines 1–72
  3. Raycast render · globalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/raycast.mjs lines 1–317

Lines

A JavaScript module in the world folder of GlobalGrid2050 that splits batches (`splitBatches`), calculates planar distances and renders with WebGL.

renderglobalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/lines.mjs · lines 1–164 of 164 · version 2026-09-27 04:02 · Entry point · pattern occluderFollower (shared by 4 files)

What it does
It draws its content. Defines 8 functions or classes, including offsetOf, splitBatches, occluderMesh, put, occluderFollower. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary.
Inputs & outputs
Out: WebGL/WebGPU drawing.
Downstream impact
Used directly by 2 files: Solar Design Studio — Site World V3 Page (globalgrid2050, https://globalgrid2050.com/solar-design-studio/202609270402-site-world-v3/); Substrate (globalgrid2050).
What it achieves
It splits batches (`splitBatches`) and is built from the offset of helper (`offsetOf`) and the occluder mesh helper (`occluderMesh`). Line renderer: raw WebGL 1, flat colour, lines fade with distance. It responds to webglcontextlost and webglcontextrestored events.
Why it matters
Loaded by the Site World V3 page at https://globalgrid2050.com/solar-design-studio/202609270402-site-world-v3/. Also imported by Substrate.
Application
globalgrid2050 · solar-design-studio/202609270402-site-world-v3/world · JavaScript module
Uses
none found
Used by
preloaded by Solar Design Studio — Site World V3 Page (modulepreload), imported by Substrate (import)
Produces
offsetOf, BACKGROUND, splitBatches, occluderMesh, occluderFollower, createLines
Defines here
offsetOf, splitBatches, occluderMesh, put, occluderFollower, createLines, init, bind
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270402-site-world-v3/world/lines.mjs:1-164 · blob f947588fc0a665b0c3789dd6b6700f686794c244 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: production_core
// Line renderer: raw WebGL 1, flat colour, lines fade with distance. No textures, no sky, no lighting.
// Survives a lost WebGL context: every buffer is rebuilt from the layers' own data on restore.
// Drawing is relative to the eye: the matrix is built with the eye at [0, 0, 0], and each batch is moved by
// u_off = (batch origin - eye), worked out in JavaScript doubles. A batch with an origin keeps its
// positions small, so 32-bit floats stay sharp however far the viewer walks from the site origin.
// Occlusion: batches marked { occluder: true, triangles } (a coarse ground mesh) are drawn first into the
// depth buffer only; then every line batch is depth tested against them, so lines behind a hill are hidden.
// Lines never write depth, so they never hide each other; only occluders hide anything.

const VS = `attribute vec3 a_p; uniform mat4 u_m; uniform vec3 u_off; uniform float u_fade; varying float v_a;
void main() { vec3 p = a_p + u_off; gl_Position = u_m * vec4(p, 1.0); v_a = clamp(1.0 - length(p) / u_fade, 0.0, 1.0); }`;
const ZERO = [0, 0, 0];

// The offset from the eye to a batch's origin, in doubles; only the small result goes to the GPU.
export const offsetOf = (origin, eye) => {
  const o = origin || ZERO;
  return [o[0] - eye[0], o[1] - eye[1], o[2] - eye[2]];
};
const FS = `precision mediump float; uniform vec4 u_c; varying float v_a;
void main() { gl_FragColor = vec4(u_c.rgb, u_c.a * v_a); }`;

export const BACKGROUND = [10 / 255, 10 / 255, 10 / 255, 1]; // #0a0a0a

// Occluders first (depth only), then lines; order within each group is kept.
export const splitBatches = batches => {
  const occluders = [], lines = [];
  for (const b of batches) (b.occluder ? occluders : lines).push(b);
  return { occluders, lines };
};

// A coarse ground mesh around (x, y) for occlusion, as an occluder batch. Pure: it only calls heightAt.
// The centre snaps to the cell grid so the mesh does not swim as the viewer moves; positions are relative
// to that centre (the batch origin), so they stay small. sink lowers the mesh so lines lying on finer
// ground than these cells are not buried where the coarse surface cuts above the true one.
// Cells with a corner that has no finite height are left out (nothing is hidden there).
export function occluderMesh(heightAt, x, y, { cell = 8, radius = 400, sink = 0.5, key = 'occluder', version } = {}) {
  if (!(cell > 0) || !(radius > 0)) throw Error('occluderMesh needs a positive cell and radius');
  const cx = Math.round(x / cell) * cell, cy = Math.round(y / cell) * cell;
  const n = Math.ceil(radius / cell), side = 2 * n + 1;
  const h = new Float64Array(side * side);
  for (let j = 0; j < side; j++) for (let i = 0; i < side; i++) {
    const v = heightAt(cx + (i - n) * cell, cy + (j - n) * cell);
    h[j * side + i] = Number.isFinite(v) ? v - sink : NaN;
  }
  const out = new Float32Array((side - 1) * (side - 1) * 18);
  let k = 0;
  const put = (i, j) => { out[k++] = (i - n) * cell; out[k++] = (j - n) * cell; out[k++] = h[j * side + i]; };
  for (let j = 0; j < side - 1; j++) for (let i = 0; i < side - 1; i++) {
    const a = h[j * side + i], b = h[j * side + i + 1], c = h[(j + 1) * side + i], d = h[(j + 1) * side + i + 1];
    if (!Number.isFinite(a + b + c + d)) continue;
    put(i, j); put(i + 1, j); put(i + 1, j + 1);
    put(i, j); put(i + 1, j + 1); put(i, j + 1);
  }
  return { key, version: version ?? `${cx},${cy},${cell},${radius},${sink}`, occluder: true,
    triangles: out.subarray(0, k), origin: [cx, cy, 0] };
}

// Keeps one occluder mesh and rebuilds it only when the viewer has moved `every` metres from where it was
// built, or the ground has changed (groundVersion). Returns the same batch object while nothing changed.
export function occluderFollower({ every = 64, ...opts } = {}) {
  let last = null, at = null, gv;
  return (heightAt, pos, groundVersion = 0) => {
    if (!last || gv !== groundVersion || Math.hypot(pos[0] - at[0], pos[1] - at[1]) > every) {
      at = [pos[0], pos[1]]; gv = groundVersion;
      const m = occluderMesh(heightAt, pos[0], pos[1], opts);
      last = { ...m, version: `${m.version},g${groundVersion}` };
    }
    return last;
  };
}

export function createLines(canvas, { onRestore = () => {} } = {}) {
  let gl, prog, loc, lost = false, scale = 0, backdrop = null;
  const cache = new Map(); // batch key -> { buf, version, count }

  function init() {
    gl = canvas.getContext('webgl', { antialias: true, alpha: false, depth: true, powerPreference: 'low-power' });
    if (!gl) throw Error('WebGL is not available in this browser');
    prog = gl.createProgram();
    for (const [type, src] of [[gl.VERTEX_SHADER, VS], [gl.FRAGMENT_SHADER, FS]]) {
      const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s);
      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS) && !gl.isContextLost()) throw Error(gl.getShaderInfoLog(s));
      gl.attachShader(prog, s);
    }
    gl.linkProgram(prog);
    loc = { p: gl.getAttribLocation(prog, 'a_p'), m: gl.getUniformLocation(prog, 'u_m'), off: gl.getUniformLocation(prog, 'u_off'),
      fade: gl.getUniformLocation(prog, 'u_fade'), c: gl.getUniformLocation(prog, 'u_c') };
    gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.depthFunc(gl.LEQUAL); gl.clearDepth(1);
    cache.clear();
  }

  canvas.addEventListener('webglcontextlost', e => { e.preventDefault(); lost = true; cache.clear(); });
  canvas.addEventListener('webglcontextrestored', () => { init(); lost = false; scale = 0; onRestore(); });
  init();

  // Uploads a batch's data once per version and binds it; returns its vertex count.
  function bind(b, data) {
    let c = cache.get(b.key);
    if (!c || c.version !== b.version) {
      if (!c) c = { buf: gl.createBuffer() };
      gl.bindBuffer(gl.ARRAY_BUFFER, c.buf); gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
      c.version = b.version; c.count = data.length / 3; cache.set(b.key, c);
    }
    gl.bindBuffer(gl.ARRAY_BUFFER, c.buf); gl.vertexAttribPointer(loc.p, 3, gl.FLOAT, false, 0, 0);
    return c.count;
  }

  return {
    lost: () => lost,
    context: () => gl, // the headset view shares this context
    setBackdrop(fn) { backdrop = typeof fn === 'function' ? fn : null; }, // painted after the clear, before occluders and lines

    // returns true when the drawing buffer changed size
    resize(nextScale) {
      const w = Math.max(1, Math.round(canvas.clientWidth * nextScale)), h = Math.max(1, Math.round(canvas.clientHeight * nextScale));
      if (canvas.width === w && canvas.height === h && scale === nextScale) return false;
      canvas.width = w; canvas.height = h; scale = nextScale;
      return true;
    },
    // matrix: projection times the view built at the eye, i.e. view([0, 0, 0], yaw, pitch).
    // eye: the viewer's position in local metres (doubles). fade: distance in metres where lines vanish.
    // batches: [{ key, version, positions: Float32Array of x,y,z pairs, color: [r,g,b,a], origin?: [x,y,z], fade?: metres }]
    //   (a batch's own fade replaces the viewer's: the imagery drape stays readable from a drone's height)
    //   and occluders: { key, version, occluder: true, triangles: Float32Array of x,y,z triples, origin? }
    // target (headset view only): { framebuffer, viewport: [x, y, w, h], clear } draws one eye into the
    //   headset's framebuffer; clear: false keeps what the first eye drew. Without it, the canvas as before.
    // Returns the number of line vertices drawn (occluder vertices are not counted).
    draw(matrix, eye, fade, batches, target) {
      if (lost) return 0;
      const { occluders, lines } = splitBatches(batches);
      gl.bindFramebuffer(gl.FRAMEBUFFER, target ? target.framebuffer : null);
      if (target) gl.viewport(...target.viewport); else gl.viewport(0, 0, canvas.width, canvas.height);
      gl.depthMask(true);
      if (!target || target.clear !== false) { gl.clearColor(...BACKGROUND); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); }
      if (backdrop && !target) backdrop(gl, canvas.width, canvas.height); // optional solid ground (raycast.mjs), flat screen only
      gl.useProgram(prog);
      gl.uniformMatrix4fv(loc.m, false, matrix); gl.uniform1f(loc.fade, fade);
      gl.enableVertexAttribArray(loc.p);
      gl.enable(gl.DEPTH_TEST);
      // Depth only: no colour, pushed slightly back so lines lying on the surface do not z-fight with it.
      gl.colorMask(false, false, false, false);
      gl.enable(gl.POLYGON_OFFSET_FILL); gl.polygonOffset(1, 4);
      for (const b of occluders) {
        const count = bind(b, b.triangles);
        gl.uniform3fv(loc.off, offsetOf(b.origin, eye));
        gl.drawArrays(gl.TRIANGLES, 0, count);
      }
      gl.disable(gl.POLYGON_OFFSET_FILL);
      gl.colorMask(true, true, true, true);
      gl.depthMask(false); // lines are tested against the ground but never hide each other
      let vertices = 0;
      for (const b of lines) {
        const count = bind(b, b.positions);
        gl.uniform3fv(loc.off, offsetOf(b.origin, eye));
        gl.uniform1f(loc.fade, b.fade > 0 ? b.fade : fade);
        gl.uniform4fv(loc.c, b.color); gl.drawArrays(gl.LINES, 0, count);
        vertices += count;
      }
      gl.depthMask(true);
      return vertices;
    }
  };
}

Plant Ground

A JavaScript module in the world folder of GlobalGrid2050 that creates site ground (`createSiteGround`), using SHA-256.

geodesyglobalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/plant-ground.mjs · lines 1–72 of 72 · version 2026-09-27 04:02 · Not reached from any entry point (candidate for review) · pattern createSiteGround (shared by 2 files)

What it does
It calculates its inputs. Defines 8 functions or classes, including createSiteGround, start, site, extent, loadFor.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary.
Downstream impact
Used directly by 2 files: Plant UI (globalgrid2050); Block UI (globalgrid2050).
What it achieves
It creates site ground (`createSiteGround`) and is built from the start step (`start`) and the site step (`site`). The measured ground under a whole plant boundary, and the mapped water beside it.
Why it matters
Imported by Plant UI and Block UI.
Application
globalgrid2050 · solar-design-studio/202609270402-site-world-v3/world · JavaScript module
VEDIC
HEIGHT (GG2050-VEDIC-000011): Solar Design Studio / LiDAR · Mathematics / Raster sampling. bng_easting_m, bng_northing_m → height_receipt_or_null
Uses
Layers (import), Terrain (import)
Used by
imported by Plant UI (import), imported by Block UI (import)
Produces
createSiteGround
Defines here
createSiteGround, start, site, extent, loadFor, heightAt, sampler, water
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270402-site-world-v3/world/plant-ground.mjs:1-72 · blob e2a9b42b4f18cf167a4fa343fac8c508432ec2e8 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unreached_review
// plant-ground.mjs: the measured ground under a whole plant boundary, and the mapped water beside it.
//
// The terrain layer keeps only the LiDAR tiles near the viewer. A plant boundary is far bigger, so this reads the
// same tiles for the boundary's extent, through the same hash checks (the manifest pins tiles.json, tiles.json
// pins every tile), and answers heights in national-grid metres. Where no tile covers a point the answer is NaN,
// never a guess; the page falls back to the world's own measuredAt there. Water comes from the manifest's water
// layer file when it has one (OS Open Rivers), else there is none and the layout says so.

import { makeApi } from './layers.mjs';
import { decodeTile, sampleTile } from './layers/terrain.mjs';

export function createSiteGround({ manifestUrl = './world/manifest.json', fetchImpl } = {}) {
  let manifest = null, api = null, index = null, dir = '', size = 256;
  const tiles = new Map(); // key -> decoded tile
  async function start() {
    if (manifest) return manifest;
    const base = new URL(manifestUrl, location.href);
    const res = await (fetchImpl || fetch)(base.href, { cache: 'no-cache' });
    if (!res.ok) throw Error(`manifest HTTP ${res.status}`);
    manifest = await res.json();
    api = makeApi(new URL('./', base).href, { fetchImpl });
    const cfg = (manifest.layers || []).find(l => l.id === 'terrain')?.config;
    if (cfg?.index && cfg?.sha256) {
      const j = await api.fetchJSON(cfg.index, cfg.sha256);
      dir = cfg.index.includes('/') ? cfg.index.slice(0, cfg.index.lastIndexOf('/') + 1) : '';
      size = j.tile_m || 256; index = (j.tiles || []).filter(t => !t.ring); // the 1 m tiles: a far ring (v06) is for the view only
    }
    return manifest;
  }
  const site = () => manifest?.site || null;
  // The tiles' outer extent in national-grid metres, or null with no terrain.
  const extent = () => (index && index.length ? { e0: Math.min(...index.map(t => t.e0)), n0: Math.min(...index.map(t => t.n0)),
    e1: Math.max(...index.map(t => t.e0)) + size, n1: Math.max(...index.map(t => t.n0)) + size } : null);

  /** Loads (verified) every tile touching the box; returns how many cover it and how many failed. */
  async function loadFor({ e0, n0, e1, n1 }, onTile = () => {}) {
    await start();
    if (!index) return { tiles: 0, failed: 0 };
    const want = index.filter(t => t.e0 < e1 && t.e0 + size > e0 && t.n0 < n1 && t.n0 + size > n0);
    let failed = 0;
    for (const t of want) {
      if (tiles.has(t.key)) continue;
      try { tiles.set(t.key, decodeTile(await api.fetchVerified(dir + t.file, t.sha256))); } catch { failed++; }
      onTile(tiles.size);
    }
    return { tiles: want.length, failed };
  }
  function heightAt(e, n) {
    for (const t of tiles.values()) {
      const s = (t.samples - 1) * t.spacing;
      if (e >= t.e0 && n >= t.n0 && e <= t.e0 + s && n <= t.n0 + s) return sampleTile(t, e - t.e0, n - t.n0);
    }
    return NaN;
  }
  // A faster lookup for the tight loop: tiles on a map keyed by their south-west corner.
  function sampler() {
    const byCorner = new Map([...tiles.values()].map(t => [t.e0 + ',' + t.n0, t]));
    return (e, n) => {
      const t = byCorner.get(Math.floor(e / size) * size + ',' + Math.floor(n / size) * size);
      return t ? sampleTile(t, e - t.e0, n - t.n0) : heightAt(e, n);
    };
  }
  /** Mapped watercourse lines ([[e, n]...]) from the manifest's water layer, or [] when there is none. */
  async function water() {
    await start();
    const cfg = (manifest.layers || []).find(l => l.id === 'water')?.config;
    if (!cfg?.index || !cfg?.sha256) return [];
    try { return ((await api.fetchJSON(cfg.index, cfg.sha256)).lines || []).map(l => l.points).filter(p => p && p.length > 1); }
    catch { return []; }
  }
  return { start, site, extent, loadFor, heightAt, sampler, water, loaded: () => tiles.size };
}

Raycast

A JavaScript module in the world folder of GlobalGrid2050 that builds pyramid (`buildPyramid`), calculates planar distances and renders with WebGL. It builds pyramid (`buildPyramid`) and creates raycaster (`createRaycaster`).

Set aside from the main view: 96% of its lines are the same as solar-design-studio/202609270439-site-world/world/raycast.mjs (globalgrid2050), which stays. (near-copy-of, proposed by families.py). 96% of lines shared with Raycast; read that one first. All set-aside files, with reasons. Nothing was deleted.

Evidence
  • line-jaccard 0.9614 to globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270439-site-world/world/raycast.mjs:1-326 f4231100f70425c6101ac754a5f9d6be62a92e4e
  • sha256 5b61e8cbf2f7c614b4418159d9f2c8ae5564d71ba887079ebd150870a3547c1c
  • target sha256 51d3c7a9cc42add5ff2bcae983b345bd7275f8433b5d8f484076d95897ec8779
  • group of 2 (identity.json near-copy edges >= 0.9)
  • canonical rule: reachable, newest stamp, core repo, most lines

renderglobalgrid2050 / solar-design-studio/202609270402-site-world-v3/world/raycast.mjs · lines 1–317 of 317 · version 2026-09-27 04:02 · pattern createRaycaster (shared by 2 files)

What it does
It draws its content. Defines 14 functions or classes, including buildPyramid, basis, sunVector, createRaycaster, sh. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary.
Inputs & outputs
Out: WebGL/WebGPU drawing.
Downstream impact
Used directly by 1 file: Substrate (globalgrid2050). Reached from 1 other entry point, including Solar Design Studio — Site World V3 Page (globalgrid2050, https://globalgrid2050.com/solar-design-studio/202609270402-site-world-v3/).
What it achieves
Solid ground on the viewer's own GPU: a WebGL2 ray cast through the height field, drawn behind the lines.
Why it matters
Imported by Substrate.
Application
globalgrid2050 · solar-design-studio/202609270402-site-world-v3/world · JavaScript module
Uses
none found
Used by
imported by Substrate (dynamic import), mentioned in Standalone (comment link)
Produces
DEFAULTS, NO_GROUND, buildPyramid, basis, sunVector, createRaycaster, gatherGrid, mountRaycast
Defines here
buildPyramid, basis, sunVector, createRaycaster, sh, tex, upload, setup, gatherGrid, slice, mountRaycast, gather, stale, backdrop
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609270402-site-world-v3/world/raycast.mjs:1-317 · blob 764aec7b6602a7f1d2cdb69e7124d90a552bfcc6 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: production_core
// Solid ground on the viewer's own GPU: a WebGL2 ray cast through the height field, drawn behind the lines.
// OPTIONAL and OFF by default. substrate.mjs imports this module only when "Solid ground (GPU)" is first switched
// on, so the first frame never waits for it. Where WebGL2 is missing (or its context fails) mountRaycast returns
// null and the world carries on in WebGL1 exactly as before. It draws only inside the substrate's own frames, so
// a still view stays at 0 fps; a new ground grid is gathered in slices between frames and asks for one frame.
//
// The method is the "positron" of the GPU night render pair (gw-lidar src/render_pair.py): a maximum-mipmap
// traversal (Tevs, Ihrke and Seidel 2008), 16 samples and 30 bisections in the 1-cell step, bilinear ground in
// lerp form. Its CuPy twin and its exact-quadratic partner are the witness; tests/raycast-vector.json holds a
// case computed there, tests/raycast.test.mjs checks the pure parts and tests/raycast-browser.cjs the shader.
// What it adds to the wireframe: the TRUE horizon (ground that is not measured is not drawn: a ray that finds no
// measured ground is sky, never a guessed plateau), sun shadows (one shadow ray per pixel, toward the sun of the
// sun control) and, when a target is set, a sightline tint (ground from which a person 1.7 m tall sees it).
// Local east (x), north (y), up (z) metres, the camera of camera.mjs / pick.mjs (yaw 0 north, pitch up +).

export const DEFAULTS = Object.freeze({ nodes: 1025, spacing: 2, farNodes: 1025, farSpacing: 8, scale: 0.5, sliceMs: 6, person: 1.7 });
export const NO_GROUND = -1e4;              // a node with no measured height: far below anything, so rays pass
const K = 16, BIS = 30, PROBE = 1e-3;

// Maximum-mipmap levels of a node grid (n = 2^k + 1 nodes a side): level 0 holds the highest of each cell's four
// nodes, level L the highest of each 2x2 block of level L-1. Pure.
export function buildPyramid(h, n) {
  const c = n - 1;
  if (c & (c - 1)) throw Error('buildPyramid needs 2^k + 1 nodes a side');
  let lv = new Float32Array(c * c);
  for (let j = 0; j < c; j++) for (let i = 0; i < c; i++)
    lv[j * c + i] = Math.max(h[j * n + i], h[j * n + i + 1], h[(j + 1) * n + i], h[(j + 1) * n + i + 1]);
  const levels = [lv];
  for (let m = c / 2; m >= 1; m /= 2) {
    const p = levels[levels.length - 1], w = m * 2, nx = new Float32Array(m * m);
    for (let j = 0; j < m; j++) for (let i = 0; i < m; i++)
      nx[j * m + i] = Math.max(p[2 * j * w + 2 * i], p[2 * j * w + 2 * i + 1], p[(2 * j + 1) * w + 2 * i], p[(2 * j + 1) * w + 2 * i + 1]);
    levels.push(nx);
  }
  return levels;
}

// Camera basis of camera.mjs view(): forward, right, up. Pure.
export function basis(yaw, pitch) {
  const c = Math.cos(pitch), f = [Math.sin(yaw) * c, Math.cos(yaw) * c, Math.sin(pitch)], r = [Math.cos(yaw), -Math.sin(yaw), 0];
  return { f, r, u: [r[1] * f[2] - r[2] * f[1], r[2] * f[0] - r[0] * f[2], r[0] * f[1] - r[1] * f[0]] };
}

// Unit vector toward the sun from grid azimuth and elevation (degrees), as sun-mode.mjs reports them. Pure.
export function sunVector({ azimuth, elevation }) {
  const a = azimuth * Math.PI / 180, e = elevation * Math.PI / 180;
  return [Math.sin(a) * Math.cos(e), Math.cos(a) * Math.cos(e), Math.sin(e)];
}

const VS = `#version 300 es
in vec2 p; void main() { gl_Position = vec4(p, 0.0, 1.0); }`;
const FS = `#version 300 es
precision highp float; precision highp int; precision highp sampler2D;
uniform sampler2D uH, uP, uH1, uP1; uniform int n, top, n1, top1, useFar, mode, useSun, useTarget; uniform vec2 size, g0, g01;
uniform float sp, sp1, tanHalf, aspect, person; uniform vec3 eye, cf, cr, cu, sun, target, bg, light;
out vec4 o;
const int K = ${K}, BIS = ${BIS}, MAX_ITER = 60000; const float PROBE = ${PROBE}, INF = 1e30;
float hz(sampler2D H, int i, int j) { return texelFetch(H, ivec2(i, j), 0).r - eye.z; }
float lerpH(sampler2D H, int ci, int cj, float u, float v) {
  float s0 = hz(H, ci, cj) * (1.0 - u) + hz(H, ci + 1, cj) * u, s1 = hz(H, ci, cj + 1) * (1.0 - u) + hz(H, ci + 1, cj + 1) * u;
  return s0 * (1.0 - v) + s1 * v; }
float cl(float x) { return min(max(x, 0.0), 1.0); }
// the positron on one grid, in its cells: origin o (local x, y metres; z metres above the eye), unit d, to tfar
float trace(sampler2D H, sampler2D P, int n, int top, float sp, vec2 g0, vec3 o, vec3 d, float tfar, out int hci, out int hcj) {
  float ex = (o.x - g0.x) / sp, ey = (o.y - g0.y) / sp, oz = o.z;
  float dx = d.x / sp, dy = d.y / sp, dz = d.z, X1 = float(n - 1);
  float tx = dx > 0.0 ? (X1 - ex) / dx : (dx < 0.0 ? -ex / dx : INF);
  float ty = dy > 0.0 ? (X1 - ey) / dy : (dy < 0.0 ? -ey / dy : INF);
  float tExit = min(tfar, min(tx, ty)), t = 0.0; int L = top; hci = 0; hcj = 0;
  if (ex < 0.0 || ey < 0.0 || ex > X1 || ey > X1) return INF;
  for (int it = 0; it < MAX_ITER; it++) {
    if (!(t < tExit)) break;
    float te = t + PROBE, qx = ex + dx * te, qy = ey + dy * te;
    int sz = 1 << L, w = (n - 1) >> L;
    int ci = int(floor(qx / float(sz))), cj = int(floor(qy / float(sz)));
    if (dx < 0.0 && float(ci * sz) == qx) ci -= 1;
    if (dy < 0.0 && float(cj * sz) == qy) cj -= 1;
    ci = clamp(ci, 0, w - 1); cj = clamp(cj, 0, w - 1);
    float x0 = float(ci * sz), y0 = float(cj * sz);
    float cx = dx > 0.0 ? (x0 + float(sz) - ex) / dx : (dx < 0.0 ? (x0 - ex) / dx : INF);
    float cy = dy > 0.0 ? (y0 + float(sz) - ey) / dy : (dy < 0.0 ? (y0 - ey) / dy : INF);
    float tc = min(min(cx, cy), tExit);
    if (tc <= t) tc = t + PROBE;
    if (min(oz + dz * t, oz + dz * tc) > texelFetch(P, ivec2(ci, cj), L).r - eye.z) { t = tc; if (L < top) L++; continue; }
    if (L > 0) { L--; continue; }
    float fx = float(ci), fy = float(cj), lo = t, hi = -1.0;
    if ((oz + dz * t) - lerpH(H, ci, cj, cl(ex + dx * t - fx), cl(ey + dy * t - fy)) <= 0.0) hi = t;
    else {
      for (int k = 1; k <= K; k++) {
        float tq = t + (tc - t) * (float(k) / float(K));
        if ((oz + dz * tq) - lerpH(H, ci, cj, cl(ex + dx * tq - fx), cl(ey + dy * tq - fy)) <= 0.0) { hi = tq; break; }
        lo = tq;
      }
      if (hi >= 0.0) for (int b = 0; b < BIS; b++) {
        float m = 0.5 * (lo + hi);
        if ((oz + dz * m) - lerpH(H, ci, cj, cl(ex + dx * m - fx), cl(ey + dy * m - fy)) <= 0.0) hi = m; else lo = m;
      }
    }
    if (hi >= 0.0) { hci = ci; hcj = cj; return hi; }
    t = tc; if (L < top) L++;
  }
  return INF;
}
// near grid first; a ray that leaves it without a hit carries on in the far grid from where it left
float castAll(vec3 o, vec3 d, float tfar, out int which, out int ci, out int cj) {
  which = 0; float t = trace(uH, uP, n, top, sp, g0, o, d, tfar, ci, cj);
  if (t < INF || useFar == 0) return t;
  vec2 lo = g0, hi = g0 + sp * float(n - 1); float tn = 0.0;
  if (all(greaterThanEqual(o.xy, lo)) && all(lessThanEqual(o.xy, hi))) {
    float ax = d.x > 0.0 ? (hi.x - o.x) / d.x : (d.x < 0.0 ? (lo.x - o.x) / d.x : INF);
    float ay = d.y > 0.0 ? (hi.y - o.y) / d.y : (d.y < 0.0 ? (lo.y - o.y) / d.y : INF);
    tn = min(ax, ay);
  }
  if (!(tn < tfar)) return INF;
  which = 1; t = trace(uH1, uP1, n1, top1, sp1, g01, o + d * tn, d, tfar - tn, ci, cj);
  return t < INF ? tn + t : INF;
}
vec3 normalAt(int which, int ci, int cj, vec3 P) {    // central differences in the hit cell of the grid it was found in
  float s = which == 0 ? sp : sp1; vec2 g = which == 0 ? g0 : g01;
  float uh = cl((P.x - g.x) / s - float(ci)), vh = cl((P.y - g.y) / s - float(cj));
  const float h = 0.015625;
  float ua = min(max(uh, h), 1.0 - h), va = min(max(vh, h), 1.0 - h), gx, gy;
  if (which == 0) { gx = lerpH(uH, ci, cj, ua + h, vh) - lerpH(uH, ci, cj, ua - h, vh); gy = lerpH(uH, ci, cj, uh, va + h) - lerpH(uH, ci, cj, uh, va - h); }
  else { gx = lerpH(uH1, ci, cj, ua + h, vh) - lerpH(uH1, ci, cj, ua - h, vh); gy = lerpH(uH1, ci, cj, uh, va + h) - lerpH(uH1, ci, cj, uh, va - h); }
  return normalize(vec3(-gx / (2.0 * h * s), -gy / (2.0 * h * s), 1.0));
}
void main() {
  vec2 ndc = 2.0 * gl_FragCoord.xy / size - 1.0;                 // pixel centres, as pick.mjs rayFromScreen
  vec3 d = normalize(cf + cr * (ndc.x * tanHalf * aspect) + cu * (ndc.y * tanHalf));
  int which, ci, cj, w2, si, sj;
  float T = castAll(vec3(eye.xy, 0.0), d, INF, which, ci, cj), shadow = -1.0, seen = -1.0, lam = 0.0;
  if (T < INF) {
    vec3 P = vec3(eye.xy, 0.0) + d * T, nrm = normalAt(which, ci, cj, P);   // local x, y; z above the eye
    if (useSun == 1) {
      shadow = castAll(P + nrm * 0.1, sun, INF, w2, si, sj) < INF ? 1.0 : 0.0;
      lam = max(dot(nrm, sun), 0.0);
    } else lam = 0.25 + 0.75 * max(dot(nrm, light), 0.0);     // no sun control on: a plain hillshade light
    if (useTarget == 1) {
      vec3 a = P + vec3(0.0, 0.0, person), v = vec3(target.xy, target.z - eye.z) - a; float L = length(v);
      seen = castAll(a, v / L, max(L - 1.0, 0.01), w2, si, sj) < INF ? 0.0 : 1.0;
    }
  }
  if (mode == 1) { o = vec4(T, shadow, seen, lam); return; }
  if (!(T < INF)) { o = vec4(bg, 1.0); return; }
  vec3 c = vec3(0.30, 0.37, 0.27) * (0.30 + 0.75 * (shadow > 0.5 ? 0.0 : lam));
  if (shadow > 0.5) c *= vec3(0.9, 0.95, 1.15);
  if (seen > 0.5) c = mix(c, vec3(0.2, 0.75, 0.85), 0.35);
  o = vec4(mix(c, bg, min(T / 3000.0, 0.8)), 1.0);
}`;

// The ray caster on a WebGL2 context: upload a grid, render a pose to the canvas or read back distances.
export function createRaycaster(gl) {
  if (!gl.getExtension('EXT_color_buffer_float')) throw Error('EXT_color_buffer_float is missing');
  const sh = (t, s) => { const x = gl.createShader(t); gl.shaderSource(x, s); gl.compileShader(x);
    if (!gl.getShaderParameter(x, gl.COMPILE_STATUS)) throw Error(gl.getShaderInfoLog(x)); return x; };
  const prog = gl.createProgram();
  gl.attachShader(prog, sh(gl.VERTEX_SHADER, VS)); gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FS)); gl.linkProgram(prog);
  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) throw Error(gl.getProgramInfoLog(prog));
  const U = {}; for (const k of ['uH', 'uP', 'uH1', 'uP1', 'n', 'top', 'n1', 'top1', 'useFar', 'mode', 'useSun', 'useTarget', 'size', 'g0', 'g01',
    'sp', 'sp1', 'tanHalf', 'aspect', 'person', 'eye', 'cf', 'cr', 'cu', 'sun', 'target', 'bg', 'light']) U[k] = gl.getUniformLocation(prog, k);
  const vao = gl.createVertexArray(); gl.bindVertexArray(vao);
  const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
  const pl = gl.getAttribLocation(prog, 'p'); gl.enableVertexAttribArray(pl); gl.vertexAttribPointer(pl, 2, gl.FLOAT, false, 0, 0);
  const grids = [null, null], texs = [[null, null], [null, null]]; let dataFb = null, dataTex = null, dataSize = [0, 0];
  const tex = () => { const t = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, t);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); return t; };
  // g: { h: Float32Array n*n (rows south to north), n, spacing, x0, y0 } local metres; level 0 near, 1 far (or null)
  function upload(g, level = 0) {
    gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
    for (const t of texs[level]) if (t) gl.deleteTexture(t);
    if (!g) { grids[level] = null; texs[level] = [null, null]; return; }
    gl.activeTexture(gl.TEXTURE0); const texH = tex(); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32F, g.n, g.n); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, g.n, g.n, gl.RED, gl.FLOAT, g.h);
    const levels = buildPyramid(g.h, g.n), c = g.n - 1;
    gl.activeTexture(gl.TEXTURE1); const texP = tex(); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
    gl.texStorage2D(gl.TEXTURE_2D, levels.length, gl.R32F, c, c);
    levels.forEach((lv, l) => gl.texSubImage2D(gl.TEXTURE_2D, l, 0, 0, c >> l, c >> l, gl.RED, gl.FLOAT, lv));
    grids[level] = { ...g, top: levels.length - 1 }; texs[level] = [texH, texP];
  }
  // p: { eye: [x, y, z], yaw, pitch, fovy, width, height, sun?: [x, y, z], target?: [x, y, z], bg?: [r, g, b] }
  function setup(p, mode) {
    const b = basis(p.yaw, p.pitch);
    gl.useProgram(prog); gl.bindVertexArray(vao);
    const g = grids[0], f = grids[1] || grids[0], tf = grids[1] ? texs[1] : texs[0];
    [texs[0][0], texs[0][1], tf[0], tf[1]].forEach((t, k) => { gl.activeTexture(gl.TEXTURE0 + k); gl.bindTexture(gl.TEXTURE_2D, t); });
    gl.uniform1i(U.uH, 0); gl.uniform1i(U.uP, 1); gl.uniform1i(U.uH1, 2); gl.uniform1i(U.uP1, 3);
    gl.uniform1i(U.n, g.n); gl.uniform1i(U.top, g.top); gl.uniform1i(U.n1, f.n); gl.uniform1i(U.top1, f.top);
    gl.uniform1i(U.useFar, grids[1] ? 1 : 0); gl.uniform1i(U.mode, mode);
    gl.uniform2f(U.g01, f.x0, f.y0); gl.uniform1f(U.sp1, f.spacing);
    gl.uniform1i(U.useSun, p.sun ? 1 : 0); gl.uniform1i(U.useTarget, p.target ? 1 : 0);
    gl.uniform2f(U.size, p.width, p.height); gl.uniform2f(U.g0, g.x0, g.y0); gl.uniform1f(U.sp, g.spacing);
    gl.uniform1f(U.tanHalf, Math.tan(p.fovy / 2)); gl.uniform1f(U.aspect, p.width / p.height); gl.uniform1f(U.person, p.person ?? DEFAULTS.person);
    gl.uniform3fv(U.eye, p.eye); gl.uniform3fv(U.cf, b.f); gl.uniform3fv(U.cr, b.r); gl.uniform3fv(U.cu, b.u);
    gl.uniform3fv(U.sun, p.sun || [0, 0, 1]); gl.uniform3fv(U.target, p.target || [0, 0, 0]); gl.uniform3fv(U.bg, p.bg || [0.04, 0.04, 0.04]);
    gl.uniform3fv(U.light, sunVector({ azimuth: 315, elevation: 45 }));
    gl.viewport(0, 0, p.width, p.height); gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND);
  }
  return {
    ready: () => !!grids[0], grid: (level = 0) => grids[level], upload,
    render(p) { if (!grids[0]) return false; gl.bindFramebuffer(gl.FRAMEBUFFER, null); setup(p, 0); gl.drawArrays(gl.TRIANGLES, 0, 3); return true; },
    // Per pixel, rows from the TOP: [distance (1e30 sky), shadow (1, 0, -1 no ground), seen (1, 0, -1), sun term].
    readData(p) {
      if (!grids[0]) return null;
      if (!dataFb || dataSize[0] !== p.width || dataSize[1] !== p.height) {
        if (dataTex) gl.deleteTexture(dataTex);
        gl.activeTexture(gl.TEXTURE4); dataTex = tex(); gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, p.width, p.height);
        dataFb = dataFb || gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, dataFb);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, dataTex, 0); dataSize = [p.width, p.height];
      }
      gl.bindFramebuffer(gl.FRAMEBUFFER, dataFb); setup(p, 1); gl.drawArrays(gl.TRIANGLES, 0, 3);
      const raw = new Float32Array(p.width * p.height * 4), out = new Float32Array(raw.length);
      gl.readPixels(0, 0, p.width, p.height, gl.RGBA, gl.FLOAT, raw); gl.bindFramebuffer(gl.FRAMEBUFFER, null);
      const row = p.width * 4;
      for (let y = 0; y < p.height; y++) out.set(raw.subarray((p.height - 1 - y) * row, (p.height - y) * row), y * row);
      return out;
    }
  };
}

// Gathers the measured ground around (cx, cy) into a node grid, a few rows per slice, then calls done(grid).
// Unmeasured nodes get NO_GROUND. Returns a cancel function.
export function gatherGrid(measuredAt, cx, cy, { nodes = DEFAULTS.nodes, spacing = DEFAULTS.spacing, sliceMs = DEFAULTS.sliceMs } = {}, done, later = f => setTimeout(f, 0)) {
  const half = (nodes - 1) / 2 * spacing, x0 = Math.round((cx - half) / spacing) * spacing, y0 = Math.round((cy - half) / spacing) * spacing;
  const h = new Float32Array(nodes * nodes); let j = 0, cancelled = false, measured = 0;
  const slice = () => {
    if (cancelled) return;
    const t0 = performance.now();
    do {                                    // at least one row a slice, however small the budget
      for (let i = 0; i < nodes; i++) { const v = measuredAt(x0 + i * spacing, y0 + j * spacing); if (Number.isFinite(v)) { h[j * nodes + i] = v; measured++; } else h[j * nodes + i] = NO_GROUND; }
      j++;
    } while (j < nodes && performance.now() - t0 < sliceMs);
    if (j < nodes) later(slice); else done({ h, n: nodes, spacing, x0, y0, measured });
  };
  later(slice);
  return () => { cancelled = true; };
}

// The optional layer. w: { lines, canvas, state, FOV, measuredAt, groundVersion, origin, sun, invalidate, doc }.
// Returns null (and the caller hides the switch) where WebGL2 cannot run it.
export function mountRaycast(w, opts = {}) {
  const o = { ...DEFAULTS, ...opts }, doc = w.doc || document;
  let gl2 = null, rc = null;
  const off = doc.createElement('canvas');
  try { gl2 = off.getContext('webgl2', { antialias: false, depth: false, alpha: false }); if (gl2) rc = createRaycaster(gl2); } catch { rc = null; }
  if (!rc) return null;
  let on = false, cancel = null, built = null, target = null, lastGv = -1, lastMs = 0, frames = 0, gatherMs = 0;
  const gl1 = w.lines.context(), T1 = gl1.createTexture(), B1 = gl1.createBuffer();
  gl1.bindTexture(gl1.TEXTURE_2D, T1);
  for (const [k, v] of [[gl1.TEXTURE_MIN_FILTER, gl1.LINEAR], [gl1.TEXTURE_MAG_FILTER, gl1.LINEAR],
    [gl1.TEXTURE_WRAP_S, gl1.CLAMP_TO_EDGE], [gl1.TEXTURE_WRAP_T, gl1.CLAMP_TO_EDGE]]) gl1.texParameteri(gl1.TEXTURE_2D, k, v);
  gl1.bindBuffer(gl1.ARRAY_BUFFER, B1); gl1.bufferData(gl1.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl1.STATIC_DRAW);
  const q = gl1.createProgram();
  for (const [t, s] of [[gl1.VERTEX_SHADER, 'attribute vec2 p; varying vec2 v; void main() { v = p * 0.5 + 0.5; gl_Position = vec4(p, 0.0, 1.0); }'],
    [gl1.FRAGMENT_SHADER, 'precision mediump float; uniform sampler2D t; varying vec2 v; void main() { gl_FragColor = texture2D(t, v); }']]) {
    const s1 = gl1.createShader(t); gl1.shaderSource(s1, s); gl1.compileShader(s1); gl1.attachShader(q, s1);
  }
  gl1.linkProgram(q);
  const qp = gl1.getAttribLocation(q, 'p'), qt = gl1.getUniformLocation(q, 't');
  // Grids are anchored in national-grid metres, so an origin move does not shift the ground under the viewer.
  // The near grid (1025 nodes at 2 m) comes first and is drawn at once; the far grid (1025 at 8 m, where the
  // far-ring tiles give ground past the 1 m tiles) follows. A far node with no measured ground stays NO_GROUND.
  let farBuilt = null;
  function gather() {
    const s = w.state(), og = w.origin(); cancel?.(); const t0 = performance.now();
    cancel = gatherGrid(w.measuredAt, s.pos[0], s.pos[1], o, g => {
      gatherMs = performance.now() - t0; lastMs = performance.now();
      rc.upload(g, 0); built = { e0: g.x0 + og.e, n0: g.y0 + og.n, measured: g.measured, cx: s.pos[0] + og.e, cy: s.pos[1] + og.n };
      w.invalidate({ ground: false });
      cancel = gatherGrid(w.measuredAt, s.pos[0], s.pos[1], { ...o, nodes: o.farNodes, spacing: o.farSpacing }, f => {
        cancel = null;
        // a far grid that measured nothing past the near one adds nothing: leave the far grid off
        const beyond = f.measured > g.measured / ((o.farSpacing / o.spacing) ** 2) * 1.05;
        rc.upload(beyond ? f : null, 1); farBuilt = beyond ? { e0: f.x0 + og.e, n0: f.y0 + og.n, measured: f.measured } : null;
        w.invalidate({ ground: false });
      });
    });
  }
  function stale() {
    if (!built) return !cancel;
    const s = w.state(), og = w.origin(), reach = (o.nodes - 1) * o.spacing / 4;
    if (Math.hypot(s.pos[0] + og.e - built.cx, s.pos[1] + og.n - built.cy) > reach) return true;
    return w.groundVersion() !== lastGv && performance.now() - lastMs > 1500;
  }
  // Called by lines.draw right after it clears: ray cast into the WebGL2 canvas, then paint it as the backdrop.
  function backdrop(gl, width, height) {
    if (!on) return;
    if (stale() && !cancel) { lastGv = w.groundVersion(); gather(); }
    if (!rc.ready() || !built) return;
    const s = w.state(), og = w.origin(), sunNow = w.sun?.();
    rc.grid(0).x0 = built.e0 - og.e; rc.grid(0).y0 = built.n0 - og.n;
    if (farBuilt && rc.grid(1)) { rc.grid(1).x0 = farBuilt.e0 - og.e; rc.grid(1).y0 = farBuilt.n0 - og.n; }
    const W = Math.max(1, Math.round(width * o.scale)), H = Math.max(1, Math.round(height * o.scale));
    if (off.width !== W || off.height !== H) { off.width = W; off.height = H; }
    rc.render({ eye: s.pos, yaw: s.yaw, pitch: s.pitch, fovy: w.FOV, width: W, height: H,
      sun: sunNow && sunNow.elevation > 0 ? sunVector(sunNow) : null, target: target ? [target[0] - w.origin().e, target[1] - w.origin().n, target[2]] : null });
    gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, T1);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);          // a canvas's top row is the texture's last
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, off); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
    gl.useProgram(q); gl.bindBuffer(gl.ARRAY_BUFFER, B1); gl.enableVertexAttribArray(qp); gl.vertexAttribPointer(qp, 2, gl.FLOAT, false, 0, 0);
    gl.uniform1i(qt, 0);
    gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND); gl.depthMask(false);
    gl.drawArrays(gl.TRIANGLES, 0, 3);
    gl.enable(gl.BLEND); gl.depthMask(true);
    frames++;
  }
  return {
    set(v) { on = !!v; w.lines.setBackdrop(on ? backdrop : null); if (!on) { cancel?.(); cancel = null; } w.invalidate({ ground: false }); },
    on: () => on,
    // A point in national-grid metres (e, n, height m) to test sightlines to; null clears it.
    setTarget(t) { target = t; w.invalidate({ ground: false }); },
    readData: p => rc.readData(p), grid: () => rc.grid(),
    debug: () => ({ on, ready: rc.ready(), gathering: !!cancel, frames, gatherMs: Math.round(gatherMs), measured: built?.measured ?? 0,
      far: farBuilt ? { measured: farBuilt.measured, nodes: o.farNodes, spacing: o.farSpacing } : null,
      nodes: o.nodes, spacing: o.spacing, scale: o.scale, sun: !!w.sun?.(), target })
  };
}