Claude Opus 5.5

paint.js

// 「土谷大地」— JavaScript だけで描く水彩画。
// 画像・ライブラリは使わない。紙の凹凸、顔料の沈殿、にじみ、縁の溜まり、かすれ、
// マスキング、リフティングを画素単位で計算し、透明水彩と同じ順序(明→暗、遠→近)で重ねる。
(function (root, factory) {
  const api = factory();
  if (typeof module === 'object' && module.exports) module.exports = api;
  else root.Suisai = api;
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';

// ───────────────────────── 基本関数 ─────────────────────────
const clamp = (x, a, b) => (x < a ? a : x > b ? b : x);
const clamp01 = 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); };
const TAU = Math.PI * 2;

function makeRng(seed) {
  let a = 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.int = n => Math.floor(f() * n);
  f.pick = arr => arr[Math.floor(f() * arr.length)];
  f.gauss = () => { let u = 0; for (let i = 0; i < 4; i++) u += f(); return (u - 2) * 1.732; };
  f.sign = () => (f() < 0.5 ? -1 : 1);
  return f;
}

function hash2(x, y, s) {
  let h = Math.imul(x | 0, 0x27d4eb2d) ^ Math.imul(y | 0, 0x165667b1) ^ Math.imul(s | 0, 0x9e3779b1);
  h = Math.imul(h ^ (h >>> 15), 0x85ebca6b); h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35); h ^= h >>> 16;
  return (h >>> 0) / 2147483648 - 1;
}
// 点サンプルの値ノイズ(5 次補間)と fbm。オクターブごとに回して格子を目立たなくする
function vnoise(x, y, s) {
  const ix = Math.floor(x), iy = Math.floor(y); let fx = x - ix, fy = y - iy;
  fx = fx * fx * fx * (fx * (fx * 6 - 15) + 10); fy = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
  const a = hash2(ix, iy, s), b = hash2(ix + 1, iy, s), c = hash2(ix, iy + 1, s), d = hash2(ix + 1, iy + 1, s);
  return a + (b - a) * fx + (c - a) * fy + (a - b - c + d) * fx * fy;
}
function fbm2(x, y, oct, s, gain) {
  gain = gain === undefined ? 0.5 : gain;
  let sum = 0, amp = 1, norm = 0;
  for (let i = 0; i < oct; i++) {
    sum += amp * vnoise(x, y, s + i * 131); norm += amp; amp *= gain;
    const nx = (0.8 * x - 0.6 * y) * 2.03 + 17.3, ny = (0.6 * x + 0.8 * y) * 2.03 - 9.1; x = nx; y = ny;
  }
  return sum / norm;
}
function vnoise1(x, s) {
  const i = Math.floor(x); let f = x - i; f = f * f * f * (f * (f * 6 - 15) + 10);
  const a = hash2(i, 7, s), b = hash2(i + 1, 7, s); return a + (b - a) * f;
}
function fbm1(x, oct, s, gain) {
  gain = gain === undefined ? 0.5 : gain;
  let sum = 0, amp = 1, norm = 0;
  for (let i = 0; i < oct; i++) { sum += amp * vnoise1(x, s + i * 71); norm += amp; amp *= gain; x = x * 2.07 + 31.7; }
  return sum / norm;
}

// 分離型の 3 次 B スプライン格子ノイズ。out に amp×(分散 1 のノイズ) を足す
function addNoise(out, bx, by, bw, bh, lx, ly, seed, amp) {
  const ox = (hash2(11, 3, seed) + 1) * 512, oy = (hash2(5, 17, seed) + 1) * 512;
  const wts = new Float32Array(bw * 4), idx = new Int32Array(bw);
  for (let x = 0; x < bw; x++) {
    const u = (bx + x) / lx + ox, i = Math.floor(u), f = u - i, f2 = f * f, f3 = f2 * f, q = 4 * x;
    idx[x] = i;
    wts[q] = (1 - 3 * f + 3 * f2 - f3) / 6; wts[q + 1] = (3 * f3 - 6 * f2 + 4) / 6;
    wts[q + 2] = (-3 * f3 + 3 * f2 + 3 * f + 1) / 6; wts[q + 3] = f3 / 6;
  }
  const i0 = idx[0] - 1, nI = idx[bw - 1] + 2 - i0 + 1;
  const lat = new Float32Array(nI);
  const rows = [0, 1, 2, 3].map(() => new Float32Array(bw));
  const build = (j, dst) => {
    for (let k = 0; k < nI; k++) lat[k] = hash2(i0 + k, j, seed);
    for (let x = 0; x < bw; x++) { const o = idx[x] - 1 - i0, q = 4 * x; dst[x] = wts[q] * lat[o] + wts[q + 1] * lat[o + 1] + wts[q + 2] * lat[o + 2] + wts[q + 3] * lat[o + 3]; }
  };
  const A = amp * 3.61;
  let curJ = null;
  for (let y = 0; y < bh; y++) {
    const v = (by + y) / ly + oy, j = Math.floor(v), f = v - j, f2 = f * f, f3 = f2 * f;
    if (j !== curJ) {
      if (curJ !== null && j === curJ + 1) { const t = rows.shift(); build(j + 2, t); rows.push(t); }
      else for (let k = 0; k < 4; k++) build(j - 1 + k, rows[k]);
      curJ = j;
    }
    const w0 = A * (1 - 3 * f + 3 * f2 - f3) / 6, w1 = A * (3 * f3 - 6 * f2 + 4) / 6, w2 = A * (-3 * f3 + 3 * f2 + 3 * f + 1) / 6, w3 = A * f3 / 6;
    const r0 = rows[0], r1 = rows[1], r2 = rows[2], r3 = rows[3], o = y * bw;
    for (let x = 0; x < bw; x++) out[o + x] += w0 * r0[x] + w1 * r1[x] + w2 * r2[x] + w3 * r3[x];
  }
}

// 箱ぼかし 3 回でガウスぼかしを近似(端は延長)
function boxH(src, dst, w, h, r) {
  const inv = 1 / (2 * r + 1);
  for (let y = 0; y < h; y++) {
    const o = y * w; let acc = src[o] * (r + 1);
    for (let k = 1; k <= r; k++) acc += src[o + (k < w ? k : w - 1)];
    for (let x = 0; x < w; x++) {
      dst[o + x] = acc * inv;
      const xa = x + r + 1, xb = x - r;
      acc += src[o + (xa < w ? xa : w - 1)] - src[o + (xb > 0 ? xb : 0)];
    }
  }
}
function boxV(src, dst, w, h, r) {
  const inv = 1 / (2 * r + 1), acc = new Float64Array(w);
  for (let x = 0; x < w; x++) acc[x] = src[x] * (r + 1);
  for (let k = 1; k <= r; k++) { const o = (k < h ? k : h - 1) * w; for (let x = 0; x < w; x++) acc[x] += src[o + x]; }
  for (let y = 0; y < h; y++) {
    const o = y * w; for (let x = 0; x < w; x++) dst[o + x] = acc[x] * inv;
    const oa = (y + r + 1 < h ? y + r + 1 : h - 1) * w, ob = (y - r > 0 ? y - r : 0) * w;
    for (let x = 0; x < w; x++) acc[x] += src[oa + x] - src[ob + x];
  }
}
function blur(a, w, h, sigma) {
  if (sigma < 0.35 || w < 2 || h < 2) return a;
  const n = 3, wi = Math.sqrt(12 * sigma * sigma / n + 1);
  let wl = Math.floor(wi); if (wl % 2 === 0) wl--;
  const wu = wl + 2, m = Math.round((12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4));
  const t = new Float32Array(a.length);
  for (let i = 0; i < n; i++) { const r = ((i < m ? wl : wu) - 1) >> 1; if (r < 1) continue; boxH(a, t, w, h, r); boxV(t, a, w, h, r); }
  return a;
}

