Skip to content

Official Codebase of 3DCodeVerse

Python
38.4 KB
Raw Blame
/**
 * The world beyond the scene — optional closures for one measured
 * requirement: from every authored camera the far field does not end
 * in bare sky at the ground line and the sky is not a void (rubric
 * `world_edge_visible`: a hard world edge, a terrain cliff or a missing
 * horizon).  `worldShell` is one way to meet it — a graded sky dome,
 * fog the colour of the horizon and, only when asked, a generic
 * silhouette line.  A sea horizon, a city skyline, a forest wall, a
 * canyon side or one distant peak close a scene just as well: build
 * what the brief describes.
 */

import * as THREE from 'three';

import { fbm2, mulberry32 } from './noise.js';
import { patchStandard, sunVector } from './shader.js';
import { skyRadiance } from './sky.js';
import { snapshotResources, attachDisposal } from './lifecycle.js';

/**
 * Four example palettes.  Any sky is just `zenith` / `horizon` /
 * `ridgeColor` / `fogDensity` numbers — pass the brief's (a dusk, a
 * storm, a blizzard, an alien sky); `mood` only picks the defaults.
 */
export const MOODS = {
  day: { zenith: 0x5d8fd6, horizon: 0xdbe3ea, ridge: 0x9fb2c4,
         fogDensity: 0.0018 },
  golden: { zenith: 0x3f5a9e, horizon: 0xf2c17e, ridge: 0x8c7a90,
            fogDensity: 0.0022 },
  night: { zenith: 0x060a18, horizon: 0x1d2a4a, ridge: 0x0c1226,
           fogDensity: 0.0025 },
  overcast: { zenith: 0x9aa6b2, horizon: 0xd8dde2, ridge: 0xaab4bd,
              fogDensity: 0.003 },
};

/**
 * One enclosure for an interior: a rectangular room on a given box,
 * with rectangular openings (windows, doors) cut where you put them.
 * What is measured for ANY interior: it reads enclosed from the
 * authored cameras (walls and a lid where the brief implies them —
 * six interior runs without them were judged "not enclosed, a diorama
 * on a flat plane, tool racks floating at a missing wall", 0.0–0.3,
 * 2026-09-07), no placed object intersects a wall (placement gate),
 * and the lid is named `Ceiling` under a `RoomShell` group so the
 * overview rig can lift it.  A nave, a vault, an L-shape or an
 * open-sided shed is hand-built to the same names.
 *
 * @param {object} opts
 *   `center` [x, y, z] and `extents` [w, h, d] of the room's inner box
 *   (often, not necessarily, the plan bounds; floor at center.y - h/2,
 *   ceiling at center.y + h/2);
 *   `thickness` wall thickness in metres (default 0.3, built OUTWARD so
 *   the inner face is exactly the bounds face); `openings` list of
 *   `{ face: 'west'|'east'|'north'|'south', center: [along, up],
 *   size: [width, height] }` in metres — `along` runs +z (west/east
 *   faces) or +x (north/south), `up` from the floor; `wallColor`,
 *   `ceilingColor` hex; `ceiling` false for an open-topped set;
 *   `glazed` true for a glass house (conservatory, greenhouse, palm
 *   house, glass atrium): every wall and the roof become clear glass
 *   panes on an iron frame grid — `frameColor` hex, `bay` [across, up]
 *   frame spacing in metres (default [1.6, 2.4]).  Measured 2026-09-23
 *   on the conservatory brief: an opaque shell around a glass house
 *   was judged "a solid white box encloses the dome, blocking the
 *   exterior view and lighting" (critical) and the room lit by fill
 *   alone; the runs that scored carved every wall into one opening and
 *   hand-built the same glass and grid.
 * @returns {THREE.Group} named 'RoomShell' — walls 'Wall_<face>' (and
 *   'Wall_<face>_<n>' panels around an opening), ceiling 'Ceiling'.
 *   All cast and receive shadows.  Glazed: the panes keep those names
 *   but cast no shadow (the sun comes through; the frame's shadow grid
 *   falls on the floor), the roof pane is 'Roof' (a see-through roof
 *   stays in the overview views), and the bars are one InstancedMesh
 *   'Frame'.
 */
