Claude Fable 5.2max

engine.js

// 金屏風「土谷大地」— 描画エンジン
// 単位は cm。本紙は 360×160(六曲一隻、一扇 60)。外部ライブラリ・画像なし。
// 絵具は「箔の上の層」として色・厚み・金属性をためておき、最後に光を当てて仕上げる。
'use strict';
(function (F) { const G = typeof globalThis !== 'undefined' ? globalThis : this; F(G); (G.BYOBU_SRC = G.BYOBU_SRC || []).push(F); })(function (G) {
  const B = (G.BYOBU = G.BYOBU || {});

  const PW = 360, PH = 160, PANEL = 60;
  const KOBERI = 0.8, OBERI = 4.2, FUCHI = 2.2, MARGIN = 5;
  const EXT = KOBERI + OBERI + FUCHI;
  B.dim = { PW, PH, PANEL, KOBERI, OBERI, FUCHI, MARGIN, EXT };

  // ---------- 色 ----------
  const s2l = (v) => (v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4));
  function hex(h) {
    const n = parseInt(h.slice(1), 16);
    return [s2l(((n >> 16) & 255) / 255), s2l(((n >> 8) & 255) / 255), s2l((n & 255) / 255)];
  }
  const mix = (a, b, t) => a + (b - a) * t;
  const mix3 = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
  const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
  const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); };
  Object.assign(B, { hex, mix, mix3, clamp, sstep });

  // ---------- 乱数とノイズ ----------
  function rng(seed) {
    let a = seed | 0;
    const f = function () {
      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.pm = (v) => (f() * 2 - 1) * v;
    f.int = (lo, hi) => lo + Math.floor(f() * (hi - lo + 1));
    f.pick = (arr) => arr[Math.floor(f() * arr.length)];
    f.gauss = () => { let s = 0; for (let i = 0; i < 4; i++) s += f(); return (s - 2) * 1.732; };
    return f;
  }
  function hash2(ix, iy, s) {
    let h = (Math.imul(ix, 0x27d4eb2d) + Math.imul(iy, 0x165667b1) + Math.imul(s, 0x9e3779b1)) | 0;
    h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
    h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
    return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
  }
  function vnoise(x, y, s) {
    const ix = Math.floor(x), iy = Math.floor(y);
    const fx = x - ix, fy = y - iy;
    const ux = fx * fx * fx * (fx * (fx * 6 - 15) + 10), uy = 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) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
  }
  // オクターブごとに回して格子の向きを隠す
  function fbm(x, y, oct, s) {
    let a = 0, amp = 0.5, n = 0;
    for (let o = 0; o < oct; o++) {
      a += amp * vnoise(x, y, s + o * 19);
      n += amp; amp *= 0.5;
      const nx = (x * 0.8 - y * 0.6) * 2.03 + 11.3, ny = (x * 0.6 + y * 0.8) * 2.03 - 7.7;
      x = nx; y = ny;
    }
    return a / n;
  }
  Object.assign(B, { rng, hash2, vnoise, fbm });

  // ---------- 曲線 ----------
  // 制御点を通る centripetal Catmull-Rom を、ほぼ等間隔の折れ線にする
  function path(pts, step) {
    step = step || 0.1;
    const xs = [], ys = [];
    const n = pts.length;
    const T = (a, b) => Math.pow(Math.hypot(b[0] - a[0], b[1] - a[1]), 0.5) + 1e-6;
    for (let i = 0; i < n - 1; i++) {
      const p1 = pts[i], p2 = pts[i + 1];
      const p0 = i > 0 ? pts[i - 1] : [2 * p1[0] - p2[0], 2 * p1[1] - p2[1]];
      const p3 = i + 2 < n ? pts[i + 2] : [2 * p2[0] - p1[0], 2 * p2[1] - p1[1]];
      const t0 = 0, t1 = t0 + T(p0, p1), t2 = t1 + T(p1, p2), t3 = t2 + T(p2, p3);
      const m = Math.max(1, Math.ceil(Math.hypot(p2[0] - p1[0], p2[1] - p1[1]) / step));
      for (let j = 0; j < m; j++) {
        const t = t1 + ((t2 - t1) * j) / m;
        for (let c = 0; c < 2; c++) {
          const A1 = (p0[c] * (t1 - t) + p1[c] * (t - t0)) / (t1 - t0);
          const A2 = (p1[c] * (t2 - t) + p2[c] * (t - t1)) / (t2 - t1);
          const A3 = (p2[c] * (t3 - t) + p3[c] * (t - t2)) / (t3 - t2);
          const B1 = (A1 * (t2 - t) + A2 * (t - t0)) / (t2 - t0);
          const B2 = (A2 * (t3 - t) + A3 * (t - t1)) / (t3 - t1);
          (c ? ys : xs).push((B1 * (t2 - t) + B2 * (t - t1)) / (t2 - t1));
        }
      }
    }
    xs.push(pts[n - 1][0]); ys.push(pts[n - 1][1]);
    return pathXY(xs, ys);
  }
  function pathXY(xs, ys) {
    const N = xs.length;
    const x = Float64Array.from(xs), y = Float64Array.from(ys), u = new Float64Array(N);
    let len = 0, x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9;
    for (let i = 0; i < N; i++) {
      if (i) len += Math.hypot(x[i] - x[i - 1], y[i] - y[i - 1]);
      u[i] = len;
      if (x[i] < x0) x0 = x[i]; if (x[i] > x1) x1 = x[i];
      if (y[i] < y0) y0 = y[i]; if (y[i] > y1) y1 = y[i];
    }
    for (let i = 0; i < N; i++) u[i] = len > 0 ? u[i] / len : 0;
    return { x, y, u, len, x0, x1, y0, y1, n: N };
  }
  // 折れ線上の位置と向き(t は 0..1 の道のり)
  function at(P, t) {
    t = clamp(t, 0, 1);
    let lo = 0, hi = P.n - 1;
    while (hi - lo > 1) { const m = (lo + hi) >> 1; if (P.u[m] <= t) lo = m; else hi = m; }
    const f = P.u[hi] > P.u[lo] ? (t - P.u[lo]) / (P.u[hi] - P.u[lo]) : 0;
    const dx = P.x[hi] - P.x[lo], dy = P.y[hi] - P.y[lo], l = Math.hypot(dx, dy) || 1;
    return { x: P.x[lo] + dx * f, y: P.y[lo] + dy * f, tx: dx / l, ty: dy / l };
  }
  Object.assign(B, { path, pathXY, at });

  // ---------- 箔 ----------
  const LP = 10.4, LOX = 4.7, LOY = 6.1; // 箔の送りと格子のずれ
  const leafOut = { i: 0, j: 0, cnt: 0, edge: 0 };
  function leafAt(X, Y, kind) {
    const gx = (X + LOX) / LP, gy = (Y + LOY) / LP;
    const i0 = Math.floor(gx), j0 = Math.floor(gy);
    const fx = gx - i0, fy = gy - j0;
    const near = 0.075;
    const di0 = fx < near ? -1 : 0, di1 = fx > 1 - near ? 1 : 0, dj0 = fy < near ? -1 : 0, dj1 = fy > 1 - near ? 1 : 0;
    let best = -1e9, cnt = 0, bi = i0, bj = j0, be = 1;
    const en = di0 | di1 | dj0 | dj1 ? (vnoise(X * 2.3, Y * 2.3, 71 + kind) - 0.5) * 0.16 + (vnoise(X * 9, Y * 9, 72) - 0.5) * 0.05 : 0;
    for (let dj = dj0; dj <= dj1; dj++) {
      for (let di = di0; di <= di1; di++) {
        const i = i0 + di, j = j0 + dj, s = 11 + kind * 40;
        const cx = (i + 0.5) * LP - LOX + (hash2(i, j, s) - 0.5) * 0.34;
        const cy = (j + 0.5) * LP - LOY + (hash2(i, j, s + 1) - 0.5) * 0.34;
        const rot = (hash2(i, j, s + 2) - 0.5) * 0.014;
        const dx = X - cx, dy = Y - cy;
        const lx = dx + dy * rot, ly = dy - dx * rot;
        const hs = LP / 2 + 0.2 + hash2(i, j, s + 3) * 0.16;
        const e = Math.min(hs - Math.abs(lx), hs - Math.abs(ly)) + en;
        if (e > 0) {
          cnt++;
          const ord = j * 4096 + i;
          if (ord > best) { best = ord; bi = i; bj = j; be = e; }
        }
      }
    }
    leafOut.i = bi; leafOut.j = bj; leafOut.cnt = cnt; leafOut.edge = be;
    return leafOut;
  }

  // ---------- 本体 ----------
  B.create = function (o) {
    o = o || {};
    const S = o.S || 16;
    const FULL = [-(EXT + MARGIN), -(EXT + MARGIN), PW + 2 * (EXT + MARGIN), PH + 2 * (EXT + MARGIN)];
    const V = o.view || FULL;
    const W = Math.max(1, Math.round(V[2] * S)), H = Math.max(1, Math.round(V[3] * S)), n = W * H;
    const pr = new Float32Array(n), pg = new Float32Array(n), pb = new Float32Array(n);
    const pa = new Float32Array(n), pm = new Float32Array(n), ph = new Float32Array(n);
    const sm = new Float32Array(n).fill(-1e9), su = new Float32Array(n), sv = new Float32Array(n), st = new Float32Array(n);
    let touched = new Int32Array(1 << 16), tn = 0;
    // 本紙の範囲(px)
    const cx0 = Math.max(0, Math.ceil((0 - V[0]) * S - 0.5)), cx1 = Math.min(W - 1, Math.floor((PW - V[0]) * S - 0.5));
    const cy0 = Math.max(0, Math.ceil((0 - V[1]) * S - 0.5)), cy1 = Math.min(H - 1, Math.floor((PH - V[1]) * S - 0.5));
    const E = { S, V, W, H, n, pr, pg, pb, pa, pm, ph, px: 1 / S, hooks: {}, clip: [cx0, cy0, cx1, cy1] };
    const O = { r: 0, g: 0, b: 0, a: 1, m: 0, h: 0 };

    function put(i, a, r, g, b, m, h) {
      const k = 1 - a;
      pr[i] = r * a + pr[i] * k; pg[i] = g * a + pg[i] * k; pb[i] = b * a + pb[i] * k;
      pm[i] = m * a + pm[i] * k; pa[i] = a + pa[i] * k;
      ph[i] += h * a;
    }
    E.put = put;

    function seg(ax, ay, bx, by, ra, rb, ua, ub, ta, tb) {
      const rmax = (ra > rb ? ra : rb) + 1;
      let x0 = Math.floor(Math.min(ax, bx) - rmax), x1 = Math.ceil(Math.max(ax, bx) + rmax);
      let y0 = Math.floor(Math.min(ay, by) - rmax), y1 = Math.ceil(Math.max(ay, by) + rmax);
      if (x0 < cx0) x0 = cx0; if (x1 > cx1) x1 = cx1; if (y0 < cy0) y0 = cy0; if (y1 > cy1) y1 = cy1;
      if (x0 > x1 || y0 > y1) return;
      const dx = bx - ax, dy = by - ay, l2 = dx * dx + dy * dy, il2 = l2 > 1e-9 ? 1 / l2 : 0, il = Math.sqrt(il2);
      for (let y = y0; y <= y1; y++) {
        const py = y + 0.5 - ay;
        let i = y * W + x0;
        for (let x = x0; x <= x1; x++, i++) {
          const px = x + 0.5 - ax;
          let t = (px * dx + py * dy) * il2;
          t = t < 0 ? 0 : t > 1 ? 1 : t;
          const qx = px - t * dx, qy = py - t * dy;
          const r = ra + t * (rb - ra);
          const m = r - Math.sqrt(qx * qx + qy * qy);
          if (m > -0.5 && m > sm[i]) {
            if (sm[i] < -1e8) {
              if (tn === touched.length) { const nt = new Int32Array(tn * 2); nt.set(touched); touched = nt; }
              touched[tn++] = i;
            }
            sm[i] = m; su[i] = ua + t * (ub - ua);
            sv[i] = clamp(((dx * py - dy * px) * il) / r, -1, 1);
            st[i] = ta + t * (tb - ta);
          }
        }
      }
    }

    // 筆の一画。P は折れ線、wid は半幅(cm、数か u の関数)、shade(o,u,v,X,Y) が色を決める
    E.stroke = function (P, wid, shade) {
      const N = P.n;
      if (N < 2) return;
      const wf = typeof wid === 'function' ? wid : null;
      let wmax = wf ? 0 : wid;
      if (wf) for (let k = 0; k <= 16; k++) wmax = Math.max(wmax, wf(k / 16));
      wmax = wmax * 1.3 + 2 / S;
      if (P.x1 + wmax < V[0] || P.x0 - wmax > V[0] + V[2] || P.y1 + wmax < V[1] || P.y0 - wmax > V[1] + V[3]) return;
      let ax = (P.x[0] - V[0]) * S, ay = (P.y[0] - V[1]) * S;
      let rw = (wf ? wf(P.u[0]) : wid) * S;
      let ra = rw > 0.6 ? rw : 0.6, ta = rw >= 0.6 ? 1 : Math.max(0, rw / 0.6), ua = P.u[0];
      for (let k = 1; k < N; k++) {
        const ex = (P.x[k] - V[0]) * S, ey = (P.y[k] - V[1]) * S, ue = P.u[k];
        // 幅が変わる筆は、長い区間を細かく刻んで幅を拾い直す
        const m = wf ? Math.max(1, Math.ceil(Math.hypot(ex - ax, ey - ay) / 3)) : 1;
        const sx = ax, sy = ay, us = ua;
        for (let j = 1; j <= m; j++) {
          const f = j / m, bx = sx + (ex - sx) * f, by = sy + (ey - sy) * f, ub = us + (ue - us) * f;
          rw = (wf ? wf(ub) : wid) * S;
          const rb = rw > 0.6 ? rw : 0.6, tb = rw >= 0.6 ? 1 : Math.max(0, rw / 0.6);
          seg(ax, ay, bx, by, ra, rb, ua, ub, ta, tb);
          ax = bx; ay = by; ra = rb; ta = tb; ua = ub;
        }
      }
      for (let t = 0; t < tn; t++) {
        const i = touched[t], m = sm[i];
        sm[i] = -1e9;
        const cov = (m >= 0.5 ? 1 : m + 0.5) * st[i];
        const x = i % W, y = (i - x) / W;
        O.a = 1; O.m = 0; O.h = 0;
        shade(O, su[i], sv[i], V[0] + (x + 0.5) / S, V[1] + (y + 0.5) / S);
        const a = cov * O.a;
        if (a > 0.003) put(i, a > 1 ? 1 : a, O.r, O.g, O.b, O.m, O.h);
      }
      tn = 0;
    };

    // 丸い点(盛り上げの点・実・雪など)
    E.dot = function (X, Y, r, shade) {
      const P = pathXY([X, X + 1e-4], [Y, Y]);
      E.stroke(P, r, shade);
    };

    // 陰関数の面。fn(o,X,Y) が o.a(被覆)と色を入れる
    E.region = function (x0, y0, x1, y1, fn) {
      let a0 = Math.floor((x0 - V[0]) * S), a1 = Math.ceil((x1 - V[0]) * S);
      let b0 = Math.floor((y0 - V[1]) * S), b1 = Math.ceil((y1 - V[1]) * S);
      if (a0 < cx0) a0 = cx0; if (a1 > cx1) a1 = cx1; if (b0 < cy0) b0 = cy0; if (b1 > cy1) b1 = cy1;
      for (let y = b0; y <= b1; y++) {
        const Y = V[1] + (y + 0.5) / S;
        let i = y * W + a0;
        for (let x = a0; x <= a1; x++, i++) {
          O.a = 0; O.m = 0; O.h = 0;
          fn(O, V[0] + (x + 0.5) / S, Y);
          if (O.a > 0.003) put(i, O.a > 1 ? 1 : O.a, O.r, O.g, O.b, O.m, O.h);
        }
      }
    };

    // ---------- 仕上げ(光を当てる) ----------
    const GOLD = [1.0, 0.64, 0.16];
    const LPOS = [112, -75, 640], VPOS = [180, 80, 780];
    const LD = (() => { const v = [-0.42, -0.52, 0.74], l = Math.hypot(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();
    const SIG2 = 2 * 0.4 * 0.4;
    const cBG = hex('#14120f'), cLacq = hex('#0a0a0b'), cKoberi = hex('#e9e2cf'), cNavy = hex('#18233a'), cThread = hex('#b7954c');
    const cFit = [0.95, 0.66, 0.2];

    function foldShade(X) {
      // 折れ目。谷折りは奥へ引っこんで暗く、山折りは角が光る
      const k = Math.round(X / PANEL);
      if (k < 1 || k > 5) return 1;
      const d = Math.abs(X - k * PANEL);
      if (d > 2.2) return 1;
      const valley = k % 2 === 1;
      let s = valley ? 1 - 0.2 * Math.exp(-d / 0.55) : 1 + 0.05 * Math.exp(-d / 0.3);
      if (d < 0.075) s *= 0.12 + 0.88 * sstep(0.03, 0.075, d);
      return s;
    }

    // 箔そのものの性質(光の当て方に依らない分)
    const LM = { tx: 0, ty: 0, tone: 1, red: 0, kind: 0, pin: 0, hue: 1, sr: 0, sg: 0, sb: 0 };
    function leafMat(X, Y) {
      const hooks = E.hooks;
      const kind = hooks.leafKind ? hooks.leafKind(X, Y) : 0;
      const lf = leafAt(X, Y, kind === 1 ? 1 : 0);
      const li = lf.i, lj = lf.j, ks = kind === 1 ? 300 : 100;
      const h1 = hash2(li, lj, ks), h2 = hash2(li, lj, ks + 1), h3 = hash2(li, lj, ks + 2), h4 = hash2(li, lj, ks + 3);
      // 箔のしわ:一枚ごとに向きの違う細かい筋
      const ang = h4 * 3.14159, ca = Math.cos(ang), sa = Math.sin(ang);
      const qx = X * ca + Y * sa, qy = -X * sa + Y * ca;
      const wr = vnoise(qx * 5.5, qy * 0.9, 31) - 0.5, wr2 = vnoise(X * 1.1 + 9, Y * 1.1, 32) - 0.5, wr3 = vnoise(X * 13, Y * 13, 33) - 0.5;
      LM.tx = (h1 - 0.5) * 0.05 + wr * 0.035 * ca + wr2 * 0.03 + wr3 * 0.012;
      LM.ty = (h2 - 0.5) * 0.05 + wr * 0.035 * sa - wr2 * 0.02;
      LM.kind = kind; LM.pin = 0;
      if (kind === 1) {
        // 銀箔の月:硫化して燻んだ銀
        const mo = hooks.moon;
        const rr = Math.hypot(X - mo.x, Y - mo.y) / mo.r;
        const t1 = fbm(X * 0.045 + 17, Y * 0.045, 3, 51), t2 = fbm(X * 0.27, Y * 0.27 + 5, 4, 52), t3 = vnoise(X * 5, Y * 5, 53), t4 = vnoise(X * 15, Y * 15, 54);
        let tar = 0.56 + (t1 - 0.5) * 0.75 + (t2 - 0.5) * 0.62 + (h3 - 0.5) * 0.5 + rr * rr * 0.2 + (t3 - 0.5) * 0.13 + (t4 - 0.5) * 0.09;
        tar = clamp(tar, 0, 1);
        let sr, sg, sb; // 明るい銀 → 褐色 → 青黒
        if (tar < 0.45) { const k = tar / 0.45; sr = mix(0.62, 0.36, k); sg = mix(0.61, 0.3, k); sb = mix(0.56, 0.22, k); }
        else { const k = (tar - 0.45) / 0.55; sr = mix(0.36, 0.105, k); sg = mix(0.3, 0.11, k); sb = mix(0.22, 0.135, k); }
        let e = 1;
        if (lf.cnt >= 2) e = 0.9;
        if (lf.edge < 0.05) e *= 0.85;
        LM.sr = sr * e; LM.sg = sg * e; LM.sb = sb * e;
        LM.tone = 1; LM.red = 0; LM.hue = 1;
        return;
      }
      let tone = 1 + (h3 - 0.5) * 0.05, red = 0;
      // 箔足(重なり)と、端の欠け
      if (lf.cnt >= 2) { tone *= 0.955; red = 0.5; }
      if (lf.edge < 0.05) { tone *= 0.9 + 0.1 * (lf.edge / 0.05); red = 0.7; }
      tone *= 1 + (fbm(X * 0.021 + 3, Y * 0.021, 3, 41) - 0.5) * 0.07;
      if (kind === 2) red += 0.6;
      LM.hue = 1 + (hash2(li, lj, ks + 5) - 0.5) * 0.025;
      // 擦れ・小さな穴から下地の赤茶がのぞく
      const pin = vnoise(X * 7.3, Y * 7.3, 61), worn = vnoise(X * 0.6, Y * 0.6, 62);
      const pth = 0.975 - sstep(0.62, 0.9, worn) * 0.07;
      if (pin > pth) LM.pin = sstep(pth, pth + 0.03, pin) * 0.5;
      // 切箔と砂子:霞のように撒いた金。一片ごとに傾きが違うので、光でちらちらする
      if (hooks.sprinkle) {
        const dens = hooks.sprinkle(X, Y);
        if (dens > 0.01) {
          const c = 1.5, ix = Math.floor(X / c), iy = Math.floor(Y / c);
          let hit = false;
          for (let dj = -1; dj <= 1 && !hit; dj++) for (let di = -1; di <= 1; di++) {
            const i = ix + di, j = iy + dj;
            if (hash2(i, j, 201) > dens * 0.72) continue;
            const fx = (i + 0.2 + 0.6 * hash2(i, j, 202)) * c, fy = (j + 0.2 + 0.6 * hash2(i, j, 203)) * c;
            const sz = 0.2 + 0.52 * Math.pow(hash2(i, j, 204), 2), rot = hash2(i, j, 205) * 3.14159, cr = Math.cos(rot), sr = Math.sin(rot);
            const lx = (X - fx) * cr + (Y - fy) * sr, ly = -(X - fx) * sr + (Y - fy) * cr;
            const e = sz - Math.max(Math.abs(lx), Math.abs(ly) * (0.8 + 0.5 * hash2(i, j, 208)));
            if (e > 0) {
              LM.tx = (hash2(i, j, 206) - 0.5) * 0.2; LM.ty = (hash2(i, j, 207) - 0.5) * 0.2;
              tone *= e < 0.035 ? 0.9 : 1.035; red += 0.25; LM.pin = 0; hit = true; break;
            }
          }
          if (!hit) {
            const qx = Math.floor(X * 9), qy = Math.floor(Y * 9);
            if (hash2(qx, qy, 211) < dens * 0.2) { LM.tx = (hash2(qx, qy, 212) - 0.5) * 0.26; LM.ty = (hash2(qx, qy, 213) - 0.5) * 0.26; tone *= 1.05; }
          }
        }
      }
      LM.tone = tone; LM.red = red;
    }
    function heights() {
      const hooks = E.hooks, ht = new Float32Array(n);
      for (let y = 0, i = 0; y < H; y++) {
        const Y = V[1] + (y + 0.5) / S;
        for (let x = 0; x < W; x++, i++) ht[i] = ph[i] + (hooks.relief ? hooks.relief(V[0] + (x + 0.5) / S, Y) : 0);
      }
      return ht;
    }
    const SDX = Math.round((-LD[0] / Math.hypot(LD[0], LD[1])) * 1.6), SDY = Math.round((-LD[1] / Math.hypot(LD[0], LD[1])) * 1.6);
    function castShadow(ht, x, y, i) {
      const sx = x - SDX, sy = y - SDY;
      if (sx >= 0 && sx < W && sy >= 0 && sy < H) {
        const dh = ht[sy * W + sx] - ht[i];
        if (dh > 0.004) return 1 - 0.42 * Math.min(1, (dh - 0.004) / 0.03);
      }
      return 1;
    }

    // 仕上げ。step を 2 以上にすると間引いた下見になる(工程の途中表示用)
    E.develop = function (rgba, step) {
      step = step || 1;
      const hooks = E.hooks, ht = heights();
      const W2 = Math.ceil(W / step);
      for (let y = 0; y < H; y += step) {
        const Y = V[1] + (y + 0.5) / S;
        for (let x = 0; x < W; x += step) {
          const i = y * W + x;
          const X = V[0] + (x + 0.5) / S;
          const dOut = Math.max(-X, X - PW, -Y, Y - PH);
          let r, g, b;
          if (dOut <= 0) {
            // --- 本紙 ---
            const xl = x > 0 ? i - 1 : i, xr = x < W - 1 ? i + 1 : i, yu = y > 0 ? i - W : i, yd = y < H - 1 ? i + W : i;
            const gxh = (ht[xr] - ht[xl]) * S * 0.5, gyh = (ht[yd] - ht[yu]) * S * 0.5;
            const panel = Math.min(5, Math.floor(X / PANEL));
            const ptilt = (panel % 2 ? 1 : -1) * 0.052;
            const A = pa[i];
            const sh = castShadow(ht, x, y, i); // 厚い絵具が落とす影
            let lr = 0, lg = 0, lb = 0;
            if (A < 0.995) {
              leafMat(X, Y);
              const lx = LPOS[0] - X, ly = LPOS[1] - Y, ll = 1 / Math.hypot(lx, ly, LPOS[2]);
              const vx = VPOS[0] - X, vy = VPOS[1] - Y, vl = 1 / Math.hypot(vx, vy, VPOS[2]);
              let hx = lx * ll + vx * vl, hy = ly * ll + vy * vl;
              const hz = LPOS[2] * ll + VPOS[2] * vl;
              hx /= hz; hy /= hz;
              const nx = ptilt + LM.tx - gxh, ny = LM.ty - gyh;
              const D = Math.exp(-((nx - hx) * (nx - hx) + (ny - hy) * (ny - hy)) / SIG2);
              if (LM.kind === 1) {
                const Is = 0.52 + 0.6 * D;
                lr = LM.sr * Is; lg = LM.sg * Is; lb = LM.sb * Is;
              } else {
                let I = (0.13 + 0.67 * D) * LM.tone;
                if (LM.kind === 2) I = I * 1.06 + 0.025;
                // 暗いところほど赤みへ寄る
                lr = GOLD[0] * I; lg = GOLD[1] * I * (0.9 + 0.1 * I) * (1 - LM.red * 0.035) * LM.hue; lb = GOLD[2] * I * (0.66 + 0.34 * I) * (1 - LM.red * 0.1);
                if (LM.pin > 0) { lr = mix(lr, 0.2, LM.pin); lg = mix(lg, 0.075, LM.pin); lb = mix(lb, 0.03, LM.pin); }
              }
              lr *= sh; lg *= sh; lb *= sh;
            }
            // 絵具
            const nl = Math.hypot(gxh, gyh, 1);
            const ndl = (-gxh * LD[0] - gyh * LD[1] + LD[2]) / nl / LD[2];
            const pshade = (0.5 + 0.5 * Math.max(0, ndl)) * (0.96 + 0.04 * ptilt / 0.052) * (0.55 + 0.45 * sh);
            // 金泥・銀泥は鈍く光る
            const lx2 = LPOS[0] - X, ly2 = LPOS[1] - Y;
            const mspec = pm[i] > 0.001 ? 0.75 + 0.5 * Math.exp(-(((lx2 * lx2 + ly2 * ly2) / 4e5))) + Math.max(0, ndl - 1) * 0.6 : 0;
            const ps = pshade + (pm[i] / Math.max(A, 1e-4)) * (mspec - 0.9) * pshade;
            r = lr * (1 - A) + pr[i] * ps; g = lg * (1 - A) + pg[i] * ps; b = lb * (1 - A) + pb[i] * ps;
            const fs = foldShade(X);
            r *= fs; g *= fs; b *= fs;
          } else {
            // --- 表装 ---
            const gap = (() => { const k = Math.round(X / PANEL); return k >= 1 && k <= 5 && Math.abs(X - k * PANEL) < 0.06 && dOut <= EXT; })();
            if (dOut <= KOBERI) {
              const w = 0.9 + 0.1 * Math.sin(X * 150) * Math.sin(Y * 150) + (vnoise(X * 30, Y * 30, 81) - 0.5) * 0.08;
              const edge = sstep(0, 0.07, dOut) * sstep(0, 0.07, KOBERI - dOut);
              r = cKoberi[0] * w * (0.62 + 0.38 * edge); g = cKoberi[1] * w * (0.62 + 0.38 * edge); b = cKoberi[2] * w * (0.62 + 0.38 * edge);
              r *= 0.8; g *= 0.8; b *= 0.8;
            } else if (dOut <= KOBERI + OBERI) {
              // 大縁:紺地に金糸の七宝繋ぎ
              const a = 1.36, R = a * 0.7071;
              const ux = X / a, uy = Y / a;
              const ix = Math.round(ux), iy = Math.round(uy);
              let line = 0;
              for (let dj = -1; dj <= 1; dj++) for (let di = -1; di <= 1; di++) {
                const d = Math.hypot((ux - ix - di) * a, (uy - iy - dj) * a);
                line = Math.max(line, 1 - sstep(0.035, 0.085, Math.abs(d - R)));
              }
              const cxm = (ux - Math.floor(ux) - 0.5) * a, cym = (uy - Math.floor(uy) - 0.5) * a;
              const dia = 1 - sstep(0.09, 0.14, Math.abs(cxm) + Math.abs(cym));
              const dn = 1 - sstep(0.05, 0.09, Math.hypot((ux - ix) * a, (uy - iy) * a));
              const pat = Math.max(line * 0.9, dia, dn);
              const twill = 0.86 + 0.14 * Math.sin((X + Y) * 95) + (vnoise(X * 40, Y * 40, 82) - 0.5) * 0.12;
              const rib = 0.78 + 0.22 * Math.sin(Y * 190 + Math.sin(X * 60) * 0.6);
              const lightG = 0.82 + 0.25 * Math.exp(-((X - 120) * (X - 120) + (Y + 40) * (Y + 40)) / 60000);
              r = mix(cNavy[0] * twill, cThread[0] * rib, pat) * lightG;
              g = mix(cNavy[1] * twill, cThread[1] * rib, pat) * lightG;
              b = mix(cNavy[2] * twill, cThread[2] * rib, pat) * lightG;
              const inner = sstep(0, 0.12, dOut - KOBERI) * 0.3 + 0.7;
              r *= inner; g *= inner; b *= inner;
            } else if (dOut <= EXT) {
              // 椽:黒漆。丸みに沿って映り込みが走る
              const q = (dOut - KOBERI - OBERI) / FUCHI; // 0 内側 → 1 外側
              const horiz = Math.max(-Y, Y - PH) >= Math.max(-X, X - PW);
              const top = horiz ? Y < 0 : X < 0;
              const pos = top ? 1 - q : q; // 光の来る側からの位置
              const gl = Math.exp(-((pos - 0.3) * (pos - 0.3)) / 0.004) * 0.085 + Math.exp(-((pos - 0.3) * (pos - 0.3)) / 0.1) * 0.012;
              const wob = 0.8 + 0.4 * vnoise((horiz ? X : Y) * 0.05, 3.3, 83);
              const bev = sstep(0, 0.12, q) * sstep(0, 0.1, 1 - q);
              const v = (0.003 + gl * wob * (top ? 1 : 0.4)) * (0.35 + 0.65 * bev);
              r = cLacq[0] + v * 1.0; g = cLacq[1] + v * 0.97; b = cLacq[2] + v * 0.92;
              const f = hooks.fitting ? hooks.fitting(X, Y, q) : 0;
              if (f > 0) {
                const sat = 0.5 + 0.5 * vnoise(X * 60, Y * 60, 84);
                const I = (0.36 + 0.5 * Math.exp(-((pos - 0.32) * (pos - 0.32)) / 0.08) * (top ? 1 : 0.6)) * (0.9 + 0.1 * sat);
                const fr = cFit[0] * I, fg = cFit[1] * I * (0.9 + 0.1 * I), fb = cFit[2] * I * (0.7 + 0.3 * I);
                const k = Math.min(1, f);
                const eng = f > 2 ? 1.3 : f > 1 ? 0.6 : 1; // 彫りの線・鋲の頭
                r = mix(r, fr * eng, k); g = mix(g, fg * eng, k); b = mix(b, fb * eng, k);
              }
            } else {
              // 背景:暗い壁。右下へ柔らかい影
              const d2 = Math.max(-(X - 1.2), X - 1.2 - PW, -(Y - 1.6), Y - 1.6 - PH) - EXT;
              const shd = 1 - 0.7 * Math.exp(-Math.max(0, d2) / 1.6);
              const nz = 0.92 + 0.16 * fbm(X * 0.5, Y * 0.5, 3, 85);
              r = cBG[0] * nz * shd; g = cBG[1] * nz * shd; b = cBG[2] * nz * shd;
            }
            if (gap) { r *= 0.15; g *= 0.15; b *= 0.15; }
          }
          // 肩をやわらかく丸めて sRGB へ
          const o4 = ((y / step) * W2 + x / step) * 4;
          rgba[o4] = enc(r); rgba[o4 + 1] = enc(g); rgba[o4 + 2] = enc(b); rgba[o4 + 3] = 255;
        }
      }
      return { w: W2, h: Math.ceil(H / step) };
    };

    // 灯りを動かす表示のために、光に依らない分を画像に焼いておく
    E.bake = function () {
      const ht = heights();
      const A = new Uint8ClampedArray(n * 4), Bm = new Uint8ClampedArray(n * 4), Cm = new Uint8ClampedArray(n * 4), Dm = new Uint8ClampedArray(n * 4);
      for (let y = cy0; y <= cy1; y++) {
        const Y = V[1] + (y + 0.5) / S;
        for (let x = cx0; x <= cx1; x++) {
          const i = y * W + x, o = i * 4, X = V[0] + (x + 0.5) / S;
          const xl = x > 0 ? i - 1 : i, xr = x < W - 1 ? i + 1 : i, yu = y > 0 ? i - W : i, yd = y < H - 1 ? i + W : i;
          const gxh = (ht[xr] - ht[xl]) * S * 0.5, gyh = (ht[yd] - ht[yu]) * S * 0.5;
          A[o] = Math.sqrt(Math.max(0, pr[i])) * 255; A[o + 1] = Math.sqrt(Math.max(0, pg[i])) * 255; A[o + 2] = Math.sqrt(Math.max(0, pb[i])) * 255; A[o + 3] = pa[i] * 255;
          Bm[o] = (0.5 + gxh * 0.5) * 255; Bm[o + 1] = (0.5 + gyh * 0.5) * 255; Bm[o + 2] = pm[i] * 255; Bm[o + 3] = castShadow(ht, x, y, i) * 255;
          if (pa[i] < 0.995) {
            leafMat(X, Y);
            if (LM.kind === 1) { Cm[o] = Math.sqrt(LM.sr) * 255; Cm[o + 1] = Math.sqrt(LM.sg) * 255; Cm[o + 2] = Math.sqrt(LM.sb) * 255; Cm[o + 3] = 128; }
            else { Cm[o] = (LM.tone / 1.3) * 255; Cm[o + 1] = (LM.red / 1.5) * 255; Cm[o + 2] = LM.pin * 255; Cm[o + 3] = LM.kind === 2 ? 255 : 0; }
            Dm[o] = (0.5 + LM.tx / 0.24) * 255; Dm[o + 1] = (0.5 + LM.ty / 0.24) * 255;
          } else { Dm[o] = 128; Dm[o + 1] = 128; Cm[o] = 196; }
          Dm[o + 3] = 255;
        }
      }
      return { A, B: Bm, C: Cm, D: Dm, w: W, h: H };
    };
    E.light = { LPOS, VPOS, LD, SIG2, GOLD };
    function enc(c) {
      if (c > 0.82) c = 0.82 + 0.18 * (1 - Math.exp(-(c - 0.82) / 0.18));
      if (c < 0) c = 0;
      const v = c <= 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
      return (v * 255 + 0.5) | 0;
    }
    return E;
  };
});
if (typeof module !== 'undefined') module.exports = globalThis.BYOBU;