Claude Opus 5.5max

daichi.js

// 「土谷大地」— JavaScript だけで描く、夜明けの棚田。
// 画像・フォント・ライブラリは使わない。地形(土の谷と大地)を関数で組み、列ごとに光線を進めて
// 水鏡・畦・森・霧・空を線形光で計算し、最後に草木・人物・題字を重ねてから現像する。
(function (root) {
'use strict';

function daichiCore(P) {
  P = P || {};
  const SEED = (P.seed | 0) || 1012;
  const PI = Math.PI, DEG = PI / 180;
  const sqrt = Math.sqrt, exp = Math.exp, pow = Math.pow, abs = Math.abs, floor = Math.floor, hypot = Math.hypot;
  const clamp = (x, a, b) => (x < a ? a : x > b ? b : x);
  const sat = x => (x < 0 ? 0 : x > 1 ? 1 : x);
  const mix = (a, b, t) => a + (b - a) * t;
  const sstep = (a, b, x) => { let t = (x - a) / (b - a); t = t < 0 ? 0 : t > 1 ? 1 : t; return t * t * (3 - 2 * t); };

  // ───────────────────────── 乱数・ノイズ ─────────────────────────
  function hash2(i, j) {
    let h = Math.imul(i | 0, 0x27d4eb2d) ^ Math.imul(j | 0, 0x165667b1) ^ SEED;
    h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
    h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
    return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
  }
  function makeRng(seed) {
    let a = (seed ^ SEED) | 0;
    const f = () => { a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
    f.range = (lo, hi) => lo + (hi - lo) * f();
    f.gauss = () => { let u = 0; for (let i = 0; i < 4; i++) u += f(); return (u - 2) * 1.732; };
    return f;
  }
  const PERM = new Uint8Array(512), GRX = new Float32Array(256), GRY = new Float32Array(256);
  (function () {
    const r = makeRng(77);
    const p = new Uint8Array(256);
    for (let i = 0; i < 256; i++) p[i] = i;
    for (let i = 255; i > 0; i--) { const j = floor(r() * (i + 1)); const t = p[i]; p[i] = p[j]; p[j] = t; }
    for (let i = 0; i < 512; i++) PERM[i] = p[i & 255];
    for (let i = 0; i < 256; i++) { const a = r() * PI * 2; GRX[i] = Math.cos(a); GRY[i] = Math.sin(a); }
  })();
  // 勾配ノイズ(おおよそ -1..1)
  function noise(x, y) {
    let xi = floor(x), yi = floor(y);
    const xf = x - xi, yf = y - yi;
    xi &= 255; yi &= 255;
    const u = xf * xf * xf * (xf * (xf * 6 - 15) + 10), v = yf * yf * yf * (yf * (yf * 6 - 15) + 10);
    const aa = PERM[PERM[xi] + yi], ab = PERM[PERM[xi] + yi + 1], ba = PERM[PERM[xi + 1] + yi], bb = PERM[PERM[xi + 1] + yi + 1];
    const n00 = GRX[aa] * xf + GRY[aa] * yf, n10 = GRX[ba] * (xf - 1) + GRY[ba] * yf;
    const n01 = GRX[ab] * xf + GRY[ab] * (yf - 1), n11 = GRX[bb] * (xf - 1) + GRY[bb] * (yf - 1);
    const a = n00 + u * (n10 - n00), b = n01 + u * (n11 - n01);
    return (a + v * (b - a)) * 1.5;
  }
  function fbm(x, y, oct) {
    let a = 0.5, s = 0;
    const n = Math.ceil(oct);
    for (let i = 0; i < n; i++) {
      const w = i + 1 > oct ? oct - i : 1;
      s += a * w * noise(x, y);
      const nx = x * 1.616 + y * 1.194, ny = -x * 1.194 + y * 1.616;
      x = nx + 17.3; y = ny - 9.1; a *= 0.5;
    }
    return s;
  }
  // 尾根状(0..1)。侵食された山肌に使う
  function rfbm(x, y, oct) {
    let a = 0.5, s = 0, prev = 1;
    const n = Math.ceil(oct);
    for (let i = 0; i < n; i++) {
      const w = i + 1 > oct ? oct - i : 1;
      let r = 1 - abs(noise(x, y)); r *= r;
      s += a * w * r * prev;
      prev = 0.35 + 0.65 * r;
      const nx = x * 1.616 + y * 1.194, ny = -x * 1.194 + y * 1.616;
      x = nx + 17.3; y = ny - 9.1; a *= 0.5;
    }
    return s;
  }

  // ───────────────────────── 画面と太陽 ─────────────────────────
  const W0 = 3840, H0 = 2400, F0 = 3770, VH0 = 800;   // 原寸・焦点距離(px)・地平線の行
  const EYE = 2.6;
  const SUN_AZ = 9 * DEG, SUN_EL = (P.sunEl || 3.4) * DEG;
  const SHx = Math.sin(SUN_AZ), SHz = Math.cos(SUN_AZ);
  const SLx = SHx * Math.cos(SUN_EL), SLy = Math.sin(SUN_EL), SLz = SHz * Math.cos(SUN_EL);
  const TAN_EL = Math.tan(SUN_EL);
  let W_ = 0, H = 0, FOC = 0, VH = 0, SS = 1, camY = 0, SC = 1;

  // ───────────────────────── 地形 ─────────────────────────
  // 本流(川)の中心線 x = X(z)。制御点 [z, x]
  const RV = [[-400, -3600], [0, -2600], [300, -1500], [450, -760], [560, -420], [680, -160], [830, 40], [1050, 217],
    [1350, 369], [1750, 246], [2300, 140], [3100, 493], [4200, 980], [6000, 1082], [9000, 1700], [14000, 2400], [60000, 7000]];
  const RZ0 = -400, RDZ = 10, RINV = 1 / RDZ, RN = ((60000 - RZ0) / RDZ | 0) + 1;
  const RX = new Float32Array(RN), RXD = new Float32Array(RN);
  (function () {
    const n = RV.length, m = new Float64Array(n);
    for (let i = 0; i < n; i++) {
      const a = RV[i > 0 ? i - 1 : 0], b = RV[i < n - 1 ? i + 1 : n - 1];
      m[i] = (b[1] - a[1]) / (b[0] - a[0]);
    }
    let seg = 0;
    for (let i = 0; i < RN; i++) {
      const z = RZ0 + i * RDZ;
      while (seg < n - 2 && z > RV[seg + 1][0]) seg++;
      const z0 = RV[seg][0], z1 = RV[seg + 1][0], h = z1 - z0, t = clamp((z - z0) / h, 0, 1);
      const t2 = t * t, t3 = t2 * t;
      let x = (2 * t3 - 3 * t2 + 1) * RV[seg][1] + (t3 - 2 * t2 + t) * h * m[seg] + (-2 * t3 + 3 * t2) * RV[seg + 1][1] + (t3 - t2) * h * m[seg + 1];
      x += 16 * noise(z / 170 + 2.3, 7.7) * sat((z - 500) / 300);   // 小さな蛇行
      RX[i] = x;
    }
    for (let i = 0; i < RN; i++) RXD[i] = (RX[i < RN - 1 ? i + 1 : i] - RX[i > 0 ? i - 1 : i]) / (RDZ * (i > 0 && i < RN - 1 ? 2 : 1));
  })();

  // 丘 [x, z, 高さ, 半径x, 半径z]
  const HILLS = [
    [40, -260, 78, 520, 430],      // 立っている丘
    [-800, 1200, 105, 400, 480],   // 左手前の山
    [-560, 1560, 62, 230, 300],
    [-1050, 880, 52, 260, 300],
    [-330, 1010, 46, 190, 250],    // その前の小山
    [330, 3300, 110, 300, 420],
    [1250, 1700, 120, 330, 400],
    [-250, 2500, 185, 520, 700],   // 中央左
    [640, 1050, 85, 230, 330],     // 右手前
    [900, 2300, 190, 380, 560],    // 右中
    [-300, 4600, 260, 800, 900],   // 奥
    [2300, 5200, 300, 900, 1100],
  ];
  const PEAK = [-4300, 19500, 1750, 6200];   // 主峰 [x, z, 高さ, 裾の半径]
  // 集落 [中心からの dx, dz, 棟方向の半長, 妻方向の半長, 軒高, 向き, 屋根(0 瓦 / 1 茅)]
  const HAM = [-96, 574];
  const HOUSES = [
    [0, 0, 6.5, 4.0, 3.3, 0.25, 1], [19, 9, 5.2, 3.4, 3.0, 0.35, 0], [-21, 6, 5.8, 3.6, 3.1, 0.12, 0], [8, 24, 5.0, 3.3, 3.0, 1.75, 0],
    [-12, -19, 4.2, 2.8, 2.6, 0.2, 0], [34, -6, 5.6, 3.5, 3.1, 0.5, 1], [-40, -8, 4.8, 3.2, 2.9, 0.05, 0], [26, 30, 3.4, 2.4, 2.4, 0.3, 0],
  ];
  const HPAD = new Float32Array(HOUSES.length);
  let ho_i = -1, ho_a = 0, ho_c = 0, ho_eave = 0;
  // (x,z) が家の中なら屋根の高さ、外なら -1e9
  function houseAt(x, z) {
    const dx = x - HAM[0], dz = z - HAM[1];
    for (let i = 0; i < HOUSES.length; i++) {
      const h = HOUSES[i], lx = dx - h[0], lz = dz - h[1];
      if (lx * lx + lz * lz > 80) continue;
      const cs = Math.cos(h[5]), sn = Math.sin(h[5]);
      const a = lx * cs + lz * sn, c = -lx * sn + lz * cs;
      const ea = h[2] - abs(a), ec = h[3] - abs(c);
      if (ea > 0 && ec > 0) {
        const roof = ec * 0.66 < ea * 0.95 ? ec * 0.66 : ea * 0.95;
        ho_i = i; ho_a = a; ho_c = c; ho_eave = HPAD[i] + h[4];
        return ho_eave + roof;
      }
    }
    ho_i = -1;
    return -1e9;
  }
  // 遠くの山並み [距離, 幅, 高さ, 稜線の波長, 位相]
  const RANGES = [
    [7500, 1500, 260, 2400, 1.3],
    [11000, 2200, 480, 3600, 7.7],
    [16500, 3000, 800, 5200, 3.9],
    [25000, 4500, 1250, 8000, 9.2],
    [38000, 7000, 2000, 12000, 5.5],
  ];

  let tb_dm = 0, tb_yf = 0, tb_bowl = 0, tb_sm = 0, tb_home = 0, tb_rid = 0.5;
  function terrainBase(x, z, oct) {
    // 本流
    let zi = (z - RZ0) * RINV; if (zi < 0) zi = 0; else if (zi > RN - 2) zi = RN - 2;
    const i0 = zi | 0, fz = zi - i0;
    const X = RX[i0] + (RX[i0 + 1] - RX[i0]) * fz, Xd = RXD[i0] + (RXD[i0 + 1] - RXD[i0]) * fz;
    const dm = (x - X) / sqrt(1 + Xd * Xd), adm = dm < 0 ? -dm : dm;
    const zc = z > 0 ? z : 0;
    const yf = -0.0045 * zc;
    const farw = zc > 3500 ? zc - 3500 : 0;
    const wf = 80 + 26 * Math.sin(z / 410 + 1.1) + farw * 0.22;
    const ws = 340 + farw * 0.1;
    const q = (adm - wf) / ws;
    let sm = q <= 0 ? 0 : q >= 1 ? 1 : q * q * (3 - 2 * q);
    const r = hypot(x, z);
    if (r > 7500) sm += (1 - sm) * sstep(7500, 11000, r);
    // 台地と丘
    let up = 38 + 22 * noise(x / 1100 + 5.2, z / 1100 + 1.3) + 9 * noise(x / 430 - 3.3, z / 430 + 8.1);
    for (let i = 0; i < HILLS.length; i++) {
      const hl = HILLS[i], a = (x - hl[0]) / hl[3], b = (z - hl[1]) / hl[4], r2 = a * a + b * b;
      if (r2 < 9) { const e = exp(-r2); up += hl[2] * e; if (i === 0) tb_home = e; } else if (i === 0) tb_home = 0;
    }
    {
      const px = x - PEAK[0], pz = z - PEAK[1], pr = hypot(px, pz) / PEAK[3];
      if (pr < 3.4) up += PEAK[2] * exp(-1.5 * pow(pr, 1.1)) * (0.80 + 0.40 * rfbm(px / 5200 + 1.7, pz / 5200 - 3.2, oct < 5 ? oct : 5));
    }
    let rough = sm;
    if (r > 5200) {
      const gap = 1 - 0.7 * exp(-pow((Math.atan2(x, z) - 0.19) / 0.17, 2));
      for (let i = 0; i < RANGES.length; i++) {
        const g = RANGES[i], e = (r - g[0]) / g[1];
        if (e > -3 && e < 3) {
          const along = x / r * g[0] / g[3] + g[4];
          const p = 0.5 + 0.5 * fbm(along, r / g[3] * 0.6 + g[4], 4) + 0.35 * rfbm(along * 2.3 + 3.1, r / g[3] + 1.7, 3);
          up += g[2] * (0.38 + 0.52 * p) * exp(-e * e) * (i < 2 ? gap : 1 - 0.3 * (1 - gap));
        }
      }
    }
    const fl = yf + 0.016 * (adm < wf + 60 ? adm : wf + 60);
    let y = fl + (up - fl) * sm;
    // 手前の谷戸(すり鉢)
    let bowl = 0;
    if (z > -160 && z < 1000) {
      const zz = zc, zr = zz / 650;
      const xa = -150 * pow(zr, 1.3) + 20 * Math.sin(z / 115 + 0.5) * (zz < 100 ? zz / 100 : 1);
      const dT = x - xa;
      const yT = -3 + 95 * exp(-zz / 215) + (z < 0 ? -z * 0.3 : 0);
      const wT = 95 + 0.46 * zz;
      const qT = abs(dT) / wT;
      if (qT < 1) {
        const s0 = qT * qT * (3 - 2 * qT), sT = pow(s0, 1.15);
        const yB = up > yT ? yT + (up - yT) * sT : up;
        const fade = z > 760 ? 1 - sat((z - 760) / 200) : 1;
        bowl = (1 - s0) * fade;
        const k = 10, hh = k - abs(y - yB), sm2 = hh > 0 ? hh * hh / (4 * k) : 0;
        const ym = (y < yB ? y : yB) - sm2;
        y += (ym - y) * fade;
      }
    }
    // なだらかなうねり(等高線をほどよく波打たせる)
    y += (5.5 * fbm(x / 230 + 1.9, z / 230 - 6.3, 3) + 1.6 * noise(x / 61 + 4.4, z / 61 + 2.8)) * sat(0.25 + adm / 160);
    // 山肌の襞
    rough = rough * rough * (1 - 0.85 * bowl);
    if (rough > 0.01) {
      let amp = (up - 30) * 0.30 + 6;
      if (amp > 80) amp = 80 + (amp - 80) * 0.28;
      const wx = x + 90 * noise(x / 700 + 2.2, z / 700 - 5.1), wz = z + 90 * noise(x / 700 - 9.4, z / 700 + 1.8);
      const rid = rfbm(wx / 560 + 7.1, wz / 560 + 3.3, oct);
      tb_rid = rid;
      y += rough * amp * (rid - 0.42);
    }
    // 川床
    if (adm < 60) {
      const wc = 24 + 7 * Math.sin(z / 137);
      const c = adm / wc;
      if (c < 1.6) { const e = 1 - c / 1.6; y -= 2.6 * e * e * (3 - 2 * e); }
    }
    // 集落の微高地
    {
      const hx = x - HAM[0], hz = z - HAM[1], d2 = hx * hx + hz * hz;
      if (d2 < 8100) { const e = 1 - sqrt(d2) / 90; y += 2.4 * e * e * (3 - 2 * e); }
    }
    tb_dm = dm; tb_yf = yf; tb_bowl = bowl; tb_sm = sm;
    return y;
  }

  // 木立(高さ場としての樹冠)。杉=円錐、広葉樹=こぶ
  let cn_rel = 0, cn_id = 0, cn_ox = 0, cn_oz = 0, cn_type = 0;
  function canopy(x, z, typeN, tuft) {
    const cedar = tuft || typeN > 0.08;
    const cs = tuft ? 0.62 : cedar ? 5.4 : 8.2, TH = tuft ? 0.58 : cedar ? 19 : 13, TR = tuft ? 0.46 : cedar ? 3.3 : 5.6;
    const ja = tuft ? 0.12 : 0.7, jf = tuft ? 3.1 : 0.8;
    const jx = x + ja * noise(x * jf + 3.3, z * jf), jz = z + ja * noise(x * jf - 14.2, z * jf + 6.1);
    const gx = jx / cs, gz = jz / cs, ix = floor(gx), iz = floor(gz);
    let best = 0, bid = 0, box = 0, boz = 0, brel = 0;
    for (let a = -1; a <= 1; a++) for (let c = -1; c <= 1; c++) {
      const cx = ix + a, cz = iz + c;
      const h1 = hash2(cx, cz), h2 = hash2(cx + 57, cz - 113), h3 = hash2(cx - 91, cz + 29);
      const ox = (gx - (cx + 0.15 + 0.7 * h1)) * cs, oz = (gz - (cz + 0.15 + 0.7 * h2)) * cs;
      const R = TR * (0.75 + 0.5 * h3), q = hypot(ox, oz) / R;
      if (q < 1) {
        const Ht = tuft ? TH * (0.30 + 1.25 * h2 * h2) : TH * (0.62 + 0.55 * h3 * h1 + 0.2 * h2);
        const pr = tuft ? pow(1 - q, 0.5) : cedar ? pow(1 - q, 0.9) : (1 - q * q * q) * (0.74 + 0.20 * noise(x * 0.8, z * 0.8) + 0.10 * noise(x * 2.3, z * 2.3));
        const h = Ht * pr;
        if (h > best) { best = h; bid = h1; box = ox / R; boz = oz / R; brel = pr; }
      }
    }
    const under = tuft ? 0.07 + 0.05 * noise(x * 1.9, z * 1.9) : TH * (cedar ? 0.34 : 0.42) * (0.8 + 0.3 * noise(x * 0.21, z * 0.21));
    if (best < under) { best = under; brel = brel * 0.5; box *= 0.3; boz *= 0.3; }
    cn_rel = brel; cn_id = bid; cn_ox = box; cn_oz = boz; cn_type = cedar ? 1 : 0;
    return best;
  }

  // ───────────────────────── 空 ─────────────────────────
  let o0 = 0, o1 = 0, o2 = 0, skyTh = 0;
  function skyGrad(dx, dy, dz, glow) {
    const y = dy > 0 ? dy : 0;
    const hl = hypot(dx, dz) + 1e-9;
    const ca = (dx * SHx + dz * SHz) / hl;
    let az = ca > 0 ? ca : 0; az = az * az; az = az * az * az;
    const a1 = exp(-y * 3.2), a2 = exp(-y * 15), a3 = exp(-y * 44);
    let r = 0.036 + 0.052 * a1, g = 0.090 + 0.160 * a1, b = 0.30 + 0.33 * a1;
    const lr = 0.50 + 0.44 * az, lg = 0.56 + 0.19 * az, lb = 0.68 - 0.17 * az;
    r += (lr - r) * a2; g += (lg - g) * a2; b += (lb - b) * a2;
    const hr = 0.62 + 0.72 * az, hg = 0.52 + 0.24 * az, hb = 0.58 - 0.24 * az;
    r += (hr - r) * a3; g += (hg - g) * a3; b += (hb - b) * a3;
    const mu = dx * SLx + dy * SLy + dz * SLz;
    const th = Math.acos(mu > 1 ? 1 : mu < -1 ? -1 : mu);
    const gl = 1.25 * exp(-th / 0.06) + 0.42 * exp(-th / 0.17) + 0.12 * exp(-th / 0.45);
    if (glow === 0) { o0 = r; o1 = g; o2 = b; skyTh = th; return; }
    o0 = r + 1.0 * gl; o1 = g + 0.68 * gl; o2 = b + 0.34 * gl; skyTh = th;
  }
  function cloudA(px, pz) {   // 高積雲の帯
    const u = px * 0.40 + 3.7, v = pz * 0.62 - 1.2;
    const w = fbm(u * 0.27 + 9.1, v * 0.27 + 2.2, 2);
    return fbm(u + 0.7 * w, v - 0.4 * w, 5) + 0.62 * w - 0.045;
  }
  function cloudB(px, pz) {   // 巻雲の筋
    const u = (px * 0.94 + pz * 0.34) * 0.085 + 11.3, v = (-px * 0.34 + pz * 0.94) * 0.55 + 4.9;
    const w = noise(u * 0.5 + 1.7, v * 0.12 + 8.8);
    return fbm(u + 0.35 * noise(u * 2.1, v * 0.6), v + 0.9 * w, 4) * 0.9 + 0.35 * w - 0.10;
  }
  let skyCloud = 0;
  function sky(dx, dy, dz, sunDisc) {
    skyGrad(dx, dy, dz);
    const th = skyTh;
    let r = o0, g = o1, b = o2, cover = 0;
    if (dy > 0.01) {
      const mu = Math.cos(th), sw0 = mu > 0 ? mu : 0, sw2 = sw0 * sw0, sw = sw2 * sw2 * sw2;
      const hf = sstep(0.01, 0.06, dy);
      // 巻雲(高い・奥)
      {
        const tt = 9.0 / dy, px = dx * tt, pz = dz * tt;
        const d = cloudB(px, pz);
        let al = sstep(-0.02, 0.34, d) * 0.62 * hf * exp(-tt / 130);
        if (al > 0.004) {
          const d2 = cloudB(px + SHx * 1.6, pz + SHz * 1.6);
          const lit = sat(0.62 + (d - d2) * 2.2);
          const cr = mix(0.44, mix(1.22, 2.0, sw), lit), cg = mix(0.40, mix(0.80, 1.45, sw), lit), cb = mix(0.52, mix(0.74, 0.86, sw), lit);
          r += (cr - r) * al; g += (cg - g) * al; b += (cb - b) * al; cover += al * 0.5;
        }
      }
      // 高積雲
      {
        const tt = 3.0 / dy, px = dx * tt, pz = dz * tt;
        const d = cloudA(px, pz);
        let al = sstep(0.0, 0.2, d) * hf * exp(-tt / 70);
        if (al > 0.004) {
          const d2 = cloudA(px + SHx * 0.55, pz + SHz * 0.55);
          const lit = sat(0.5 + (d - d2) * 2.6), thin = 1 - sstep(0.0, 0.42, d);
          const lr = mix(1.12, 2.3, sw) * (0.8 + 0.5 * thin), lg = mix(0.60, 1.5, sw) * (0.8 + 0.5 * thin), lb = mix(0.50, 0.78, sw) * (0.8 + 0.5 * thin);
          const sr = mix(0.25, 0.52, sw * 0.7), sg = mix(0.25, 0.33, sw * 0.7), sb = mix(0.37, 0.30, sw * 0.7);
          const cr = mix(sr, lr, lit), cg = mix(sg, lg, lit), cb = mix(sb, lb, lit);
          r += (cr - r) * al; g += (cg - g) * al; b += (cb - b) * al; cover += al;
        }
      }
    }
    if (sunDisc && th < 0.02) {
      const e = 1 - sstep(0.0078, 0.0092, th);
      const k = e * (1 - 0.85 * sat(cover)) * sunDisc;
      r += 70 * k; g += 52 * k; b += 30 * k;
    }
    skyCloud = cover;
    o0 = r; o1 = g; o2 = b;
  }

  // ───────────────────────── 霧 ─────────────────────────
  // (r,g,b) を o0..o2 に持つ色へ、視点から (x,y,z) までの霞と谷霧をかける
  function fog(x, y, z, len, dx, dy, dz) {
    const BH = 1 / 32000, HH = 1100, BM = 1 / 430, HM = 24;
    let d = (y - camY) / HH;
    let th = BH * len * exp(-camY / HH) * (abs(d) < 1e-4 ? 1 : (1 - exp(-d)) / d);
    const h0 = camY, h1 = y + 0.0045 * (z > 0 ? z : 0) + 2;
    d = (h1 - h0) / HM;
    let tm = BM * len * exp(-h0 / HM) * (abs(d) < 1e-4 ? 1 : (1 - exp(-d)) / d);
    const mod = (0.5 + 0.9 * sat(0.5 + 0.9 * fbm(x / 800 + 1.3, z / 1300 + 4.4, 3))) * (0.05 + 0.95 * sstep(380, 1900, len));
    tm *= mod;
    const tr = exp(-(th + tm));
    // 霞の色は地平の空から
    const hl = hypot(dx, dz) + 1e-9, e = 0.035;
    const r = o0, g = o1, b = o2;
    skyGrad(dx / hl * 0.9994, e, dz / hl * 0.9994, 0);
    {
      // 前方散乱は実際の視線と太陽の角度で
      const mu = dx * SLx + dy * SLy + dz * SLz, ang = Math.acos(mu > 1 ? 1 : mu < -1 ? -1 : mu);
      const gl = 0.62 * exp(-ang / 0.11) + 0.26 * exp(-ang / 0.34);
      o0 += 1.0 * gl; o1 += 0.68 * gl; o2 += 0.34 * gl;
    }
    const wm = tm / (th + tm + 1e-9);
    let fr = o0 * (0.56 + 0.40 * wm), fg = o1 * (0.66 + 0.36 * wm), fb = o2 * (0.96 + 0.24 * wm);
    // 朝霧の帯:谷底から 2〜34m に浮く薄い層を視線に沿って 4 点で積む。日なたは金色、山影では青い
    let tau = 0;
    if (h1 < 36 && len > 380 && SHM) {
      const hc = camY, hh = h1 - 2;
      const ta = (hc - 34) / (hc - hh), tb = hh < 2 ? (hc - 2) / (hc - hh) : 1;
      if (tb > ta) {
        const seg = (tb - ta) * len * 0.25;
        let lit = 0;
        for (let i = 0; i < 4; i++) {
          const f = ta + (tb - ta) * (i + 0.5) * 0.25, px = x * f, pz = z * f, ph = hc + (hh - hc) * f;
          let prof = 1 - abs(ph - 18) / 16; if (prof <= 0) continue;
          let dn = fbm(px / 230 + 7.7, pz / 640 - 3.1, 3) + 0.5 * noise(px / 61 - 2.2, pz / 150 + 5.3) * 0.5 + 0.02;
          if (dn <= 0) continue;
          const dd = dn * prof * sstep(380, 1000, f * len) * seg * (1 / 115);
          tau += dd; lit += dd * sunVis(px, hc + (y - hc) * f, pz);
        }
        if (tau > 0) {
          const lf = lit / tau, w2 = tau / (th + tm + tau);
          const mr = fr * (0.40 + 0.85 * lf) + 0.03, mg = fg * (0.46 + 0.78 * lf) + 0.04, mb = fb * (0.62 + 0.55 * lf) + 0.07;
          fr += (mr - fr) * w2; fg += (mg - fg) * w2; fb += (mb - fb) * w2;
        }
      }
    }
    const tr2 = tau > 0 ? exp(-(th + tm + tau)) : tr;
    o0 = r * tr2 + fr * (1 - tr2); o1 = g * tr2 + fg * (1 - tr2); o2 = b * tr2 + fb * (1 - tr2);
  }

  // ───────────────────────── 朝日の影 ─────────────────────────
  let SHM = null, SHna = 0, SHnb = 0;
  const SHA0 = -200, SHB0 = -2600, SHC = 5;
  function buildShadow() {
    SHna = 1300; SHnb = 1040;
    SHM = new Float32Array(SHna * SHnb);
    const drop = SHC * TAN_EL;
    for (let ib = 0; ib < SHnb; ib++) {
      const b = SHB0 + ib * SHC;
      let M = -1e9;
      const aEnd = SHA0 + (SHna - 1) * SHC;
      for (let a = 60000; a > aEnd; a -= (a > 12000 ? 400 : 120)) {
        const h = terrainBase(a * SHx + b * SHz, a * SHz - b * SHx, 4) - (a - aEnd) * TAN_EL;
        if (h > M) M = h;
      }
      for (let ia = SHna - 1; ia >= 0; ia--) {
        const a = SHA0 + ia * SHC;
        const h = terrainBase(a * SHx + b * SHz, a * SHz - b * SHx, 6);
        M -= drop; if (h > M) M = h;
        SHM[ib * SHna + ia] = M;
      }
    }
    return SHM;
  }
  function setShadow(m) { SHM = m; SHna = 1300; SHnb = 1040; }
  function sunVis(x, y, z) {
    if (!SHM) return 1;
    const a = (x * SHx + z * SHz - SHA0) / SHC, b = (x * SHz - z * SHx - SHB0) / SHC;
    if (a < 0 || b < 0 || a >= SHna - 1 || b >= SHnb - 1) return 1;
    const ia = a | 0, ib = b | 0, fa = a - ia, fb = b - ib, i = ib * SHna + ia;
    const m = (SHM[i] * (1 - fa) + SHM[i + 1] * fa) * (1 - fb) + (SHM[i + SHna] * (1 - fa) + SHM[i + SHna + 1] * fa) * fb;
    const d = (y - m) / 2.4 + 1;
    return d <= 0 ? 0 : d >= 1 ? 1 : d * d * (3 - 2 * d);
  }

  // ───────────────────────── 列ごとの描画 ─────────────────────────
  const KMAX = 12000, TNEAR = 20, TFAR = 62000, DT0 = 0.30, REL = 0.00115;
  const DELTA = 0.8, INV_DELTA = 1 / DELTA, LEVEE_H = 0.26;
  const LC_GRASS = 0, LC_PADDY = 1, LC_FOREST = 2, LC_RIVER = 3, LC_ROCK = 4, LC_HOUSE = 5;
  const MAT_SKY = 0, MAT_GRASS = 1, MAT_FOREST = 2, MAT_WATER = 3, MAT_LEVEE = 4, MAT_WALL = 5, MAT_RIVER = 6, MAT_ROCK = 7, MAT_HOUSE = 8;
  const T = new Float32Array(KMAX), B = new Float32Array(KMAX), GX = new Float32Array(KMAX), GZ = new Float32Array(KMAX);
  const SLP = new Float32Array(KMAX), CAN = new Float32Array(KMAX), CREL = new Float32Array(KMAX), CID = new Float32Array(KMAX);
  const COX = new Float32Array(KMAX), COZ = new Float32Array(KMAX), FADE = new Float32Array(KMAX), SURF = new Float32Array(KMAX);
  const YW = new Float32Array(KMAX), HZ = new Float32Array(KMAX), BWL = new Float32Array(KMAX), DMv = new Float32Array(KMAX);
  const DL = new Float32Array(KMAX), LW = new Float32Array(KMAX), LA = new Float32Array(KMAX), RB = new Float32Array(KMAX);
  const HOME = new Float32Array(KMAX), RID = new Float32Array(KMAX), SMV = new Float32Array(KMAX);
  const LC = new Uint8Array(KMAX), VIL = new Uint8Array(KMAX), CTY = new Uint8Array(KMAX), CCF = new Uint8Array(KMAX), CC = new Float32Array(KMAX * 3);
  const HULL = new Int32Array(KMAX);
  let K = 0, colS = 0;

  let tq_n = 0, tq_fr = 0, tq_q = 0;
  function terrQ(b, kk, wl, F) {
    const dl = DL[kk], u = b / dl, n = floor(u), fr = u - n;
    const q = fr / wl;
    const lev = q < 1 ? (q < 0.25 ? q * 4 : q > 0.7 ? (1 - q) / 0.3 : 1) : 0;
    tq_n = n; tq_fr = fr; tq_q = q;
    return b + F * (n * dl + LEVEE_H * lev - b);
  }
  // 標本 k と k+1 のあいだ a での段。畦の幅は両端からなめらかに
  function terrAt(b, k, a) {
    const kk = a < 0.5 ? k : k + 1;
    if (LC[k] === LC_PADDY && LC[k + 1] === LC_PADDY && DL[k] === DL[k + 1]) return terrQ(b, kk, LW[k] + (LW[k + 1] - LW[k]) * a, FADE[k] + (FADE[k + 1] - FADE[k]) * a);
    return terrQ(b, kk, LW[kk], FADE[kk]);
  }

  function march(s) {
    colS = s;
    let k = 0, t = TNEAR;
    while (k < KMAX && t < TFAR) {
      const dt = t * REL > DT0 ? t * REL : DT0;
      const x = s * t;
      let oct = Math.log2(520 / (2.2 * dt)); oct = oct < 2 ? 2 : oct > 8 ? 8 : oct;
      const b = terrainBase(x, t, oct);
      T[k] = t; B[k] = b; YW[k] = tb_yf - 0.8; BWL[k] = tb_bowl; DMv[k] = tb_dm; HOME[k] = tb_home; RID[k] = tb_rid; SMV[k] = tb_sm;
      const e = dt * 0.6 > 0.3 ? dt * 0.6 : 0.3;
      GX[k] = (terrainBase(x + e, t, oct) - b) / e;
      k++; t += dt;
    }
    K = k;
    for (k = 0; k < K; k++) {
      const k0 = k > 0 ? k - 1 : 0, k1 = k < K - 1 ? k + 1 : K - 1;
      const t = T[k], x = s * t, b = B[k];
      const gx = GX[k], gz = (B[k1] - B[k0]) / (T[k1] - T[k0]) - s * gx;
      GZ[k] = gz;
      const slope = hypot(gx, gz);
      SLP[k] = slope;
      const yf = YW[k] + 0.8, hAb = b - yf, adm = abs(DMv[k]);
      let lc = LC_GRASS, fm = 0, gvv = 0;
      CAN[k] = 0; CCF[k] = 0; VIL[k] = 0;
      const wc = 24 + 7 * Math.sin(t / 137);
      const hdx = x - HAM[0], hdz = t - HAM[1], hd2 = hdx * hdx + hdz * hdz;
      let village = false;
      if (hd2 < 5200 + 900 * noise(x / 23, t / 23)) {
        village = true; VIL[k] = 1;
        const top = houseAt(x, t);
        if (top > -1e8) { lc = LC_HOUSE; CAN[k] = top - b; LC[k] = lc; continue; }
      }
      if (b < YW[k] && adm < 60) lc = LC_RIVER;
      else if (b > 930 + 220 * noise(x / 1500 + 4.1, t / 1500 - 2.9)) {
        // 高い峰:岩と残雪
        lc = LC_ROCK;
        const sn = sstep(1080, 1420, b) * sstep(0.56, 0.36, RID[k] + 0.08 * noise(x / 400, t / 400)) + sstep(1650, 2000, b) * 0.8;
        CREL[k] = sat(sn) * sstep(1.25, 0.7, slope);
      } else {
        const lu = noise(x / 330 + 11.1, t / 330 - 4.2);
        const smT = 0.20 + 0.13 * lu;        // 山裾のどこまでを田にするか(谷の断面でなめらかに決める)
        let paddy = false;
        fm = 0; gvv = 0;
        // 視線に沿った勾配は 30m ほどでならして、田と草地の境をなめらかに
        let jj = Math.round(30 / (T[k1] - T[k0])); jj = jj < 1 ? 1 : jj > 80 ? 80 : jj;
        const ka = k - jj > 0 ? k - jj : 0, kb = k + jj < K - 1 ? k + jj : K - 1;
        const dep = (camY - b) / t, dbdt = (B[kb] - B[ka]) / (T[kb] - T[ka]);
        const vis = dep > 0.02 ? 1 + dbdt / dep : -1;      // 踏面のうち視点から見える割合
        const homeZone = (HOME[k] > 0.10 && t < 900) || (BWL[k] > 0.015 && t < 780);     // 立っている丘と手前の谷戸
        if (homeZone) paddy = slope < 0.44 && vis > 0.36 + 0.1 * lu && t > 100;
        else if (SMV[k] < smT && slope < 0.30) paddy = true;
        const bank = adm < wc + 17;
        if (bank || village) paddy = false;
        if (hypot(x, t) > 6200) paddy = hAb < 8;
        if (paddy) {
          // 田の中の小さな木立
          gvv = noise(x / 95 + 31.4, t / 95 - 17.7) + 0.5 * noise(x / 37 + 3.1, t / 37 + 9.9);
          if (gvv > 0.93 && t > 430 && hAb > 4) paddy = false; else gvv = 0;
        }
        if (paddy) lc = LC_PADDY;
        else {
          // 森の濃さ 0..1(縁では低木になって地面へ降りる)
          fm = homeZone ? 0 : sat(sstep(smT, smT + 0.11, SMV[k]) + sstep(0.25, 0.42, slope) * sstep(3, 9, hAb));
          if (gvv > 0) fm = sstep(0.93, 1.18, gvv);
          if (bank) fm = sstep(0.02, 0.34, noise(x / 46 + 8.2, t / 46 - 1.9) + 0.4 * noise(x / 13, t / 13)) * sstep(wc + 1, wc + 6, adm) * sstep(wc + 17, wc + 10, adm);   // 川辺の並木
          if (village) {                                                                                                      // 屋敷林(家の裏手と脇)
            let dh = 1e9;
            for (let i = 0; i < HOUSES.length; i++) { const d = hypot(hdx - HOUSES[i][0], hdz - HOUSES[i][1]); if (d < dh) dh = d; }
            const fv = sstep(0.0, 0.32, noise(x / 19 + 3.3, t / 19 + 7.1) + 0.5 * sat(hdz / 30) - 0.30 * sat(-hdz / 25) - 0.10) * sstep(8.5, 14, dh) * sstep(5000, 3400, hd2);
            fm = bank ? (fv > fm ? fv : fm) * sstep(8.5, 14, dh) : fv;
          }
          lc = fm > 0.02 && t > 430 ? LC_FOREST : LC_GRASS;
        }
      }
      LC[k] = lc;
      if (lc === LC_FOREST) {
        const dt = t * REL > DT0 ? t * REL : DT0;
        const ty = noise(x / 260 - 8.8, t / 260 + 21.3) + 0.25 * noise(x / 70, t / 70) - (village ? 0.5 : 0);
        const tap = fm * 1.7 < 1 ? fm * 1.7 : 1;
        if (dt < 4.5) {
          let c = canopy(x, t, ty);
          const lf = sstep(4.5, 2.2, dt);
          c = mix((ty > 0.08 ? 13 : 9), c, lf) * tap;
          CAN[k] = c; CREL[k] = mix(0.6, cn_rel, lf); CID[k] = cn_id; COX[k] = cn_ox * lf; COZ[k] = cn_oz * lf; CTY[k] = cn_type;
        } else { CAN[k] = (ty > 0.08 ? 13 : 9) * tap; CREL[k] = 0.6; CID[k] = 0.5; COX[k] = 0; COZ[k] = 0; CTY[k] = ty > 0.08 ? 1 : 0; }
      } else if (lc === LC_GRASS && t < 520) {
        // 草むら
        const lf = 1 - sstep(330, 520, t);
        const c = canopy(x, t, 0, true) * (0.75 + 0.5 * noise(x / 9.3, t / 9.3));
        CAN[k] = c * lf; CREL[k] = cn_rel; CID[k] = cn_id; COX[k] = cn_ox; COZ[k] = cn_oz;
      } else if (lc === LC_GRASS) { CREL[k] = 0.5; CID[k] = 0.5; COX[k] = 0; COZ[k] = 0; }
    }
    // 畦が画面で潰れる距離では段をならす
    const fs = FOC * SS, vhs = VH * SS;
    for (k = 0; k < K; k++) {
      let F = 0;
      if (LC[k] === LC_PADDY) {
        const k0 = k > 0 ? k - 1 : 0, k1 = k < K - 1 ? k + 1 : K - 1;
        // 谷底の平らな田は段を細かく
        const dl = B[k] - YW[k] < 4.0 + 1.6 * noise(colS * T[k] / 210 + 3.3, T[k] / 210 - 7.1) ? 0.2 : DELTA;
        const dv = abs(fs * (B[k1] - camY) / T[k1] - fs * (B[k0] - camY) / T[k0]), db = abs(B[k1] - B[k0]);
        const rows = db > 1e-5 ? dv / db * dl / SS : 1e9;
        const lat = dl / (abs(GX[k]) + 1e-5) * FOC / T[k];
        const R = rows < lat ? rows : lat;        // 一段が画面で何 px か
        F = sstep(1.3, 2.8, R);
        let wl = 0.75 * SLP[k] / dl; wl = wl < 0.004 ? 0.004 : wl > 0.4 ? 0.4 : wl;
        const px = wl * R;
        // 細すぎる畦は 1.1px に太らせ、そのぶん薄く
        if (px < 1.1) { LW[k] = 1.1 / R < 0.5 ? 1.1 / R : 0.5; LA[k] = px / 1.1; } else { LW[k] = wl; LA[k] = 1; }
        DL[k] = dl; RB[k] = R;
      }
      FADE[k] = F;
      const lc = LC[k];
      SURF[k] = lc === LC_RIVER ? YW[k] : lc === LC_PADDY ? B[k] : B[k] + CAN[k];
    }
    // 各点から先を見たときの地平の勾配(水面の映り込み判定用)。上側凸包を奥から手前へ
    let top = -1;
    for (k = K - 1; k >= 0; k--) {
      while (top >= 1) {
        const a = HULL[top], c = HULL[top - 1];
        if ((SURF[a] - SURF[k]) * (T[c] - T[k]) <= (SURF[c] - SURF[k]) * (T[a] - T[k])) top--; else break;
      }
      HZ[k] = top >= 0 ? (SURF[HULL[top]] - SURF[k]) / (T[HULL[top]] - T[k]) : -1e9;
      HULL[++top] = k;
    }
  }

  function surfAt(k, a) {
    const kk = a < 0.5 ? k : k + 1, lc = LC[kk];
    const b = B[k] + (B[k + 1] - B[k]) * a;
    if (lc === LC_PADDY) return terrAt(b, k, a);
    if (lc === LC_RIVER) { const yw = YW[kk]; return b > yw ? b : yw; }
    if (VIL[k] | VIL[k + 1]) {
      const t = T[k] + (T[k + 1] - T[k]) * a, top = houseAt(colS * t, t);
      if (top > -1e8) return top;
      if (LC[k] === LC_HOUSE || LC[k + 1] === LC_HOUSE) return b;
    }
    return b + CAN[k] + (CAN[k + 1] - CAN[k]) * a;
  }

  // 光の色
  const SUNC = [1.30, 0.64, 0.25], AMBC = [0.150, 0.205, 0.32], AMBW = [0.36, 0.25, 0.16];
  // 拡散面の陰影。albedo と法線、木漏れの透過量
  // 雲の影:手前は陰に、棚田の中ほどから先に朝日が落ちる
  function dapple(x, z) {
    if (z > 3200) return 1;
    const f = fbm(x / 480 + 3.1, z / 700 - 1.7, 3) + 0.55 * sstep(120, 420, z) - 0.30;
    const d = 0.20 + 0.80 * sstep(-0.12, 0.18, f);
    return z > 2200 ? d + (1 - d) * (z - 2200) / 1000 : d;
  }
  function lit(ar, ag, ab, nx, ny, nz, x, y, z, vx, vy, vz, trans, ao) {
    const vis = sunVis(x, y, z) * dapple(x, z);
    let nl = nx * SLx + ny * SLy + nz * SLz; if (nl < 0) nl = 0;
    const up = 0.5 + 0.5 * ny;
    let warm = nx * SHx + nz * SHz; warm = 0.5 + 0.5 * warm;
    const back = vx * SLx + vy * SLy + vz * SLz;            // 太陽へ向かって見ているほど大
    const tr = trans * vis * (back > 0 ? back * back * back : 0);
    const er = (AMBC[0] * up + AMBW[0] * warm * 0.6) * ao + SUNC[0] * nl * vis;
    const eg = (AMBC[1] * up + AMBW[1] * warm * 0.6) * ao + SUNC[1] * nl * vis;
    const eb = (AMBC[2] * up + AMBW[2] * warm * 0.6) * ao + SUNC[2] * nl * vis;
    o0 = ar * er + tr * 0.115; o1 = ag * eg + tr * 0.105; o2 = ab * eb + tr * 0.014;
    return vis;
  }

  // 映り込む山肌の色(列の標本 k をそのまま見たとして)
  function sampleColor(k) {
    if (!CCF[k]) {
      const t = T[k], x = colS * t, lc = LC[k];
      const gl = 1 / sqrt(GX[k] * GX[k] + GZ[k] * GZ[k] + 1);
      const len = t * sqrt(1 + colS * colS), y = SURF[k];
      const vx = colS * t / len, vz = t / len, vy = (y - camY) / len;
      if (lc === LC_FOREST) lit(0.040, 0.066, 0.030, -GX[k] * gl, gl, -GZ[k] * gl, x, y, t, vx, vy, vz, 0.10, 0.7);
      else if (lc === LC_PADDY || lc === LC_RIVER) { skyGrad(vx, 0.12, vz); o0 *= 0.55; o1 *= 0.55; o2 *= 0.55; }
      else lit(0.085, 0.125, 0.045, -GX[k] * gl, gl, -GZ[k] * gl, x, y, t, vx, vy, vz, 0.2, 0.9);
      fog(x, y, t, len, vx, vy, vz);
      CC[k * 3] = o0; CC[k * 3 + 1] = o1; CC[k * 3 + 2] = o2; CCF[k] = 1;
    }
    o0 = CC[k * 3]; o1 = CC[k * 3 + 1]; o2 = CC[k * 3 + 2];
  }

  // 水面。視線 (vx,vy,vz)、標本 k(hit の手前側)、水位 yw
  function water(x, yw, z, k, vx, vy, vz, pid, river) {
    const len = hypot(x, z);
    // さざ波
    const calm = river ? 0.35 : pid * pid;
    const rough = (river ? 0.016 : 0.0025 + 0.022 * calm * sat(0.4 + noise(x / 140 + 5.5, z / 140 - 2.2)));
    const lam = river ? 1.9 : 1.1;
    // 1px が水面で何 m か。波が画素より細かくなる距離では鏡にして、照り返しは式で足す
    const hgt = camY - yw, fp = z * z / (FOC * (hgt > 1 ? hgt : 1));
    const lod = sat((lam * 0.6 / fp - 0.8) * 0.5);
    const amp = rough * lod;
    const rx = amp * (noise(x / lam, z / (lam * 0.6)) + 0.5 * noise(x / (lam * 0.31) + 7.7, z / (lam * 0.2) - 3.1));
    const rz = amp * (noise(x / lam + 41.3, z / (lam * 0.6) + 17.9) + 0.5 * noise(x / (lam * 0.31) - 2.2, z / (lam * 0.2) + 9.4));
    let nx = -rx, ny = 1, nz = -rz;
    const nl = 1 / sqrt(nx * nx + ny * ny + nz * nz); nx *= nl; ny *= nl; nz *= nl;
    const dn = vx * nx + vy * ny + vz * nz;
    let rdx = vx - 2 * dn * nx, rdy = vy - 2 * dn * ny, rdz = vz - 2 * dn * nz;
    if (rdy < 0.002) rdy = 0.002;
    // 山が映るか
    const hs = hypot(rdx, rdz), mref = rdy / hs * sqrt(1 + colS * colS);
    let hitK = -1;
    if (mref < HZ[k]) {
      const t0 = T[k];
      for (let j = k + 2; j < K; j += 2) {
        if (SURF[j] > yw + mref * (T[j] - t0)) { hitK = j; break; }
        if (HZ[j] < mref && SURF[j] < yw + mref * (T[j] - t0)) break;
      }
    }
    let rr, rg, rb;
    if (hitK >= 0) { sampleColor(hitK); rr = o0; rg = o1; rb = o2; }
    else { sky(rdx, rdy, rdz, 1); rr = o0; rg = o1; rb = o2; }
    if (lod < 1 && hitK < 0) {
      // 太陽の照り返し:さざ波で縦に伸びる光の道と、きらめき
      const el = Math.asin(-vy), daz = Math.atan2(vx, vz) - SUN_AZ;
      const de = (el - SUN_EL) / (0.010 + rough * 1.7), da = daz / (0.007 + rough * 0.5);
      const e2 = de * de + da * da;
      if (e2 < 12) {
        const h = hash2(floor(x / 0.4), floor(z / 0.4));
        const g = exp(-e2) * (5 + 90 * h * h * h * h * h * h * h * h) * (1 - lod) * (1 - 0.85 * sat(skyCloud));
        rr += g; rg += g * 0.74; rb += g * 0.42;
      }
    }
    let c = -dn; c = c < 0 ? 0 : c;
    const f5 = (1 - c) * (1 - c) * (1 - c) * (1 - c) * (1 - c);
    let fres = 0.02 + 0.98 * f5; fres = fres + (1 - fres) * 0.42;
    // 泥の底
    const br = 0.030 + 0.02 * pid, bg = 0.034 + 0.018 * pid, bb = 0.030 + 0.01 * pid;
    o0 = br + (rr - br) * fres; o1 = bg + (rg - bg) * fres; o2 = bb + (rb - bb) * fres;
  }

  // 1 列(最終解像度の列 ix)を描いて frame に書き込む
  function renderColumn(ix, rgb, depth, matb, bw, bx) {
    const ss = SS, Hs = H * ss, fs = FOC * ss, vhs = VH * ss, wgt = 1 / (ss * ss);
    const W = bw || W_, base = bw ? bx : ix;      // 書き込み先のバッファ幅と列
    for (let py = 0; py < H; py++) { const i = (py * W + base) * 3; rgb[i] = 0; rgb[i + 1] = 0; rgb[i + 2] = 0; depth[py * W + base] = 1e9; matb[py * W + base] = 0; }
    for (let j = 0; j < ss; j++) {
      const u = ix + (j + 0.5) / ss, s = (u - W_ * 0.5) / FOC;
      march(s);
      const hsc = sqrt(1 + s * s);
      let row = Hs - 1;
      for (let k = 1; k < K && row >= 0; k++) {
        const lcK = LC[k], t0 = T[k - 1], t1 = T[k];
        const ns = (lcK === LC_PADDY || LC[k - 1] === LC_PADDY) && t1 < 1000 ? 4 : 1;
       for (let sbs = 1; sbs <= ns && row >= 0; sbs++) {
        const a0 = (sbs - 1) / ns, a1 = sbs / ns;
        const sq = sbs < ns ? surfAt(k - 1, a1) : lcK === LC_PADDY ? terrQ(B[k], k, LW[k], FADE[k]) : SURF[k];
        const vk = vhs - fs * (sq - camY) / (t0 + (t1 - t0) * a1);
        while (row >= 0 && row + 0.5 > vk) {
          const m = (vhs - (row + 0.5)) / fs;
          let lo = a0, hi = a1;
          for (let it = 0; it < (ns > 1 ? 5 : 7); it++) {
            const mid = (lo + hi) * 0.5;
            if (camY + m * (t0 + (t1 - t0) * mid) > surfAt(k - 1, mid)) lo = mid; else hi = mid;
          }
          const a = hi, t = t0 + (t1 - t0) * a, x = s * t, y = camY + m * t;
          const kk = a < 0.5 ? k - 1 : k, lc = LC[kk];
          const len = t * sqrt(1 + s * s + m * m), vx = s * t / len, vy = m * t / len, vz = t / len;
          const gxx = GX[kk], gzz = GZ[kk], gl = 1 / sqrt(gxx * gxx + gzz * gzz + 1);
          let mat = MAT_GRASS;
          if (lc === LC_PADDY) {
            const F = FADE[kk];
            const sLo = surfAt(k - 1, lo), sHi = surfAt(k - 1, hi);
            const b = B[k - 1] + (B[k] - B[k - 1]) * a;
            terrAt(b, k - 1, a);
            const n = tq_n, q = tq_q, dl = DL[kk];
            // 田ごとの番号。仕切りは視線と揃わないよう、傾けた軸で切る
            const CR = 0.8525, SR = 0.5227;
            const xr = x * CR + t * SR, zr = -x * SR + t * CR;
            const ax = abs(gxx * CR + gzz * SR) > abs(-gxx * SR + gzz * CR);
            // 細い段ほど長い田に
            const bandW = dl / (SLP[kk] + 0.004);
            const hn = hash2(n, dl < 0.5 ? 78 : 77), Lc = dl < 0.5 ? 24 + 22 * hn : clamp(bandW * (5 + 9 * hn), 22, 70) * (bandW < 3.5 ? 2.2 : 1);
            const cc = ((ax ? zr : xr) + 400 * hn) / Lc, ci = floor(cc), cf = (cc - ci) * Lc;
            const pid = hash2(n * 2 + (ax ? 1 : 0), ci);
            // 仕切りの畦も遠くでは太らせて薄く
            let wc = 0.42, ac = 1;
            const cnx = ax ? -SR : CR, cnz = ax ? CR : SR;
            const cpx = wc * FOC / t * hypot(cnx - s * cnz, (camY - y) / t * cnz);
            if (cpx < 1.1) { wc *= 1.1 / cpx; ac = cpx / 1.1; }
            let wr, wg, wb;
            // 水鏡
            water(x, n * dl, t, kk, vx, vy, vz, pid, false);
            wr = o0; wg = o1; wb = o2;
            const kind = hash2(ci + 31, n - 17);
            if (kind < 0.16) {          // 早苗が伸びた田
              const gmix = (0.30 + 0.25 * hash2(n, ci + 5)) * F;
              lit(0.055, 0.105, 0.030, 0, 1, 0, x, y, t, vx, vy, vz, 0.5, 1);
              wr += (o0 - wr) * gmix; wg += (o1 - wg) * gmix; wb += (o2 - wb) * gmix;
            } else if (kind > 0.955) {  // 水を落とした田
              lit(0.12, 0.095, 0.065, 0, 1, 0, x, y, t, vx, vy, vz, 0, 1);
              wr += (o0 - wr) * 0.8 * F; wg += (o1 - wg) * 0.8 * F; wb += (o2 - wb) * 0.8 * F;
            }
            // 畦と法面
            const dn = noise(x / 2.3, t / 2.3);
            lit(0.070 + 0.02 * dn, 0.105 + 0.03 * dn, 0.040, -gxx * gl * 0.5, 1, -gzz * gl * 0.5, x, y, t, vx, vy, vz, 0.55, 1);
            const lr = o0, lg = o1, lb = o2;
            const isWall = sHi - sLo > 0.45 * dl * F && y < sHi - 0.02;
            if (isWall) {
              // 法面(こちらを向く側)。草と土
              lit(0.060, 0.074, 0.036, -vx * 0.9, 0.35, -vz * 0.9, x, y, t, vx, vy, vz, 0.12, 0.75);
              mat = MAT_WALL;
            } else if (q < 1) {
              const al = LA[kk];
              o0 = wr + (lr - wr) * al; o1 = wg + (lg - wg) * al; o2 = wb + (lb - wb) * al; mat = MAT_LEVEE;
            } else if (cf < wc) {
              o0 = wr + (lr - wr) * ac; o1 = wg + (lg - wg) * ac; o2 = wb + (lb - wb) * ac; mat = MAT_LEVEE;
            } else { o0 = wr; o1 = wg; o2 = wb; mat = MAT_WATER; }
            if (F < 1) {
              // 潰れる距離では水 78% + 畦 22% の平均色
              const ar = wr * 0.78 + lr * 0.22, ag = wg * 0.78 + lg * 0.22, ab = wb * 0.78 + lb * 0.22;
              o0 = ar + (o0 - ar) * F; o1 = ag + (o1 - ag) * F; o2 = ab + (o2 - ab) * F;
              if (F < 0.5) mat = MAT_WATER;
            }
          } else if (lc === LC_RIVER) {
            const b = B[k - 1] + (B[k] - B[k - 1]) * a;
            if (b < YW[kk] + 0.02) { water(x, YW[kk], t, kk, vx, vy, vz, 0.5, true); mat = MAT_RIVER; }
            else lit(0.16, 0.15, 0.11, -gxx * gl, gl, -gzz * gl, x, y, t, vx, vy, vz, 0.1, 1);
          } else if (VIL[kk] && houseAt(x, t) > -1e8) {
            const h = HOUSES[ho_i], cs = Math.cos(h[5]), sn = Math.sin(h[5]);
            const ea = h[2] - abs(ho_a), ec = h[3] - abs(ho_c);
            const side = ec * 0.66 < ea * 0.95;             // 平側か妻側か
            const la = side ? 0 : (ho_a > 0 ? 1 : -1), lcn = side ? (ho_c > 0 ? 1 : -1) : 0;
            const wx = la * cs - lcn * sn, wz = la * sn + lcn * cs;   // 外向きの水平法線
            if (y < ho_eave - 0.05) {
              // 壁
              const hgt = y - (ho_eave - h[4]);
              const tone = hgt < 1.0 ? 0.55 : 1;
              lit(0.30 * tone, 0.27 * tone, 0.22 * tone, wx, 0.05, wz, x, y, t, vx, vy, vz, 0, 0.9);
              const along = side ? ho_a : ho_c;
              const wv = along / 2.3 + ho_i * 0.37, wf = wv - floor(wv);
              if (hash2(ho_i, floor(wv) + 40) < 0.45 && wf > 0.3 && wf < 0.62 && hgt > 1.0 && hgt < 2.0) { o0 += 1.5; o1 += 0.78; o2 += 0.26; }   // 窓の灯
            } else {
              const tn = noise(x / 0.9, t / 0.9);
              if (h[6]) lit(0.105 + 0.02 * tn, 0.088 + 0.015 * tn, 0.058, wx * 0.55, 0.83, wz * 0.55, x, y, t, vx, vy, vz, 0, 1);
              else lit(0.058 + 0.01 * tn, 0.062 + 0.01 * tn, 0.074, wx * 0.55, 0.83, wz * 0.55, x, y, t, vx, vy, vz, 0, 1);
            }
            mat = MAT_HOUSE;
          } else if (lc === LC_ROCK) {
            const sn = CREL[kk], tn = noise(x / 90, t / 90) * 0.5 + noise(x / 23, t / 23) * 0.25;
            const ar = mix(0.105 + 0.03 * tn, 0.80, sn), ag = mix(0.098 + 0.03 * tn, 0.82, sn), ab = mix(0.110 + 0.03 * tn, 0.90, sn);
            lit(ar, ag, ab, -gxx * gl, gl, -gzz * gl, x, y, t, vx, vy, vz, 0, 1);
            mat = MAT_ROCK;
          } else if (lc === LC_FOREST) {
            const rel = CREL[kk], id = CID[kk], ce = CTY[kk];
            const sTop = surfAt(k - 1, hi), bg = B[k - 1] + (B[k] - B[k - 1]) * a;
            const cH = sTop - bg > 0.5 ? sTop - bg : 0.5;
            // 葉むらの模様。側面でも縦筋にならないよう高さも使う
            const tn = noise((x + t * 0.37) / 1.5, y / 1.2 + t * 0.11) + 0.5 * noise((x - t * 0.23) / 0.55, y / 0.5 - t * 0.31);
            let nx, ny, nz, ao, trn;
            if (sTop - y > 0.4) {
              // 樹冠の側面:下ほど暗く
              const hr = sat((y - bg) / cH);
              nx = -vx * 0.75 + COX[kk] * 0.35; nz = -vz * 0.75 + COZ[kk] * 0.35; ny = 0.15 + 0.6 * hr;
              ao = (0.14 + 0.86 * hr * hr) * (0.8 + 0.3 * tn); trn = 0.22 * hr;
            } else {
              // 樹冠の上面:木の軸から外へ向く
              nx = COX[kk] * 0.9 - gxx * 0.4; nz = COZ[kk] * 0.9 - gzz * 0.4; ny = 0.55 + 0.5 * rel;
              ao = (0.25 + 0.75 * rel * rel) * (0.85 + 0.25 * tn); trn = 0.28 * rel;
            }
            const nl = 1 / sqrt(nx * nx + ny * ny + nz * nz); nx *= nl; ny *= nl; nz *= nl;
            const tone = (0.8 + 0.5 * id) * (1 + 0.22 * tn);
            if (!ce && id < 0.075 && cH > 5) {
              // 山桜
              const pk = 0.8 + 0.4 * tn;
              lit(0.40 * pk, 0.25 * pk, 0.28 * pk, nx, ny, nz, x, y, t, vx, vy, vz, trn * 1.2, ao * 0.8 + 0.2);
              o0 += 0.012; o2 += 0.006;
            } else lit((ce ? 0.030 : 0.046) * tone, (ce ? 0.054 : 0.078) * tone, 0.030 * (0.8 + 0.4 * id), nx, ny, nz, x, y, t, vx, vy, vz, trn, ao);
            mat = MAT_FOREST;
          } else {
            const dn = noise(x / 6.1, t / 6.1) + 0.5 * noise(x / 1.4, t / 1.4), big = noise(x / 37 + 2.2, t / 37 - 5.7);
            const rel = CREL[kk], id = CID[kk];
            let nx = COX[kk] * 0.5 - gxx, nz = COZ[kk] * 0.5 - gzz, ny = 1;
            const nl = 1 / sqrt(nx * nx + ny * ny + nz * nz); nx *= nl; ny *= nl; nz *= nl;
            const dry = sat(0.35 + 0.8 * big) * (0.5 + 0.5 * id);     // 枯れ草まじり
            lit(0.074 + 0.040 * dry + 0.012 * dn, 0.118 + 0.014 * dry + 0.020 * dn, 0.032 + 0.006 * dry, nx, ny, nz, x, y, t, vx, vy, vz, 0.12 + 0.55 * rel * rel, 0.40 + 0.60 * rel);
          }
          fog(x, y, t, len, vx, vy, vz);
          const py = (row / ss) | 0, pi = py * W + base, i3 = pi * 3;
          rgb[i3] += o0 * wgt; rgb[i3 + 1] += o1 * wgt; rgb[i3 + 2] += o2 * wgt;
          if (t * hsc < depth[pi]) depth[pi] = t * hsc;
          if (j === 0 && (row % ss) === ss - 1) matb[pi] = mat;
          row--;
        }
       }
      }
      // 空
      let lastPy = -1, sr = 0, sg = 0, sb = 0;
      while (row >= 0) {
        const py = (row / ss) | 0;
        if (py !== lastPy) {
          const dy = (VH - (py + 0.5)) / FOC, dx = (ix + 0.5 - W_ * 0.5) / FOC;
          const l = 1 / sqrt(dx * dx + dy * dy + 1);
          sky(dx * l, dy * l, l, 1);
          sr = o0; sg = o1; sb = o2; lastPy = py;
        }
        const i3 = (py * W + base) * 3;
        rgb[i3] += sr * wgt; rgb[i3 + 1] += sg * wgt; rgb[i3 + 2] += sb * wgt;
        row--;
      }
    }
  }

  function setup(scale, ss) {
    SC = scale; W_ = Math.round(W0 * scale); H = Math.round(H0 * scale); FOC = F0 * scale; VH = VH0 * scale; SS = ss || 1;
    camY = terrainBase(0, 0, 8) + EYE;
    for (let i = 0; i < HOUSES.length; i++) HPAD[i] = terrainBase(HAM[0] + HOUSES[i][0], HAM[1] + HOUSES[i][1], 8) + 0.35;
    return { W: W_, H };
  }

  // ───────────────────────── 現像 ─────────────────────────
  function blurH(src, dst, w, h, sigma) {
    const rad = Math.max(1, Math.ceil(sigma * 2.6)), ker = new Float32Array(rad * 2 + 1);
    let sum = 0;
    for (let i = -rad; i <= rad; i++) { ker[i + rad] = exp(-i * i / (2 * sigma * sigma)); sum += ker[i + rad]; }
    for (let i = 0; i < ker.length; i++) ker[i] /= sum;
    for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
      let r = 0, g = 0, b = 0;
      for (let i = -rad; i <= rad; i++) {
        let xx = x + i; xx = xx < 0 ? 0 : xx >= w ? w - 1 : xx;
        const k = ker[i + rad], o = (y * w + xx) * 3;
        r += src[o] * k; g += src[o + 1] * k; b += src[o + 2] * k;
      }
      const o = (y * w + x) * 3; dst[o] = r; dst[o + 1] = g; dst[o + 2] = b;
    }
  }
  function blurV(src, dst, w, h, sigma) {
    const rad = Math.max(1, Math.ceil(sigma * 2.6)), ker = new Float32Array(rad * 2 + 1);
    let sum = 0;
    for (let i = -rad; i <= rad; i++) { ker[i + rad] = exp(-i * i / (2 * sigma * sigma)); sum += ker[i + rad]; }
    for (let i = 0; i < ker.length; i++) ker[i] /= sum;
    for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
      let r = 0, g = 0, b = 0;
      for (let i = -rad; i <= rad; i++) {
        let yy = y + i; yy = yy < 0 ? 0 : yy >= h ? h - 1 : yy;
        const k = ker[i + rad], o = (yy * w + x) * 3;
        r += src[o] * k; g += src[o + 1] * k; b += src[o + 2] * k;
      }
      const o = (y * w + x) * 3; dst[o] = r; dst[o + 1] = g; dst[o + 2] = b;
    }
  }
  function downsample(src, w, h, f) {
    const w2 = Math.ceil(w / f), h2 = Math.ceil(h / f), dst = new Float32Array(w2 * h2 * 3);
    for (let y = 0; y < h2; y++) for (let x = 0; x < w2; x++) {
      let r = 0, g = 0, b = 0, n = 0;
      for (let yy = y * f; yy < Math.min(h, (y + 1) * f); yy++) for (let xx = x * f; xx < Math.min(w, (x + 1) * f); xx++) {
        const o = (yy * w + xx) * 3; r += src[o]; g += src[o + 1]; b += src[o + 2]; n++;
      }
      const o = (y * w2 + x) * 3; dst[o] = r / n; dst[o + 1] = g / n; dst[o + 2] = b / n;
    }
    return { d: dst, w: w2, h: h2 };
  }
  // 光のにじみ:数段のぼかしを足し戻す
  function bloom(hdr, w, h) {
    const out = new Float32Array(hdr);
    const levels = [[4, 2.2, 0.060], [8, 3.0, 0.050], [16, 4.0, 0.045], [32, 5.0, 0.035]];
    let total = 0;
    for (const [f0, sg, wt] of levels) {
      const f = Math.max(1, Math.round(f0 * SC * 2) / 2 | 0) || 1;
      const ds = downsample(hdr, w, h, f), tmp = new Float32Array(ds.d.length);
      // 太陽そのものは振り切れるので抑えてから
      for (let i = 0; i < ds.d.length; i++) if (ds.d[i] > 12) ds.d[i] = 12;
      const s = sg * (f0 * SC / f);
      blurH(ds.d, tmp, ds.w, ds.h, s); blurV(tmp, ds.d, ds.w, ds.h, s);
      for (let y = 0; y < h; y++) {
        const fy = (y + 0.5) / f - 0.5, y0 = clamp(floor(fy), 0, ds.h - 1), y1 = clamp(y0 + 1, 0, ds.h - 1), ty = sat(fy - y0);
        for (let x = 0; x < w; x++) {
          const fx = (x + 0.5) / f - 0.5, x0 = clamp(floor(fx), 0, ds.w - 1), x1 = clamp(x0 + 1, 0, ds.w - 1), tx = sat(fx - x0);
          const a = (y0 * ds.w + x0) * 3, b = (y0 * ds.w + x1) * 3, c = (y1 * ds.w + x0) * 3, d = (y1 * ds.w + x1) * 3, o = (y * w + x) * 3;
          for (let ch = 0; ch < 3; ch++) {
            const v = (ds.d[a + ch] * (1 - tx) + ds.d[b + ch] * tx) * (1 - ty) + (ds.d[c + ch] * (1 - tx) + ds.d[d + ch] * tx) * ty;
            out[o + ch] += (v - hdr[o + ch]) * wt;
          }
        }
      }
      total += wt;
    }
    return out;
  }
  // 階調:フィルム風の肩 + 影の色
  function develop(hdr, w, h, rgba, xa, xb) {
    const EXPO = 0.86, x0 = xa || 0, x1 = xb || w;
    for (let y = 0; y < h; y++) for (let x = x0; x < x1; x++) {
      const i = (y * w + x) * 3, o = (y * w + x) * 4;
      let r = hdr[i] * EXPO, g = hdr[i + 1] * EXPO, b = hdr[i + 2] * EXPO;
      // 輝度で肩を付け、色は保つ
      const l = 0.2126 * r + 0.7152 * g + 0.0722 * b + 1e-6;
      const lt = l * (2.51 * l + 0.03) / (l * (2.43 * l + 0.59) + 0.14);
      const k = lt / l;
      const cr = r * (2.51 * r + 0.03) / (r * (2.43 * r + 0.59) + 0.14), cg = g * (2.51 * g + 0.03) / (g * (2.43 * g + 0.59) + 0.14), cb = b * (2.51 * b + 0.03) / (b * (2.43 * b + 0.59) + 0.14);
      r = mix(r * k, cr, 0.55); g = mix(g * k, cg, 0.55); b = mix(b * k, cb, 0.55);
      // 影をわずかに持ち上げて藍に寄せる
      r = r + 0.006 * (1 - r); g = g + 0.010 * (1 - g); b = b + 0.020 * (1 - b);
      // ゆるい S 字で締める
      r = r + 0.10 * r * (1 - r) * (2 * r - 1) * 2; g = g + 0.10 * g * (1 - g) * (2 * g - 1) * 2; b = b + 0.10 * b * (1 - b) * (2 * b - 1) * 2;
      { const l2 = 0.2126 * r + 0.7152 * g + 0.0722 * b, sa = 1.30; r = l2 + (r - l2) * sa; g = l2 + (g - l2) * sa; b = l2 + (b - l2) * sa; }
      // 周辺減光
      const vx = (x / w - 0.5) * 2, vy = (y / h - 0.5) * 2, vg = 1 - 0.16 * pow(vx * vx * 0.9 + vy * vy * 0.8, 1.3);
      r *= vg; g *= vg; b *= vg;
      // 粒子(紙目)と量子化の乱れ止め
      const gn = (hash2(x, y) - 0.5) * 0.012 + (hash2(x + 911, y - 377) - 0.5) / 255;
      r = sat(r); g = sat(g); b = sat(b);
      rgba[o] = (pow(r, 1 / 2.2) + gn) * 255 + 0.5;
      rgba[o + 1] = (pow(g, 1 / 2.2) + gn) * 255 + 0.5;
      rgba[o + 2] = (pow(b, 1 / 2.2) + gn) * 255 + 0.5;
      rgba[o + 3] = 255;
    }
  }

  // ───────────────────────── 筆(前景の描き込み) ─────────────────────────
  // 被覆マスクに形を溜めてから色を置く。線は太さの変わるカプセルの連なり
  let TG = null, TW = 0, THt = 0, TCH = 3, U = 1;
  let MK = new Float32Array(1 << 18), mx0 = 0, my0 = 0, mw = 0, mh = 0;
  function target(buf, w, h, ch) { TG = buf; TW = w; THt = h; TCH = ch; }
  function mBegin(x0, y0, x1, y1) {
    mx0 = Math.max(0, floor(x0) - 1); my0 = Math.max(0, floor(y0) - 1);
    const xe = Math.min(TW, Math.ceil(x1) + 2), ye = Math.min(THt, Math.ceil(y1) + 2);
    mw = xe - mx0; mh = ye - my0;
    if (mw <= 0 || mh <= 0) { mw = mh = 0; return false; }
    if (MK.length < mw * mh) MK = new Float32Array(mw * mh + (mw * mh >> 1)); else MK.fill(0, 0, mw * mh);
    return true;
  }
  function mCapsule(ax, ay, ar, bx, by, br) {
    const R = (ar > br ? ar : br) + 1.5;
    let x0 = floor((ax < bx ? ax : bx) - R) - mx0, x1 = Math.ceil((ax > bx ? ax : bx) + R) - mx0;
    let y0 = floor((ay < by ? ay : by) - R) - my0, y1 = Math.ceil((ay > by ? ay : by) + R) - my0;
    if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > mw - 1) x1 = mw - 1; if (y1 > mh - 1) y1 = mh - 1;
    const dx = bx - ax, dy = by - ay, l2 = dx * dx + dy * dy, il2 = l2 > 1e-9 ? 1 / l2 : 0;
    for (let y = y0; y <= y1; y++) {
      const py = y + my0 + 0.5 - ay;
      for (let x = x0; x <= x1; x++) {
        const px = x + mx0 + 0.5 - ax;
        let t = (px * dx + py * dy) * il2; t = t < 0 ? 0 : t > 1 ? 1 : t;
        const ex = px - dx * t, ey = py - dy * t, d = sqrt(ex * ex + ey * ey), r = ar + (br - ar) * t;
        const c = r >= 0.5 ? r - d + 0.5 : 2 * r * (1 - d);
        if (c > 0) { const i = y * mw + x, cc = c > 1 ? 1 : c; if (cc > MK[i]) MK[i] = cc; }
      }
    }
  }
  function mEllipse(cx, cy, rx, ry, rot) {
    const R = (rx > ry ? rx : ry) + 1.5;
    let x0 = floor(cx - R) - mx0, x1 = Math.ceil(cx + R) - mx0, y0 = floor(cy - R) - my0, y1 = Math.ceil(cy + R) - my0;
    if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > mw - 1) x1 = mw - 1; if (y1 > mh - 1) y1 = mh - 1;
    const cs = Math.cos(rot), sn = Math.sin(rot), m = rx < ry ? rx : ry;
    for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) {
      const px = x + mx0 + 0.5 - cx, py = y + my0 + 0.5 - cy;
      const u = (px * cs + py * sn) / rx, v = (-px * sn + py * cs) / ry;
      const c = (1 - sqrt(u * u + v * v)) * m + 0.5;
      if (c > 0) { const i = y * mw + x, cc = c > 1 ? 1 : c; if (cc > MK[i]) MK[i] = cc; }
    }
  }
  // 多角形(非ゼロ規則)。縦 4 分割の走査線で被覆を足す
  const XS = new Float64Array(256), XD = new Int8Array(256);
  function mPoly(P) {
    const n = P.length >> 1, SUB = 4;
    for (let r = 0; r < mh; r++) for (let sbi = 0; sbi < SUB; sbi++) {
      const ys = my0 + r + (sbi + 0.5) / SUB;
      let c = 0;
      for (let i = 0; i < n; i++) {
        const j = (i + 1) % n, ax = P[i * 2], ay = P[i * 2 + 1], bx = P[j * 2], by = P[j * 2 + 1];
        if ((ay <= ys && by > ys) || (by <= ys && ay > ys)) { XS[c] = ax + (ys - ay) / (by - ay) * (bx - ax); XD[c] = by > ay ? 1 : -1; c++; if (c > 250) break; }
      }
      if (c < 2) continue;
      for (let i = 1; i < c; i++) { const x = XS[i], d = XD[i]; let j = i - 1; while (j >= 0 && XS[j] > x) { XS[j + 1] = XS[j]; XD[j + 1] = XD[j]; j--; } XS[j + 1] = x; XD[j + 1] = d; }
      let wn = 0;
      for (let i = 0; i < c - 1; i++) {
        wn += XD[i];
        if (wn === 0) continue;
        let xa = XS[i] - mx0, xb = XS[i + 1] - mx0;
        if (xa < 0) xa = 0; if (xb > mw) xb = mw; if (xb <= xa) continue;
        const ia = floor(xa), ib = floor(xb), row = r * mw;
        if (ia === ib) MK[row + ia] += (xb - xa) / SUB;
        else {
          MK[row + ia] += (ia + 1 - xa) / SUB;
          for (let x = ia + 1; x < ib; x++) MK[row + x] += 1 / SUB;
          if (ib < mw) MK[row + ib] += (xb - ib) / SUB;
        }
      }
    }
  }
  function mCommit(r, g, b, al) {
    for (let y = 0; y < mh; y++) {
      let i = y * mw, o = ((y + my0) * TW + mx0) * TCH;
      for (let x = 0; x < mw; x++, i++, o += TCH) {
        let a = MK[i];
        if (a > 0) { a = (a > 1 ? 1 : a) * al; TG[o] += (r - TG[o]) * a; TG[o + 1] += (g - TG[o + 1]) * a; TG[o + 2] += (b - TG[o + 2]) * a; }
      }
    }
  }
  // 小さな楕円をじかに置く(花びら・葉・点)
  function dab(cx, cy, rx, ry, rot, r, g, b, al) {
    const R = (rx > ry ? rx : ry) + 1;
    let x0 = floor(cx - R), x1 = Math.ceil(cx + R), y0 = floor(cy - R), y1 = Math.ceil(cy + R);
    if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > TW - 1) x1 = TW - 1; if (y1 > THt - 1) y1 = THt - 1;
    const cs = Math.cos(rot), sn = Math.sin(rot), m = rx < ry ? rx : ry;
    for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) {
      const px = x + 0.5 - cx, py = y + 0.5 - cy;
      const u = (px * cs + py * sn) / rx, v = (-px * sn + py * cs) / ry;
      let c = (1 - sqrt(u * u + v * v)) * m + 0.5;
      if (c > 0) { c = (c > 1 ? 1 : c) * al; const o = (y * TW + x) * TCH; TG[o] += (r - TG[o]) * c; TG[o + 1] += (g - TG[o + 1]) * c; TG[o + 2] += (b - TG[o + 2]) * c; }
    }
  }
  // ぼけた点(煙・光)
  function soft(cx, cy, rad, r, g, b, al, add) {
    const R = rad * 2.4;
    let x0 = floor(cx - R), x1 = Math.ceil(cx + R), y0 = floor(cy - R), y1 = Math.ceil(cy + R);
    if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > TW - 1) x1 = TW - 1; if (y1 > THt - 1) y1 = THt - 1;
    const k = 1 / (rad * rad);
    for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) {
      const px = x + 0.5 - cx, py = y + 0.5 - cy, c = exp(-(px * px + py * py) * k) * al, o = (y * TW + x) * TCH;
      if (add) { TG[o] += r * c; TG[o + 1] += g * c; TG[o + 2] += b * c; }
      else { TG[o] += (r - TG[o]) * c; TG[o + 1] += (g - TG[o + 1]) * c; TG[o + 2] += (b - TG[o + 2]) * c; }
    }
  }
  // 太さの変わる線。pts = [x, y, 半径, ...]
  function stroke(pts, r, g, b, al) {
    let x0 = 1e9, y0 = 1e9, x1 = -1e9, y1 = -1e9, rm = 0;
    for (let i = 0; i < pts.length; i += 3) { if (pts[i] < x0) x0 = pts[i]; if (pts[i] > x1) x1 = pts[i]; if (pts[i + 1] < y0) y0 = pts[i + 1]; if (pts[i + 1] > y1) y1 = pts[i + 1]; if (pts[i + 2] > rm) rm = pts[i + 2]; }
    if (!mBegin(x0 - rm - 2, y0 - rm - 2, x1 + rm + 2, y1 + rm + 2)) return;
    for (let i = 0; i + 5 < pts.length; i += 3) mCapsule(pts[i], pts[i + 1], pts[i + 2], pts[i + 3], pts[i + 4], pts[i + 5]);
    mCommit(r, g, b, al);
  }
  // 2 次曲線を刻んで [x,y,r...] に
  function quad(x0, y0, cx, cy, x1, y1, r0, r1, n, out) {
    out.length = 0;
    for (let i = 0; i <= n; i++) {
      const t = i / n, a = (1 - t) * (1 - t), b2 = 2 * t * (1 - t), c = t * t;
      out.push(a * x0 + b2 * cx + c * x1, a * y0 + b2 * cy + c * y1, r0 + (r1 - r0) * t);
    }
    return out;
  }

  // ── 草地の細い葉と野の花 ──
  function paintMeadow(depth, mat) {
    const rng = makeRng(31), W = W_, pts = [];
    const sx = 4.6 * U, sy = 3.4 * U;
    for (let gy = floor(VH); gy < H; gy += sy) for (let gx = 0; gx < W; gx += sx) {
      const x = gx + rng() * sx, y = gy + rng() * sy, xi = x | 0, yi = y | 0;
      if (xi >= W || yi >= H) continue;
      const pi = yi * W + xi;
      if (mat[pi] !== MAT_GRASS) continue;
      const d = depth[pi];
      if (d > 430) continue;
      const s = (x - W * 0.5) / FOC, t = d / sqrt(1 + s * s), xw = s * t, yw = camY + (VH - y) / FOC * t;
      const ppm = FOC / t;                      // 1m が何 px か
      const Lm = 0.30 + 0.85 * rng() * rng();
      const L = Lm * ppm;
      if (L < 2.2 * U) continue;
      const light = sunVis(xw, yw, t) * dapple(xw, t);
      const lean = rng.gauss() * 0.30 + 0.10;
      const tx = x + lean * L, ty = y - L * (0.72 + 0.28 * rng());
      const r0 = clamp(0.011 * ppm, 0.38 * U, 1.5 * U);
      quad(x, y, x + lean * L * 0.15, y - L * 0.62, tx, ty, r0, 0.16 * U, L > 14 * U ? 5 : 3, pts);
      const k = rng(), dry = rng() < 0.16;
      let r, g, b;
      if (k < 0.34) { const e = 0.35 + 1.0 * rng(); r = (dry ? 0.19 : 0.105) * e * light + 0.012; g = (dry ? 0.15 : 0.112) * e * light + 0.018; b = 0.030 * e * light + 0.014; }
      else { const e = 0.5 + 0.9 * rng(); r = (dry ? 0.030 : 0.012) * e; g = (dry ? 0.030 : 0.021) * e; b = 0.014 * e; }
      stroke(pts, r, g, b, 0.9);
      // 穂
      if (rng() < 0.10 && L > 6 * U) {
        const hr = clamp(0.016 * ppm, 0.6 * U, 2.6 * U);
        dab(tx, ty, hr * 0.7, hr * 2.2, lean * 0.6, 0.050 + 0.42 * light, 0.046 + 0.34 * light, 0.036 + 0.20 * light, 0.85);
      }
      // 野の花
      if (t < 190 && rng() < 0.05 && noise(xw / 7.3 + 1.1, t / 7.3 - 4.2) > 0.05) {
        const fr = clamp(0.022 * ppm, 0.7 * U, 2.4 * U), yel = rng() < 0.35;
        const fy = y - L * 0.5 * rng();
        if (yel) dab(x, fy, fr, fr * 0.8, 0, 0.16 + 0.5 * light, 0.13 + 0.38 * light, 0.016, 0.9);
        else dab(x, fy, fr, fr * 0.8, 0, 0.13 + 0.62 * light, 0.14 + 0.56 * light, 0.19 + 0.46 * light, 0.9);
      }
    }
  }

  // ── 山桜の枝(画面右上から差し出す) ──
  function paintCherry() {
    const rng = makeRng(207);
    const wood = [], tips = [];
    function grow(x, y, ang, len, w, depth, fs) {
      const steps = Math.max(3, Math.round(len / (30 * U)));
      const sl = len / steps;
      let a = ang, side = rng() < 0.5 ? 1 : -1, since = 1;
      const bend = rng.range(-0.35, 0.35) / steps;
      for (let i = 0; i < steps; i++) {
        const f = (i + 1) / steps;
        const w0 = w * (1 - 0.6 * i / steps), w1 = w * (1 - 0.6 * f);
        a += bend + rng.gauss() * 0.055 + 0.03 * Math.cos(a) * (depth > 0 ? 1 : 0.35);    // 先へ行くほど自重で垂れる
        const nx = x + Math.cos(a) * sl, ny = y + Math.sin(a) * sl;
        wood.push(x, y, w0 * 0.5, nx, ny, w1 * 0.5, depth);
        x = nx; y = ny; since++;
        if (w1 < 7 * U && rng() < 0.62) tips.push(x + rng.gauss() * 5 * U, y + rng.gauss() * 5 * U, fs, depth);
        if (depth < 2 && since > (depth === 0 ? 3 : 2) && rng() < (depth === 0 ? 0.5 : 0.36)) {
          side = -side; since = 0;
          grow(x, y, a + side * rng.range(0.45, 0.95) - 0.10, len * rng.range(0.22, 0.42) * (1.15 - 0.55 * f), w1 * rng.range(0.40, 0.58), depth + 1, fs);
        }
      }
      tips.push(x, y, fs, depth);
      if (depth === 0) {
        grow(x, y, a + rng.range(0.25, 0.5), len * 0.26, w * 0.24, 1, fs);
        grow(x, y, a - rng.range(0.25, 0.5), len * 0.22, w * 0.22, 1, fs);
      }
    }
    // 大枝 [x, y, 向き, 長さ, 太さ, 花の大きさ]
    const limbs = [[3910, 150, PI + 0.02, 1080, 30, 0.82], [3910, 830, PI - 0.16, 640, 20, 1.05], [3680, -50, PI * 0.66, 420, 13, 0.75]];
    for (const l of limbs) grow(l[0] * U, l[1] * U, l[2], l[3] * U, l[4] * U, 0, l[5]);
    // 枝:黒に近い焦茶。太い枝は日の側(左下)に細い照り
    for (let i = 0; i < wood.length; i += 7) {
      if (!mBegin(Math.min(wood[i], wood[i + 3]) - wood[i + 2] - 3, Math.min(wood[i + 1], wood[i + 4]) - wood[i + 2] - 3, Math.max(wood[i], wood[i + 3]) + wood[i + 2] + 3, Math.max(wood[i + 1], wood[i + 4]) + wood[i + 2] + 3)) continue;
      mCapsule(wood[i], wood[i + 1], wood[i + 2], wood[i + 3], wood[i + 4], wood[i + 5]);
      mCommit(0.011, 0.009, 0.011, 1);
    }
    for (let i = 0; i < wood.length; i += 7) {
      const r = wood[i + 2];
      if (r < 2.0 * U) continue;
      const ox = -r * 0.40, oy = r * 0.55;
      stroke([wood[i] + ox, wood[i + 1] + oy, r * 0.17, wood[i + 3] + ox, wood[i + 4] + oy, wood[i + 5] * 0.17], 0.26, 0.15, 0.085, 0.7);
    }
    // 花。tone: 0 陰の花 … 1 朝日に透ける花
    function flower(cx, cy, R, tone) {
      const rot = rng() * PI * 2, sq = 0.5 + 0.5 * rng(), tilt = rng() * PI;
      const ct = Math.cos(tilt), st = Math.sin(tilt);
      const e = tone * (0.85 + 0.3 * rng());
      // 陰は藤色、日が透けると桃〜白
      const pr = 0.20 + 0.86 * e, pg = 0.15 + 0.60 * e, pb = 0.23 + 0.54 * e;
      for (let p = 0; p < 5; p++) {
        const a = rot + p * PI * 0.4, lx = Math.cos(a) * 0.56 * R, ly = Math.sin(a) * 0.56 * R * sq;
        const px = cx + lx * ct - ly * st, py = cy + lx * st + ly * ct;
        const pa = Math.atan2(lx * st + ly * ct, lx * ct - ly * st), v = 0.9 + 0.2 * rng();
        dab(px, py, 0.50 * R, 0.35 * R * (0.55 + 0.45 * sq), pa, pr * v, pg * v, pb * v, 0.84);
      }
      dab(cx, cy, 0.15 * R, 0.15 * R * sq, tilt, 0.36 * e + 0.09, 0.07 * e + 0.03, 0.13 * e + 0.05, 0.75);
      if (R > 7 * U) for (let p = 0; p < 4; p++) { const a = rng() * PI * 2, d = 0.27 * R; dab(cx + Math.cos(a) * d, cy + Math.sin(a) * d * sq, 0.045 * R, 0.045 * R, 0, 0.95 * e + 0.05, 0.72 * e + 0.04, 0.25 * e, 0.85); }
    }
    for (let pass = 0; pass < 2; pass++) {
      for (let i = 0; i < tips.length; i += 4) {
        const x = tips[i], y = tips[i + 1], fs = tips[i + 2];
        const R = 13.5 * U * fs * (0.8 + 0.4 * hash2(i, 9));
        const sunny = 0.25 + 0.75 * hash2(i, 21);          // 房ごとの日当たり
        const n = pass === 0 ? 2 + (hash2(i, 3) * 3 | 0) : 2 + (hash2(i, 5) * 4 | 0);
        for (let j = 0; j < n; j++) {
          const a = rng() * PI * 2, d = R * (0.3 + 1.7 * sqrt(rng()));
          const fx = x + Math.cos(a) * d, fy = y + Math.sin(a) * d * 0.85 + R * 0.25;
          if (pass === 0) {
            if (rng() < 0.30) {     // 赤い若葉
              const la = rng() * PI * 2, e = 0.6 + 0.8 * rng();
              dab(fx, fy, R * 0.74, R * 0.21, la, 0.22 * e, 0.080 * e, 0.036 * e, 0.9);
            } else flower(fx, fy, R * 0.9, 0.10 + 0.20 * rng());
          } else flower(fx, fy, R * (0.85 + 0.3 * rng()), (0.42 + 0.5 * rng()) * (0.55 + 0.6 * sunny));
        }
      }
    }
    // 舞う花びら
    for (let i = 0; i < 150; i++) {
      const u = rng(), x = (3840 - 3300 * pow(u, 0.8) + rng.gauss() * 260) * U, y = (140 + 2100 * pow(u, 0.9) + rng.gauss() * 330) * U;
      const R = (2.2 + 4.6 * rng() * rng()) * U, e = 0.40 + 0.62 * rng();
      dab(x, y, R, R * (0.28 + 0.34 * rng()), rng() * PI, 0.95 * e, 0.66 * e, 0.70 * e, 0.85);
    }
  }

  // ── 白鷺 ──
  function paintBirds() {
    const rng = makeRng(88), pts = [];
    // [x, y, 大きさ, 羽の位相]
    const flock = [[1310, 905, 30, 0.2], [1408, 872, 28, 0.9], [1492, 902, 27, 0.5], [1590, 866, 26, 0.05], [1668, 884, 24, 0.7], [1752, 850, 22, 0.35], [2238, 772, 11, 0.6], [2272, 781, 10, 0.1], [2301, 768, 9, 0.8]];
    for (const [fx, fy, fsz, ph] of flock) {
      const x = fx * U, y = fy * U, s = fsz * U, far = fsz < 14;
      const wr = far ? 0.50 : 0.92, wg = far ? 0.47 : 0.80, wb = far ? 0.50 : 0.66;      // 朝日を受けた白
      const sr = far ? 0.34 : 0.36, sg = far ? 0.35 : 0.38, sb = far ? 0.44 : 0.50;      // 陰の白
      const up = Math.sin(ph * PI * 2);                 // 羽ばたき
      // 奥の翼
      quad(x - 0.05 * s, y - 0.05 * s, x - 0.25 * s, y - (0.55 * up + 0.25) * s, x - 0.70 * s, y - (0.95 * up + 0.05) * s, 0.17 * s, 0.03 * s, 6, pts);
      stroke(pts, sr, sg, sb, 1);
      // 胴・首・くちばし・脚
      stroke([x - 0.42 * s, y + 0.03 * s, 0.05 * s, x - 0.1 * s, y, 0.14 * s, x + 0.3 * s, y - 0.04 * s, 0.12 * s, x + 0.48 * s, y - 0.10 * s, 0.07 * s, x + 0.58 * s, y - 0.10 * s, 0.05 * s], wr, wg, wb, 1);
      stroke([x + 0.58 * s, y - 0.10 * s, 0.035 * s, x + 0.82 * s, y - 0.07 * s, 0.008 * s], 0.42, 0.30, 0.10, 1);
      stroke([x - 0.40 * s, y + 0.04 * s, 0.022 * s, x - 0.98 * s, y + 0.10 * s, 0.012 * s], 0.05, 0.05, 0.06, 0.9);
      // 手前の翼
      quad(x + 0.10 * s, y - 0.04 * s, x + 0.05 * s, y - (0.70 * up + 0.22) * s, x - 0.38 * s, y - (1.15 * up + 0.02) * s, 0.20 * s, 0.035 * s, 6, pts);
      stroke(pts, wr, wg, wb, 1);
    }
  }

  // ── 朝餉の煙・残月 ──
  function paintSmokeMoon() {
    const rng = makeRng(404);
    const h = HOUSES[0], wx = HAM[0] + h[0] + 3, wz = HAM[1] + h[1], wy = HPAD[0] + h[4] + 2.6;
    let x = W_ * 0.5 + FOC * wx / wz, y = VH - FOC * (wy - camY) / wz;
    for (let i = 0; i < 90; i++) {
      const f = i / 90;
      x += (0.4 + 3.2 * f * f) * U + rng.gauss() * 0.5 * U; y -= (2.6 * (1 - 0.75 * f)) * U;
      const rad = (2.2 + 15 * f) * U;
      soft(x, y, rad, 0.34 + 0.16 * f, 0.31 + 0.13 * f, 0.33 + 0.09 * f, 0.20 * (1 - f) * (0.6 + 0.4 * rng()) + 0.012, false);
    }
    // 有明の細い月
    const mx = 760 * U, my = 232 * U, R = 23 * U, ox = -9.5 * U, oy = -7.5 * U;
    for (let yy = floor(my - R - 2); yy <= my + R + 2; yy++) for (let xx = floor(mx - R - 2); xx <= mx + R + 2; xx++) {
      if (xx < 0 || yy < 0 || xx >= TW || yy >= THt) continue;
      const d1 = hypot(xx + 0.5 - mx, yy + 0.5 - my), d2 = hypot(xx + 0.5 - mx - ox, yy + 0.5 - my - oy);
      const c = sat(R - d1 + 0.5) * (1 - sat(R * 1.02 - d2 + 0.5));
      const o = (yy * TW + xx) * 3, e = sat(R - d1 + 0.5) * 0.035;
      TG[o] += (1.10 - TG[o]) * c * 0.9 + e; TG[o + 1] += (1.06 - TG[o + 1]) * c * 0.9 + e; TG[o + 2] += (1.0 - TG[o + 2]) * c * 0.9 + e * 1.2;
    }
  }

  // ── 人物(丘の端で朝日を見ている後ろ姿) ──
  function paintFigure(depth, mat) {
    const W = W_, fx = Math.round(2096 * U);
    // 草地の上端をさがす
    let ey = H - 1;
    while (ey > VH && mat[ey * W + fx] !== MAT_WATER && mat[ey * W + fx] !== MAT_LEVEE) ey--;
    const fy = ey + 9 * U, d = depth[Math.min(H - 1, Math.round(fy)) * W + fx];
    const s = fx - W * 0.5, t = d / sqrt(1 + s * s / (FOC * FOC));
    const h = 1.74 * FOC / t * 1.3;                // 身長(px)。見分けられるよう少しだけ大きく
    const X = (u) => fx + u * h, Y = (v) => fy - v * h;
    const ink = [0.013, 0.014, 0.020];
    // 草に落ちる影(手前へ)
    soft(X(-0.04), Y(-0.03), 0.10 * h, 0.004, 0.006, 0.008, 0.5, false);
    if (!mBegin(X(-0.3), Y(1.05), X(0.3), Y(-0.05))) return;
    // 脚
    mCapsule(X(-0.060), Y(0.50), 0.050 * h, X(-0.072), Y(0.02), 0.036 * h);
    mCapsule(X(0.058), Y(0.50), 0.050 * h, X(0.082), Y(0.02), 0.036 * h);
    mCapsule(X(-0.072), Y(0.015), 0.034 * h, X(-0.100), Y(0.010), 0.028 * h);
    mCapsule(X(0.082), Y(0.015), 0.034 * h, X(0.112), Y(0.010), 0.028 * h);
    // 胴(上着)
    mPoly([X(-0.135), Y(0.80), X(-0.085), Y(0.835), X(0.085), Y(0.835), X(0.135), Y(0.80), X(0.125), Y(0.60), X(0.118), Y(0.46), X(-0.118), Y(0.46), X(-0.125), Y(0.60)]);
    // 腕(ポケットに手)
    mCapsule(X(-0.128), Y(0.79), 0.040 * h, X(-0.165), Y(0.62), 0.036 * h);
    mCapsule(X(-0.165), Y(0.62), 0.036 * h, X(-0.120), Y(0.49), 0.032 * h);
    mCapsule(X(0.128), Y(0.79), 0.040 * h, X(0.165), Y(0.62), 0.036 * h);
    mCapsule(X(0.165), Y(0.62), 0.036 * h, X(0.120), Y(0.49), 0.032 * h);
    // 首・頭
    mCapsule(X(0), Y(0.84), 0.034 * h, X(0), Y(0.88), 0.032 * h);
    mEllipse(X(0), Y(0.925), 0.060 * h, 0.070 * h, 0);
    mCommit(ink[0], ink[1], ink[2], 1);
    // 朝日の縁取り(右側)
    const rim = (pts) => stroke(pts, 0.85, 0.56, 0.26, 0.85);
    const rr = Math.max(0.55 * U, 0.008 * h);
    rim([X(0.045), Y(0.975), rr * 0.6, X(0.062), Y(0.93), rr, X(0.048), Y(0.875), rr * 0.6]);
    rim([X(0.09), Y(0.835), rr * 0.7, X(0.142), Y(0.80), rr, X(0.20), Y(0.62), rr, X(0.155), Y(0.50), rr * 0.6]);
    rim([X(0.108), Y(0.44), rr * 0.7, X(0.122), Y(0.03), rr * 0.7]);
    rim([X(-0.012), Y(0.40), rr * 0.5, X(-0.034), Y(0.04), rr * 0.5]);
  }

  // ── 手前の草(右下の隅)。穂が朝日に透ける ──
  function paintForeGrass() {
    const rng = makeRng(555), pts = [];
    const blade = (bx, h, lean, w, lit) => {
      const tx = bx + lean * h, ty = H - h;
      quad(bx, H + 20 * U, bx + lean * h * 0.12, H - h * 0.72, tx, ty, w, 0.5 * U, 14, pts);
      stroke(pts, 0.007, 0.010, 0.010, 1);
      if (lit) { for (let i = 0; i < pts.length; i += 3) { pts[i] -= pts[i + 2] * 0.55; pts[i + 2] *= 0.22; } stroke(pts, 0.20 * lit, 0.20 * lit, 0.05 * lit, 0.8); }
      return [tx, ty];
    };
    const clumps = [[3380, 60, 520], [3640, 80, 640], [3180, 50, 330], [3800, 60, 500], [2960, 40, 210], [120, 60, 300], [20, 40, 420]];
    for (const [cx, sp, hmax] of clumps) {
      const n = 16 + (rng() * 10 | 0);
      for (let i = 0; i < n; i++) {
        const bx = (cx + rng.gauss() * sp) * U, h = hmax * (0.35 + 0.65 * rng()) * U;
        const lean = rng.gauss() * 0.22 - 0.10, w = (2.0 + 3.2 * rng()) * U;
        const tip = blade(bx, h, lean, w, rng() < 0.4 ? 0.4 + 0.8 * rng() : 0);
        if (rng() < 0.30 && h > 180 * U) {
          // 穂:細い毛を束ねる
          const L = (46 + 40 * rng()) * U, a0 = Math.atan2(-1, lean * 0.9) + rng.gauss() * 0.2;
          const dx = Math.cos(a0), dy = Math.sin(a0);
          soft(tip[0] + dx * L * 0.5, tip[1] + dy * L * 0.5, L * 0.42, 0.42, 0.30, 0.16, 0.16, true);
          for (let j = 0; j < 70; j++) {
            const f = rng(), px = tip[0] + dx * L * f, py = tip[1] + dy * L * f;
            const sa = a0 + (rng() < 0.5 ? 1 : -1) * rng.range(0.25, 0.75), sl = (5 + 9 * rng()) * U * (1 - 0.5 * f);
            stroke([px, py, 0.7 * U, px + Math.cos(sa) * sl, py + Math.sin(sa) * sl, 0.25 * U], 1.0, 0.80, 0.50, 0.6);
          }
          stroke([tip[0], tip[1], 1.2 * U, tip[0] + dx * L, tip[1] + dy * L, 0.5 * U], 0.55, 0.42, 0.22, 0.9);
        }
      }
    }
  }

  // ── 題字と印(現像のあと、画面の上に置く) ──
  // 筆画 = [x, y, 筆圧] の列(100 の升目)
  const GLYPH = {
    '土': [[[30, 39.5, 2.7], [33.5, 37.8, 1.9], [50, 36.4, 1.6], [66, 35.2, 1.9], [70.5, 35.6, 2.8]],
      [[50, 12.5, 2.9], [50.4, 17, 2.2], [50.2, 46, 2.1], [50, 77, 2.5]],
      [[11, 81.5, 3.0], [15.5, 79.6, 2.1], [50, 77.4, 1.8], [84.5, 75.8, 2.2], [89.5, 76.6, 3.3]]],
    '谷': [[[39.5, 10.5, 2.7], [35.5, 18.5, 2.1], [27, 30, 0.5]],
      [[60.5, 10.5, 0.9], [66, 18.5, 1.9], [73.5, 27.5, 3.0], [75.5, 30, 1.4]],
      [[50, 25.5, 2.5], [44.5, 37.5, 2.1], [31.5, 51.5, 1.6], [12.5, 62, 0.4]],
      [[50.5, 27.5, 1.0], [60, 40, 1.8], [73.5, 52.5, 2.9], [85.5, 60.2, 3.5], [92.5, 61, 0.8]],
      [[33, 64.5, 2.7], [33.8, 69, 2.0], [35.6, 90.5, 2.0]],
      [[33.5, 65.2, 2.0], [50, 63.6, 1.6], [65.5, 62.2, 2.1], [68.2, 63.8, 2.9], [67.2, 70, 2.1], [65.2, 91.5, 2.2]],
      [[35.8, 89.4, 1.7], [50, 88.9, 1.6], [65.4, 88.6, 2.1]]],
    '大': [[[12.5, 41.5, 3.0], [17, 39.6, 2.0], [50, 37, 1.7], [82.5, 34.8, 2.1], [87.5, 35.6, 3.1]],
      [[50, 9.5, 2.9], [50.2, 15.5, 2.2], [49.2, 38, 2.1], [44, 58, 2.0], [32, 76, 1.5], [13, 90.5, 0.4]],
      [[50, 38.5, 1.0], [56, 52, 1.7], [68, 70, 2.7], [82, 84, 3.6], [91.5, 87, 0.9]]],
    '地': [[[8.5, 37.5, 2.5], [12, 36.2, 1.7], [22, 34.6, 1.5], [33.5, 33, 2.2]],
      [[21.5, 12.5, 2.6], [21.8, 17, 1.9], [21.6, 45, 1.8], [21.3, 71.5, 2.3]],
      [[5.5, 76.5, 2.9], [9.5, 75.2, 2.1], [22, 69, 1.5], [37.5, 60.5, 0.4]],
      [[38, 45.5, 2.4], [42, 43.6, 1.7], [66, 37.4, 1.5], [84.5, 32.2, 2.1], [86.8, 34, 2.8], [85.2, 44, 2.0], [82.6, 57.5, 2.0], [77.5, 53, 0.4]],
      [[62, 11.5, 2.7], [62.2, 16.5, 2.0], [62, 42, 1.9], [62, 66.5, 2.4]],
      [[46, 23.5, 2.6], [46.2, 28.5, 1.9], [45.6, 60, 1.9], [46.6, 73.5, 2.3], [52.5, 80, 2.3], [70, 81.2, 2.1], [88, 80.6, 2.5], [91.2, 77, 2.3], [91, 65.5, 0.4]]],
  };
  // Catmull-Rom で筆画をなめらかにつないで描く
  function glyphMask(ch, ox, oy, size, wk) {
    const k = size / 100;
    for (const st of GLYPH[ch]) {
      const n = st.length;
      let px = 0, py = 0, pr = 0, first = true;
      for (let i = 0; i < n - 1; i++) {
        const p0 = st[i > 0 ? i - 1 : 0], p1 = st[i], p2 = st[i + 1], p3 = st[i + 2 < n ? i + 2 : n - 1];
        const segs = Math.max(4, Math.ceil(hypot(p2[0] - p1[0], p2[1] - p1[1]) * k / 1.5));
        for (let j = 0; j <= segs; j++) {
          const t = j / segs, t2 = t * t, t3 = t2 * t;
          const x = 0.5 * (2 * p1[0] + (p2[0] - p0[0]) * t + (2 * p0[0] - 5 * p1[0] + 4 * p2[0] - p3[0]) * t2 + (3 * p1[0] - p0[0] - 3 * p2[0] + p3[0]) * t3);
          const y = 0.5 * (2 * p1[1] + (p2[1] - p0[1]) * t + (2 * p0[1] - 5 * p1[1] + 4 * p2[1] - p3[1]) * t2 + (3 * p1[1] - p0[1] - 3 * p2[1] + p3[1]) * t3);
          const r = (p1[2] + (p2[2] - p1[2]) * (t * t * (3 - 2 * t))) * k * wk;
          const sx = ox + x * k, sy = oy + y * k;
          if (!first) mCapsule(px, py, pr, sx, sy, r);
          px = sx; py = sy; pr = r; first = false;
        }
      }
    }
  }
  function paintTitle(rgba) {
    U = SC;
    target(rgba, W_, H, 4);
    const size = 124 * U, pitch = 142 * U, x0 = 172 * U, y0 = 1690 * U, chars = ['土', '谷', '大', '地'];
    for (let i = 0; i < 4; i++) {
      const oy = y0 + i * pitch;
      // 墨のにじみをひとまわり
      if (mBegin(x0 - 8 * U, oy - 8 * U, x0 + size + 8 * U, oy + size + 8 * U)) { glyphMask(chars[i], x0 + 1.5 * U, oy + 2.5 * U, size, 1.9); mCommit(8, 10, 14, 0.30); }
      if (mBegin(x0 - 8 * U, oy - 8 * U, x0 + size + 8 * U, oy + size + 8 * U)) { glyphMask(chars[i], x0, oy, size, 1.0); mCommit(247, 238, 216, 0.95); }
    }
    // 朱印「大地」
    const sz = 76 * U, sx = x0 + (size - sz) * 0.5, sy = y0 + 4 * pitch + 6 * U;
    if (mBegin(sx - 2, sy - 2, sx + sz + 2, sy + sz + 2)) {
      const c = 7 * U;
      mPoly([sx + c, sy, sx + sz - c, sy + 1 * U, sx + sz, sy + c, sx + sz - 1 * U, sy + sz - c, sx + sz - c, sy + sz, sx + c, sy + sz - 1 * U, sx, sy + sz - c, sx + 1 * U, sy + c]);
      // 印泥のかすれ
      for (let y = 0; y < mh; y++) for (let x = 0; x < mw; x++) { const n1 = noise((x + mx0) / (5 * U) + 3.1, (y + my0) / (5 * U)), n2 = hash2(x + mx0, y + my0); MK[y * mw + x] *= sat(0.80 + 0.5 * n1 + 0.18 * n2); }
      mCommit(196, 48, 36, 0.94);
    }
    if (mBegin(sx, sy, sx + sz, sy + sz)) {
      glyphMask('大', sx + sz * 0.17, sy + sz * 0.04, sz * 0.66 * 0.72, 1.55);
      glyphMask('地', sx + sz * 0.17, sy + sz * 0.50, sz * 0.66 * 0.72, 1.55);
      mCommit(248, 236, 220, 0.93);
    }
  }

  function paintDetails(hdr, depth, mat) {
    U = SC;
    target(hdr, W_, H, 3);
    paintSmokeMoon();
    paintBirds();
    paintMeadow(depth, mat);
    paintFigure(depth, mat);
    paintCherry();
    paintForeGrass();
  }

  return {
    W0, H0, setup, buildShadow, setShadow, renderColumn, bloom, develop, paintDetails, paintTitle, terrainBase, sky, makeRng, noise, fbm, hash2,
    get camY() { return camY; },
  };
}

// ワーカー側:列の束を受け取って描いて返す
function workerMain(coreFn, scope) {
  let core = null, H = 0;
  scope.onmessage = (e) => {
    const m = e.data;
    if (m.type === 'init') {
      core = coreFn(m.opt); H = core.setup(m.scale, m.ss).H; core.setShadow(m.shadow);
      scope.postMessage({ type: 'ready' });
    } else if (m.type === 'cols') {
      const n = m.to - m.from, rgb = new Float32Array(n * H * 3), depth = new Float32Array(n * H), mat = new Uint8Array(n * H);
      for (let i = 0; i < n; i++) core.renderColumn(m.from + i, rgb, depth, mat, n, i);
      scope.postMessage({ type: 'done', from: m.from, to: m.to, rgb, depth, mat }, [rgb.buffer, depth.buffer, mat.buffer]);
    }
  };
}

if (typeof module === 'object' && module.exports) module.exports = { daichiCore, workerMain };
else { root.daichiCore = daichiCore; root.daichiWorkerMain = workerMain; }
})(typeof self !== 'undefined' ? self : globalThis);