export function roomShell(opts = {}) {
  const [cx, cy, cz] = opts.center || [0, 0, 0];
  const [w, h, d] = opts.extents || [10, 3, 10];
  const t = opts.thickness || 0.3;
  const y0 = cy - h / 2;
  const wallMat = new THREE.MeshStandardMaterial({ color: opts.wallColor === undefined ? 0xb9ad98 : opts.wallColor, roughness: 0.92 });
  const ceilMat = new THREE.MeshStandardMaterial({ color: opts.ceilingColor === undefined ? 0x8d7f6a : opts.ceilingColor, roughness: 0.95 });
  const group = new THREE.Group();
  group.name = 'RoomShell';
  // The enclosure is not a placed thing: it defines the floor line.  Loop 22's crypt (2026-09-09)
  // read "RoomShell is floating 39.8 m above the ground" against a terrain the env session had
  // sunk to -40 m, and the judge repeated it as critical.  Same tag as a bird: not on anything.
  group.userData.placement = 'free';
  const glazed = !!opts.glazed;
  const glassMat = glazed ? new THREE.MeshStandardMaterial({
    color: 0xe6f2f0, roughness: 0.05, metalness: 0.0, transparent: true, opacity: 0.14,
    depthWrite: false, side: THREE.DoubleSide }) : null;
  const frameMat = glazed ? new THREE.MeshStandardMaterial({
    color: opts.frameColor === undefined ? 0x1f2b24 : opts.frameColor, roughness: 0.55, metalness: 0.6 }) : null;
  const [bayA, bayU] = opts.bay || [1.6, 2.4];
  const bar = 0.08;                  // frame bar section (m)
  const bars = [];                   // [cx, cy, cz, sx, sy, sz] per bar
  // The frame of one glazed panel: bars on a WORLD lattice (so the grid runs on across
  // adjacent panels and around an opening) plus a bar on every panel edge.
  // `thin` is the slab's thickness axis (0 x, 1 y, 2 z), known from the face — never the smallest
  // size: a sill or a strip between openings can be narrower than the wall is thick.
  const frame = (sx, sy, sz, x, y, z, thin) => {
    const dims = [sx, sy, sz], c = [x, y, z];
    const [i, j] = [0, 1, 2].filter((k) => k !== thin);
    const step = (k) => (k === 1 ? bayU : bayA);
    const lines = (k) => {
      const lo = c[k] - dims[k] / 2, hi = c[k] + dims[k] / 2, s = step(k), out = [lo, hi];
      for (let v = Math.ceil((lo + 0.3) / s) * s; v < hi - 0.3; v += s) out.push(v);
      return out;
    };
    for (const [along, across] of [[i, j], [j, i]]) {
      for (const v of lines(across)) {
        const size = [0, 0, 0], at = [...c];
        size[thin] = Math.max(0.04, Math.min(dims[thin], 0.15)); size[along] = dims[along]; size[across] = bar;
        at[across] = v;
        bars.push([...at, ...size]);
      }
    }
  };
  const slab = (name, sx, sy, sz, x, y, z, mat, thin) => {
    if (glazed) {
      // one sheet of glass in the slab's mid-plane (a 0.3 m glass BOX is four faces of glass)
      const sheet = [sx, sy, sz];
      sheet[thin] = 0.02;
      const m = new THREE.Mesh(new THREE.BoxGeometry(...sheet), glassMat);
      m.name = name === 'Ceiling' ? 'Roof' : name; m.position.set(x, y, z);
      group.add(m);
      frame(sx, sy, sz, x, y, z, thin);
      return m;
    }
    const m = new THREE.Mesh(new THREE.BoxGeometry(sx, sy, sz), mat);
    m.name = name; m.position.set(x, y, z); m.castShadow = true; m.receiveShadow = true;
    group.add(m);
    return m;
  };
  // each face: its outward normal axis, the length along the face, and where along/up map
  const faces = {
    west:  { x: cx - w / 2 - t / 2, z: cz, len: d, axis: 'x' },
    east:  { x: cx + w / 2 + t / 2, z: cz, len: d, axis: 'x' },
    north: { x: cx, z: cz - d / 2 - t / 2, len: w, axis: 'z' },
    south: { x: cx, z: cz + d / 2 + t / 2, len: w, axis: 'z' },
  };
  for (const [face, f] of Object.entries(faces)) {
    const holes = (opts.openings || []).filter((o) => o.face === face);
    // 1-D cuts along the face: solid spans between openings, and above/below each opening
    const along = (v) => (f.axis === 'x' ? [f.x, v] : [v, f.z]);   // -> [x, z] on the face
    const origin = (f.axis === 'x' ? cz : cx) - f.len / 2;
    const place = (name, a0, a1, b0, b1) => {
      if (a1 - a0 < 0.01 || b1 - b0 < 0.01) return;
      const [x, z] = along(origin + (a0 + a1) / 2);
      const sx = f.axis === 'x' ? t : a1 - a0, sz = f.axis === 'x' ? a1 - a0 : t;
      slab(name, sx, b1 - b0, sz, x, y0 + (b0 + b1) / 2, z, wallMat, f.axis === 'x' ? 0 : 2);
    };
    if (!holes.length) { place(`Wall_${face}`, 0, f.len, 0, h); continue; }
    const clipped = holes.map((o) => ({
      a0:Math.max(0, o.center[0] - o.size[0] / 2),
      a1:Math.min(f.len, o.center[0] + o.size[0] / 2),
      b0:Math.max(0, o.center[1] - o.size[1] / 2),
      b1:Math.min(h, o.center[1] + o.size[1] / 2),
    })).filter((o) => o.a1 > o.a0 && o.b1 > o.b0);
    // Subtract the UNION of openings. Independent sills/lintels fill each
    // other's holes when windows overlap horizontally or sit above a door.
    const cuts = [...new Set([0, f.len, ...clipped.flatMap((o) => [o.a0, o.a1])])]
        .sort((a, b) => a - b);
    let n = 0;
    for (let i = 0; i + 1 < cuts.length; i++) {
      const a0 = cuts[i], a1 = cuts[i + 1], middle = (a0 + a1) / 2;
      const intervals = clipped.filter((o) => o.a0 < middle && middle < o.a1)
          .sort((a, b) => a.b0 - b.b0);
      let bottom = 0;
      for (const opening of intervals) {
        place(`Wall_${face}_${n++}`, a0, a1, bottom, opening.b0);
        bottom = Math.max(bottom, opening.b1);
      }
      place(`Wall_${face}_${n++}`, a0, a1, bottom, h);
    }
  }
  if (opts.ceiling !== false) slab('Ceiling', w + 2 * t, t, d + 2 * t, cx, y0 + h + t / 2, cz, ceilMat, 1);
  if (bars.length) {
    const inst = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1), frameMat, bars.length);
    const m4 = new THREE.Matrix4(), q = new THREE.Quaternion();
    bars.forEach(([x, y, z, sx, sy, sz], n) => inst.setMatrixAt(n, m4.compose(new THREE.Vector3(x, y, z), q, new THREE.Vector3(sx, sy, sz))));
    inst.instanceMatrix.needsUpdate = true;
    inst.computeBoundingBox(); inst.computeBoundingSphere();
    inst.name = 'Frame'; inst.castShadow = true; inst.receiveShadow = true;
    group.add(inst);
  }
  const owned = snapshotResources(group);
  owned.add(wallMat);owned.add(ceilMat);
  if (glazed) { owned.add(glassMat); owned.add(frameMat); }
  return attachDisposal(group, owned);
}