// 補間
function crSpline(pts, segs) {
  segs = segs || 10; const out = [], n = pts.length;
  for (let i = 0; i < n - 1; i++) {
    const p0 = pts[i > 0 ? i - 1 : 0], p1 = pts[i], p2 = pts[i + 1], p3 = pts[i + 2 < n ? i + 2 : n - 1];
    for (let k = 0; k < segs; k++) {
      const t = k / segs, t2 = t * t, t3 = t2 * t, q = new Array(p1.length);
      for (let c = 0; c < p1.length; c++) q[c] = 0.5 * (2 * p1[c] + (-p0[c] + p2[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t2 + (-p0[c] + 3 * p1[c] - 3 * p2[c] + p3[c]) * t3);
      out.push(q);
    }
  }
  out.push(pts[n - 1].slice()); return out;
}
// x の関数として制御点を通す(非等間隔エルミート)
function curve1D(ctrl) {
  const n = ctrl.length, m = new Float64Array(n);
  for (let i = 0; i < n; i++) { const a = ctrl[i > 0 ? i - 1 : 0], b = ctrl[i < n - 1 ? i + 1 : n - 1]; m[i] = (b[1] - a[1]) / (b[0] - a[0] || 1); }
  return x => {
    if (x <= ctrl[0][0]) return ctrl[0][1]; if (x >= ctrl[n - 1][0]) return ctrl[n - 1][1];
    let i = 0; while (i < n - 2 && x > ctrl[i + 1][0]) i++;
    const x0 = ctrl[i][0], x1 = ctrl[i + 1][0], h = x1 - x0, t = (x - x0) / h, t2 = t * t, t3 = t2 * t;
    return (2 * t3 - 3 * t2 + 1) * ctrl[i][1] + (t3 - 2 * t2 + t) * h * m[i] + (-2 * t3 + 3 * t2) * ctrl[i + 1][1] + (t3 - t2) * h * m[i + 1];
  };
}

// ───────────────────────── 絵 ─────────────────────────
function createPainting(opt) {
  opt = opt || {};
  const W = Math.round(opt.width || 2400), H = Math.round(W * 17 / 24), N = W * H;
  const S = W / 2400;                       // 画素単位の寸法はすべて S 倍する
  const SEED = opt.seed || 20261012;
  const rnd = makeRng(SEED);

  const D0 = new Float32Array(N), D1 = new Float32Array(N), D2 = new Float32Array(N);  // 顔料の光学濃度
  const paperH = new Float32Array(N);       // 紙の高さ 0..1
  const gran = new Float32Array(N);         // 沈殿のむら(平均ほぼ 0、凹みで正)
  const frame = new Float32Array(N);        // マスキングテープの枠
  const resist = new Float32Array(N);       // マスキング液
  const resistLayers = new Map();
  const PAPER = [0.975, 0.964, 0.938];

  // 描画域(テープの内側)
  const MG = Math.round(112 * S);
  const px0 = MG, py0 = MG, pw = W - 2 * MG, ph = H - 2 * MG, px1 = px0 + pw, py1 = py0 + ph;
  const X = u => px0 + u * pw, Y = v => py0 + v * ph;

  let dirty = null;
  function touch(x0, y0, x1, y1) {
    if (!dirty) dirty = [x0, y0, x1, y1];
    else { if (x0 < dirty[0]) dirty[0] = x0; if (y0 < dirty[1]) dirty[1] = y0; if (x1 > dirty[2]) dirty[2] = x1; if (y1 > dirty[3]) dirty[3] = y1; }
  }
  function step(label) { const r = dirty || [0, 0, 0, 0]; dirty = null; return { label, rect: r }; }

  // ── バッファ ──
  function mkbuf(x0, y0, x1, y1) {
    x0 = Math.max(0, Math.floor(x0)); y0 = Math.max(0, Math.floor(y0)); x1 = Math.min(W, Math.ceil(x1)); y1 = Math.min(H, Math.ceil(y1));
    const w = Math.max(0, x1 - x0), h = Math.max(0, y1 - y0);
    return { x: x0, y: y0, w, h, a: new Float32Array(w * h) };
  }
  const like = (b, a) => ({ x: b.x, y: b.y, w: b.w, h: b.h, a: a || new Float32Array(b.w * b.h) });
  const bblur = (b, s) => { blur(b.a, b.w, b.h, s); return b; };
  function fbmArr(b, lam, oct, seed, gain, aniso) {
    gain = gain === undefined ? 0.5 : gain; aniso = aniso || 1;
    const out = new Float32Array(b.w * b.h); let l = lam, a = 1, tot = 0;
    for (let o = 0; o < oct; o++) {
      if (l < 1.3 && o > 0) break;
      addNoise(out, b.x, b.y, b.w, b.h, l, l * aniso, seed + o * 101, a); tot += a * a; l *= 0.5; a *= gain;
    }
    const k = 1 / Math.sqrt(tot); for (let i = 0; i < out.length; i++) out[i] *= k;
    return out;
  }
  function fillFn(b, fn) { let i = 0; for (let y = 0; y < b.h; y++) for (let x = 0; x < b.w; x++) b.a[i++] = fn(b.x + x + 0.5, b.y + y + 0.5); return b; }

  // 多角形(偶奇則、縦 4 倍サンプル+横は面積)。P = [x,y,x,y,…]
  function fillPoly(b, P, val) {
    val = val === undefined ? 1 : val;
    const n = P.length >> 1; let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
    for (let i = 0; i < n; i++) { const x = P[2 * i], y = P[2 * i + 1]; if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; }
    const y0 = Math.max(b.y, Math.floor(minY)), y1 = Math.min(b.y + b.h - 1, Math.floor(maxY));
    const x0 = Math.max(b.x, Math.floor(minX)), x1 = Math.min(b.x + b.w - 1, Math.floor(maxX));
    if (x1 < x0 || y1 < y0) return;
    const rw = x1 - x0 + 1, acc = new Float32Array(rw + 1), xs = [], SUB = 4;
    for (let y = y0; y <= y1; y++) {
      acc.fill(0);
      for (let s = 0; s < SUB; s++) {
        const sy = y + (s + 0.5) / SUB; xs.length = 0;
        for (let i = 0, j = n - 1; i < n; j = i++) {
          const ay = P[2 * i + 1], by = P[2 * j + 1];
          if ((ay > sy) !== (by > sy)) xs.push(P[2 * i] + (sy - ay) / (by - ay) * (P[2 * j] - P[2 * i]));
        }
        xs.sort((p, q) => p - q);
        for (let k = 0; k + 1 < xs.length; k += 2) {
          let xa = xs[k] - x0, xb = xs[k + 1] - x0;
          if (xb <= 0 || xa >= rw) continue; if (xa < 0) xa = 0; if (xb > rw) xb = rw;
          const ia = Math.floor(xa), ib = Math.floor(xb);
          if (ia === ib) acc[ia] += xb - xa;
          else { acc[ia] += ia + 1 - xa; for (let q = ia + 1; q < ib; q++) acc[q] += 1; if (ib < rw) acc[ib] += xb - ib; }
        }
      }
      const o = (y - b.y) * b.w + (x0 - b.x), k = val / SUB;
      for (let x = 0; x < rw; x++) { const c = acc[x] * k; if (c > b.a[o + x]) b.a[o + x] = c; }
    }
  }
  // 太さの変わる線分(丸筆の一画)
  function seg(b, ax, ay, ar, bx, by, br, val) {
    val = val === undefined ? 1 : val;
    const R = Math.max(ar, br) + 1;
    const x0 = Math.max(b.x, Math.floor(Math.min(ax, bx) - R)), x1 = Math.min(b.x + b.w - 1, Math.ceil(Math.max(ax, bx) + R));
    const y0 = Math.max(b.y, Math.floor(Math.min(ay, by) - R)), y1 = Math.min(b.y + b.h - 1, Math.ceil(Math.max(ay, by) + R));
    const dx = bx - ax, dy = by - ay, L2 = dx * dx + dy * dy || 1e-9;
    for (let y = y0; y <= y1; y++) {
      const o = (y - b.y) * b.w - b.x, py = y + 0.5 - ay;
      for (let x = x0; x <= x1; x++) {
        const pxx = x + 0.5 - ax; let t = (pxx * dx + py * dy) / L2; t = t < 0 ? 0 : t > 1 ? 1 : t;
        const ex = pxx - dx * t, ey = py - dy * t, d = Math.sqrt(ex * ex + ey * ey);
        let c = ar + (br - ar) * t - d + 0.5; if (c <= 0) continue; if (c > 1) c = 1; c *= val;
        if (c > b.a[o + x]) b.a[o + x] = c;
      }
    }
  }
  function stroke(b, pts, val) { for (let i = 0; i + 1 < pts.length; i++) seg(b, pts[i][0], pts[i][1], pts[i][2], pts[i + 1][0], pts[i + 1][1], pts[i + 1][2], val); }
  function disc(b, cx, cy, r, val) { seg(b, cx, cy, r, cx + 0.01, cy, r, val); }
  function boundsOf(pts, pad) {
    let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
    for (const p of pts) { const r = (p[2] || 0) + pad; if (p[0] - r < x0) x0 = p[0] - r; if (p[1] - r < y0) y0 = p[1] - r; if (p[0] + r > x1) x1 = p[0] + r; if (p[1] + r > y1) y1 = p[1] + r; }
    return [x0, y0, x1, y1];
  }

  // ── にじみ・縁・かすれ ──
  function warp(b, amp, lam, seed, oct) {
    const dx = fbmArr(b, lam, oct || 2, seed), dy = fbmArr(b, lam, oct || 2, seed + 977);
    const src = b.a, w = b.w, h = b.h, out = new Float32Array(src.length);
    for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i++) {
      let sx = x + amp * dx[i], sy = y + amp * dy[i];
      sx = sx < 0 ? 0 : sx > w - 1.001 ? w - 1.001 : sx; sy = sy < 0 ? 0 : sy > h - 1.001 ? h - 1.001 : sy;
      const ix = sx | 0, iy = sy | 0, fx = sx - ix, fy = sy - iy, o = iy * w + ix;
      out[i] = (src[o] * (1 - fx) + src[o + 1] * fx) * (1 - fy) + (src[o + w] * (1 - fx) + src[o + w + 1] * fx) * fy;
    }
    b.a = out; return b;
  }
  // 乾いた紙に置いた水の縁:角が丸まり、紙目で細かく揺れる。wob は揺れ幅(px)
  function organic(b, soft, wob, lam, seed) {
    blur(b.a, b.w, b.h, soft);
    const n = wob > 0 ? fbmArr(b, lam, 3, seed) : null, k = 0.4 / soft, aa = 0.55 * k, amp = wob * k, a = b.a;
    for (let i = 0; i < a.length; i++) { const v = a[i]; if (v <= 0.001) { a[i] = 0; continue; } if (v >= 0.999) { a[i] = 1; continue; } a[i] = sstep(0.5 - aa, 0.5 + aa, v + (n ? amp * n[i] * 4 * v * (1 - v) : 0)); }
    return b;
  }
  // 縁に顔料が寄ってできる濃い線
  function fringe(b, e, r, pow, comp) {
    pow = pow || 1.5; comp = comp === undefined ? 0.12 : comp;
    const c = b.a.slice(); blur(c, b.w, b.h, r); const a = b.a;
    for (let i = 0; i < a.length; i++) {
      const m = a[i]; if (m <= 0) continue;
      let t = (m - c[i]) * 2.1; t = t < 0 ? 0 : t > 1 ? 1 : t;
      a[i] = m * (1 - comp * e + e * Math.pow(t, pow));
    }
    return b;
  }
  function vary(b, amp, lam, seed, oct, aniso) {
    const n = fbmArr(b, lam, oct || 3, seed, 0.5, aniso), a = b.a;
    for (let i = 0; i < a.length; i++) { const q = 1 + amp * n[i]; a[i] *= q > 0 ? q : 0; }
    return b;
  }
  // 濡れた紙の上のにじみ:大きく歪めてからぼかし、所々だけ縁を残し、最後に細かく毛羽立てる
  function wetify(b, sigma, seed, lost, feather) {
    lost = lost === undefined ? 0.7 : lost; feather = feather === undefined ? 0.3 : feather;
    warp(b, sigma * 0.9, sigma * 3.5, seed);
    const sharp = b.a.slice(); blur(sharp, b.w, b.h, sigma * 0.22);
    blur(b.a, b.w, b.h, sigma);
    const n = fbmArr(b, sigma * 6, 2, seed + 7), a = b.a;
    for (let i = 0; i < a.length; i++) { const t = 1 - lost + lost * sstep(-0.9, 0.4, n[i]); a[i] = sharp[i] + (a[i] - sharp[i]) * t; }
    if (feather > 0) warp(b, sigma * feather, sigma * 0.6, seed + 13, 3);
    return b;
  }
  // かすれ:筆が紙の山にしか触れない。dry 0(べた)→1(ほぼ乗らない)
  function drybrush(b, dry, seed, lam, aniso, dryArr) {
    const st = fbmArr(b, lam || 9 * S, 2, seed, 0.5, aniso || 1), a = b.a;
    for (let y = 0, i = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x;
      for (let x = 0; x < b.w; x++, i++, gi++) {
        if (a[i] <= 0) continue;
        const d = dryArr ? dryArr[i] : dry, tex = paperH[gi] * 0.62 + 0.19 + 0.19 * st[i];
        a[i] *= sstep(d - 0.09, d + 0.09, tex);
      } }
    return b;
  }
  // バックラン(乾きかけに水が戻ってできる花形のしみ)
  function bloom(b, cx, cy, rad, str, seed) {
    const a = b.a, x0 = Math.max(0, Math.floor(cx - rad * 1.6 - b.x)), x1 = Math.min(b.w - 1, Math.ceil(cx + rad * 1.6 - b.x));
    const y0 = Math.max(0, Math.floor(cy - rad * 1.6 - b.y)), y1 = Math.min(b.h - 1, Math.ceil(cy + rad * 1.6 - b.y));
    for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) {
      const dx = b.x + x - cx, dy = b.y + y - cy, ang = Math.atan2(dy, dx);
      const rr = rad * (1 + 0.28 * fbm1(ang * 1.9 + seed, 3, seed) + 0.10 * fbm2((b.x + x) / (rad * 0.16), (b.y + y) / (rad * 0.16), 3, seed + 5));
      const d = Math.sqrt(dx * dx + dy * dy) / rr, i = y * b.w + x;
      const inner = 1 - sstep(0.15, 1.0, d), edge = Math.exp(-Math.pow((d - 1) / 0.055, 2)) + 0.35 * Math.exp(-Math.pow((d - 1.04) / 0.16, 2)) * (d > 1 ? 1 : 0.3);
      a[i] *= 1 - str * 0.55 * inner * (d < 1 ? 1 : 0) - str * 0.25 * (d < 1 ? 1 : 0) + str * 0.9 * edge;
    }
    return b;
  }

  // ── 顔料 ──
  function pig(r, g, b, gr) { return { k: [-Math.log(r), -Math.log(g), -Math.log(b)], g: gr || 0 }; }
  const P = {
    cobalt: pig(0.30, 0.47, 0.80, 0.12), cerulean: pig(0.36, 0.63, 0.83, 0.20), ultra: pig(0.23, 0.31, 0.72, 0.30), prussian: pig(0.10, 0.30, 0.48, 0.05),
    rawSienna: pig(0.86, 0.63, 0.31, 0.16), ochre: pig(0.90, 0.74, 0.38, 0.20), gold: pig(0.91, 0.63, 0.16, 0.05),
    bSienna: pig(0.71, 0.37, 0.21, 0.20), umber: pig(0.46, 0.31, 0.21, 0.16), sepia: pig(0.36, 0.28, 0.23, 0.15),
    rose: pig(0.90, 0.38, 0.52, 0.03), crimson: pig(0.72, 0.15, 0.24, 0.03), red: pig(0.90, 0.26, 0.15, 0.06),
    sap: pig(0.47, 0.62, 0.21, 0.08), viridian: pig(0.20, 0.58, 0.47, 0.22), aureolin: pig(0.98, 0.90, 0.30, 0.03), naples: pig(0.99, 0.91, 0.70, 0.05),
    neutral: pig(0.30, 0.33, 0.41, 0.2), graphite: pig(0.43, 0.43, 0.45, 0),
  };
  let granK = 1, T0 = D0, T1 = D1, T2 = D2, frameOn = true;
  function beginLayer() { T0 = new Float32Array(N); T1 = new Float32Array(N); T2 = new Float32Array(N); }
  function endLayer(b, K, M) {
    for (let y = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x; for (let x = 0; x < b.w; x++, gi++) {
      const e = T0[gi] + T1[gi] + T2[gi]; if (e <= 0) continue;
      const f = e <= K ? 1 : (K + (M - K) * (1 - Math.exp(-(e - K) / (M - K)))) / e;
      D0[gi] += T0[gi] * f; D1[gi] += T1[gi] * f; D2[gi] += T2[gi] * f; } }
    T0 = D0; T1 = D1; T2 = D2; touch(b.x, b.y, b.x + b.w, b.y + b.h);
  }
  function dep(b, pg, s, mod) {
    s = s === undefined ? 1 : s; if (s <= 0 || b.w === 0 || b.h === 0) return;
    const k0 = pg.k[0] * s, k1 = pg.k[1] * s, k2 = pg.k[2] * s, g = pg.g * granK, a = b.a, bw = b.w;
    for (let y = 0; y < b.h; y++) {
      let gi = (b.y + y) * W + b.x, i = y * bw;
      for (let x = 0; x < bw; x++, gi++, i++) {
        let m = a[i]; if (m < 1e-4) continue;
        if (mod) m *= mod[i];
        m *= (frameOn ? frame[gi] : 1) * (1 - resist[gi]);
        if (g) { const q = 1 + g * gran[gi]; m *= q > 0.2 ? q : 0.2; }
        if (m <= 0) continue;
        T0[gi] += m * k0; T1[gi] += m * k1; T2[gi] += m * k2;
      }
    }
    touch(b.x, b.y, b.x + b.w, b.y + b.h);
  }
  // 複数の顔料を、それぞれ別のむらで置く(濡れているうちに色を差す)
  function charge(b, list, c, lam, seed) {
    list.forEach((it, i) => {
      let mod = null;
      if (c > 0) { mod = fbmArr(b, lam * (0.8 + 0.4 * ((i * 37) % 5) / 5), 3, seed + i * 53); for (let k = 0; k < mod.length; k++) { const q = 1 + c * mod[k]; mod[k] = q > 0 ? q : 0; } }
      dep(b, it[0], it[1], mod);
    });
  }
  function lift(b, s) {
    const a = b.a;
    for (let y = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x, i = y * b.w;
      for (let x = 0; x < b.w; x++, gi++, i++) {
        let m = a[i] * s * (1 - resist[gi]); if (m <= 1e-4) continue; if (m > 1) m = 1; m = 1 - m;
        D0[gi] *= m; D1[gi] *= m; D2[gi] *= m;
      } }
    touch(b.x, b.y, b.x + b.w, b.y + b.h);
  }
  function rebuildResist() {
    resist.fill(0);
    for (const b of resistLayers.values()) for (let y = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x, i = y * b.w; for (let x = 0; x < b.w; x++, gi++, i++) if (b.a[i] > resist[gi]) resist[gi] = b.a[i]; }
  }
  function mask(name, b) { resistLayers.set(name, b); rebuildResist(); }
  function unmask(name) { resistLayers.delete(name); rebuildResist(); }

  // ── 紙 ──
  function makePaper() {
    const full = { x: 0, y: 0, w: W, h: H };
    addNoise(paperH, 0, 0, W, H, 3.4 * S, 3.4 * S, SEED + 1, 0.52);
    addNoise(paperH, 0, 0, W, H, 1.9 * S, 1.9 * S, SEED + 2, 0.50);
    addNoise(paperH, 0, 0, W, H, 7 * S, 7 * S, SEED + 3, 0.20);
    addNoise(paperH, 0, 0, W, H, 1.3 * S, 1.3 * S, SEED + 4, 0.22);
    const fine = fbmArr(full, 1.7 * S, 1, SEED + 5);
    for (let i = 0; i < N; i++) paperH[i] = 0.5 + 0.5 * Math.tanh(paperH[i] * 0.95);
    const hb = paperH.slice(); blur(hb, W, H, 3.2 * S);
    for (let i = 0; i < N; i++) {
      let g = (hb[i] - paperH[i]) * 3.4; g = g > 0 ? Math.pow(g, 1.25) * 1.25 : g * 0.75;
      gran[i] = g - 0.05 + 0.2 * fine[i];
    }
    // テープ:ほぼ直線、ところどころ絵具が潜り込む
    const side = (n, s) => { const a = new Float32Array(n); for (let i = 0; i < n; i++) { const t = i / S; a[i] = (0.9 * fbm1(t / 70, 3, s) + 5.0 * Math.pow(Math.max(0, vnoise1(t / 17, s + 1) - 0.62) / 0.38, 1.5)) * S; } return a; };
    const tl = side(H, SEED + 20), tr = side(H, SEED + 21), tt = side(W, SEED + 22), tb = side(W, SEED + 23);
    for (let y = 0, i = 0; y < H; y++) for (let x = 0; x < W; x++, i++) {
      const d = Math.min(x + 0.5 - (px0 - tl[y]), (px1 + tr[y]) - (x + 0.5), y + 0.5 - (py0 - tt[x]), (py1 + tb[x]) - (y + 0.5));
      frame[i] = d <= -0.5 ? 0 : clamp01(d + 0.5) * (1 + 0.32 * Math.exp(-Math.max(d, 0) / (2.6 * S)));
    }
  }

  // ── 仕上げ(濃度 → 色) ──
  const LUTN = 1 << 14, LUTMAX = 10, EXPL = new Float32Array(LUTN + 2);
  for (let i = 0; i < LUTN + 2; i++) EXPL[i] = Math.exp(-i * LUTMAX / LUTN);
  const LK = LUTN / LUTMAX;
  function compose(out, rect) {
    out = out || new Uint8ClampedArray(N * 4);
    const x0 = rect ? Math.max(0, rect[0]) : 0, y0 = rect ? Math.max(0, rect[1]) : 0, x1 = rect ? Math.min(W, rect[2]) : W, y1 = rect ? Math.min(H, rect[3]) : H;
    for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++) {
      const i = y * W + x, h = paperH[i];
      let L = 1 - 0.016 * (1 - h) * (1 - h);
      if (x > 0 && y > 0 && x < W - 1 && y < H - 1) L += 0.05 * (paperH[i - W - 1] - paperH[i + W + 1]);
      const dth = (hash2(x, y, 99) * 0.5) / 255;
      const d0 = D0[i] * LK, d1 = D1[i] * LK, d2 = D2[i] * LK, o = i * 4;
      out[o] = 255 * (PAPER[0] * EXPL[d0 < LUTN ? d0 | 0 : LUTN] * L + dth) + 0.5;
      out[o + 1] = 255 * (PAPER[1] * EXPL[d1 < LUTN ? d1 | 0 : LUTN] * L + dth) + 0.5;
      out[o + 2] = 255 * (PAPER[2] * EXPL[d2 < LUTN ? d2 | 0 : LUTN] * L + dth) + 0.5;
      out[o + 3] = 255;
    }
    return out;
  }

  // ───────────────────────── 本番:土・谷・大地 ─────────────────────────
  const SUN = { u: 0.385, v: 0.335 };
  const AR = ph / pw;

  function ridgeArr(ctrl, amp, lam, seed, sharp, trees) {
    sharp = sharp || 0;
    const f = curve1D(ctrl.map(p => [X(p[0]), Y(p[1])])), out = new Float32Array(W);
    for (let x = 0; x < W; x++) {
      const t = (x - px0) / (lam * pw);
      const n = fbm1(t, 7, seed, 0.55), r = 1 - 2 * Math.abs(fbm1(t * 0.7 + 5, 5, seed + 9, 0.5));
      out[x] = f(x) + amp * ph * (n * (1 - sharp) - r * sharp);
      if (trees) out[x] -= trees * S * (0.35 + 0.65 * Math.abs(vnoise1(x / (2.3 * S), seed + 77))) * (0.55 + 0.45 * vnoise1(x / (19 * S), seed + 78));
    }
    return out;
  }

  // ── 空:紙を濡らして、暖色→ばら色→青の順に流す ──
  function paintSky() {
    const b = mkbuf(px0 - 12 * S, py0 - 12 * S, px1 + 12 * S, py1 + 12 * S);
    const w = b.w, h = b.h, n = w * h;
    // 雲:雲底の平面に遠近をつけたノイズ(地平に近いほど薄く長い)
    const q = 3, cw = Math.ceil(w / q) + 2, chh = Math.ceil(h / q) + 2, cl = new Float32Array(cw * chh);
    for (let j = 0; j < chh; j++) for (let i = 0; i < cw; i++) {
      const u = (b.x + i * q - px0) / pw, v = (b.y + j * q - py0) / ph;
      if (v > 0.47) continue;
      const dv = 0.66 - v, Z = 0.5 / dv, Xc = (u - 0.42) * Z * 1.6;
      const wz = 0.4 * fbm2(Xc * 0.7 + 9, Z * 1.1, 2, SEED + 301);
      const nn = fbm2(Xc * 1.25 + 3.1 + wz, Z * 2.6 + wz * 0.6, 5, SEED + 300, 0.55);
      const r = Math.hypot(u - SUN.u, (v - SUN.v) * AR * 1.2);
      const bias = -0.19
        + 0.25 * Math.exp(-Math.pow((v - (0.17 - 0.08 * u)) / 0.085, 2)) * sstep(0.30, 0.72, u)
        + 0.16 * Math.exp(-Math.pow((v - 0.318) / 0.04, 2)) * sstep(0.46, 0.8, u)
        + 0.12 * Math.exp(-Math.pow((v - 0.21) / 0.05, 2)) * (1 - sstep(0.2, 0.5, u))
        - 0.30 * Math.exp(-Math.pow(r / 0.10, 2));
      cl[j * cw + i] = sstep(0.02, 0.32, nn + bias) * (1 - sstep(0.36, 0.44, v));
    }
    const C = like(b);
    for (let y = 0, i = 0; y < h; y++) { const fy = y / q, jy = fy | 0, ty = fy - jy;
      for (let x = 0; x < w; x++, i++) { const fx = x / q, ix = fx | 0, tx = fx - ix, o = jy * cw + ix;
        C.a[i] = (cl[o] * (1 - tx) + cl[o + 1] * tx) * (1 - ty) + (cl[o + cw] * (1 - tx) + cl[o + cw + 1] * tx) * ty; } }
    warp(C, 10 * S, 70 * S, SEED + 310); bblur(C, 7 * S); warp(C, 2.2 * S, 13 * S, SEED + 311, 2);
    const c = C.a, cs = c.slice(); blur(cs, w, h, 9 * S); const dl = Math.round(15 * S) * w;
    const blue = like(b), cer = like(b), warm = like(b), rose = like(b), gray = like(b);
    for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i++) {
      const u = (b.x + x + 0.5 - px0) / pw, v = (b.y + y + 0.5 - py0) / ph;
      const dx = u - SUN.u, dy = (v - SUN.v) * AR, cc = c[i];
      const rW = Math.hypot(dx, dy * 1.35), rB = Math.hypot(dx * 0.8, dy * 1.75);
      const nearB = Math.exp(-Math.pow(rB / 0.30, 2)), hot = Math.exp(-Math.pow(rW / 0.045, 2));
      const land = 1 - sstep(0.49, 0.61, v);                                       // 地面の側へは淡い下塗りだけ残す
      const litLow = clamp01((cs[i] - (i + dl < n ? cs[i + dl] : cs[i])) * 3.0);   // 朝日は雲を下から照らす
      const bv = Math.pow(sstep(0.50, -0.10, v), 1.3);
      blue.a[i] = (0.60 * bv * (1 - 0.9 * nearB) + 0.07 * sstep(0.55, 1.0, u) * bv) * (1 - 0.86 * cc);
      cer.a[i] = 0.15 * Math.exp(-Math.pow((v - 0.27) / 0.13, 2)) * (1 - nearB) * (1 - 0.85 * cc);
      warm.a[i] = (0.44 * Math.exp(-Math.pow(rW / 0.17, 1.5)) * (1 - 0.8 * hot)
        + 0.17 * sstep(0.28, 0.47, v) * (0.35 + 0.65 * Math.exp(-Math.pow(dx / 0.45, 2)))) * (1 - 0.3 * cc) * (0.42 + 0.58 * land) * (1 - 0.5 * sstep(0.7, 1.0, v))
        + 0.24 * litLow * (0.4 + 0.6 * Math.exp(-Math.pow(rW / 0.5, 2)));
      rose.a[i] = (0.10 * Math.exp(-Math.pow((rW - 0.225) / 0.10, 2)) + 0.055 * Math.exp(-Math.pow((v - 0.39) / 0.06, 2))) * land
        + 0.06 * litLow + 0.03 * cc;
      gray.a[i] = 0.22 * sstep(0.4, 1.0, cc) * (0.55 + 0.45 * (1 - nearB)) * (1 - 0.6 * litLow);
    }
    const lo = fbmArr(b, 320 * S, 3, SEED + 320), md = fbmArr(b, 70 * S, 3, SEED + 321);
    for (const f of [blue, cer, warm, rose, gray]) for (let i = 0; i < n; i++) f.a[i] *= Math.max(0, 1 + 0.11 * lo[i] + 0.05 * md[i]);
    dep(warm, P.naples, 1); dep(warm, P.gold, 0.08);
    dep(rose, P.rose, 1);
    dep(blue, P.cobalt, 1); dep(blue, P.ultra, 0.10);
    dep(cer, P.cerulean, 1);
    dep(gray, P.cobalt, 0.55); dep(gray, P.rose, 0.40); dep(gray, P.bSienna, 0.20);
    // 巻雲:半乾きの空を湿らせた平筆でさっと拭う。太陽から放射する向きに流す
    { const cb = like(b), r = makeRng(SEED + 330), sxp = X(SUN.u), syp = Y(SUN.v);
      for (let k = 0; k < 9; k++) {
        const u0 = r.range(0.50, 0.98), v0 = r.range(0.035, 0.24), x0 = X(u0), y0 = Y(v0);
        const base = Math.atan2(y0 - syp, x0 - sxp) + r.range(-0.22, 0.22), len = r.range(160, 420) * S, nf = 5 + r.int(6);
        for (let f = 0; f < nf; f++) {
          const off = r.range(-16, 16) * S, a = base + r.range(-0.05, 0.05), l = len * r.range(0.45, 1), s0 = r.range(-0.5, 0.1) * len;
          const sx = x0 + Math.cos(a) * s0 - Math.sin(a) * off, sy = y0 + Math.sin(a) * s0 + Math.cos(a) * off, pts = [], n = 14;
          for (let q = 0; q <= n; q++) { const t = q / n, cv = r.range(0.04, 0.10) * l * t * t; pts.push([sx + Math.cos(a) * l * t - Math.sin(a) * cv, sy + Math.sin(a) * l * t + Math.cos(a) * cv, (1.2 + 5.5 * Math.sin(Math.PI * Math.pow(t, 0.7))) * S * r.range(0.8, 1.2)]); }
          stroke(cb, pts, r.range(0.25, 0.6));
        }
      }
      bblur(cb, 4.5 * S); warp(cb, 5 * S, 40 * S, SEED + 331);
      const nz = fbmArr(b, 9 * S, 2, SEED + 332, 0.5, 0.25);
      for (let i = 0; i < cb.a.length; i++) cb.a[i] *= Math.max(0, 0.75 + 0.5 * nz[i]);
      lift(cb, 0.62);
      const tint = like(b); for (let i = 0; i < n; i++) tint.a[i] = cb.a[i] * 0.5; dep(tint, P.rose, 0.10); dep(tint, P.naples, 0.16); }
    // 太陽:湿らせた筆で丸く抜く
    const sx = X(SUN.u), sy = Y(SUN.v), sr = 0.0112 * pw;
    const d = mkbuf(sx - sr * 3, sy - sr * 3, sx + sr * 3, sy + sr * 3);
    disc(d, sx, sy, sr); bblur(d, 1.6 * S); lift(d, 0.93);
  }

  // ── 山:稜線はくっきり、裾は水でぼかして霧に消す ──
  function mountain(o) {
    const rd = o.ridge, foot = new Float32Array(W); let top = Infinity, bot = -Infinity;
    for (let x = 0; x < W; x++) { foot[x] = o.foot(x); if (x >= px0 && x < px1) { if (rd[x] < top) top = rd[x]; if (foot[x] > bot) bot = foot[x]; } }
    const b = mkbuf(px0 - 12 * S, top - 10 * S, px1 + 12 * S, bot + 6 * S), a = b.a, w = b.w, h = b.h;
    for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i++) a[i] = clamp01(b.y + y + 1 - rd[b.x + x]);
    if (o.fr) fringe(b, o.fr, (o.frR || 2.2) * S, 1.6, 0.04);
    if (o.fr2) fringe(b, o.fr2, (o.fr2R || 18) * S, 1, 0.35);
    const mn = fbmArr(b, 280 * S, 3, o.seed + 1, 0.5, 0.3), fd = o.fade === undefined ? 0.4 : o.fade, fn = o.fadeN === undefined ? 0.16 : o.fadeN;
    for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i++) {
      if (a[i] <= 0) continue;
      const gx = b.x + x, t = (b.y + y + 0.5 - rd[gx]) / Math.max(10 * S, foot[gx] - rd[gx]);
      a[i] *= (1 - sstep(fd, 1.0, t + fn * mn[i])) * (1 - sstep(0.86, 1.0, t)) * sstep(0, 60 * S, foot[gx] - rd[gx]);
    }
    granK = o.gran === undefined ? 1 : o.gran;
    if (o.clip) for (const cr of o.clip) for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i++) if (a[i] > 0) a[i] *= clamp01(cr[b.x + x] + 2.5 * S - (b.y + y));
    vary(b, o.v === undefined ? 0.12 : o.v, 150 * S, o.seed + 2);
    if (o.streak) vary(b, o.streak, 22 * S, o.seed + 3, 2, 7);
    if (o.forest) vary(b, o.forest, 7 * S, o.seed + 5, 2, 1.7);
    // 太陽に近い側は空気が光って淡く、暖かくなる
    const ns = new Float32Array(w * h), cool = new Float32Array(w * h), sg = o.sunW || 0.20;
    for (let x = 0; x < w; x++) { const q = Math.exp(-Math.pow(((b.x + x - px0) / pw - SUN.u) / sg, 2)); for (let y = 0; y < h; y++) { ns[y * w + x] = q; cool[y * w + x] = 1 - (o.sunK === undefined ? 0.5 : o.sunK) * q; } }
    o.pigs.forEach((it, k) => {
      const nz = o.charge ? fbmArr(b, (o.chargeL || 190) * S * (0.8 + 0.13 * k), 3, o.seed + 40 + k * 7) : null;
      const base = it[2] === 'w' ? ns : cool, mod = new Float32Array(w * h);
      for (let i = 0; i < mod.length; i++) mod[i] = base[i] * (nz ? Math.max(0, 1 + o.charge * nz[i]) : 1);
      dep(b, it[0], it[1], mod);
    });
    granK = 1;
    return b;
  }
  const lineFn = ctrl => curve1D(ctrl.map(p => [X(p[0]), Y(p[1])]));

  // 尾根筋の陰を一段重ねる:尾根の線でくっきり切り、反対側は水でぼかす
  function spurGlaze(mb, rd, o) {
    const g = like(mb), w = mb.w, h = mb.h, r = makeRng(o.seed);
    for (let k = 0; k < o.n; k++) {
      const xs = X(mix(o.x0, o.x1, (k + r.range(0.15, 0.85)) / o.n)), ys = rd[clamp(Math.round(xs), 0, W - 1)];
      const sl = r.range(o.slope[0], o.slope[1]), L = r.range(0.6, 1.4) * o.len * S, str = r.range(0.5, 1), sd = o.seed + k * 13, depth = r.range(0.5, 1.0) * o.depth * S;
      for (let y = Math.max(0, Math.floor(ys - mb.y)); y < h; y++) {
        const dy = mb.y + y + 0.5 - ys; if (dy > depth * 1.3) break;
        const sx = xs + dy * sl + 26 * S * fbm1(dy / (80 * S), 3, sd) * Math.min(1, dy / (40 * S));
        const fy = (1 - sstep(depth * 0.5, depth * 1.3, dy)) * sstep(0, 16 * S, dy) * str;
        const xa = Math.max(mb.x, Math.floor(o.dir > 0 ? sx - 4 * L : sx - 1)), xb = Math.min(mb.x + w - 1, Math.ceil(o.dir > 0 ? sx + 1 : sx + 4 * L));
        for (let gx = xa; gx <= xb; gx++) {
          const sgn = (gx + 0.5 - sx) * o.dir, v = clamp01(0.5 - sgn) * Math.exp(Math.min(0, sgn) / L) * fy, i = y * w + gx - mb.x;
          if (v > g.a[i]) g.a[i] = v;
        }
      }
    }
    fringe(g, 0.35, 2 * S, 1.5, 0);
    for (let i = 0; i < g.a.length; i++) g.a[i] *= Math.min(1, mb.a[i]);
    vary(g, 0.2, 60 * S, o.seed + 3);
    granK = 0.5; charge(g, o.pigs, 0.4, 120 * S, o.seed + 5); granK = 1;
  }

  // 谷に溜まる朝霧:横に長いむらで顔料を拭き取る
  function mist() {
    const b = mkbuf(px0 - 12 * S, Y(0.40), px1 + 12 * S, Y(0.70)), w = b.w, h = b.h;
    const n1 = fbmArr(b, 460 * S, 4, SEED + 500, 0.55, 0.15), n2 = fbmArr(b, 130 * S, 3, SEED + 501, 0.5, 0.3);
    const base = lineFn([[-0.05, 0.635], [0.15, 0.605], [0.30, 0.572], [0.42, 0.553], [0.50, 0.548], [0.62, 0.556], [0.80, 0.585], [1.05, 0.625]]);
    for (let x = 0; x < w; x++) { const by = base(b.x + x), u = (b.x + x - px0) / pw;
      for (let y = 0; y < h; y++) { const i = y * w + x, d = (b.y + y + 0.5 - by) / ph;
        const dd = d + 0.012 * n2[i] + 0.016 * n1[i], band = dd < 0 ? Math.exp(-Math.pow(dd / 0.03, 2)) : Math.exp(-Math.pow(dd / 0.05, 2));
        const wisp = Math.exp(-Math.pow((d + 0.062) / 0.014, 2)) * sstep(-0.1, 0.6, n1[i]) * 0.75 + Math.exp(-Math.pow((d + 0.105) / 0.011, 2)) * sstep(0.1, 0.9, -n1[i]) * 0.5;
        const glow = 0.25 * Math.exp(-Math.pow((u - 0.44) / 0.16, 2));
        b.a[i] = clamp01((band * (0.9 + glow) + wisp) * (0.74 + 0.32 * n1[i] + 0.16 * n2[i]));
      } }
    warp(b, 16 * S, 130 * S, SEED + 502); bblur(b, 3 * S); lift(b, 0.9);
  }

  // ── 楓の枝(左上から差しかかる)。葉は 7 裂、枝は節ごとに対生 ──
  function makeMaple() {
    const r = makeRng(SEED + 900), branches = [], leaves = [], petioles = [];
    const leafAt = (x, y, ang, sz) => leaves.push({ x, y, ang, sz, sq: r.range(0.5, 1), sqa: r() * Math.PI, hue: 0, sh: r() < 0.3, v: r() });
    function grow(x, y, ang, len, rad, depth) {
      const n = 5, raw = [[x, y, rad]]; let a = ang, cx = x, cy = y;
      for (let i = 1; i <= n; i++) {
        a += r.range(-0.17, 0.17) + (depth >= 2 ? 0.05 * Math.sin(Math.PI / 2 - a) : 0);          // 細い枝は少し垂れる
        cx += Math.cos(a) * len / n; cy += Math.sin(a) * len / n; raw.push([cx, cy, Math.max(0.45 * S, rad * Math.pow(1 - i / n, 0.8) + 0.2 * S)]);
      }
      const pts = crSpline(raw, 5); branches.push(pts);
      const at = t => { const i = clamp(Math.round(t * (pts.length - 1)), 1, pts.length - 2), q = pts[i], q0 = pts[i - 1], q1 = pts[i + 1]; return [q[0], q[1], q[2], Math.atan2(q1[1] - q0[1], q1[0] - q0[0])]; };
      if (depth < 3) {
        const nc = depth === 0 ? 6 : depth === 1 ? 2 + r.int(2) : 1 + r.int(2);
        for (let c = 0; c < nc; c++) {
          const t = depth === 0 ? 0.12 + 0.86 * (c + r.range(0.1, 0.9)) / nc : r.range(0.25, 0.95), q = at(t), sd = (c % 2 ? 1 : -1) * (r() < 0.85 ? 1 : -1);
          grow(q[0], q[1], q[3] + sd * r.range(0.45, 1.0), len * r.range(0.34, 0.58) * (depth === 0 ? 1 - 0.35 * t : 1), Math.max(0.7 * S, q[2] * 0.62), depth + 1);
        }
      }
      if (depth >= 1) {
        const nn = depth === 1 ? 1 : 2 + r.int(2);
        for (let k = 0; k < nn; k++) {
          const q = at(depth === 1 ? r.range(0.55, 1) : 0.3 + 0.7 * (k + r.range(0.2, 0.8)) / nn);
          for (const sd of [1, -1]) {
            if (r() < 0.26) continue;
            let pa = q[3] + sd * r.range(0.5, 1.2); pa += 0.45 * Math.sin(Math.PI / 2 - pa);            // 葉柄は下へ垂れる
            const pl = r.range(9, 24) * S, lx = q[0] + Math.cos(pa) * pl, ly = q[1] + Math.sin(pa) * pl;
            if (ly > Y(0.33) && r() < 0.7) continue;
            petioles.push([[q[0], q[1], 0.5 * S], [lx, ly, 0.32 * S]]);
            leafAt(lx, ly, pa + r.range(-0.35, 0.35) + 0.3 * Math.sin(Math.PI / 2 - pa), r.range(25, 44) * S);
          }
        }
        const e = pts[pts.length - 1], ea = at(1)[3]; leafAt(e[0], e[1], ea + 0.4 * Math.sin(Math.PI / 2 - ea), r.range(28, 44) * S);
      }
    }
    grow(X(-0.03), Y(0.055), 0.14, 0.40 * pw, 11 * S, 0);
    grow(X(0.10), Y(-0.03), 0.80, 0.13 * pw, 5 * S, 1);
    grow(X(-0.03), Y(0.205), -0.05, 0.13 * pw, 4.2 * S, 1);
    for (const L of leaves) L.hue = clamp01(0.55 + 0.55 * fbm2(L.x / (230 * S), L.y / (230 * S), 2, SEED + 901) + r.range(-0.22, 0.22));
    return { branches, leaves, petioles };
  }
  const LOBES = [[-2.22, 0.36], [-1.46, 0.70], [-0.72, 0.93], [0, 1], [0.72, 0.93], [1.46, 0.70], [2.22, 0.36]];
  function leafPoly(L, jr) {
    const c = [0.1, 0], u = [[0, 0]];
    const pol = (a, rad) => [c[0] + Math.cos(a) * rad, c[1] + Math.sin(a) * rad];
    u.push(pol(LOBES[0][0] - 0.45, 0.13));
    for (let i = 0; i < LOBES.length; i++) {
      const a = LOBES[i][0], len = LOBES[i][1] * (0.9 + 0.2 * jr()), w = 0.19;
      u.push(pol(a - w, len * 0.52), pol(a - w * 0.42, len * 0.80), pol(a, len), pol(a + w * 0.42, len * 0.80), pol(a + w, len * 0.52));
      if (i < LOBES.length - 1) u.push(pol((a + LOBES[i + 1][0]) / 2, 0.27 * Math.min(len, LOBES[i + 1][1]) * (0.9 + 0.2 * jr())));
    }
    u.push(pol(LOBES[6][0] + 0.45, 0.13));
    const ca = Math.cos(L.ang), sa = Math.sin(L.ang), nx = Math.cos(L.sqa), ny = Math.sin(L.sqa), out = [], tips = [];
    const gs = L.gs || 1;
    const tr = q => { let x = q[0], y = q[1]; const d = (x * nx + y * ny) * (L.sq - 1); x += d * nx; y += d * ny; return [L.x + (x * ca - y * sa) * L.sz, L.y + (x * sa + y * ca) * L.sz * gs]; };
    for (const q of u) { const t = tr(q); out.push(t[0], t[1]); }
    for (const lb of LOBES) tips.push(tr(pol(lb[0], lb[1] * 0.82)));
    return { poly: out, tips, c: tr(c) };
  }
  let MAPLE = null;
  function maskMaple() {
    MAPLE = makeMaple();
    const mb = mkbuf(px0 - 12 * S, py0 - 12 * S, X(0.56), Y(0.46)), jr = makeRng(SEED + 902);
    for (const L of MAPLE.leaves) { L.shape = leafPoly(L, jr); fillPoly(mb, L.shape.poly); }
    for (const br of MAPLE.branches) stroke(mb, br);
    for (const br of MAPLE.petioles) stroke(mb, br);
    MAPLE.buf = mb; mask('maple', mb);
  }
  function paintMaple() {
    unmask('maple');
    const mb = MAPLE.buf, r = makeRng(SEED + 903), sxp = X(SUN.u), syp = Y(SUN.v);
    beginLayer();
    for (const L of MAPLE.leaves) {
      const sh = L.shape, sz = L.sz, t = mkbuf(L.x - sz * 1.3, L.y - sz * 1.3, L.x + sz * 1.3, L.y + sz * 1.3);
      if (!t.w || !t.h) continue;
      fillPoly(t, sh.poly); fringe(t, 0.5, 1.4 * S, 1.4, 0.1);
      const glow = Math.exp(-Math.hypot(L.x - sxp, L.y - syp) / (0.32 * pw)), st = (L.sh ? 1.0 : 0.82) * (1 - 0.25 * glow) * (0.9 + 0.2 * L.v);
      const h = clamp01(L.hue - 0.18 * glow);
      const pg = h < 0.22 ? [[P.aureolin, 0.75], [P.sap, 0.20]] : h < 0.45 ? [[P.aureolin, 0.70], [P.gold, 0.34]] : h < 0.70 ? [[P.gold, 0.74], [P.red, 0.26]] : h < 0.9 ? [[P.red, 0.80], [P.gold, 0.28]] : [[P.red, 0.66], [P.crimson, 0.46]];
      const vm = fbmArr(t, sz * 0.5, 2, 9100 + (L.x | 0) * 7 + (L.y | 0)); for (let i = 0; i < vm.length; i++) vm[i] = Math.max(0.3, 1 + 0.22 * vm[i]);
      for (const it of pg) dep(t, it[0], it[1] * st, vm);
      // 葉先ほど赤く
      const tp = like(t); for (let y = 0, i = 0; y < t.h; y++) for (let x = 0; x < t.w; x++, i++) { if (t.a[i] <= 0) continue; const d = Math.hypot(t.x + x - sh.c[0], t.y + y - sh.c[1]) / sz; tp.a[i] = t.a[i] * sstep(0.3, 0.9, d); }
      dep(tp, P.red, 0.30 * st); if (h > 0.6) dep(tp, P.crimson, 0.14 * st);
      if (L.sh) { dep(t, P.crimson, 0.16); dep(t, P.ultra, 0.13); }
      // 葉脈
      const vn = like(t); for (const q of sh.tips) seg(vn, sh.c[0], sh.c[1], 0.5 * S, q[0], q[1], 0.25 * S, 0.8);
      for (let i = 0; i < vn.a.length; i++) vn.a[i] *= Math.min(1, t.a[i]);
      dep(vn, h < 0.45 ? P.rawSienna : P.crimson, 0.30); dep(vn, P.bSienna, 0.12);
    }
    endLayer(mb, 3.2, 4.8);
    { const pt = like(mb); for (const br of MAPLE.petioles) stroke(pt, br); dep(pt, P.crimson, 0.55); dep(pt, P.bSienna, 0.40); dep(pt, P.sepia, 0.2); }
    // 枝:濃い焦茶。上側に朝日の細い照り
    const bb = like(mb), hl = like(mb);
    const tw = like(mb);
    for (const br of MAPLE.branches) { if (br[0][2] > 2.0 * S) { stroke(bb, br); stroke(hl, br.map(q => [q[0] - q[2] * 0.25, q[1] - q[2] * 0.55, q[2] * 0.22])); } else stroke(tw, br); }
    dep(tw, P.sepia, 0.62); dep(tw, P.crimson, 0.26); dep(tw, P.ultra, 0.24);
    const tx = like(bb, bb.a.slice()); drybrush(tx, 0.16, SEED + 904, 18 * S, 0.2);
    for (let i = 0; i < bb.a.length; i++) bb.a[i] = 0.55 * bb.a[i] + 0.45 * tx.a[i];
    fringe(bb, 0.3, 1.4 * S);
    dep(bb, P.sepia, 0.95); dep(bb, P.ultra, 0.60); dep(bb, P.bSienna, 0.22);
    for (let i = 0; i < hl.a.length; i++) hl.a[i] *= bb.a[i] > 0 ? 1 : 0; lift(hl, 0.45); dep(hl, P.naples, 0.3);
  }

  function* sceneMain() {
    makePaper(); yield step('紙');
    // ── 形の定義 ──
    // 稜線。手前から L4(左)L5(右)L3 L2 L1
    const L1 = ridgeArr([[-0.05, 0.435], [0.10, 0.418], [0.22, 0.402], [0.33, 0.428], [0.45, 0.438], [0.58, 0.408], [0.70, 0.388], [0.82, 0.402], [0.95, 0.378], [1.05, 0.39]], 0.016, 0.11, SEED + 400, 0.35);
    const L2 = ridgeArr([[-0.05, 0.425], [0.08, 0.405], [0.2, 0.432], [0.32, 0.458], [0.42, 0.472], [0.50, 0.462], [0.565, 0.432], [0.612, 0.398], [0.646, 0.353], [0.668, 0.357], [0.70, 0.39], [0.745, 0.404], [0.80, 0.415], [0.90, 0.372], [1.05, 0.355]], 0.017, 0.10, SEED + 420, 0.4);
    const L3 = ridgeArr([[-0.05, 0.37], [0.06, 0.358], [0.15, 0.392], [0.25, 0.442], [0.35, 0.482], [0.43, 0.502], [0.52, 0.497], [0.62, 0.472], [0.72, 0.442], [0.80, 0.412], [0.90, 0.422], [1.05, 0.385]], 0.015, 0.09, SEED + 440, 0.3, 2.5);
    const L5 = ridgeArr([[-0.05, 0.70], [0.36, 0.66], [0.44, 0.585], [0.48, 0.548], [0.56, 0.515], [0.64, 0.485], [0.72, 0.445], [0.80, 0.40], [0.88, 0.355], [0.96, 0.30], [1.05, 0.262]], 0.012, 0.075, SEED + 460, 0.3, 4.2);
    const L4 = ridgeArr([[-0.05, 0.198], [0.03, 0.212], [0.10, 0.262], [0.17, 0.332], [0.24, 0.402], [0.31, 0.458], [0.38, 0.503], [0.45, 0.532], [0.50, 0.57], [0.58, 0.66], [1.05, 0.70]], 0.013, 0.07, SEED + 480, 0.3, 5.5);
    const HZ = Y(0.505), XC = X(0.44), F = 0.885 * ph;
    const proj = (gx, z) => [XC + F * gx / z, HZ + F / z];
    const unproj = (x, y) => { const z = F / (y - HZ); return [(x - XC) * z / F, z]; };
    const fgR = ridgeArr([[-0.05, 0.80], [0.10, 0.792], [0.22, 0.812], [0.36, 0.832], [0.48, 0.822], [0.58, 0.79], [0.68, 0.757], [0.78, 0.735], [0.88, 0.712], [0.96, 0.69], [1.05, 0.672]], 0.004, 0.05, SEED + 700);
    const rvS = crSpline([[0.452, 0.531], [0.50, 0.540], [0.565, 0.553], [0.52, 0.568], [0.40, 0.580], [0.335, 0.598], [0.40, 0.622], [0.535, 0.648], [0.60, 0.682], [0.52, 0.722], [0.36, 0.762], [0.25, 0.81], [0.16, 0.88], [0.10, 0.97]].map(p => [X(p[0]), Y(p[1])]), 26);
    const pathPts = crSpline([[0.395, 1.05, 0.090], [0.45, 0.968, 0.060], [0.52, 0.908, 0.036], [0.595, 0.848, 0.0185], [0.652, 0.797, 0.010], [0.688, 0.756, 0.006]].map(p => [X(p[0]), Y(p[1]), p[2] * pw]), 16);

    // 鉛筆の下描き:稜線、丘、道、地平の当たり。紙の山にだけ黒鉛が乗る
    { const b = mkbuf(px0 - 10 * S, py0 - 10 * S, px1 + 10 * S, py1 + 10 * S), r = makeRng(SEED + 50);
      const line = (pts, press, sd) => { const ph0 = r() * 1000; let prev = null;
        for (let i = 0; i < pts.length; i++) {
          const q = pts[i], x = q[0] + 1.5 * S * vnoise1(i / 8 + ph0, sd), y = q[1] + 1.5 * S * vnoise1(i / 8 + ph0 + 50, sd + 1), pr = press * (0.78 + 0.4 * vnoise1(i / 13 + ph0 + 9, sd + 2));
          if (prev && pr > 0.2) seg(b, prev[0], prev[1], 0.6 * S, x, y, 0.6 * S, clamp01(pr));
          prev = [x, y];
        } };
      const along = (arr, x0, x1, dy) => { const o = []; for (let x = X(x0); x <= X(x1); x += 6 * S) o.push([x, arr[Math.round(x)] + dy]); return o; };
      line(along(L1, 0, 1, -2 * S), 0.55, 51); line(along(L2, 0, 1, -2 * S), 0.6, 52); line(along(L3, 0, 1, -1.5 * S), 0.6, 53);
      line(along(L5, 0.46, 1, -2 * S), 0.75, 54); line(along(L4, 0, 0.46, -2 * S), 0.75, 55); line(along(L4, 0.02, 0.40, 3 * S), 0.35, 56);
      line(along(fgR, 0, 1, -3 * S), 0.8, 57);
      for (const sd of [-1, 1]) line(pathPts.map(q => [q[0] + sd * q[2], q[1]]), 0.7, 58 + sd);
      line(rvS.filter((q, i) => i % 3 === 0), 0.5, 60);
      { const o = []; for (let x = X(0.06); x < X(0.94); x += 7 * S) o.push([x, HZ + 34 * S]); line(o, 0.3, 61); }
      { const o = []; for (let i = 0; i <= 40; i++) { const a = i / 40 * TAU; o.push([X(SUN.u) + Math.cos(a) * 0.0125 * pw, Y(SUN.v) + Math.sin(a) * 0.0125 * pw]); } line(o, 0.4, 62); }
      for (let y = 0, i = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x; for (let x = 0; x < b.w; x++, i++, gi++) if (b.a[i] > 0) b.a[i] *= 0.3 + 0.7 * sstep(0.26, 0.62, paperH[gi]); }
      dep(b, P.graphite, 0.6); }
    yield step('鉛筆の下描き');
    maskMaple();
    paintSky(); yield step('空');

    // 遠い山から順に。遠いほど淡く青く
    mountain({ ridge: L1, clip: [L4, L5], foot: lineFn([[-0.05, 0.56], [1.05, 0.56]]), fade: 0.25, pigs: [[P.cobalt, 0.15], [P.rose, 0.045], [P.naples, 0.10, 'w'], [P.rose, 0.03, 'w']], fr: 0.5, frR: 1.8, v: 0.08, seed: 410, sunK: 0.62 });
    mountain({ ridge: L2, clip: [L4, L5], foot: lineFn([[-0.05, 0.58], [1.05, 0.58]]), fade: 0.28, pigs: [[P.cobalt, 0.25], [P.rose, 0.06], [P.neutral, 0.04], [P.naples, 0.10, 'w'], [P.rose, 0.04, 'w']], fr: 0.6, frR: 1.9, v: 0.10, seed: 430, sunK: 0.58, charge: 0.3 });
    yield step('遠い山');
    mountain({ ridge: L3, clip: [L4, L5], foot: lineFn([[-0.05, 0.60], [1.05, 0.60]]), fade: 0.3, pigs: [[P.cobalt, 0.30], [P.ultra, 0.13], [P.bSienna, 0.08], [P.viridian, 0.05], [P.rose, 0.03], [P.naples, 0.08, 'w']], fr: 0.7, v: 0.12, seed: 450, sunK: 0.5, charge: 0.35, streak: 0.05 });
    yield step('中景の山');

    // 谷を挟む左右の山。左は近く濃く、右は少し遠く暖かい
    const m5 = mountain({ ridge: L5, foot: lineFn([[-0.05, 0.60], [0.45, 0.585], [0.70, 0.60], [1.05, 0.655]]), fade: 0.36, fadeN: 0.2,
      pigs: [[P.ultra, 0.33], [P.bSienna, 0.19], [P.sap, 0.25], [P.gold, 0.10], [P.rose, 0.04], [P.naples, 0.10, 'w']], fr: 0.5, fr2: 0.25, v: 0.14, seed: 470, sunK: 0.45, sunW: 0.26, charge: 0.5, chargeL: 150, streak: 0.06, forest: 0.07, gran: 0.6 });
    spurGlaze(m5, L5, { n: 9, x0: 0.56, x1: 1.02, slope: [-0.95, -0.35], dir: -1, len: 55, depth: 300, seed: 475, pigs: [[P.ultra, 0.24], [P.bSienna, 0.10], [P.sap, 0.09]] });
    yield step('右の山');
    const m4 = mountain({ ridge: L4, foot: lineFn([[-0.05, 0.665], [0.2, 0.625], [0.40, 0.582], [0.5, 0.57], [1.05, 0.61]]), fade: 0.34, fadeN: 0.22,
      pigs: [[P.ultra, 0.41], [P.bSienna, 0.22], [P.sap, 0.29], [P.viridian, 0.07], [P.gold, 0.05], [P.naples, 0.12, 'w']], fr: 0.55, fr2: 0.3, v: 0.15, seed: 490, sunK: 0.42, sunW: 0.22, charge: 0.5, chargeL: 170, streak: 0.07, forest: 0.08, gran: 0.6 });
    spurGlaze(m4, L4, { n: 8, x0: -0.02, x1: 0.40, slope: [0.25, 0.85], dir: 1, len: 70, depth: 420, seed: 495, pigs: [[P.ultra, 0.30], [P.sepia, 0.12], [P.sap, 0.10]] });
    yield step('左の山');
    mist(); yield step('霧');

    // ── 谷底:大地。地平 HZ へ消える平面に、川と田畑を遠近法で置く ──
    const VB = mkbuf(px0 - 12 * S, Y(0.515), px1 + 12 * S, Y(0.905));
    const vfTop = lineFn([[-0.05, 0.615], [0.12, 0.588], [0.28, 0.556], [0.40, 0.536], [0.47, 0.530], [0.56, 0.534], [0.72, 0.552], [0.88, 0.576], [1.05, 0.605]]);
    const emerge = like(VB);
    { const nz = fbmArr(VB, 220 * S, 3, SEED + 600, 0.5, 0.3);
      for (let x = 0; x < VB.w; x++) { const ty = vfTop(VB.x + x); for (let y = 0; y < VB.h; y++) { const i = y * VB.w + x; emerge.a[i] = sstep(-0.004, 0.05, (VB.y + y + 0.5 - ty) / ph + 0.012 * nz[i]); } } }
    // 手前の土手(画面下の左右)と、逆光で光る草・薄の穂。谷を塗る前にマスキング液で伏せておく
    const nbR = ridgeArr([[-0.05, 0.895], [0.06, 0.882], [0.16, 0.902], [0.27, 0.952], [0.36, 1.03], [0.58, 1.04], [0.66, 0.985], [0.76, 0.93], [0.86, 0.897], [0.95, 0.878], [1.05, 0.862]], 0.005, 0.04, SEED + 806);
    const bladePts = (x, y, len, ang, curve, w0, w1) => {
      const n = Math.max(4, Math.round(len / (5 * S))), dx = Math.cos(ang), dy = Math.sin(ang), pts = [];
      for (let k = 0; k <= n; k++) { const t = k / n, q = curve * len * t * t; pts.push([x + dx * len * t - dy * q, y + dy * len * t + dx * q, Math.max(0.22 * S, mix(w0, w1 === undefined ? 0 : w1, Math.pow(t, 0.75)))]); }
      return pts;
    };
    const GM = mkbuf(px0 - 12 * S, Y(0.50), px1 + 12 * S, py1 + 12 * S);
    const PL = like(GM), STM = like(GM), LB = like(GM), DKL = like(GM), PLs = like(GM);
    function susuki(bx, by, sc, seed, nC) {
      const r = makeRng(seed), nCulm = nC || 3 + r.int(4), nLeaf = Math.round((14 + r.int(10)) * Math.min(1, sc + 0.3));
      for (let k = 0; k < nLeaf; k++) {
        const a0 = r.range(-1.0, 1.0), ang = -Math.PI / 2 + a0, len = r.range(100, 250) * S * sc, cv = (a0 > 0 ? 1 : -1) * r.range(0.2, 0.6) + 0.1;
        stroke(r() < 0.38 ? LB : DKL, bladePts(bx + r.range(-14, 14) * S * sc, by, len, ang, cv, r.range(1.8, 3.4) * S * sc), r.range(0.6, 1));
      }
      for (let k = 0; k < nCulm; k++) {
        const ang = -Math.PI / 2 + r.range(-0.30, 0.34) + 0.06, len = r.range(210, 370) * S * sc, cv = r.range(0.04, 0.2);
        const st = bladePts(bx + r.range(-12, 12) * S * sc, by, len, ang, cv, 1.5 * S * sc, 0.7 * S * sc);
        stroke(STM, st);
        // 穂:茎の先から片側へなびく細い枝を十数本
        const n = st.length - 1, tip = st[n], pv = st[n - 2], ta = Math.atan2(tip[1] - pv[1], tip[0] - pv[0]), nR = 11 + r.int(8), pl = r.range(64, 112) * S * sc;
        for (let j = 0; j < nR; j++) {
          const o = st[Math.max(0, n - r.int(Math.max(2, Math.round(n * 0.16))))], a2 = ta + r.range(-0.22, 0.55), l2 = pl * r.range(0.5, 1), c2 = r.range(0.25, 0.75);
          const rc = bladePts(o[0], o[1], l2, a2, c2, r.range(0.75, 1.15) * S * Math.sqrt(sc), 0.3 * S);
          stroke(PL, rc, r.range(0.8, 1));
          for (let q = 2; q < rc.length; q++) {            // 穂の毛羽
            const hp = rc[q], hq = rc[q - 1], dx = hp[0] - hq[0], dy = hp[1] - hq[1], dl = Math.hypot(dx, dy) || 1;
            for (let e = 0; e < 2; e++) { const t = r(), sx = mix(hq[0], hp[0], t), sy = mix(hq[1], hp[1], t), sd = r.sign(), hl = r.range(2, 5.5) * S * Math.sqrt(sc);
              seg(PL, sx, sy, 0.45 * S, sx + (-dy / dl * sd * 0.8 + dx / dl * 0.6) * hl, sy + (dx / dl * sd * 0.8 + dy / dl * 0.6) * hl, 0.2 * S, 0.75); }
          }
          stroke(PLs, rc.map(q => [q[0] + 0.9 * S, q[1] + 1.1 * S, q[2] * 0.7]), 0.8);     // 穂の陰(右下に少しずらす)
        }
      }
    }
    susuki(X(0.075), Y(0.945), 1.0, 9001); susuki(X(0.165), Y(0.972), 0.92, 9002); susuki(X(0.012), Y(0.99), 1.05, 9003, 3);
    susuki(X(0.83), Y(0.955), 1.0, 9004); susuki(X(0.915), Y(0.93), 1.06, 9005); susuki(X(0.985), Y(0.965), 0.95, 9006, 4); susuki(X(0.745), Y(0.99), 0.8, 9007, 3);
    // 丘の稜線に立つ遠い薄(小さく)
    for (const q of [[0.80, 0.46], [0.835, 0.4], [0.90, 0.42], [0.565, 0.34], [0.30, 0.36], [0.255, 0.33]]) susuki(X(q[0]), fgR[Math.round(X(q[0]))] + 10 * S, q[1], 9100 + Math.round(q[0] * 100), 2 + (q[1] > 0.4 ? 1 : 0));
    // 稜線の草の葉先。明るい葉(伏せる)と暗い葉(あとで描く)
    const DKB = like(GM);
    { const r = makeRng(SEED + 820);
      for (let x = px0 - 6 * S; x < px1 + 6 * S; x += r.range(2.2, 6.5) * S) {
        const gx = clamp(Math.round(x), 0, W - 1), tall = r() < 0.07, len = (tall ? r.range(26, 52) : r.range(6, 22)) * S, ang = -Math.PI / 2 + r.range(-0.5, 0.5) + 0.1;
        stroke(r() < 0.42 ? LB : DKB, bladePts(x, fgR[gx] + r.range(2, 9) * S, len, ang, r.range(-0.4, 0.5), (tall ? 1.3 : r.range(0.7, 1.2)) * S), r.range(0.6, 1));
      } }
    for (let i = 0; i < GM.a.length; i++) GM.a[i] = Math.max(PL.a[i], STM.a[i], LB.a[i]);
    mask('fgres', GM);

    // 川:画面上の S 字を地面に写し、岸を地面上で等幅に取ってから投影し直す
    const rvG = rvS.map(p => unproj(p[0], p[1]));
    const RV = like(VB), bankG = [];
    { const L = [], R = [];
      for (let i = 0; i < rvG.length; i++) {
        const a = rvG[Math.max(0, i - 1)], c = rvG[Math.min(rvG.length - 1, i + 1)], g = rvG[i];
        let tx = c[0] - a[0], tz = c[1] - a[1]; const tl = Math.hypot(tx, tz) || 1; tx /= tl; tz /= tl;
        const hw = 0.17 * (0.85 + 0.25 * fbm1(i / 40, 2, SEED + 620));
        L.push(proj(g[0] + tz * hw, g[1] - tx * hw)); R.push(proj(g[0] - tz * hw, g[1] + tx * hw));
        bankG.push([g[0], g[1], tz, -tx, hw]);
      }
      for (let i = 0; i + 1 < L.length; i++) { const j = Math.min(L.length - 1, i + 2); fillPoly(RV, [L[i][0], L[i][1], L[j][0], L[j][1], R[j][0], R[j][1], R[i][0], R[i][1]]); }
      stroke(RV, rvS.map(p => [p[0], p[1], 0.9 * S]));
      warp(RV, 1.6 * S, 20 * S, SEED + 621); }
    const RVd = like(VB, RV.a.slice()); bblur(RVd, 2.0 * S); for (let i = 0; i < RVd.a.length; i++) RVd.a[i] = Math.max(RV.a[i], sstep(0.2, 0.4, RVd.a[i]));
    mask('river', RVd);
    const FG = mkbuf(px0 - 12 * S, Y(0.64), px1 + 12 * S, py1 + 12 * S);
    for (let y = 0, i = 0; y < FG.h; y++) for (let x = 0; x < FG.w; x++, i++) FG.a[i] = clamp01(FG.y + y + 1 - fgR[FG.x + x]);
    mask('fg', FG);

    // 下塗り:濡れた紙に、稲の金・草の緑を横長のむらで流す(地面の座標でむらを作るので遠くほど細くなる)
    const th = -9 * Math.PI / 180, cs = Math.cos(th), sn = Math.sin(th), PZ = 8;
    const g2 = (a, bb) => [a * cs - (bb - PZ) * sn, a * sn + (bb - PZ) * cs + PZ];
    const toAB = (gx, z) => [gx * cs + (z - PZ) * sn, -gx * sn + (z - PZ) * cs + PZ];
    { const q = 2, cw = Math.ceil(VB.w / q) + 2, chh = Math.ceil(VB.h / q) + 2, z1 = new Float32Array(cw * chh), z2 = new Float32Array(cw * chh);
      for (let j = 0; j < chh; j++) for (let i = 0; i < cw; i++) {
        const g = unproj(VB.x + i * q, Math.max(HZ + 6 * S, VB.y + j * q)), ab = toAB(g[0], g[1]);
        z1[j * cw + i] = fbm2(ab[0] * 0.55 + 11, ab[1] * 1.7 + 5, 3, SEED + 632); z2[j * cw + i] = fbm2(ab[0] * 0.4 + 71, ab[1] * 1.3 + 31, 3, SEED + 633);
      }
      const up = src => { const o = like(VB); for (let y = 0, i = 0; y < VB.h; y++) { const fy = y / q, jy = fy | 0, ty = fy - jy; for (let x = 0; x < VB.w; x++, i++) { const fx = x / q, ix = fx | 0, tx = fx - ix, k = jy * cw + ix; o.a[i] = (src[k] * (1 - tx) + src[k + 1] * tx) * (1 - ty) + (src[k + cw] * (1 - tx) + src[k + cw + 1] * tx) * ty; } } return o; };
      const n1 = up(z1), n2 = up(z2), nz = fbmArr(VB, 160 * S, 3, SEED + 630);
      const base = like(VB), gold = like(VB), green = like(VB);
      for (let y = 0, i = 0; y < VB.h; y++) { const v = (VB.y + y - py0) / ph, df = mix(0.45, 1.0, sstep(0.54, 0.74, v));
        for (let x = 0; x < VB.w; x++, i++) {
          const e = emerge.a[i] * (0.85 + 0.15 * nz[i]) * df, pale = 1 - 0.55 * sstep(0.12, 0.45, n2.a[i]);
          base.a[i] = e; gold.a[i] = e * sstep(-0.18, 0.22, n1.a[i]) * pale; green.a[i] = e * sstep(0.02, 0.36, -n1.a[i]) * pale;
        } }
      dep(base, P.naples, 0.30); dep(base, P.ochre, 0.07);
      dep(gold, P.aureolin, 0.30); dep(gold, P.rawSienna, 0.22);
      dep(green, P.sap, 0.34); dep(green, P.aureolin, 0.12); dep(green, P.cobalt, 0.04); }
    yield step('谷の下塗り');

    // 田畑:地面の長方形を再帰的に割り、画面上で程よい大きさになったものを一枚の畑とする
    const fr = makeRng(SEED + 640), fields = [];
    (function sub(a0, b0, a1, b1, d) {
      const cor = [g2(a0, b0), g2(a1, b0), g2(a1, b1), g2(a0, b1)];
      let zmin = Infinity, zmax = -Infinity; for (const c of cor) { if (c[1] < zmin) zmin = c[1]; if (c[1] > zmax) zmax = c[1]; }
      if (zmax < 1.9) return;
      if (zmin < 1.9) { if (d > 11) return; const am = (a0 + a1) / 2, bm = (b0 + b1) / 2; sub(a0, b0, am, bm, d + 1); sub(am, b0, a1, bm, d + 1); sub(a0, bm, am, b1, d + 1); sub(am, bm, a1, b1, d + 1); return; }
      const sp = cor.map(c => proj(c[0], c[1]));
      let sx0 = Infinity, sx1 = -Infinity, sy0 = Infinity, sy1 = -Infinity;
      for (const q of sp) { if (q[0] < sx0) sx0 = q[0]; if (q[0] > sx1) sx1 = q[0]; if (q[1] < sy0) sy0 = q[1]; if (q[1] > sy1) sy1 = q[1]; }
      if (sx1 < px0 - 30 * S || sx0 > px1 + 30 * S || sy0 > Y(0.905) || sy1 < Y(0.535)) return;
      const zc = (zmin + zmax) / 2, sh = sy1 - sy0, sw = sx1 - sx0;
      const tH = clamp(235 * S / zc, 7 * S, 80 * S) * fr.range(0.7, 1.35), tW = Math.max(90 * S, tH * fr.range(4.5, 9));
      const road = d <= 2 ? 0.05 : 0;
      if (sh > tH && d < 15) { const bm = mix(b0, b1, fr.range(0.36, 0.6)); sub(a0, b0, a1, bm - road, d + 1); sub(a0, bm + road, a1, b1, d + 1); return; }
      if (sw > tW && d < 15) { const am = mix(a0, a1, fr.range(0.36, 0.64)); sub(a0, b0, am - road, b1, d + 1); sub(am + road, b0, a1, b1, d + 1); return; }
      fields.push({ sp, z: zc, a: (a0 + a1) / 2, b: (b0 + b1) / 2, sw, sh, cx: (sx0 + sx1) / 2, cy: (sy0 + sy1) / 2, id: fields.length });
    })(-22, 1.0, 26, 60, 0);
    const REC = {
      rice: [[P.aureolin, 0.34], [P.rawSienna, 0.36], [P.gold, 0.08]],
      riceG: [[P.aureolin, 0.36], [P.sap, 0.22], [P.rawSienna, 0.14]],
      stubble: [[P.ochre, 0.28], [P.bSienna, 0.06], [P.naples, 0.2]],
      earth: [[P.bSienna, 0.34], [P.umber, 0.20], [P.ochre, 0.12]],
      green: [[P.sap, 0.42], [P.rawSienna, 0.12], [P.cobalt, 0.05]],
      dgreen: [[P.sap, 0.44], [P.ultra, 0.14], [P.bSienna, 0.07]],
    };
    const inValley = (x, y) => { const vx = Math.round(x) - VB.x, vy = Math.round(y) - VB.y; return vx >= 0 && vx < VB.w && vy >= 0 && vy < VB.h ? emerge.a[vy * VB.w + vx] : 0; };
    const lines = like(VB);
    for (const f of fields) {
      const tn = fbm2(f.a * 0.21 + 3, f.b * 0.15 + 7, 2, SEED + 641), q = fr() + tn * 0.5;
      f.type = q < 0.10 ? 'dgreen' : q < 0.24 ? 'green' : q < 0.37 ? 'earth' : q < 0.52 ? 'stubble' : q < 0.62 ? 'riceG' : 'rice';
      if (f.cy - f.sh / 2 > fgR[clamp(Math.round(f.cx), 0, W - 1)] + 4 * S) continue;      // 前景の丘に隠れる
      const v = (f.cy - py0) / ph, near = sstep(0.56, 0.72, v);
      // 遠くは 3 割、近くは 6 割ほどの畑にだけ色を重ねる。残りは下塗りのまま
      if (fr() < 0.30 + 0.32 * near) {
        const gap = fr.range(-0.6, 2.6) * S + 3 * S / f.z, kx = Math.max(0.5, 1 - 2 * gap / f.sw), ky = Math.max(0.45, 1 - 2 * gap / f.sh);
        const P4 = []; for (const q4 of f.sp) P4.push(f.cx + (q4[0] - f.cx) * kx, f.cy + (q4[1] - f.cy) * ky);
        const t = mkbuf(f.cx - f.sw / 2 - 8 * S, f.cy - f.sh / 2 - 8 * S, f.cx + f.sw / 2 + 8 * S, f.cy + f.sh / 2 + 8 * S);
        if (t.w && t.h) {
          fillPoly(t, P4);
          if (f.sh > 12 * S) { organic(t, 2.0 * S, 1.8 * S, 14 * S, SEED + 650 + f.id); fringe(t, 0.5, 1.8 * S); vary(t, 0.16, 60 * S, SEED + 651 + f.id); }
          else bblur(t, 0.8 * S);
          for (let y = 0; y < t.h; y++) for (let x = 0; x < t.w; x++) t.a[y * t.w + x] *= inValley(t.x + x, t.y + y);
          const df = mix(0.35, 1.0, near), st = fr.range(0.55, 1.05) * df;
          for (const it of REC[f.type]) dep(t, it[0], it[1] * st);
          if (df < 0.9) dep(t, P.cobalt, 0.07 * (1 - df));
          f.painted = true;
        }
      }
      // 畦や農道:面相筆で引く細い線。全部は引かず、所々かすれさせる
      const lw = clamp(1.5 * S / Math.sqrt(f.z / 3), 0.45 * S, 1.3 * S);
      for (let e = 0; e < 4; e++) {
        if (fr() > (e % 2 === 0 ? 0.42 : 0.2)) continue;
        const A = f.sp[e], B = f.sp[(e + 1) % 4], nseg = Math.max(2, Math.round(Math.hypot(B[0] - A[0], B[1] - A[1]) / (9 * S)));
        const t0 = fr.range(0, 0.25), t1 = fr.range(0.7, 1), ph0 = fr() * 100;
        for (let k = 0; k < nseg; k++) {
          const ta = mix(t0, t1, k / nseg), tb = mix(t0, t1, (k + 1) / nseg);
          const pr = 0.55 + 0.45 * vnoise1(k / 3.1 + ph0, SEED + 660 + f.id); if (pr < 0.42) continue;
          seg(lines, mix(A[0], B[0], ta), mix(A[1], B[1], ta) + 0.6 * S * vnoise1(k / 2 + ph0, 5), lw * pr, mix(A[0], B[0], tb), mix(A[1], B[1], tb) + 0.6 * S * vnoise1((k + 1) / 2 + ph0, 5), lw * pr, 0.85);
        }
      }
    }
    for (let i = 0; i < lines.a.length; i++) lines.a[i] *= emerge.a[i] * emerge.a[i];
    { const dfl = like(VB); for (let y = 0, i = 0; y < VB.h; y++) { const df = mix(0.3, 1, sstep(0.55, 0.72, (VB.y + y - py0) / ph)); for (let x = 0; x < VB.w; x++, i++) dfl.a[i] = lines.a[i] * df; }
      dep(dfl, P.sap, 0.45); dep(dfl, P.bSienna, 0.35); dep(dfl, P.ultra, 0.30); }
    yield step('田畑');
    unmask('river');

    // 川:岸は砂の色を少し、水は空を映す。太陽の真下は紙の白を残す
    { const bank = like(VB), nz = fbmArr(VB, 30 * S, 3, SEED + 670);
      for (let i = 0; i < bank.a.length; i++) bank.a[i] = Math.max(0, RVd.a[i] - RV.a[i]) * emerge.a[i] * clamp01(0.55 + 0.6 * nz[i]);
      dep(bank, P.naples, 0.40); dep(bank, P.ochre, 0.20); dep(bank, P.neutral, 0.10); dep(bank, P.sap, 0.08);
      const wa = like(VB), st = fbmArr(VB, 90 * S, 2, SEED + 671, 0.5, 0.16), refl = like(VB), lineB = like(VB);
      for (let y = 0, i = 0; y < VB.h; y++) { const v = (VB.y + y - py0) / ph, near = sstep(0.555, 0.80, v), dR = Math.round(mix(3, 15, near) * S), d2 = Math.max(1, Math.round(1.6 * S));
        for (let x = 0; x < VB.w; x++, i++) {
          const m = RV.a[i]; if (m <= 0) continue;
          const u = (VB.x + x - px0) / pw, glit = Math.exp(-Math.pow((u - 0.405) / 0.085, 2)) * (1 - 0.45 * near);
          wa.a[i] = m * emerge.a[i] * mix(0.25, 1.0, near) * (1 - 0.9 * glit) * Math.max(0.2, 1 + 0.22 * st[i]);
          refl.a[i] = m * (1 - (y >= dR ? RV.a[i - dR * VB.w] : 0)) * emerge.a[i] * (0.4 + 0.6 * near) * clamp01(0.7 + 0.5 * st[i]);
          lineB.a[i] = m * (1 - (y + d2 < VB.h ? RV.a[i + d2 * VB.w] : 0)) * emerge.a[i] * (0.3 + 0.7 * near);
        } }
      dep(wa, P.cobalt, 0.24); dep(wa, P.cerulean, 0.16); dep(wa, P.rose, 0.03);
      bblur(refl, 1.4 * S); dep(refl, P.sap, 0.34); dep(refl, P.ultra, 0.26); dep(refl, P.bSienna, 0.12);
      dep(lineB, P.neutral, 0.5); dep(lineB, P.umber, 0.25);
      // きらめき:乾いたあとで細く拭き取る
      const sp = like(VB), r = makeRng(SEED + 672);
      for (let k = 0; k < 46; k++) { const g = rvS[r.int(rvS.length)], v = (g[1] - py0) / ph; if (v < 0.57) continue;
        const x = g[0] + r.range(-70, 70) * S * (v - 0.5) * 3, y = g[1] + r.range(-14, 14) * S * (v - 0.5) * 3, l = r.range(4, 22) * S * (0.4 + (v - 0.55) * 3);
        seg(sp, x - l, y, 0.3 * S, x + l, y + r.range(-0.6, 0.6) * S, r.range(0.5, 1.0) * S, r.range(0.5, 1)); }
      for (let i = 0; i < sp.a.length; i++) sp.a[i] *= RV.a[i];
      lift(sp, 0.9); }
    yield step('川');

    // 木立と集落。奥から手前へ。朝日を背にするので、てっぺんだけ明るく、影は手前へ長く伸びる
    const SHX = (X(SUN.u) - XC) / F, TANE = (HZ - Y(SUN.v)) / F;
    function tree(gx, z, hgt, o) {
      o = o || {}; const r = makeRng(o.seed || 1), q = proj(gx, z), bx = q[0], by = q[1], hs = F * hgt / z, ws = hs * (o.wid || 0.9);
      if (bx < px0 - 60 * S || bx > px1 + 60 * S) return;
      if (by > fgR[clamp(Math.round(bx), 0, W - 1)] + hs * 0.2) return;
      const vis = inValley(bx, by); if (vis < 0.05) return;
      const v = (by - py0) / ph, af = sstep(0.545, 0.70, v), st = mix(0.42, 1, af) * vis * (o.str || 1);
      if (o.shadow !== false && hs > 3 * S) {
        const L = Math.min(hgt / TANE, z * 0.42), zt = z - L, tp = proj(gx - SHX * L, zt), k = z / zt, hw = ws * 0.42;
        const t = mkbuf(Math.min(bx, tp[0]) - hw * k - 8 * S, by - 4 * S, Math.max(bx, tp[0]) + hw * k + 8 * S, tp[1] + 8 * S);
        if (t.w && t.h) { fillPoly(t, [bx - hw * 0.8, by, bx + hw * 0.8, by, tp[0] + hw * k * 0.7, tp[1], tp[0] - hw * k * 0.7, tp[1]]); bblur(t, Math.max(1.2 * S, hs * 0.10));
          for (let y = 0; y < t.h; y++) { const f = 1 - 0.65 * clamp01((t.y + y - by) / Math.max(1, tp[1] - by)); for (let x = 0; x < t.w; x++) t.a[y * t.w + x] *= f * inValley(t.x + x, t.y + y) * (1 - RV.a[clamp((t.y + y - VB.y), 0, VB.h - 1) * VB.w + clamp(t.x + x - VB.x, 0, VB.w - 1)] * 0.7); }
          dep(t, P.cobalt, 0.24 * st); dep(t, P.ultra, 0.11 * st); dep(t, P.rose, 0.06 * st); }
      }
      const t = mkbuf(bx - ws * 0.9 - 6 * S, by - hs * 1.15 - 6 * S, bx + ws * 0.9 + 6 * S, by + 4 * S);
      if (!t.w || !t.h) return;
      const autumn = o.autumn || 0, mid = like(t), dk = like(t), cy = by - hs * 0.60;
      if (o.kind === 'cedar') {
        // 杉:細い穂先を一筆、脇に小さな点
        seg(t, bx, by, ws * 0.13, bx + r.range(-0.03, 0.03) * ws, by - hs * 1.05, 0.3 * S);
        seg(mid, bx + ws * 0.02, by, ws * 0.10, bx + ws * 0.02, by - hs * 0.8, 0.2 * S);
        const nd = clamp(Math.round(hs / (3.2 * S)), 2, 12);
        for (let k = 0; k < nd; k++) { const f = (k + 0.5) / nd, sd = r.sign(), yy = by - hs * f * 0.9, wv = ws * 0.2 * (1 - f * 0.75) * r.range(0.6, 1.2);
          seg(t, bx, yy - hs * 0.03, 0.5 * S, bx + sd * wv, yy + hs * 0.02, 0.35 * S); if (sd > 0) seg(mid, bx, yy - hs * 0.03, 0.5 * S, bx + sd * wv, yy + hs * 0.02, 0.35 * S); }
      } else {
        const nd = clamp(Math.round(hs * 0.75 / S), 4, 44), lob = r() * 9;
        for (let k = 0; k < nd; k++) {
          const ang = r() * TAU, rad = Math.pow(r(), 0.6) * (1 + 0.3 * vnoise1(ang * 1.3 + lob, 3));
          const dx = Math.cos(ang) * rad * ws * 0.46, dy = Math.sin(ang) * rad * hs * 0.40, rr = Math.max(0.7 * S, ws * r.range(0.06, 0.15));
          const a2 = r() * Math.PI, ex = Math.cos(a2) * rr * 0.9, ey = Math.sin(a2) * rr * 0.5, px = bx + dx, py = cy + dy;
          seg(t, px - ex, py - ey, rr * 0.8, px + ex, py + ey, rr);
          const shd = dx / ws * 1.0 + dy / hs * 1.7 + r.range(-0.22, 0.22);
          if (shd > -0.12) seg(mid, px - ex, py - ey, rr * 0.75, px + ex, py + ey, rr * 0.95);
          if (shd > 0.22) seg(dk, px - ex, py - ey, rr * 0.7, px + ex, py + ey, rr * 0.85);
        }
        if (hs > 9 * S) { const lean = r.range(-0.08, 0.08) * ws; seg(dk, bx, by, Math.max(0.5 * S, hs * 0.028), bx + lean, cy + hs * 0.1, Math.max(0.4 * S, hs * 0.016)); }
      }
      if (hs > 14 * S) fringe(t, 0.35, 1.3 * S);
      const lc = o.kind === 'cedar' ? [[P.sap, 0.40], [P.viridian, 0.16], [P.ultra, 0.14]] : autumn > 0.66 ? [[P.gold, 0.46], [P.bSienna, 0.22], [P.red, 0.08]] : autumn > 0.33 ? [[P.aureolin, 0.42], [P.rawSienna, 0.28], [P.sap, 0.12]] : [[P.sap, 0.44], [P.aureolin, 0.26], [P.rawSienna, 0.08]];
      for (const it of lc) dep(t, it[0], it[1] * st);
      const sc = o.kind === 'cedar' ? [[P.ultra, 0.40], [P.viridian, 0.14], [P.sepia, 0.2]] : [[P.sap, 0.30], [P.ultra, 0.32], [P.bSienna, 0.14 + 0.14 * autumn]];
      for (const it of sc) dep(mid, it[0], it[1] * st);
      dep(dk, P.ultra, 0.30 * st); dep(dk, P.sepia, 0.24 * st);
    }
    function house(gx, z, wid, o) {
      const r = makeRng(o.seed), q = proj(gx, z), bx = q[0], by = q[1], ws = F * wid / z, hw = ws * 0.5, wallH = ws * r.range(0.26, 0.36), roofH = ws * r.range(0.30, 0.42);
      const vis = inValley(bx, by); if (vis < 0.2 || by > fgR[clamp(Math.round(bx), 0, W - 1)]) return;
      const st = mix(0.5, 1, sstep(0.545, 0.70, (by - py0) / ph)) * vis;
      const t = mkbuf(bx - ws - 6 * S, by - wallH - roofH - 6 * S, bx + ws + 6 * S, by + ws * 0.7 + 6 * S);
      // 影
      { const L = Math.min((wid * 0.6) / TANE, z * 0.3), zt = z - L, tp = proj(gx - SHX * L, zt), k = z / zt, s = like(t);
        fillPoly(s, [bx - hw, by, bx + hw, by, tp[0] + hw * k, tp[1], tp[0] - hw * k, tp[1]]); bblur(s, Math.max(0.8 * S, ws * 0.04));
        dep(s, P.cobalt, 0.32 * st); dep(s, P.ultra, 0.16 * st); dep(s, P.rose, 0.07 * st); }
      // 壁は拭き取って白く残し、屋根だけ色を置く
      const wall = like(t); fillPoly(wall, [bx - hw * 0.9, by - wallH, bx + hw * 0.9, by - wallH, bx + hw * 0.9, by, bx - hw * 0.9, by]); lift(wall, 0.85 * vis);
      const ws2 = like(t); fillPoly(ws2, [bx - hw * 0.9, by - wallH, bx + hw * 0.9, by - wallH, bx + hw * 0.9, by, bx - hw * 0.9, by]);
      dep(ws2, P.cobalt, 0.10 * st); dep(ws2, P.rose, 0.03 * st); dep(ws2, P.naples, 0.12 * st);
      const roof = like(t), ov = hw * 1.18, rg = hw * r.range(0.35, 0.7);
      fillPoly(roof, [bx - ov, by - wallH + 0.5 * S, bx - rg, by - wallH - roofH, bx + rg, by - wallH - roofH, bx + ov, by - wallH + 0.5 * S]);
      if (ws > 14 * S) fringe(roof, 0.4, 1.2 * S);
      const warmRoof = r() < 0.3;
      if (warmRoof) { dep(roof, P.bSienna, 0.55 * st); dep(roof, P.umber, 0.25 * st); dep(roof, P.ultra, 0.12 * st); }
      else { dep(roof, P.neutral, 0.55 * st); dep(roof, P.ultra, 0.22 * st); dep(roof, P.bSienna, 0.10 * st); }
      // 朝日を受ける棟の線だけ拭き取る
      const rim = like(t); seg(rim, bx - rg, by - wallH - roofH + 0.3 * S, 0.45 * S, bx + rg, by - wallH - roofH + 0.3 * S, 0.45 * S); lift(rim, 0.6);
      // 戸口の暗がり
      const dr = like(t); fillPoly(dr, [bx - hw * 0.25, by - wallH * 0.75, bx + hw * 0.1, by - wallH * 0.75, bx + hw * 0.1, by, bx - hw * 0.25, by]); dep(dr, P.neutral, 0.5 * st); dep(dr, P.sepia, 0.3 * st);
    }
    { const items = [], r = makeRng(SEED + 680);
      // 川沿いの木立
      for (let i = 4; i < bankG.length - 2; i += 2) {
        const g = bankG[i]; if (g[1] > 44) continue;
        for (const sd of [1, -1]) {
          const dn = fbm1(i / 14 + sd * 9, 2, SEED + 681); if (dn < 0.02 || r() < 0.3) continue;
          const off = g[4] + r.range(0.04, 0.16), big = r() < 0.18;
          items.push({ k: 't', x: g[0] + sd * g[2] * off, z: g[1] + sd * g[3] * off, h: (big ? r.range(0.12, 0.17) : r.range(0.05, 0.10)), o: { seed: 1000 + i * 2 + sd, wid: r.range(0.8, 1.15), autumn: r() < 0.3 ? r() : 0 } });
        }
      }
      // 畑の境の並木、点在する木
      for (const f of fields) {
        if (r() > 0.11 || f.z > 30) continue;
        const A = unproj(f.sp[3][0], f.sp[3][1]), B = unproj(f.sp[2][0], f.sp[2][1]), n = 2 + r.int(5), t0 = r.range(0, 0.5), kind = r() < 0.3 ? 'cedar' : 'round';
        for (let k = 0; k < n; k++) { const t = t0 + k * r.range(0.07, 0.12); if (t > 1) break; items.push({ k: 't', x: mix(A[0], B[0], t), z: mix(A[1], B[1], t), h: kind === 'cedar' ? r.range(0.12, 0.17) : r.range(0.06, 0.11), o: { seed: 3000 + f.id * 8 + k, kind, wid: kind === 'cedar' ? 1 : r.range(0.85, 1.1), autumn: r() < 0.25 ? r() : 0 } }); }
      }
      for (let k = 0; k < 26; k++) { const z = 3 + 34 * Math.pow(r(), 1.6), x = r.range(-1.2, 1.2) * z * 0.85; items.push({ k: 't', x, z, h: r.range(0.06, 0.13), o: { seed: 5000 + k, autumn: r() < 0.3 ? r() : 0 } }); }
      // 集落:右手の山裾と、左奥に小さくもう一つ
      const hamlet = (cx, cz, n, sp, seed) => { const hr = makeRng(seed);
        for (let k = 0; k < n; k++) { const x = cx + hr.gauss() * sp, z = cz + hr.gauss() * sp * 0.9; items.push({ k: 'h', x, z, w: hr.range(0.14, 0.24), o: { seed: seed + k } }); }
        for (let k = 0; k < n * 1.3; k++) { const x = cx + hr.gauss() * sp * 1.3, z = cz + hr.gauss() * sp * 1.1 + 0.05, cd = hr() < 0.45; items.push({ k: 't', x, z, h: cd ? hr.range(0.16, 0.24) : hr.range(0.08, 0.15), o: { seed: seed + 100 + k, kind: cd ? 'cedar' : 'round', autumn: hr() < 0.3 ? hr() : 0 } }); } };
      hamlet(4.6, 8.6, 9, 0.75, 7000); hamlet(-4.4, 12.5, 6, 0.8, 7200); hamlet(2.2, 19, 5, 0.9, 7400);
      items.sort((a, b) => b.z - a.z);
      for (const it of items) { const q = proj(it.x, it.z); const vx = Math.round(q[0]) - VB.x, vy = Math.round(q[1]) - VB.y;
        if (vx >= 0 && vx < VB.w && vy >= 0 && vy < VB.h && RV.a[vy * VB.w + vx] > 0.3) continue;      // 川の中には立てない
        if (it.k === 't') tree(it.x, it.z, it.h, it.o); else house(it.x, it.z, it.w, it.o); } }
    yield step('木立と集落');
    // 谷の奥にかかる薄い霧(もう一度、軽く拭う)
    { const b = mkbuf(px0 - 12 * S, Y(0.46), px1 + 12 * S, Y(0.68)), n1 = fbmArr(b, 520 * S, 4, SEED + 690, 0.55, 0.11);
      for (let y = 0, i = 0; y < b.h; y++) { const v = (b.y + y - py0) / ph; for (let x = 0; x < b.w; x++, i++) { const u = (b.x + x - px0) / pw;
        b.a[i] = clamp01(Math.exp(-Math.pow((v - 0.553) / 0.026, 2)) * (0.45 + 0.35 * Math.exp(-Math.pow((u - 0.43) / 0.2, 2))) + 0.5 * sstep(0.35, 1.2, n1[i]) * sstep(0.54, 0.575, v) * (1 - sstep(0.60, 0.67, v))); } }
      warp(b, 14 * S, 120 * S, SEED + 691); bblur(b, 4 * S); lift(b, 0.75); }
    yield step('谷の霧');
    unmask('fg');

    // ── 前景:土の丘。こちら向きの斜面は陰、稜線の際だけ朝日が差す。道は赤土 ──
    const FB = FG, PM = like(FB);
    for (let i = 0; i + 1 < pathPts.length; i++) {
      const a = pathPts[i], c = pathPts[Math.min(pathPts.length - 1, i + 2)]; let tx = c[0] - a[0], ty = c[1] - a[1]; const tl = Math.hypot(tx, ty) || 1; tx /= tl; ty /= tl;
      fillPoly(PM, [a[0] + ty * a[2], a[1] - tx * a[2], a[0] - ty * a[2], a[1] + tx * a[2], c[0] - ty * c[2], c[1] + tx * c[2], c[0] + ty * c[2], c[1] - tx * c[2]]);
    }
    organic(PM, 4 * S, 7 * S, 34 * S, SEED + 801);
    const spikeArr = (seed, dens, hmax) => { const a = new Float32Array(W), r = makeRng(seed);
      for (let x = 0; x < W;) { const hgt = Math.pow(r(), 2.4) * hmax * S, w = r.range(1.4, 4.2) * S, cx = x + r.range(0, 3) * S;
        for (let q = Math.floor(cx - w); q <= cx + w; q++) if (q >= 0 && q < W) a[q] = Math.max(a[q], hgt * (1 - Math.abs(q - cx) / w));
        x += r.range(1.5, 8) * S / dens; }
      return a; };
    const fgD = like(FB), rimW = new Float32Array(W), spk = spikeArr(SEED + 815, 1, 26);
    for (let x = 0; x < W; x++) rimW[x] = (21 + 15 * fbm1(x / (170 * S), 3, SEED + 807) + 3 * fbm1(x / (30 * S), 2, SEED + 808)) * S;
    for (let y = 0, i = 0; y < FB.h; y++) for (let x = 0; x < FB.w; x++, i++) fgD.a[i] = FB.y + y + 0.5 - fgR[FB.x + x];
    { const base = like(FB), rim = like(FB), pathA = like(FB), shade = like(FB), pshade = like(FB);
      const nzA = fbmArr(FB, 260 * S, 3, SEED + 802), nzB = fbmArr(FB, 60 * S, 3, SEED + 803), nzC = fbmArr(FB, 26 * S, 3, SEED + 804, 0.55, 1.6);
      for (let y = 0, i = 0; y < FB.h; y++) for (let x = 0; x < FB.w; x++, i++) {
        const m = FB.a[i]; if (m <= 0) continue;
        const d = fgD.a[i], pm = PM.a[i], gx = FB.x + x, u = (gx - px0) / pw, dn = clamp01(d / (py1 - fgR[gx])), rw = rimW[gx];
        base.a[i] = m * (1 - 0.8 * pm) * (0.85 + 0.2 * nzA[i]);
        rim.a[i] = m * Math.exp(-d / (rw * 1.3)) * Math.max(0, 0.8 + 0.4 * nzB[i]);
        pathA.a[i] = m * pm * (0.9 + 0.15 * nzB[i]);
        const lf = 0.25 + 0.75 * sstep(-0.5, 0.5, nzC[i]);                 // 光の帯と陰の境:草の葉先でくっきり切れ、所々は水でぼける
        const sh = mix(sstep(rw * 0.4, rw * 1.9, d), clamp01(d - (rw - spk[gx]) + 0.5), lf);
        const vign = sstep(0.1, 1.0, dn) * (0.55 + 0.45 * Math.abs(u - 0.5) * 2);
        shade.a[i] = m * (1 - pm) * sh * (0.62 + 0.55 * vign) * Math.max(0.3, 1 + 0.2 * nzA[i] + 0.05 * nzB[i]);
        pshade.a[i] = m * pm * sh * (0.55 + 0.4 * dn) * Math.max(0.3, 1 + 0.2 * nzB[i]);
      }
      dep(base, P.rawSienna, 0.36); dep(base, P.gold, 0.14); dep(base, P.sap, 0.20);
      dep(rim, P.aureolin, 0.34); dep(rim, P.gold, 0.10);
      dep(pathA, P.rawSienna, 0.22); dep(pathA, P.bSienna, 0.13); dep(pathA, P.ochre, 0.10);
      yield step('前景の下塗り');
      granK = 0.6;
      fringe(shade, 0.22, 2 * S, 1.5, 0);
      charge(shade, [[P.ultra, 0.56], [P.bSienna, 0.37], [P.sap, 0.44], [P.rose, 0.06]], 0.4, 190 * S, SEED + 805);
      // もう一段濃い陰を、乾いてから形として重ねる(縁の立ったグレーズ)
      { const g2b = like(FB), nzD = fbmArr(FB, 380 * S, 3, SEED + 816, 0.5, 0.2);
        for (let i = 0; i < g2b.a.length; i++) g2b.a[i] = FB.a[i] * (1 - PM.a[i]) * (nzD[i] > 0.55 && fgD.a[i] > rimW[FB.x + (i % FB.w)] * 3.2 ? 1 : 0);
        organic(g2b, 3 * S, 6 * S, 24 * S, SEED + 817); fringe(g2b, 0.3, 2 * S);
        dep(g2b, P.ultra, 0.13); dep(g2b, P.sepia, 0.06); dep(g2b, P.sap, 0.08); }
      charge(pshade, [[P.bSienna, 0.34], [P.ultra, 0.30], [P.rose, 0.10], [P.umber, 0.10]], 0.3, 120 * S, SEED + 809);
      granK = 1; }
    yield step('斜面の陰');
    { // 草の筆致:下から上へ払う。暗い筆とやや明るい枯れ色を、かすれさせて重ねる
      const r = makeRng(SEED + 810);
      for (const pass of [{ n: 1300, pig: [[P.ultra, 0.34], [P.sepia, 0.24], [P.sap, 0.18]], dry: 0.42, w: [1.0, 2.8] }, { n: 500, pig: [[P.bSienna, 0.30], [P.umber, 0.16]], dry: 0.5, w: [1.0, 2.2] }]) {
        const t = like(FB);
        for (let k = 0; k < pass.n; k++) {
          const x = px0 + r() * pw, y0 = fgR[clamp(Math.round(x), 0, W - 1)], y = mix(y0 + 10 * S, py1 + 30 * S, Math.pow(r(), 0.75)), dn = clamp01((y - y0) / (py1 - y0));
          const sc = mix(0.35, 1.8, dn), len = r.range(18, 64) * S * sc, ang = -Math.PI / 2 + r.range(-0.55, 0.55) + 0.12;
          stroke(t, bladePts(x, y, len, ang, r.range(-0.4, 0.4), r.range(pass.w[0], pass.w[1]) * S * sc), r.range(0.4, 1));
        }
        for (let i = 0; i < t.a.length; i++) t.a[i] *= FB.a[i] * (1 - 0.8 * PM.a[i]) * sstep(4 * S, 30 * S, fgD.a[i]);
        drybrush(t, pass.dry, SEED + 811 + pass.n, 14 * S, 3);
        for (const it of pass.pig) dep(t, it[0], it[1]);
      }
      // 枯れ草の明るい筋:濡らした細筆で拭う
      const lt = like(FB);
      for (let k = 0; k < 520; k++) {
        const x = px0 + r() * pw, y0 = fgR[clamp(Math.round(x), 0, W - 1)], y = mix(y0 + 20 * S, py1 + 30 * S, Math.pow(r(), 0.8)), dn = clamp01((y - y0) / (py1 - y0));
        const sc = mix(0.4, 1.7, dn);
        stroke(lt, bladePts(x, y, r.range(22, 70) * S * sc, -Math.PI / 2 + r.range(-0.6, 0.6) + 0.12, r.range(-0.4, 0.45), r.range(0.7, 1.5) * S * sc), r.range(0.3, 0.75));
      }
      for (let i = 0; i < lt.a.length; i++) lt.a[i] *= FB.a[i] * (1 - PM.a[i]);
      lift(lt, 0.55); dep(lt, P.gold, 0.16); dep(lt, P.rawSienna, 0.14);
    }
    yield step('草');
    { // 道の土:轍の筋、小石、草むらの落とす影
      const r = makeRng(SEED + 830), t = like(FB), sh = like(FB), peb = like(FB), pebS = like(FB);
      for (let i = 0; i + 1 < pathPts.length; i++) { const a = pathPts[i], c = pathPts[i + 1]; let tx = c[0] - a[0], ty = c[1] - a[1]; const tl = Math.hypot(tx, ty) || 1; tx /= tl; ty /= tl;
        for (const off of [-0.42, 0.38]) { const w = 0.5 + 0.5 * vnoise1(i / 5 + off * 9, SEED + 831); if (w < 0.25) continue;
          seg(t, a[0] + ty * a[2] * off, a[1] - tx * a[2] * off, a[2] * 0.05 * w, c[0] + ty * c[2] * off, c[1] - tx * c[2] * off, c[2] * 0.05 * w, 0.45 * w); } }
      for (let k = 0; k < 230; k++) { const a = pathPts[r.int(pathPts.length - 1)], off = r.range(-1, 1), x = a[0] + off * a[2] * 0.9 + r.range(-4, 4) * S, y = a[1] + r.range(-10, 10) * S, rr = Math.max(0.5 * S, a[2] * r.range(0.008, 0.035));
        if (r() < 0.16) { seg(peb, x - rr * 0.5, y, rr * 0.6, x + rr * 0.6, y + rr * 0.15, rr * 0.5); seg(pebS, x - rr * 0.6, y + rr * 0.8, rr * 0.4, x + rr * 1.1, y + rr * 0.95, rr * 0.3, 0.7); } else disc(t, x, y, rr * 0.7, r.range(0.25, 0.7)); }
      // 草の影:道の左縁から右下へ
      for (let k = 0; k < 13; k++) { const a = pathPts[4 + r.int(pathPts.length - 10)], w = a[2], x0 = a[0] - w * r.range(0.7, 1.15), y0 = a[1] + r.range(-12, 12) * S, l = w * r.range(0.5, 1.5);
        stroke(sh, bladePts(x0, y0, l, 0.38 + r.range(-0.12, 0.12), r.range(-0.12, 0.12), Math.max(1.2 * S, w * r.range(0.035, 0.09))), r.range(0.4, 0.9)); }
      bblur(sh, 2.6 * S);
      for (const q of [t, sh, peb, pebS]) for (let i = 0; i < q.a.length; i++) q.a[i] *= PM.a[i] * FB.a[i];
      drybrush(t, 0.35, SEED + 832, 12 * S, 1);
      dep(t, P.bSienna, 0.30); dep(t, P.umber, 0.20); dep(t, P.ultra, 0.08);
      dep(sh, P.ultra, 0.22); dep(sh, P.rose, 0.08); dep(sh, P.bSienna, 0.10);
      lift(peb, 0.45); dep(pebS, P.ultra, 0.3); dep(pebS, P.sepia, 0.22);
      // 道の縁にかぶさる草
      const eg = like(FB);
      for (let k = 0; k < 170; k++) { const a = pathPts[r.int(pathPts.length - 1)], sd = r.sign(), w = a[2], x = a[0] + sd * w * r.range(0.9, 1.3), y = a[1] + r.range(-8, 8) * S, sc = clamp(w / (40 * S), 0.3, 2.2);
        stroke(eg, bladePts(x, y, r.range(12, 40) * S * sc, -Math.PI / 2 - sd * r.range(0.1, 0.9), -sd * r.range(0.1, 0.6), r.range(0.8, 2.0) * S * sc), r.range(0.5, 1)); }
      for (let i = 0; i < eg.a.length; i++) eg.a[i] *= FB.a[i];
      drybrush(eg, 0.3, SEED + 833, 10 * S, 3);
      dep(eg, P.ultra, 0.26); dep(eg, P.sap, 0.30); dep(eg, P.sepia, 0.14);
    }
    yield step('道');
    { // 手前の土手:いちばん暗い。薄の株が立つ
      const nb = mkbuf(px0 - 12 * S, Y(0.82), px1 + 12 * S, py1 + 12 * S), sp2 = spikeArr(SEED + 843, 0.8, 44);
      for (let y = 0, i = 0; y < nb.h; y++) for (let x = 0; x < nb.w; x++, i++) nb.a[i] = clamp01(nb.y + y + 0.5 - (nbR[nb.x + x] - sp2[nb.x + x]) + 0.5);
      fringe(nb, 0.25, 2 * S, 1.5, 0);
      const nz = fbmArr(nb, 190 * S, 3, SEED + 841);
      for (let y = 0, i = 0; y < nb.h; y++) for (let x = 0; x < nb.w; x++, i++) nb.a[i] *= (0.7 + 0.4 * clamp01((nb.y + y - nbR[nb.x + x]) / (110 * S))) * Math.max(0.5, 1 + 0.18 * nz[i]);
      granK = 0.6; charge(nb, [[P.ultra, 0.36], [P.sepia, 0.16], [P.sap, 0.26], [P.bSienna, 0.12]], 0.35, 170 * S, SEED + 842); granK = 1;
    }
    yield step('手前の土手');
    { // 人物。背中は陰、左の輪郭だけ朝日が縁取る
      const cx = X(0.690), fy = fgR[Math.round(cx)] + 5 * S, k = 0.061 * ph / 80;
      const t = mkbuf(cx - 30 * k, fy - 90 * k, cx + 80 * k, fy + 50 * k);
      const Q = pts => { const o = []; for (const q of pts) o.push(cx + q[0] * k, fy - q[1] * k); return o; };
      const ell = (b, ex, ey, rx, ry) => { const o = []; for (let i = 0; i < 24; i++) { const a = i / 24 * TAU; o.push([ex + Math.cos(a) * rx, ey + Math.sin(a) * ry]); } fillPoly(b, Q(o)); };
      // 足もとの影(右下へ、陰の中へ消える)
      const sh = like(t); fillPoly(sh, Q([[-5, 2], [5, 2], [34, -20], [22, -24]])); bblur(sh, 2.2 * S);
      for (let y = 0; y < t.h; y++) for (let x = 0; x < t.w; x++) sh.a[y * t.w + x] *= 1 - 0.7 * clamp01((t.y + y - fy) / (24 * k));
      dep(sh, P.ultra, 0.42); dep(sh, P.rose, 0.10); dep(sh, P.bSienna, 0.12);
      const legs = like(t), coat = like(t), coatSh = like(t), head = like(t), rim = like(t);
      fillPoly(legs, Q([[-6.4, 40], [-0.5, 40], [-1.5, 2.5], [-5.9, 2.5]])); fillPoly(legs, Q([[0.5, 40], [6.6, 40], [6.9, 2.5], [2.6, 2.5]]));
      ell(legs, -4.3, 1.6, 3.0, 1.9); ell(legs, 5.3, 1.6, 3.0, 1.9);
      dep(legs, P.ultra, 0.95); dep(legs, P.sepia, 0.55); dep(legs, P.crimson, 0.12);
      fillPoly(coat, Q([[-9.6, 63.2], [-6, 66.4], [-2.6, 67.6], [2.6, 67.6], [6, 66.4], [9.6, 63.2], [9.1, 52], [8.2, 44], [8.7, 36], [-8.3, 36], [-8.0, 44], [-8.9, 52]]));
      stroke(coat, [[cx - 10.0 * k, fy - 61.5 * k, 2.4 * k], [cx - 11.3 * k, fy - 51 * k, 2.3 * k], [cx - 10.2 * k, fy - 42 * k, 2.0 * k]]);
      stroke(coat, [[cx + 10.0 * k, fy - 61.5 * k, 2.4 * k], [cx + 11.3 * k, fy - 51 * k, 2.3 * k], [cx + 10.0 * k, fy - 42 * k, 2.0 * k]]);
      fringe(coat, 0.3, 1.2 * S);
      dep(coat, P.red, 0.95); dep(coat, P.crimson, 0.42); dep(coat, P.ultra, 0.12);
      for (let y = 0, i = 0; y < t.h; y++) for (let x = 0; x < t.w; x++, i++) coatSh.a[i] = coat.a[i] * sstep(-2.5 * k, 1.5 * k, t.x + x - cx + 0.12 * (t.y + y - (fy - 50 * k)));
      dep(coatSh, P.ultra, 0.30); dep(coatSh, P.crimson, 0.22); dep(coatSh, P.sepia, 0.10);
      { const sl = like(t); seg(sl, cx - 8.3 * k, fy - 60 * k, 0.35 * k, cx - 8.1 * k, fy - 44 * k, 0.3 * k); seg(sl, cx + 8.3 * k, fy - 60 * k, 0.35 * k, cx + 8.1 * k, fy - 44 * k, 0.3 * k); dep(sl, P.ultra, 0.5); dep(sl, P.crimson, 0.3); }
      fillPoly(head, Q([[-2.0, 66.8], [2.0, 66.8], [1.9, 71], [-1.9, 71]])); ell(head, 0.2, 75.2, 4.5, 5.3);
      dep(head, P.sepia, 1.0); dep(head, P.ultra, 0.7);
      // 逆光の縁:頭の左上、左肩、左腕の外側を細く拭って暖色を差す
      stroke(rim, [[cx - 4.5 * k, fy - 76 * k, 0.5 * k], [cx - 3.2 * k, fy - 79.3 * k, 0.55 * k], [cx - 0.2 * k, fy - 80.4 * k, 0.5 * k], [cx + 2.4 * k, fy - 79.6 * k, 0.35 * k]]);
      stroke(rim, [[cx - 2.8 * k, fy - 67.4 * k, 0.4 * k], [cx - 6.2 * k, fy - 66.2 * k, 0.55 * k], [cx - 10.0 * k, fy - 63.2 * k, 0.6 * k], [cx - 12.3 * k, fy - 57 * k, 0.55 * k], [cx - 13.0 * k, fy - 50 * k, 0.5 * k], [cx - 11.8 * k, fy - 42.5 * k, 0.35 * k]]);
      stroke(rim, [[cx - 6.6 * k, fy - 38 * k, 0.4 * k], [cx - 6.5 * k, fy - 20 * k, 0.4 * k], [cx - 6.1 * k, fy - 5 * k, 0.3 * k]]);
      lift(rim, 0.8); dep(rim, P.naples, 0.45); dep(rim, P.gold, 0.10);
    }
    yield step('人');
    // 暗い葉(稜線の草、薄の葉)
    dep(DKB, P.ultra, 0.42); dep(DKB, P.sap, 0.40); dep(DKB, P.sepia, 0.30);
    dep(DKL, P.ultra, 0.40); dep(DKL, P.sap, 0.36); dep(DKL, P.sepia, 0.26); dep(DKL, P.bSienna, 0.14);
    yield step('草の葉');
    { // 雁の列:遠く、谷の奥へ渡っていく
      const r = makeRng(SEED + 860), bb = mkbuf(X(0.50), Y(0.14), X(0.74), Y(0.30));
      const lead = [X(0.565), Y(0.232)];
      for (let k = 0; k < 9; k++) {
        const arm = k === 0 ? 0 : (k % 2 ? 1 : -1), idx = Math.ceil(k / 2);
        const x = lead[0] + idx * (27 + r.range(-4, 4)) * S + r.range(-3, 3) * S, y = lead[1] + arm * idx * (9.5 + r.range(-2, 2)) * S - idx * 3.2 * S + r.range(-2, 2) * S;
        const sp = r.range(6.2, 8.6) * S * (1 - idx * 0.035), ph2 = r.range(-0.9, 0.9), up = Math.sin(ph2) * sp * 0.55;
        stroke(bb, [[x - sp, y - up + sp * 0.12, 0.3 * S], [x - sp * 0.45, y - up * 0.75, 0.75 * S], [x, y, 0.95 * S], [x + sp * 0.5, y - up * 0.7, 0.75 * S], [x + sp * 1.05, y - up + sp * 0.1, 0.3 * S]]);
        seg(bb, x - sp * 0.42, y + 0.2 * S, 0.5 * S, x + 0.1 * sp, y, 0.9 * S);
      }
      dep(bb, P.neutral, 0.62); dep(bb, P.ultra, 0.30); dep(bb, P.rose, 0.08); }
    yield step('雁');
    { // 飛沫:筆をはじいて前景に散らす
      const r = makeRng(SEED + 870), sp = mkbuf(px0, Y(0.78), px1, py1), sp2 = like(sp);
      for (let k = 0; k < 150; k++) {
        const cl = r() < 0.5 ? 0.17 : 0.84, x = X(clamp01(cl + r.gauss() * 0.11)), y = Y(0.80 + 0.2 * Math.pow(r(), 0.7)), rr = (0.5 + 2.4 * Math.pow(r(), 4)) * S;
        seg(r() < 0.75 ? sp : sp2, x, y, rr, x + r.range(-1, 1) * rr, y + r.range(0, 1.6) * rr, rr * 0.8, r.range(0.5, 1));
      }
      for (const q of [sp, sp2]) for (let y = 0, i = 0; y < q.h; y++) for (let x = 0; x < q.w; x++, i++) if (q.y + y < fgR[q.x + x] + 8 * S) q.a[i] = 0;
      fringe(sp, 0.5, 1.2 * S); dep(sp, P.ultra, 0.42); dep(sp, P.sepia, 0.36);
      dep(sp2, P.bSienna, 0.5); dep(sp2, P.gold, 0.2); }
    paintMaple(); yield step('楓');
    { // 散った葉:道と草の上に数枚、宙に二枚。濃い所は拭ってから色を置く
      const r = makeRng(SEED + 910), jr = makeRng(SEED + 911), list = [];
      for (let k = 0; k < 11; k++) { const v = r.range(0.85, 0.985), u = k < 7 ? r.range(0.40, 0.56) - (v - 0.9) * 0.9 : r.range(0.17, 0.40), sc = 0.55 + (v - 0.84) * 4;
        list.push({ x: X(u), y: Y(v), ang: r() * TAU, sz: r.range(20, 30) * S * sc, sq: r.range(0.75, 1), sqa: r() * Math.PI, gs: r.range(0.42, 0.6), hue: r.range(0.3, 1), sh: false, v: r(), ground: true }); }
      list.push({ x: X(0.338), y: Y(0.476), ang: 2.2, sz: 21 * S, sq: 0.55, sqa: 0.7, hue: 0.8, v: 0.5 });
      for (const L of list) {
        const sh = leafPoly(L, jr), t = mkbuf(L.x - L.sz * 1.3, L.y - L.sz * 1.3, L.x + L.sz * 1.3, L.y + L.sz * 1.3);
        if (L.ground && FG.a[clamp(Math.round(L.y) - FG.y, 0, FG.h - 1) * FG.w + clamp(Math.round(L.x) - FG.x, 0, FG.w - 1)] < 0.5) continue;
        if (L.ground) { const sd = like(t); fillPoly(sd, sh.poly.map((q, i) => q + (i % 2 ? 1.6 * S : 1.2 * S))); dep(sd, P.ultra, 0.30); dep(sd, P.sepia, 0.2); }
        fillPoly(t, sh.poly); lift(t, 0.88); fringe(t, 0.45, 1.2 * S, 1.4, 0.1);
        const h = L.hue, pg = h < 0.45 ? [[P.aureolin, 0.70], [P.gold, 0.34]] : h < 0.70 ? [[P.gold, 0.74], [P.red, 0.26]] : h < 0.9 ? [[P.red, 0.80], [P.gold, 0.28]] : [[P.red, 0.66], [P.crimson, 0.46]];
        for (const it of pg) dep(t, it[0], it[1] * (L.ground ? 0.95 : 0.8));
        if (L.ground) { dep(t, P.bSienna, 0.12); }
        const vn = like(t); for (const q of sh.tips) seg(vn, sh.c[0], sh.c[1], 0.45 * S, q[0], q[1], 0.22 * S, 0.7); for (let i = 0; i < vn.a.length; i++) vn.a[i] *= Math.min(1, t.a[i]); dep(vn, P.crimson, 0.28);
      } }
    yield step('散る葉');
    // マスキングを剥がして、光る葉と穂に淡く色を差す
    unmask('fgres');
    dep(LB, P.aureolin, 0.26); dep(LB, P.naples, 0.22); dep(LB, P.gold, 0.08); dep(LB, P.sap, 0.07);
    dep(STM, P.naples, 0.40); dep(STM, P.rawSienna, 0.20); dep(STM, P.gold, 0.06);
    dep(PL, P.naples, 0.50); dep(PL, P.ochre, 0.10);
    for (let i = 0; i < PLs.a.length; i++) PLs.a[i] *= PL.a[i]; dep(PLs, P.rawSienna, 0.22); dep(PLs, P.neutral, 0.12);
    yield step('薄');
    { // 余白に鉛筆で題を書く。字は一画ずつの折れ線(100×100 の枡)
      const K = [
        [[[22, 41], [50, 39], [79, 37]], [[50, 13], [50.5, 48], [50, 83]], [[9, 85], [50, 83.5], [91, 82]]],
        [[[37, 9], [31, 21], [23, 31]], [[62, 8], [70, 19], [79, 28]], [[50, 25], [38, 43], [17, 59]], [[51, 26], [66, 44], [87, 57]], [[33, 62], [33.5, 77], [34, 91]], [[33, 62], [68, 60.5], [67.5, 76], [67, 91]], [[34, 89.5], [67, 89]]],
        [[[13, 39], [50, 37], [87, 34]], [[50, 9], [49.5, 37], [41, 62], [13, 89]], [[51, 39], [63, 64], [89, 88]]],
        [[[7, 37], [22, 35.5], [37, 33]], [[22, 13], [22.5, 44], [22, 73]], [[5, 78], [21, 72], [39, 63]], [[35, 42], [60, 36], [84, 29], [82, 45], [79, 59], [72, 54]], [[59, 11], [59.2, 40], [59, 67]], [[45, 25], [44.5, 52], [45, 76], [53, 86], [72, 87], [89, 86], [91, 71]]],
      ];
      const cs = 33 * S, gap = 44 * S, ox = px0 + 6 * S, oy = py1 + 38 * S, b = mkbuf(ox - 8 * S, oy - 8 * S, ox + gap * 4 + 8 * S, oy + cs + 10 * S), r = makeRng(SEED + 990);
      K.forEach((ch, ci) => ch.forEach((st, si) => {
        const jx = r.range(-0.8, 0.8) * S, jy = r.range(-0.8, 0.8) * S, sl = 0.07;
        const pts = crSpline(st.map(q => [ox + ci * gap + (q[0] + (50 - q[1]) * sl) / 100 * cs + jx, oy + q[1] / 100 * cs + jy]), 7), n = pts.length - 1;
        for (let i = 0; i < n; i++) { const t = i / n, pr = 0.95 - 0.35 * t + 0.1 * vnoise1(i / 3 + ci * 7 + si * 3, 5); seg(b, pts[i][0], pts[i][1], 0.8 * S, pts[i + 1][0], pts[i + 1][1], 0.8 * S, clamp01(pr)); }
      }));
      for (let y = 0, i = 0; y < b.h; y++) { let gi = (b.y + y) * W + b.x; for (let x = 0; x < b.w; x++, i++, gi++) if (b.a[i] > 0) b.a[i] *= 0.45 + 0.55 * sstep(0.22, 0.6, paperH[gi]); }
      frameOn = false; dep(b, P.graphite, 0.95); frameOn = true; }
    yield step('題');
  }

  return { W, H, S, run: sceneMain, compose };
}

return { createPainting };
});