/**
 * A graded sky dome + fog the colour of its horizon, and an opt-in
 * silhouette line.
 *
 * @param {object} [opts]
 *   `rand` seeded PRNG (REQUIRED when `ridge` is on — no Math.random);
 *   `mood` one of `day | golden | night | overcast` (default `day`;
 *   see MOODS); `radius` metres to the sky (default 4000 — far outside
 *   any scene); `bounds` content radius in metres (the same number
 *   `sunRig` takes) — clamps fog so the far edge of the content stays
 *   visible on km-scale worlds; `ridge` true adds a generic seeded
 *   silhouette line at 82 % of the radius (default off: a brief whose
 *   horizon is something — a sea, a plain, a skyline, a forest edge,
 *   one named mountain — builds that horizon instead); `ridgeHeight`
 *   its crest as a fraction of the radius (default 0.055);
 *   `ridgeProfile(azimuthDeg) => 0..1` shapes the crest by compass
 *   sector (0 = no crest there; default a seeded random walk), so it
 *   can be directional or partial; `zenith`/`horizon`/`ridgeColor` hex
 *   overrides; `fogDensity` override (wins over the clamp).
 * @returns {{group: THREE.Group, fog: THREE.FogExp2, radius: number}}
 *   Add `group` to the scene and assign `fog` to `scene.fog`.
 */
export function worldShell(opts = {}) {
  const rand = opts.rand || (() => 0.5);
  const mood = MOODS[opts.mood] || MOODS.day;
  const radius = opts.radius || 4000;
  const zenith = new THREE.Color(
      opts.zenith === undefined ? mood.zenith : opts.zenith);
  const horizon = new THREE.Color(
      opts.horizon === undefined ? mood.horizon : opts.horizon);

  const group = new THREE.Group();
  group.name = 'WorldShell';

  // SKY: gradient by vertex colour, rendered on the inside faces. A
  // shader would be nicer; vertex colours are free and never break.
  const sky = new THREE.SphereGeometry(radius, 32, 18);
  const pos = sky.attributes.position;
  const cols = new Float32Array(pos.count * 3);
  const c = new THREE.Color();
  for (let i = 0; i < pos.count; i++) {
    // t: 1 at the zenith, 0 at (and below) the horizon.
    const t = Math.max(0, pos.getY(i) / radius);
    c.copy(horizon).lerp(zenith, Math.pow(t, 0.62));
    cols[i * 3] = c.r; cols[i * 3 + 1] = c.g; cols[i * 3 + 2] = c.b;
  }
  sky.setAttribute('color', new THREE.BufferAttribute(cols, 3));
  const skyMesh = new THREE.Mesh(sky, new THREE.MeshBasicMaterial({
    vertexColors: true, side: THREE.BackSide, fog: false,
    depthWrite: false,
  }));
  skyMesh.name = 'SkyGradient';
  skyMesh.renderOrder = -2;
  group.add(skyMesh);

  // HORIZON RIDGE (opt-in): a generic silhouette line at 82% of the sky
  // radius — one way to keep the ground line off bare sky.  A brief that
  // gives its horizon a character builds that instead, or shapes this
  // one through `ridgeHeight` / `ridgeProfile`.
  if (opts.ridge === true) {
    const rr = radius * 0.82;
    const seg = 160;
    const ridgeH = radius
        * (opts.ridgeHeight === undefined ? 0.055 : opts.ridgeHeight);
    const profile = typeof opts.ridgeProfile === 'function'
        ? opts.ridgeProfile : null;
    const shape = [];
    let h = 0.5;
    for (let i = 0; i <= seg; i++) {
      h = Math.max(0.12, Math.min(1,
          h + (rand() - 0.5) * 0.35));
      shape.push(profile
          ? Math.max(0, Math.min(1, profile((i / seg) * 360))) : h);
    }
    shape[seg] = shape[0];              // close the loop seamlessly
    const geo = new THREE.BufferGeometry();
    const verts = [];
    for (let i = 0; i < seg; i++) {
      const a0 = (i / seg) * Math.PI * 2;
      const a1 = ((i + 1) / seg) * Math.PI * 2;
      const x0 = Math.cos(a0) * rr, z0 = Math.sin(a0) * rr;
      const x1 = Math.cos(a1) * rr, z1 = Math.sin(a1) * rr;
      const y0 = shape[i] * ridgeH, y1 = shape[i + 1] * ridgeH;
      // Two triangles: base ring (-ridgeH keeps the skirt below any
      // terrain relief) up to the jagged crest.
      verts.push(x0, -ridgeH, z0, x1, -ridgeH, z1, x0, y0, z0);
      verts.push(x1, -ridgeH, z1, x1, y1, z1, x0, y0, z0);
    }
    geo.setAttribute('position',
        new THREE.BufferAttribute(new Float32Array(verts), 3));
    geo.computeVertexNormals();
    const ridge = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({
      color: opts.ridgeColor === undefined ? mood.ridge : opts.ridgeColor,
      side: THREE.DoubleSide, fog: false, depthWrite: false,
    }));
    ridge.name = 'HorizonRidge';
    ridge.renderOrder = -1;
    group.add(ridge);
  }

  // FOG matched to the horizon tint — the whole trick. Mood densities
  // assume a ~450 m world; on km-scale scenes they dissolve the
  // content, so `bounds` clamps rho <= 1.05 / (2*bounds) to keep the
  // content's far edge >= 1/3 transmittance. Explicit fogDensity wins.
  let density = mood.fogDensity;
  if (opts.bounds) {
    density = Math.min(density, 1.05 / Math.max(2 * opts.bounds, 150));
  }
  const fog = new THREE.FogExp2(
      horizon.getHex(),
      opts.fogDensity === undefined ? density : opts.fogDensity);

  attachDisposal(group, snapshotResources(group));
  return { group, fog, radius, dispose:group.userData.dispose };
}

/**
 * The middle distance, one optional filler: an annulus of rolling land
 * that blends into `heightAt` at its inner seam and rises into fbm
 * hills outward, painted as a field patchwork, with an instanced layer
 * of distant copses when asked.  Two draw calls, fogged like any land,
 * deterministic in `seed`.
 *
 * What is measured: the middle distance reads as the world continuing
 * in the brief's terms (the judge's finding is "a diorama on a vast,
 * empty flat plane" — seen even over a 6 km textured ground), and
 * whatever you build reaches the backdrop (a land edge short of it is
 * a visible cliff: rubric `world_edge_visible`).  A desert, a sea, a
 * city, a marsh or a forest needs its own continuation; the defaults
 * here sketch temperate farmland.
 *
 * @param {object} [opts]
 *   `inner` metres from the origin where this filler begins — where
 *   your own ground stops (default 160; the content need not be
 *   centred or circular, pass what fits your ground); `shellRadius`
 *   the sky radius `worldShell` used (default 4000 — pass
 *   `shell.radius` when you overrode it); `outer` metres where it ends
 *   (default `0.82 * shellRadius`, the shell's ridge line, and clamped
 *   there: a 2 km annulus under a 3.28 km ridge left a 1.2 km pale
 *   void flagged from every camera; past the ridge it would paint over
 *   that silhouette); `baseY` ground level at the seam (default 0);
 *   `heightAt` (x, z) => y, the scene's ground truth — the seam band
 *   blends from it into the relief so the joint cannot step; `relief`
 *   hill amplitude in metres at full distance (default 26; 0 for a
 *   plain or a sea); `rise` metres of gentle climb per metre of
 *   distance (default 0.004 — receding land reads flat without it);
 *   `colors` [a, b, c] hex land tones (default a muted
 *   scrub/field/dust trio — pass the brief's soil or vegetation);
 *   `fields` false paints one tone instead of the patchwork (sand,
 *   snow, scrub); `clumps` distant-copse count (default 0 — a
 *   temperate cue: pass a count only where the brief has woodland);
 *   `clumpColor` hex (default dark grey-green); `seed` (default 7);
 *   `name` group name.
 * @returns {THREE.Group} Named `Outskirts`, in world space about the
 *   origin.
 */
export function makeOutskirts(opts = {}) {
  const inner = opts.inner === undefined ? 160 : opts.inner;
  const shellRadius =
      opts.shellRadius === undefined ? 4000 : opts.shellRadius;
  // worldShell puts its ridge ring at 82% of the sky radius.
  const ridgeR = shellRadius * 0.82;
  const outer = Math.max(
      Math.min(opts.outer === undefined ? ridgeR : opts.outer, ridgeR),
      inner * 2);
  const baseY = opts.baseY === undefined ? 0 : opts.baseY;
  const heightAt =
      typeof opts.heightAt === 'function' ? opts.heightAt : null;
  const relief = opts.relief === undefined ? 26 : opts.relief;
  const rise = opts.rise === undefined ? 0.004 : opts.rise;
  const seed = opts.seed === undefined ? 7 : opts.seed;
  const rand = mulberry32(seed);
  const tones = (opts.colors || [0x7a7d5a, 0x8a825f, 0x6e7355])
      .map((c) => new THREE.Color(c));

  const group = new THREE.Group();
  group.name = opts.name || 'Outskirts';

  // RELIEF over a polar grid: amplitude zero at the seam, half by 1.8
  // seam-radii — rolls must already read in the MIDDLE ground, or the
  // annulus repeats the flat-plane defect it exists to kill. Shallow
  // valleys allowed (-relief/4): land that only bulges reads as foam.
  const noise = { seed, octaves: 4 };
  // Centre the field on its own measured mean: fbm2's centre drifts by
  // seed and region (grass.js clumpField measured the same), and an
  // uncalibrated offset either buries the annulus or foams it.
  let fbmMean = 0;
  const CAL = 16;
  for (let i = 0; i < CAL; i++) {
    for (let j = 0; j < CAL; j++) {
      const rr = inner + (outer - inner) * (i + 0.5) / CAL;
      const aa = ((j + 0.5) / CAL) * Math.PI * 2;
      fbmMean += fbm2(Math.cos(aa) * rr * 0.004,
                      Math.sin(aa) * rr * 0.004, noise);
    }
  }
  fbmMean /= CAL * CAL;
  const lift = (x, z, r) => {
    const ramp = THREE.MathUtils.smoothstep(r, inner, inner * 1.8);
    const h = (fbm2(x * 0.004, z * 0.004, noise) - fbmMean)
        * relief * ramp + rise * (r - inner) * ramp;
    const seam = heightAt
        ? heightAt(x, z) * (1 - THREE.MathUtils.smoothstep(
              r, inner, inner * 1.6))
        : 0;
    return baseY + Math.max(h, -relief * 0.25) + seam;
  };

  const RADIAL = 26;
  const AROUND = 96;
  const verts = [];
  const at = (i, j) => {
    // Radial spacing eases outward: detail near the seam, none far out.
    const t = i / RADIAL;
    const r = inner + (outer - inner) * t * t;
    const a = (j / AROUND) * Math.PI * 2;
    const x = Math.cos(a) * r;
    const z = Math.sin(a) * r;
    return [x, lift(x, z, r), z];
  };
  for (let i = 0; i < RADIAL; i++) {
    for (let j = 0; j < AROUND; j++) {
      const j1 = (j + 1) % AROUND;
      verts.push(...at(i, j), ...at(i + 1, j), ...at(i, j1));
      verts.push(...at(i + 1, j), ...at(i + 1, j1), ...at(i, j1));
    }
  }
  const geo = new THREE.BufferGeometry();
  geo.setAttribute('position',
      new THREE.BufferAttribute(new Float32Array(verts), 3));
  geo.computeVertexNormals();

  // LAND COVER in the fragment, not the lattice: by default a patchwork
  // of worked land at km scale (one fbm picks the field, a second
  // lightens or tires it) — one "the world continues" cue among many;
  // `fields: false` keeps the mottle and drops the patchwork.
  const patchwork = opts.fields !== false;
  const mat = new THREE.MeshStandardMaterial({
    color: 0xffffff, roughness: 0.96, metalness: 0, name: 'OutskirtsLand',
  });
  patchStandard(mat, {
    name: patchwork ? 'outskirts:fields' : 'outskirts:plain',   // the program cache key
    uniforms: {
      uOutA: { value: tones[0] },
      uOutB: { value: tones[1] || tones[0] },
      uOutC: { value: tones[2] || tones[0] },
    },
    vertexHead: 'varying vec2 vOutW;',
    vertexBody:
        '  vOutW = (modelMatrix * vec4(transformed, 1.0)).xz;',
    fragmentHead: [
      'uniform vec3 uOutA;',
      'uniform vec3 uOutB;',
      'uniform vec3 uOutC;',
      'varying vec2 vOutW;',
    ].join('\n'),
    fragmentBody: [
      '  float ofPick = astraFbm2(vOutW * 0.0016, 3);',
      ...(patchwork ? [
        '  vec3 ofCol = mix(uOutA, uOutB,',
        '      smoothstep(0.35, 0.5, ofPick));',
        '  ofCol = mix(ofCol, uOutC, smoothstep(0.58, 0.72, ofPick));',
      ] : ['  vec3 ofCol = uOutA;']),
      '  ofCol *= 0.9 + 0.2 * astraFbm2(vOutW * 0.012 + 37.0, 2);',
      '  diffuseColor.rgb = ofCol;',
    ].join('\n'),
  });
  const land = new THREE.Mesh(geo, mat);
  land.name = 'OutskirtsLand';
  land.receiveShadow = false;
  group.add(land);

  // WOODS (opt-in, `clumps` > 0): one instanced layer of squashed dark
  // blobs, CLUSTERED — 2-6 blobs strung along a heading make a copse or
  // a hedgerow, and a lone pancake reads as debris. Placement is biased
  // toward the seam (rand^1.8): that is the band a camera actually
  // resolves, and 140 singles spread over 28 km2 measured out to an
  // empty plain.
  const clumps = opts.clumps === undefined ? 0 : opts.clumps;
  if (clumps > 0) {
    const blob = new THREE.SphereGeometry(1, 6, 4);
    blob.scale(1, 0.55, 1);
    const cmat = new THREE.MeshStandardMaterial({
      color: opts.clumpColor === undefined ? 0x46523c : opts.clumpColor,
      roughness: 1, metalness: 0, name: 'OutskirtsCopse',
    });
    const cap = clumps * 8;
    const copse = new THREE.InstancedMesh(blob, cmat, cap);
    const m = new THREE.Matrix4();
    const q = new THREE.Quaternion();
    const s = new THREE.Vector3();
    const p = new THREE.Vector3();
    const tint = new THREE.Color();
    // Woods gather in azimuth SECTORS with open field between them: an
    // even collar of bushes read as a hedge wall around the diorama.
    const sectors = [];
    const nSec = 5 + Math.floor(rand() * 4);
    for (let i = 0; i < nSec; i++) {
      sectors.push(rand() * Math.PI * 2);
    }
    let n = 0;
    for (let i = 0; i < clumps && n < cap; i++) {
      const r = inner * 1.08
          + (outer * 0.8 - inner * 1.08) * Math.pow(rand(), 1.8);
      const a = sectors[Math.floor(rand() * nSec)]
          + (rand() - 0.5) * 0.55;
      const heading = rand() * Math.PI * 2;
      const wood = 3 + Math.floor(rand() * 6);
      const spread = 8 + 26 * rand();
      for (let k = 0; k < wood && n < cap; k++) {
        const along = (k / wood - 0.5) * spread * wood * 0.5;
        const x = Math.cos(a) * r + Math.cos(heading) * along
            + (rand() - 0.5) * spread;
        const z = Math.sin(a) * r + Math.sin(heading) * along
            + (rand() - 0.5) * spread;
        const rr = Math.hypot(x, z);
        if (rr < inner * 1.02) continue;
        // Crowns, not boulders: several small lobes overlapping close
        // to the ground read as canopy; one huge lobe reads as a rock.
        const sc = (5 + 9 * rand()) * (0.85 + 0.6 * (rr / outer));
        p.set(x, lift(x, z, rr) + sc * 0.55 * 0.45, z);
        s.set(sc * (0.8 + 0.5 * rand()), sc * 0.55, sc);
        q.setFromAxisAngle(
            new THREE.Vector3(0, 1, 0), rand() * Math.PI);
        copse.setMatrixAt(n, m.compose(p, q, s));
        // instanceColor MULTIPLIES material.color: the jitter must sit
        // around white, or the base tint is applied twice and the
        // whole layer renders near-black (measured).
        const f = 0.85 + 0.45 * rand();
        tint.setRGB(f * (0.92 + 0.12 * rand()),
                    f * (0.95 + 0.10 * rand()),
                    f * (0.88 + 0.12 * rand()));
        copse.setColorAt(n, tint);
        n++;
      }
    }
    copse.count = n;
    copse.instanceMatrix.needsUpdate = true;
    if (copse.instanceColor) copse.instanceColor.needsUpdate = true;
    copse.name = 'OutskirtsCopses';
    group.add(copse);
  }
  return attachDisposal(group, snapshotResources(group));
}


// Per-mood sun rig numbers lifted from the flagship scenes the audits
// hold up, re-measured on THIS renderer (ACES, exposure 1.0, no post
// chain, three's physical light units — a directional intensity I on a
// white lambert surface is I/pi of radiance).  `fillFloor` is a hard
// readability floor: scenes below it rendered 70-90% pure black.
// `glowCore`/`glowHalo` are LINEAR HDR radiances added to the baked
// environment on the light vector (a half-float bake, so the sun's
// reflection in water and chrome is a real highlight, not a clipped
// white blob); the moon (night) is a soft cool patch.
const RIGS = {
  day: { sun: 0xfff0d8, intensity: 5.4, azimuth: 35, elevation: 48,
         fillSky: 0x9db8e8, fillGround: 0x8a7f6a, fill: 1.4,
         sunFloor: 4.5,
         fillFloor: 1.2, glowCore: 5.0, glowHalo: 0.35, disc: true,
         discColor: 0xfff6e0 },
  golden: { sun: 0xffb36b, intensity: 5.6, azimuth: 35, elevation: 10,
            fillSky: 0x9bb8ff, fillGround: 0x8a6f5a, fill: 1.1,
            sunFloor: 4.5,
            fillFloor: 1.0, glowCore: 5.0, glowHalo: 0.6, disc: true,
            discColor: 0xffd9a0 },
  // Night is a MOON rig.  Measured on this renderer: the reference's
  // moon 0.8 / fill 0.6 (graded for a post chain at exposure 1.1) put
  // the ground at 17/255 and every wall at 4 — 30-40% of the frame
  // under the dark threshold.  2.2 / 1.0 reads "dark but lit": ground
  // ~45, shade ~14, sky 26-38.  No sun floor: night asks stand.
  night: { sun: 0xb5c7e8, intensity: 2.2, azimuth: 35, elevation: 45,
           fillSky: 0x3f5378, fillGround: 0x2a2a33, fill: 1.0,
           fillFloor: 0.8, glowCore: 1.6, glowHalo: 0.06, disc: true,
           discColor: 0xe8eef8 },
  overcast: { sun: 0xdfe4ea, intensity: 2.6, azimuth: 35, elevation: 55,
              fillSky: 0xc5ccd4, fillGround: 0x6f7377, fill: 2.0,
              sunFloor: 2.0,
              fillFloor: 1.6, glowCore: 0.25, glowHalo: 0.3, disc: false,
              discColor: 0xe9edf2 },
};

/**
 * A PRACTICAL — a lantern, lamp, sign, window or fire: an emissive surface
 * (it glows, and the post chain blooms it, but it lights nothing) plus its
 * own small PointLight beside it, which is what lights the ground around
 * it.  Starting numbers for one, measured on this renderer; flicker
 * scales `intensity`.
 */
export const PRACTICAL = Object.freeze({ emissiveIntensity: 3.0, intensity: 1.5, distance: 9, decay: 2 });

/**
 * The matched sun + fill + environment + sun-disc package. Failing
 * scenes break this package piecewise; built here, the env-map sun,
 * the shadow sun and the visible disc agree by construction and the
 * environment texture is never degenerate.
 *
 * The environment is baked from the SAME graded sky model the dome in
 * ./sky.js draws (`skyRadiance`), as a linear half-float equirect: what
 * a chrome ball, a puddle or a window reflects is the sky the camera
 * sees, sun-side haze and all, and the sun in it is HDR (radiance
 * `glowCore`) so it reads as a highlight — bloom-friendly, never a
 * clipped white band.  Overcast keeps the flat gradient bake.
 *
 * A below-horizon `elevation` (any mood) lights nothing, so the rig
 * supplies another key to meet the luminance floor: it switches to the
 * night rig — by default a moon opposite the sun's azimuth, 30-55 deg
 * up (`moonAzimuth` / `moonElevation` put it where the brief's dusk
 * wants its glow; keep an above-horizon `elevation` with
 * `mood: 'golden'` for a sun still up) — the fill and env take the
 * night mood, the disc is the moon.  `sunDir` still reports the
 * authored (below-horizon) sun so `makeSky()` paints twilight/night on
 * it; `lightDir` is the vector the shadow light actually uses (equal
 * to `sunDir` by day).
 * An explicit night mood with an above-horizon direction uses that
 * direction for the moon; `skySunDir` supplies the set sun to both
 * the environment bake and `makeSky()`.
 *
 * @param {object} [opts]
 *   `mood` one of `day | golden | night | overcast` (default `day`;
 *   `night` is a moon rig); `bounds` radius in metres of the content
 *   the shadow frustum covers (default 150); `azimuth`/`elevation`
 *   sun direction in degrees (per-mood defaults; golden sits low);
 *   `moonAzimuth`/`moonElevation` where the moon stands when the sun
 *   is set (default opposite the sun, 30-55 deg up);
 *   `sunColor`/`intensity` sun overrides; `fill` hemisphere intensity,
 *   clamped UP to the mood's readability floor (its `fillFloor` in RIGS);
 *   `fillSky`/`fillGround` hex overrides of the hemisphere's colours (the
 *   baked environment's ground half follows `fillGround`);
 *   `zenith`/`horizon` hex overrides matching `worldShell()`'s (used by
 *   the overcast/gradient bake and the below-horizon half);
 *   `radius` sky radius the disc sits inside (default 4000, matching
 *   `worldShell`); `disc` true/false forces the visible disc (default
 *   on, except overcast).
 * @returns {{sun: THREE.DirectionalLight, fill: THREE.HemisphereLight,
 *   envTex: THREE.DataTexture, sunDisc: THREE.Mesh|null,
 *   sunDir: THREE.Vector3, skySunDir: THREE.Vector3, lightDir: THREE.Vector3, night: boolean,
 *   moonDir: THREE.Vector3|null, mood: string}}
 *   Add `sun`, `fill` (and `sunDisc` when present) to the scene and
 *   assign `envTex` to `scene.environment`. The sun aims at the origin.
 *   `sunDir` is the normalized sun vector — for a physical atmosphere
 *   dome hand the whole rig to `makeSky(scene, { rig })` from ./sky.js,
 *   which uses `skySunDir` and hides the flat disc (the
 *   Preetham shader draws its own sun; a night rig's moon disc stays).
 */
export function sunRig(opts = {}) {
  let moodName = RIGS[opts.mood] ? opts.mood : 'day';
  const bounds = opts.bounds || 150;
  const radius = opts.radius || 4000;

  // The one light vector everything below shares — sun position, baked
  // env glow and visible disc can therefore never disagree.
  const azDeg = opts.azimuth === undefined ? RIGS[moodName].azimuth : opts.azimuth;
  const elDeg = opts.elevation === undefined ? RIGS[moodName].elevation : opts.elevation;
  const belowHorizon = elDeg < 0;
  if (belowHorizon) moodName = 'night';
  const rig = RIGS[moodName];
  const mood = MOODS[moodName];
  const night = moodName === 'night';
  const zenith = new THREE.Color(
      opts.zenith === undefined ? mood.zenith : opts.zenith);
  const horizon = new THREE.Color(
      opts.horizon === undefined ? mood.horizon : opts.horizon);
  const dir = sunVector(azDeg, elDeg);
  // A set sun lights nothing from under the ground: the moon rises
  // opposite it, 30-55 deg up (deeper sun, higher moon).
  const moonDir = belowHorizon
      ? sunVector(
          opts.moonAzimuth === undefined ? azDeg + 180 : opts.moonAzimuth,
          opts.moonElevation === undefined
              ? Math.max(30, Math.min(55, 25 - elDeg)) : opts.moonElevation)
      : (night ? dir.clone() : null);
  const lightDir = belowHorizon ? moonDir.clone() : dir.clone();

  // SUN: shadow-casting directional, ortho frustum fitted to `bounds`.
  // The sky dome is an UNLIT basic material at its literal hex while
  // the world is PBR-lit, and the two were never calibrated against
  // each other: measured over 11 delivered scenes, a midday meadow's
  // ground sat at 0.17 of its own sky (a lit subject lands near 0.5).
  // Requests below the floor are clamped UP, exactly as `fill` is —
  // a scene that asks for 2.8 in daylight is asking for dusk it did
  // not mean, and every colour in it dies with the light.
  const sun = new THREE.DirectionalLight(
      opts.sunColor === undefined ? rig.sun : opts.sunColor,
      Math.max(rig.sunFloor || 0,
               opts.intensity === undefined ? rig.intensity
                                            : opts.intensity));
  sun.name = 'SunRigSun';
  sun.position.copy(lightDir).multiplyScalar(bounds * 2);
  sun.castShadow = true;
  // The map is sized to the FRUSTUM, not fixed: an ortho box of 2*bounds
  // metres on a flat 2048 map gave every scene whatever texel it happened
  // to get, and the library default (bounds 150) landed at 0.146 m —
  // measured 2026-09-01, the contact shadow of a 1 m sphere came back as
  // a 14-texel staircase on OUR PCFSoftShadowMap (no post chain to hide
  // it).  Target 0.05 m/texel, clamped to the 1024..4096 a WebGL2 context
  // can be relied on for; the 4096 depth map is ~33 MB, one light.
  const shadowRes = Math.max(1024, Math.min(4096,
      2 ** Math.ceil(Math.log2(Math.max(1, (2 * bounds) / 0.05)))));
  sun.shadow.mapSize.set(shadowRes, shadowRes);
  const texel = (2 * bounds) / shadowRes;
  // Acne is cured along the NORMAL (one texel of it), not by pushing the
  // whole depth back: a constant bias big enough for a 0.3 m texel is
  // what detaches a shadow from the foot of the thing casting it.
  sun.shadow.bias = -0.0001;
  sun.shadow.normalBias = texel;
  sun.shadow.camera.left = -bounds;
  sun.shadow.camera.right = bounds;
  sun.shadow.camera.top = bounds;
  sun.shadow.camera.bottom = -bounds;
  sun.shadow.camera.near = bounds * 0.5;
  sun.shadow.camera.far = bounds * 3.5;

  // FILL: hemisphere with a per-mood readability FLOOR. Requests below
  // the floor are clamped UP — under-filling is the measured failure.
  // The darkest tenth of a frame is not shade the fill reaches — it is
  // albedo the libraries have already multiplied toward zero at contact
  // points, and no light colour rescues a black albedo.
  // Colours are the mood's unless the scene tints them (a cool sky over
  // a warm ground is the usual daylight pair), and the env bake's ground
  // half below follows the same override.
  const fillSky = opts.fillSky === undefined ? rig.fillSky : opts.fillSky;
  const fillGround = opts.fillGround === undefined ? rig.fillGround : opts.fillGround;
  const fill = new THREE.HemisphereLight(
      fillSky, fillGround,
      Math.max(rig.fillFloor,
               opts.fill === undefined ? rig.fill : opts.fill));
  fill.name = 'SunRigFill';

  // ENV: linear half-float equirect.  256 wide, not 64: PMREM sizes its
  // cube at width/4, so 64 gave every metal, glass and water reflection
  // in the corpus a 16 px source.  Never 1 px on either axis — width 1
  // emits invalid GLSL and every lit material fails.
  const w = 256;
  const h = 128;
  const data = new Uint16Array(w * h * 4);
  const groundC = new THREE.Color(fillGround);
  const glowC = new THREE.Color(rig.discColor);
  const px = new THREE.Vector3();
  const pc = new THREE.Color();
  // The explicit night mood is a moon rig with its light ABOVE the
  // horizon: the sky model must still see a set sun, or it bakes noon.
  const skySun = night && dir.y >= 0
      ? new THREE.Vector3(dir.x, -0.6, dir.z).normalize() : dir;
  const skyAt = (v) => {
    if (moodName === 'overcast') {
      pc.copy(horizon).lerp(zenith, Math.pow(v.y, 0.62));
      return [pc.r, pc.g, pc.b];
    }
    return skyRadiance(v, skySun);
  };
  // The ground half is the horizon radiance tinted by the ground: what a
  // lit field bounces back up (warm by day, near-black at night).
  const hz = skyAt(new THREE.Vector3(1, 0.001, 0));
  const ground = [hz[0] * groundC.r * 1.6, hz[1] * groundC.g * 1.6, hz[2] * groundC.b * 1.6];
  const half = THREE.DataUtils.toHalfFloat;
  for (let row = 0; row < h; row++) {
    // Row 0 is v=0, the nadir: three's equirect shader samples at
    // v = 0.5 + asin(dir.y) / PI and a DataTexture never flips Y.
    const lat = ((row + 0.5) / h - 0.5) * Math.PI;
    for (let col = 0; col < w; col++) {
      const lon = ((col + 0.5) / w - 0.5) * Math.PI * 2;
      px.set(Math.cos(lat) * Math.cos(lon), Math.sin(lat),
             Math.cos(lat) * Math.sin(lon));
      let c;
      if (px.y >= 0) {
        c = skyAt(px);
      } else {
        const t = Math.min(1, -px.y * 3);
        c = [hz[0] + (ground[0] - hz[0]) * t, hz[1] + (ground[1] - hz[1]) * t,
             hz[2] + (ground[2] - hz[2]) * t];
      }
      const ang = Math.acos(Math.max(-1, Math.min(1, px.dot(lightDir))));
      const glow = rig.glowCore * Math.exp(-(ang * ang) / 0.004) +
          rig.glowHalo * Math.exp(-(ang * ang) / 0.245);
      const i = (row * w + col) * 4;
      data[i] = half(c[0] + glowC.r * glow);
      data[i + 1] = half(c[1] + glowC.g * glow);
      data[i + 2] = half(c[2] + glowC.b * glow);
      data[i + 3] = half(1);
    }
  }
  const envTex = new THREE.DataTexture(data, w, h, THREE.RGBAFormat, THREE.HalfFloatType);
  envTex.mapping = THREE.EquirectangularReflectionMapping;
  envTex.colorSpace = THREE.LinearSRGBColorSpace;
  envTex.magFilter = THREE.LinearFilter;
  envTex.minFilter = THREE.LinearFilter;
  envTex.needsUpdate = true;

  // SUN DISC (moon at night): on the SAME normalized light vector, at
  // 90% of the sky radius so a worldShell ridge (82%), when there is
  // one, occludes it at the skyline the way hills occlude a low sun.
  let sunDisc = null;
  if (opts.disc === undefined ? rig.disc : opts.disc) {
    // The moon subtends about half a degree, independent of sky radius.
    // Keep the existing daytime disc grade; only the moon was oversized.
    const discRadius = night
        ? radius * 0.9 * Math.tan(THREE.MathUtils.degToRad(0.52 / 2))
        : radius * 0.012;
    sunDisc = new THREE.Mesh(
        new THREE.CircleGeometry(discRadius, night ? 48 : 24),
        new THREE.MeshBasicMaterial({
          color: rig.discColor, fog: false, depthWrite: false,
        }));
    sunDisc.name = night ? 'MoonDisc' : 'SunDisc';
    sunDisc.position.copy(lightDir).multiplyScalar(radius * 0.9);
    sunDisc.lookAt(0, 0, 0);
    sunDisc.renderOrder = -1.5;
  }

  const owned = sunDisc ? snapshotResources(sunDisc) : new Set();
  owned.add(envTex);owned.add(sun);owned.add(fill);
  const lifecycle = attachDisposal(new THREE.Group(), owned);
  return { sun, fill, envTex, sunDisc, sunDir: dir.clone(), skySunDir: skySun.clone(), lightDir,
           night, moonDir, mood: moodName, dispose:lifecycle.userData.dispose };
}