Claude Opus 5.5
sumi.js
/* 水墨画「土谷大地」
* 紙・墨・筆をすべて JavaScript で計算して描く。画像素材・外部ライブラリなし。
* 設計座標は 2000×3000、scale で実ピクセルに写す。
*/
(function (G) {
'use strict';
const DW = 2000, DH = 3000;
const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
const lerp = (a, b, t) => a + (b - a) * t;
const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); };
function rng(seed) {
let a = seed >>> 0;
const r = () => {
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;
};
r.r = (lo, hi) => lo + (hi - lo) * r();
return r;
}
function hash(ix, iy, sd) {
let h = Math.imul(ix, 0x27d4eb2d) ^ Math.imul(iy, 0x165667b1) ^ Math.imul(sd | 0, 0x9e3779b1);
h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
return ((h ^ (h >>> 16)) >>> 0) * 2.3283064365386963e-10;
}
function vn(x, y, sd) {
const ix = Math.floor(x), iy = Math.floor(y), fx = x - ix, fy = y - iy;
const ux = fx * fx * (3 - 2 * fx), uy = fy * fy * (3 - 2 * fy);
const a = hash(ix, iy, sd), b = hash(ix + 1, iy, sd), c = hash(ix, iy + 1, sd), d = hash(ix + 1, iy + 1, sd);
return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
}
function fbm(x, y, oct, sd) {
let v = 0, a = 0.5, n = 0;
for (let o = 0; o < oct; o++) {
v += a * vn(x, y, sd + o * 17); n += a; a *= 0.5;
const nx = 1.6 * x - 1.2 * y + 5.3; y = 1.2 * x + 1.6 * y + 1.7; x = nx;
}
return v / n;
}
const fbm1 = (x, oct, sd) => fbm(x, 0.37, oct, sd);
function ridged1(x, oct, sd) {
let v = 0, a = 0.5, n = 0;
for (let o = 0; o < oct; o++) {
const t = 1 - Math.abs(2 * vn(x, 0.5 + o, sd + o * 13) - 1);
v += a * t * t; n += a; a *= 0.5; x = x * 2.1 + 3.7;
}
return v / n;
}
// 峰の並びから稜線関数をつくる。pk = [中心x, 高さ, 左幅, 右幅]
function ridgeOf(base, pk, crag, sd, o) {
o = o || {};
const warp = o.warp == null ? 120 : o.warp, tk = o.terrace == null ? 24 : o.terrace;
let hm = 0; for (const p of pk) hm = Math.max(hm, p[1]);
return (x) => {
const xw = x + (fbm1(x * 0.0026, 3, sd + 3) - 0.5) * warp + (fbm1(x * 0.016, 2, sd + 4) - 0.5) * warp * 0.3;
let e = 0;
for (const p of pk) {
const a = (xw - p[0]) / (xw < p[0] ? p[2] : p[3]);
const h = p[1] * Math.exp(-Math.pow(Math.abs(a), p[4] || 2));
e += h * h * h * h;
}
e = Math.sqrt(Math.sqrt(e));
e += tk * Math.sin(e * 0.02 + sd * 1.7) * Math.min(1, e / 120); // 肩と崖の段
const k = clamp(e / hm, 0, 1);
const c = ridged1(x * 0.016, 4, sd + 77) - 0.5;
const n = fbm1(x * 0.007, 3, sd + 1) - 0.5;
return base - e * (1 + 0.18 * n) - crag * c * (0.3 + 0.7 * k);
};
}
// 折れ点をなだらかにつないだ土坡の稜線
function curveOf(pts, rough, sd) {
return (x) => {
let y = pts[pts.length - 1][1];
if (x <= pts[0][0]) y = pts[0][1];
else for (let i = 0; i < pts.length - 1; i++) {
if (x <= pts[i + 1][0]) { y = lerp(pts[i][1], pts[i + 1][1], sstep(pts[i][0], pts[i + 1][0], x)); break; }
}
return y + (ridged1(x * 0.02, 3, sd) - 0.5) * rough + (fbm1(x * 0.006, 3, sd + 5) - 0.5) * rough * 1.6;
};
}
function create(opt) {
opt = opt || {};
const s = opt.scale || 1;
const W = Math.round(DW * s), H = Math.round(DH * s), N = W * H;
const R = rng(opt.seed == null ? 1012 : opt.seed);
const inkC = new Float32Array(N); // 青墨(遠景寄り)
const inkW = new Float32Array(N); // 茶墨(近景寄り)
const tmp = new Float32Array(N); // 筆ひとまとまりの含み
const red = new Float32Array(N); // 朱
const grain = new Float32Array(N); // 紙の目
const paper = new Float32Array(N); // 紙の明るさ
const age = new Float32Array(N); // 紙の焼け
const scratch = new Float32Array(Math.max(W, H) + 8);
let bx0 = W, by0 = H, bx1 = -1, by1 = -1;
const mark = (x0, y0, x1, y1) => {
if (x0 < bx0) bx0 = x0; if (y0 < by0) by0 = y0; if (x1 > bx1) bx1 = x1; if (y1 > by1) by1 = y1;
};
function splat(buf, x, y, v) {
const ix = Math.floor(x), iy = Math.floor(y);
if (ix < 0 || iy < 0 || ix >= W - 1 || iy >= H - 1) return;
const fx = x - ix, fy = y - iy, i = iy * W + ix;
buf[i] += v * (1 - fx) * (1 - fy); buf[i + 1] += v * fx * (1 - fy);
buf[i + W] += v * (1 - fx) * fy; buf[i + W + 1] += v * fx * fy;
}
// ---- 筆 -------------------------------------------------------------
// 点列 [x, y, 幅] を Catmull-Rom でなぞる
function sample(pts, step) {
const X = [], Y = [], Wd = [], L = [];
let len = 0, lx = 0, ly = 0;
const n = pts.length;
for (let i = 0; i < n - 1; i++) {
const p0 = pts[Math.max(i - 1, 0)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[Math.min(i + 2, n - 1)];
const m = Math.max(1, Math.ceil(Math.hypot(p2[0] - p1[0], p2[1] - p1[1]) / step));
for (let j = i === 0 ? 0 : 1; j <= m; j++) {
const t = j / m, t2 = t * t, t3 = t2 * t;
const a = -0.5 * t3 + t2 - 0.5 * t, b = 1.5 * t3 - 2.5 * t2 + 1, c = -1.5 * t3 + 2 * t2 + 0.5 * t, d = 0.5 * t3 - 0.5 * t2;
const x = a * p0[0] + b * p1[0] + c * p2[0] + d * p3[0];
const y = a * p0[1] + b * p1[1] + c * p2[1] + d * p3[1];
const w = Math.max(0.3, a * p0[2] + b * p1[2] + c * p2[2] + d * p3[2]);
if (X.length) len += Math.hypot(x - lx, y - ly);
X.push(x); Y.push(y); Wd.push(w); L.push(len); lx = x; ly = y;
}
}
return { X, Y, Wd, L, n: X.length, len };
}
// 毛の一本一本を走らせる。dry が大きいほど掠れ、ramp で筆尾ほど渇く。
// nib = [角度, 扁平率] を渡すと、斜めに寝かせた穂先(書の筆)になる
function stroke(pts, o) {
o = o || {};
if (pts.length < 2) return;
const P = sample(pts, 0.8 / s), n = P.n;
if (n < 2) return;
let mw = 0, x0 = 1e9, y0 = 1e9, x1 = -1e9, y1 = -1e9;
for (let k = 0; k < n; k++) {
if (P.Wd[k] > mw) mw = P.Wd[k];
if (P.X[k] < x0) x0 = P.X[k]; if (P.X[k] > x1) x1 = P.X[k];
if (P.Y[k] < y0) y0 = P.Y[k]; if (P.Y[k] > y1) y1 = P.Y[k];
}
const pad = mw * 0.5 + 2;
mark(Math.max(0, Math.floor((x0 - pad) * s)), Math.max(0, Math.floor((y0 - pad) * s)),
Math.min(W - 1, Math.ceil((x1 + pad) * s)), Math.min(H - 1, Math.ceil((y1 + pad) * s)));
const nib = o.nib, nb = Math.max(1, Math.round(mw * s * (o.bd || (nib ? 2.6 : 1.05))));
const dry = o.dry || 0, ramp = o.ramp == null ? 0.5 : o.ramp, v0 = o.v == null ? 1 : o.v;
const nx = new Float32Array(n), ny = new Float32Array(n);
if (!nib) for (let k = 0; k < n; k++) {
const a = Math.max(0, k - 3), b = Math.min(n - 1, k + 3);
const dx = P.X[b] - P.X[a], dy = P.Y[b] - P.Y[a], l = Math.hypot(dx, dy) || 1;
nx[k] = -dy / l; ny[k] = dx / l;
}
const ca = nib ? Math.cos(nib[0]) : 0, sa = nib ? Math.sin(nib[0]) : 0, fl = nib ? nib[1] : 0;
const L = P.len || 1, sd = (R() * 1e9) | 0;
for (let b = 0; b < nb; b++) {
let off = 0, ox = 0, oy = 0;
if (nib) {
const a = R() * 6.2832, rr = R() < 0.3 ? R.r(0.93, 1) : Math.sqrt(R());
off = Math.cos(a) * rr; const pv = Math.sin(a) * rr * fl;
ox = off * ca - pv * sa; oy = off * sa + pv * ca;
} else off = nb === 1 ? 0 : ((b + 0.5 + (R() - 0.5) * 0.7) / nb) * 2 - 1;
const load = 0.55 + 0.45 * R(), cross = off * mw * 0.24;
for (let k = 0; k < n; k++) {
const hw = P.Wd[k] * 0.5;
const x = (nib ? P.X[k] + ox * hw : P.X[k] + nx[k] * off * hw) * s;
const y = (nib ? P.Y[k] + oy * hw : P.Y[k] + ny[k] * off * hw) * s;
let a = nib ? 1 : Math.min(1, P.Wd[k] / mw * 1.15);
if (dry > 0) {
const ix = x | 0, iy = y | 0;
if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue;
const dn = vn(P.L[k] * 0.028, cross, sd) * 0.7 + vn(P.L[k] * 0.09, cross * 2.3, sd + 1) * 0.3;
const th = dry * (1 - ramp + ramp * P.L[k] / L);
a *= sstep(th - 0.1, th + 0.1, dn * 0.68 + grain[iy * W + ix] * 0.32);
if (a <= 0) continue;
}
splat(tmp, x, y, v0 * load * a);
}
}
}
// 滲んだ点
function dab(cx, cy, rx, ry, v, hard) {
hard = hard == null ? 0.25 : hard;
const X = cx * s, Y = cy * s, RX = Math.max(0.7, rx * s), RY = Math.max(0.7, ry * s);
const xa = Math.max(0, Math.floor(X - RX - 1)), xb = Math.min(W - 1, Math.ceil(X + RX + 1));
const ya = Math.max(0, Math.floor(Y - RY - 1)), yb = Math.min(H - 1, Math.ceil(Y + RY + 1));
if (xa > xb || ya > yb) return;
mark(xa, ya, xb, yb);
for (let py = ya; py <= yb; py++) {
const dy = (py - Y) / RY;
for (let px = xa; px <= xb; px++) {
const dx = (px - X) / RX, q = dx * dx + dy * dy;
if (q < 1) tmp[py * W + px] += v * (1 - sstep(hard, 1, Math.sqrt(q)));
}
}
}
function boxBlur(buf, x0, y0, x1, y1, r) {
r = Math.max(1, Math.round(r));
const inv = 1 / (2 * r + 1);
for (let y = y0; y <= y1; y++) {
const row = y * W; let acc = 0;
for (let x = x0; x <= Math.min(x1, x0 + r); x++) acc += buf[row + x];
for (let x = x0; x <= x1; x++) {
scratch[x - x0] = acc * inv;
const a = x - r, b = x + r + 1;
if (a >= x0) acc -= buf[row + a];
if (b <= x1) acc += buf[row + b];
}
for (let x = x0; x <= x1; x++) buf[row + x] = scratch[x - x0];
}
for (let x = x0; x <= x1; x++) {
let acc = 0;
for (let y = y0; y <= Math.min(y1, y0 + r); y++) acc += buf[y * W + x];
for (let y = y0; y <= y1; y++) {
scratch[y - y0] = acc * inv;
const a = y - r, b = y + r + 1;
if (a >= y0) acc -= buf[a * W + x];
if (b <= y1) acc += buf[b * W + x];
}
for (let y = y0; y <= y1; y++) buf[y * W + x] = scratch[y - y0];
}
}
// tmp に溜めた筆を紙へ移す。重なりは飽和させる
function flush(D, o) {
o = o || {};
if (bx1 < bx0) return;
const br = o.blur ? o.blur * s : 0, pad = br ? Math.ceil(br * 2.5) + 1 : 0;
const x0 = Math.max(0, bx0 - pad), y0 = Math.max(0, by0 - pad), x1 = Math.min(W - 1, bx1 + pad), y1 = Math.min(H - 1, by1 + pad);
if (br >= 0.5) { boxBlur(tmp, x0, y0, x1, y1, br); boxBlur(tmp, x0, y0, x1, y1, br); }
const warm = o.warm == null ? 0.5 : o.warm, k = o.k || 2.2, gk = o.grain || 0;
for (let y = y0; y <= y1; y++) {
for (let i = y * W + x0, e = y * W + x1; i <= e; i++) {
const v = tmp[i];
if (v === 0) continue;
tmp[i] = 0;
let m = 1 - Math.exp(-k * v);
if (gk) m *= 1 - gk * (1 - grain[i]);
if (o.lift) { const f = 1 - Math.min(1, m * D); inkC[i] *= f; inkW[i] *= f; }
else { inkC[i] += D * m * (1 - warm); inkW[i] += D * m * warm; }
}
}
bx0 = W; by0 = H; bx1 = -1; by1 = -1;
}
// ---- 紙 -------------------------------------------------------------
function makePaper() {
for (let py = 0, i = 0; py < H; py++) {
const y = py / s, ey = Math.min(y, DH - y) / DH;
for (let px = 0; px < W; px++, i++) {
const x = px / s, ex = Math.min(x, DW - x) / DW;
const g = vn(x * 0.42, y * 0.42, 11) * 0.55 + vn(x * 1.07, y * 0.93, 12) * 0.45;
grain[i] = g;
const cloud = fbm(x * 0.0019, y * 0.0019, 4, 21) - 0.5;
const mott = fbm(x * 0.028, y * 0.028, 3, 22) - 0.5;
paper[i] = 1 + cloud * 0.03 + mott * 0.018 + (g - 0.5) * 0.028;
const edge = 1 - sstep(0, 0.09, Math.min(ex * 1.5, ey));
age[i] = clamp(0.25 + cloud * 0.9 + edge * 0.55 + mott * 0.2, 0, 1);
}
}
// 漉き込まれた繊維
const nf = 5200;
for (let k = 0; k < nf; k++) {
let x = R() * DW, y = R() * DH, a = R() * Math.PI * 2;
const len = R.r(16, 100), cv = R.r(-0.03, 0.03), v = (R() < 0.55 ? 1 : -1) * R.r(0.01, 0.034), st = 0.8 / s;
for (let t = 0; t < len; t += st) {
splat(paper, x * s, y * s, v * 0.9);
x += Math.cos(a) * st; y += Math.sin(a) * st; a += cv * st;
}
}
}
// ---- 空と月 ---------------------------------------------------------
const MOON = [1318, 598, 74];
function sky() {
const [mx, my, mr] = MOON;
const yb = Math.min(H, Math.round(1500 * s));
for (let py = 0; py < yb; py++) {
const y = py / s, fall = 1 - sstep(480, 1400, y);
for (let px = 0; px < W; px++) {
const x = px / s, i = py * W + px;
const n = fbm(x * 0.0011, y * 0.0017, 4, 41);
let v = (0.058 * sstep(0.36, 0.8, n) + 0.012) * fall;
const dm = Math.hypot(x - mx, y - my);
v += 0.056 * Math.exp(-Math.pow((dm - mr) / 170, 2)) * (0.55 + 0.9 * n); // 月のまわりを烘す
v *= 0.06 + 0.94 * sstep(mr - 1.3, mr + 1.3, dm + (vn(x * 0.21, y * 0.21, 43) - 0.5) * 1.6);
inkC[i] += v;
}
}
}
// ---- 山 -------------------------------------------------------------
function mountain(o) {
const sd = o.sd, D = o.D, warm = o.warm || 0, occ = o.occ == null ? 1 : o.occ, soft = o.soft || 0.8;
const X0 = Math.max(0, Math.floor((o.x0 == null ? 0 : o.x0) * s)), X1 = Math.min(W - 1, Math.ceil((o.x1 == null ? DW : o.x1) * s));
const n = X1 - X0 + 1, ry = new Float32Array(n), g0 = new Float32Array(n), gs = new Float32Array(n), gw = new Float32Array(n);
let top = 1e9;
for (let c = 0; c < n; c++) { ry[c] = o.ridge((X0 + c) / s); if (ry[c] < top) top = ry[c]; }
const kw = Math.max(1, Math.round(26 * s)), kw2 = Math.max(1, Math.round(80 * s));
for (let c = 0; c < n; c++) {
const a = Math.max(0, c - kw), b = Math.min(n - 1, c + kw);
g0[c] = clamp((ry[b] - ry[a]) / ((b - a) / s), -2.4, 2.4);
}
const smooth = (src, dst, w) => {
for (let c = 0; c < n; c++) {
let acc = 0, m = 0;
for (let j = Math.max(0, c - w); j <= Math.min(n - 1, c + w); j += 2) { acc += src[j]; m++; }
dst[c] = acc / m;
}
};
smooth(g0, gs, kw); smooth(gs, gw, kw2);
const mv = o.mistVar == null ? 170 : o.mistVar;
const ml = (x) => o.mist + (fbm1(x * 0.0023, 3, sd + 8) - 0.5) * mv;
const span = Math.max(1, o.mist - top), minDep = o.minDepth || 150;
// 襞:落ちる筋に沿って伸ばした起伏を、左からの光で起こす
const shade = (x, y, d, g) => {
const q = x - d * g * 0.4 + (fbm(x * 0.004, y * 0.004, 2, sd + 6) - 0.5) * 70, yy = d * 0.0034 + 3.1;
return sstep(-0.03, 0.055, fbm((q + 5) * 0.0085, yy, 4, sd + 3) - fbm((q - 5) * 0.0085, yy, 4, sd + 3));
};
const rimK = o.rimK == null ? 0.42 : o.rimK, rim = o.rim || 22;
for (let c = 0; c < n; c++) {
const px = X0 + c, x = px / s, r = ry[c], g = clamp(gw[c], -1.5, 1.5), mlx = ml(x);
const hf = clamp((mlx - r) / span, 0, 1);
const pres = sstep(0, 0.1, hf);
if (pres <= 0) continue;
const dep = Math.max(minDep, mlx - r);
const Dl = D * (0.3 + 0.7 * hf) * pres, oc = occ * pres;
const side = 0.72 + 0.5 * sstep(-0.7, 0.9, gs[c]); // 右へ落ちる斜面は陰
for (let py = Math.max(0, Math.floor((r - 3) * s)); py < H; py++) {
const y = py / s, d = y - r, i = py * W + px;
let cov = 1;
if (d < 4) {
const wob = (vn(x * 0.6, y * 0.6, sd) - 0.5) * 3;
cov = clamp((d + wob) * s * soft + 0.5, 0, 1);
if (cov <= 0) continue;
}
if (oc) { const k = 1 - cov * oc; inkC[i] *= k; inkW[i] *= k; }
const u = d / dep;
if (u >= 1) continue;
const sh = shade(x, y, d, g);
const lg = fbm(x * 0.0035, y * 0.0035, 3, sd + 5);
let v = Dl * Math.pow(1 - u, 2.1) * (0.4 + 0.55 * sh * (1 - 0.7 * u)) * (0.58 + 0.84 * lg) * side;
v += Dl * rimK * Math.exp(-d / rim) * (0.5 + 0.6 * sh);
v *= cov;
inkC[i] += v * (1 - warm); inkW[i] += v * warm;
}
}
const col = (x) => clamp(Math.round(x * s) - X0, 0, n - 1);
o.r = (x) => ry[col(x)];
o.g = (x) => gs[col(x)];
o.gw = (x) => gw[col(x)];
o.ml = ml;
o.shade = (x, y) => shade(x, y, y - ry[col(x)], clamp(gw[col(x)], -1.5, 1.5));
o.top = top; o.span = span;
o.xa = X0 / s; o.xb = X1 / s;
return o;
}
// 稜線の輪郭。ところどころ筆を離す
function contour(o, wk, dk) {
let seg = [];
const draw = () => {
if (seg.length > 3) { stroke(seg, { dry: 0.44, ramp: 0.3 }); flush(o.D * dk, { warm: o.warm, grain: 0.35 }); }
seg = [];
};
for (let x = o.xa; x <= o.xb; x += 5) {
const r = o.r(x), hf = (o.ml(x) - r) / o.span;
const nz = vn(x * 0.011, 5.5, o.sd + 31);
const sh = sstep(-0.5, 1.1, o.g(x)), gap = 0.5 - 0.24 * sh;
if (nz < gap || hf < 0.12) { draw(); continue; }
seg.push([x, r + 1, (0.5 + 4.6 * Math.pow(sstep(gap, 0.92, nz), 1.7)) * wk * (0.5 + 0.5 * hf) * (0.55 + 0.75 * sh)]);
}
draw();
}
// 落ちる筋をたどる一筆。稜線の外へは出ない
function fallLine(o, x, y, L, w0, bias, wig) {
const pts = [];
for (let t = 0; t <= L; t += 12) {
pts.push([x, y, lerp(w0, 0.7, Math.pow(t / L, 0.8))]);
x += (clamp(o.gw(x) * 0.42, -0.7, 0.7) + bias * (1 - t / L)) * 12 + (vn(t * 0.03, wig, o.sd + 19) - 0.5) * 11;
y += 12;
const r = o.r(x) + 5;
if (y < r) y = r;
}
return pts;
}
// 峰の頂から下ろす骨の線
function spines(o, wk, dk) {
for (let x = o.xa + 40; x < o.xb - 40; x += 8) {
const r = o.r(x);
let isTop = true;
for (let dx = -64; dx <= 64 && isTop; dx += 8) if (dx && o.r(x + dx) < r) isTop = false;
if (!isTop) continue;
const dep = o.ml(x) - r;
if (dep < 220) continue;
for (const sg of [-1, 1, 0.2]) {
if (R() < 0.2) continue;
const pts = fallLine(o, x + sg * R.r(3, 12), r + R.r(3, 12), dep * R.r(0.2, 0.46), R.r(2.4, 4.2) * wk, sg * R.r(0.2, 0.55), R() * 99);
if (pts.length < 3) continue;
stroke(pts, { dry: 0.4, ramp: 0.7 });
flush(o.D * dk * R.r(0.7, 1.1), { warm: o.warm, grain: 0.4 });
}
}
}
// 陰の面に側筆を寝かせて擦る
function shadeStrokes(o, count, wk) {
for (let k = 0; k < count; k++) {
const xs = R.r(o.xa + 10, o.xb - 10), r = o.r(xs), dep = o.ml(xs) - r;
if (dep < 190) continue;
const d0 = dep * (0.03 + Math.pow(R(), 1.3) * 0.5), y = r + d0;
if (R() > o.shade(xs, y) * 0.95 + 0.03) continue;
const L = Math.min(dep * R.r(0.1, 0.3), 250);
const pts = fallLine(o, xs, y, L, R.r(9, 26) * wk, 0, R() * 99);
if (pts.length < 3) continue;
pts[0][2] *= 0.12; pts[1][2] *= 0.6;
stroke(pts, { dry: R.r(0.6, 0.76), ramp: 0.25 });
flush(o.D * R.r(0.2, 0.46) * Math.pow(1 - (d0 + L * 0.5) / dep, 1.2), { warm: o.warm, grain: 0.6 });
}
}
// 披麻皴:襞に沿う細い渇筆
function cun(o, count, wk) {
for (let k = 0; k < count; k++) {
const xs = R.r(o.xa + 10, o.xb - 10), r = o.r(xs), dep = o.ml(xs) - r;
if (dep < 170) continue;
const d0 = dep * (0.02 + Math.pow(R(), 1.6) * 0.42), L = Math.min(dep * R.r(0.07, 0.26), 200);
const pts = fallLine(o, xs, r + d0, L, R.r(1.3, 3.4) * wk, 0, R() * 99);
if (pts.length < 3) continue;
stroke(pts, { dry: 0.5, ramp: 0.5 });
flush(o.D * R.r(0.25, 0.62) * Math.pow(1 - (d0 + L * 0.5) / dep, 1.2), { warm: o.warm, grain: 0.5 });
}
}
// 米点:横に寝かせた湿った点を稜線ぞいに重ねる
function miDots(o, count, size, dk) {
let m = 0;
for (let k = 0; k < count; k++) {
const xs = R.r(o.xa + 6, o.xb - 6), r = o.r(xs), dep = o.ml(xs) - r;
if (dep < 150) continue;
if (vn(xs * 0.008, 2.2, o.sd + 61) < 0.44) continue;
const d = dep * Math.pow(R(), 2.6) * 0.4 + 2;
const rx = size * R.r(0.5, 1.4), fade = 1 - d / dep;
dab(xs, r + d, rx, rx * R.r(0.26, 0.4), R.r(0.35, 0.9) * fade * fade);
if (++m % 60 === 0) flush(o.D * dk, { warm: o.warm, blur: 0.7 });
}
flush(o.D * dk, { warm: o.warm, blur: 0.7 });
}
// 苔点:稜のきわに濃墨の小点を群れさせる
function moss(o, count, size, dk) {
let m = 0;
for (let k = 0; k < count; k++) {
const xs = R.r(o.xa + 6, o.xb - 6), r = o.r(xs), dep = o.ml(xs) - r;
if (dep < 150 || vn(xs * 0.012, 2.2, o.sd + 61) < 0.5) continue;
const d = Math.max(0, Math.min(dep * 0.3, 90) * Math.pow(R(), 2.6) + R.r(-2, 6)), rr = size * R.r(0.5, 1.2);
dab(xs, r + d, rr * R.r(0.9, 1.6), rr * R.r(0.6, 0.9), R.r(0.5, 1.1) * (1 - d / dep), 0.35);
if (++m % 80 === 0) flush(o.D * dk, { warm: o.warm, blur: 0.4 });
}
flush(o.D * dk, { warm: o.warm, blur: 0.4 });
}
// 稜線の小さな樹
function ridgeTrees(o, count, size, dk) {
for (let k = 0; k < count; k++) {
const x = R.r(o.xa + 10, o.xb - 10), r = o.r(x), hf = (o.ml(x) - r) / o.span;
if (hf < 0.22 || vn(x * 0.013, 8.1, o.sd + 71) < 0.47 || Math.abs(o.g(x)) > 1.5) continue;
const h = size * R.r(0.55, 1.25), lean = R.r(-0.12, 0.12) * h;
stroke([[x, r + 2, 1.7], [x + lean * 0.5, r - h * 0.5, 1.3], [x + lean, r - h, 0.7]]);
const nt = 3 + ((R() * 3) | 0);
for (let j = 0; j < nt; j++) {
const t = (j + 0.6) / (nt + 0.4), w = h * 0.36 * (1 - t * 0.75);
dab(x + lean * (0.35 + 0.65 * t) + R.r(-1.5, 1.5), r - h * (0.32 + 0.68 * t), w, Math.max(1.1, h * 0.085), 0.9, 0.2);
}
flush(o.D * dk * R.r(0.75, 1.15) * (0.5 + 0.5 * hf), { warm: o.warm, blur: 0.5 });
}
}
// 霞:すでに置いた墨を横たなびきに抜く
function mist(y0, y1, k, sd, xc, xw) {
const pa = Math.max(0, Math.floor(y0 * s)), pb = Math.min(H - 1, Math.ceil(y1 * s)), h = y1 - y0;
for (let py = pa; py <= pb; py++) {
const y = py / s, band = sstep(y0, y0 + h * 0.4, y) * (1 - sstep(y1 - h * 0.35, y1, y));
for (let px = 0; px < W; px++) {
const x = px / s, i = py * W + px;
let m = k * band * sstep(0.36, 0.6, fbm(x * 0.0012, y * 0.0052, 4, sd));
if (xw) m *= Math.exp(-Math.pow((x - xc) / xw, 2));
if (m > 0) { inkC[i] *= 1 - m; inkW[i] *= 1 - m; }
}
}
}
// ---- 平らな土地(洲) -----------------------------------------------
function spit(o) {
const dir = Math.sign(o.xb - o.xa), len = Math.abs(o.xb - o.xa), lo = Math.min(o.xa, o.xb), hi = Math.max(o.xa, o.xb);
const top = (x) => o.y + (fbm1(x * 0.006, 3, o.sd) - 0.5) * 18 + (o.tilt || 0) * (x - o.xa) * dir;
const thick = (x) => {
const t = Math.abs(x - o.xa) / len;
return o.th * (1 - Math.pow(t, 2.6)) * (0.62 + 0.76 * fbm1(x * 0.009, 2, o.sd + 4));
};
for (let px = Math.max(0, Math.floor(lo * s)); px <= Math.min(W - 1, Math.ceil(hi * s)); px++) {
const x = px / s, t = Math.abs(x - o.xa) / len, th = thick(x);
if (th < 0.6) continue;
const yt = top(x);
for (let py = Math.max(0, Math.floor((yt - 2) * s)); py <= Math.min(H - 1, Math.ceil((yt + th * 1.3) * s)); py++) {
const y = py / s, d = y - yt, i = py * W + px;
const cov = clamp((d + (vn(x * 0.5, y * 0.5, o.sd) - 0.5) * 2.4) * s * 0.7 + 0.5, 0, 1);
if (cov <= 0) continue;
// 下の縁は水に溶けず、溜まった墨がわずかに濃く乾く
const u = d / th, uu = u + (vn(x * 0.04, y * 0.15, o.sd + 6) - 0.5) * 0.34, inn = 1 - sstep(0.93, 1.05, uu);
if (inn <= 0) continue;
{ const k = 1 - cov * 0.96 * inn; inkC[i] *= k; inkW[i] *= k; }
const body = (1 - 0.66 * u + 0.2 * sstep(0.7, 0.97, uu)) * inn;
const tex = 0.5 + 0.8 * fbm(x * 0.0045, y * 0.055, 3, o.sd + 2);
const v = o.D * body * tex * cov * (0.55 + 0.45 * (1 - t));
inkC[i] += v * (1 - o.warm); inkW[i] += v * o.warm;
}
}
// 上端の線と横の渇筆
let seg = [];
const draw = () => { if (seg.length > 2) { stroke(seg, { dry: 0.36, ramp: 0.3 }); flush(o.D * 1.5, { warm: o.warm, grain: 0.4 }); } seg = []; };
for (let x = lo; x <= hi; x += 7) {
if (thick(x) < 2 || vn(x * 0.02, 1.5, o.sd + 8) < 0.26) { draw(); continue; }
seg.push([x, top(x) + 1, 1 + 2.6 * vn(x * 0.03, 3.3, o.sd + 9)]);
}
draw();
for (let k = 0; k < (o.hatch || 14); k++) {
const x = R.r(lo + 20, hi - 20), th = thick(x); if (th < 5) continue;
const y = top(x) + th * R.r(0.25, 1.3), l = R.r(40, 170);
stroke([[x - l / 2, y + R.r(-2, 2), 1.2], [x, y, R.r(2, 4.5)], [x + l / 2, y + R.r(-2, 2), 1]], { dry: 0.55, ramp: 0.3 });
flush(o.D * R.r(0.5, 1.1), { warm: o.warm, grain: 0.5 });
}
o.top = top; o.thick = thick;
return o;
}
function grass(x, y, n, h, D, warm) {
for (let j = 0; j < n; j++) {
const a = R.r(-0.55, 0.55), l = h * R.r(0.5, 1.15), bx = x + R.r(-h * 0.35, h * 0.35);
stroke([[bx, y, 1.9], [bx + Math.sin(a) * l * 0.5, y - l * 0.55, 1.3], [bx + Math.sin(a) * l * 1.25, y - l * Math.cos(a), 0.4]]);
}
flush(D, { warm });
}
// ---- 樹 -------------------------------------------------------------
function fan(r, cx, cy, rad, a0, spread, n, k) {
for (let j = 0; j < n; j++) {
const f = n === 1 ? 0.5 : (j + r() * 0.8 - 0.4) / (n - 1);
const a = a0 + spread * (f - 0.5), L = rad * (0.68 + 0.32 * r()) * (1 - 0.28 * Math.abs(2 * f - 1));
const ex = cx + Math.cos(a) * L, ey = cy + Math.sin(a) * L;
stroke([[cx, cy, 2.5 * k], [(cx + ex) / 2 + r.r(-1, 1) * k, (cy + ey) / 2 + r.r(-1, 1) * k, 1.7 * k], [ex, ey, 0.45 * k]]);
}
}
function pine(bx, by, h, lean, D, sd, warm) {
const r = rng(sd), k = h / 720, ph = r() * 6.28, kk = Math.max(0.5, k);
const tp = (t) => [bx + lean * h * Math.pow(t, 1.3) + Math.sin(t * 4.6 + ph) * h * 0.042 * (0.35 + t), by - h * t];
const tw = (t) => lerp(0.06 * h, 0.012 * h, Math.pow(t, 0.7));
const pts = [];
for (let i = 0; i <= 18; i++) { const t = i / 18, p = tp(t); pts.push([p[0], p[1], tw(t)]); }
// 根張り
for (const sg of [-1, 1]) {
const w = tw(0);
stroke([[bx + sg * w * 0.28, by - h * 0.035, w * 0.42], [bx + sg * w * 0.78, by + 3 * k, w * 0.26], [bx + sg * w * r.r(1.3, 1.9), by + r.r(8, 16) * k, 1.1]], { dry: 0.3 });
}
flush(D * 0.9, { warm, grain: 0.4 });
// 幹:淡墨の渇筆 → 輪郭 → 片陰 → 魚鱗皴
stroke(pts, { dry: 0.5, ramp: 0, v: 0.8 }); flush(D * 0.34, { warm, grain: 0.6 });
for (const sgn of [-1, 1]) {
stroke(pts.map((p, i) => [p[0] + sgn * p[2] * 0.47 + (vn(i * 1.9, sgn + 2, sd) - 0.5) * p[2] * 0.2, p[1], (1.3 + 3.2 * vn(i * 0.8, sgn, sd + 1)) * kk]), { dry: 0.36, ramp: 0.1 });
flush(D * 1.15, { warm, grain: 0.35 });
}
const shade = lean >= 0 ? 1 : -1;
stroke(pts.map((p) => [p[0] + shade * p[2] * 0.26, p[1], p[2] * 0.42]), { dry: 0.6, ramp: 0 });
flush(D * 0.7, { warm, grain: 0.6 });
let row = 0;
for (let t = 0.015; t < 0.86; t += 0.021 / Math.sqrt(kk)) {
const p = tp(t), w = tw(t), cols = w > 22 ? 3 : w > 11 ? 2 : 1;
row++;
for (let c = 0; c < cols; c++) {
if (r() < 0.32) continue;
const ox = ((c + 0.5 + (row % 2) * 0.5) / (cols + 0.5) - 0.5) * w * 0.82 + r.r(-1.5, 1.5), a = w * r.r(0.12, 0.2), oy = r.r(-3, 3) * k;
stroke([[p[0] + ox - a, p[1] + oy - a * 0.55, 0.9 * kk], [p[0] + ox - a * 0.35, p[1] + oy + a * 0.5, 1.9 * kk], [p[0] + ox + a * 0.9, p[1] + oy - a * 0.25, 0.7 * kk]], { dry: 0.25 });
}
}
flush(D * 0.72, { warm, grain: 0.35 });
// 枝ぶり
const nbr = 5 + (r() < 0.5 ? 1 : 0), side0 = lean >= 0 ? -1 : 1, tips = [];
const along = (bp, u) => {
const m = bp.length - 1, i0 = Math.min(m - 1, Math.floor(u * m)), f = clamp(u * m - i0, 0, 1.2);
return [lerp(bp[i0][0], bp[i0 + 1][0], f), lerp(bp[i0][1], bp[i0 + 1][1], f)];
};
for (let j = 0; j < nbr; j++) {
const t = clamp(lerp(0.4, 0.93, j / (nbr - 1)) + r.r(-0.03, 0.03), 0, 1), p = tp(t);
const side = (j % 2 ? -1 : 1) * side0;
const len = h * lerp(0.36, 0.14, (t - 0.4) / 0.53) * r.r(0.8, 1.2) * (side === side0 ? 1.15 : 0.86);
const up = r.r(0.1, 0.28), droop = r.r(0.1, 0.24), ph2 = r() * 9, bp = [];
for (let q = 0; q <= 8; q++) {
const u = q / 8;
bp.push([p[0] + side * len * (0.92 * u + 0.08 * u * u), p[1] - len * up * Math.sin(Math.min(1, u * 1.6) * 1.5708) + len * droop * u * u * u + Math.sin(u * 8 + ph2) * len * 0.022, lerp(tw(t) * 0.4, 1.3 * k + 0.6, Math.pow(u, 0.45))]);
}
stroke(bp, { dry: 0.3, ramp: 0.5 }); flush(D * 1.1, { warm, grain: 0.3 });
const nf = Math.max(3, Math.round(len / (27 * k)));
for (let q = 0; q < nf; q++) {
const f = q / (nf - 1), a = along(bp, lerp(0.26, 1.04, f)), mid = 1 - Math.abs(f * 2 - 1);
tips.push([a[0] + r.r(-6, 6) * k, a[1] - r.r(0, 7) * k, r.r(0.82, 1.12)]);
if (mid > 0.25 && r() < 0.85) tips.push([a[0] + r.r(-11, 11) * k, a[1] - (16 + 15 * mid) * k * r.r(0.8, 1.2), r.r(0.75, 1)]);
if (mid > 0.65 && r() < 0.6) tips.push([a[0] + r.r(-12, 12) * k, a[1] - 46 * k * r.r(0.85, 1.15), r.r(0.65, 0.85)]);
}
// 垂れる小枝
for (let q = 3; q <= 7; q += 2) {
if (r() < 0.45) continue;
const b = bp[q], l = len * r.r(0.07, 0.14);
stroke([[b[0], b[1], 1.8 * kk], [b[0] + side * l * 0.3, b[1] + l * 0.6, 1.2 * kk], [b[0] + side * l * 0.75, b[1] + l, 0.5]], { dry: 0.25 });
}
flush(D * 1.0, { warm });
}
const tt = tp(1);
for (let q = 0; q < 8; q++) tips.push([tt[0] + r.r(-50, 50) * k, tt[1] + r.r(-20, 22) * k, r.r(0.8, 1.1)]);
// 葉むら:淡墨を含ませ、薄い葉、濃い葉の順に重ねる
for (const t of tips) dab(t[0], t[1] - 13 * k, 32 * k, 16 * k, 0.5, 0.3);
flush(D * 0.1, { warm: warm * 0.7, blur: 1.6 * k });
const fans = (dk, prob, jit, sc) => {
let m = 0;
for (const t of tips) {
if (r() > prob) continue;
const rad = 42 * k * t[2] * sc * r.r(0.85, 1.15);
fan(r, t[0] + r.r(-jit, jit) * k, t[1] + r.r(-jit, jit) * k, rad, -Math.PI / 2 + r.r(-0.24, 0.24), r.r(2.4, 3.0), Math.round(r.r(12, 18)), kk);
if (++m % 6 === 0) flush(D * dk, { warm, k: 2.4 });
}
flush(D * dk, { warm, k: 2.4 });
};
fans(0.36, 0.55, 12, 1.12);
fans(1.1, 1, 0, 1);
}
// 点葉の雑木
function leafTree(x, y, h, D, warm, sd) {
const r = rng(sd), lean = r.r(-0.18, 0.18) * h;
stroke([[x, y, h * 0.06 + 1], [x + lean * 0.4, y - h * 0.45, h * 0.04 + 0.8], [x + lean, y - h * 0.85, 0.8]], { dry: 0.2 });
for (let j = 0; j < 3; j++) {
const t = r.r(0.4, 0.75), a = -Math.PI / 2 + (j % 2 ? 1 : -1) * r.r(0.5, 1.1), l = h * r.r(0.18, 0.32);
const px = x + lean * t, py = y - h * 0.85 * t;
stroke([[px, py, h * 0.025 + 0.8], [px + Math.cos(a) * l, py + Math.sin(a) * l, 0.6]]);
}
flush(D * 1.1, { warm });
const n = Math.round(18 + h * 0.5);
for (let j = 0; j < n; j++) {
const a = r() * Math.PI * 2, q = Math.sqrt(r()), rx = h * r.r(0.045, 0.09);
dab(x + lean * 0.8 + Math.cos(a) * q * h * 0.36, y - h * 0.72 + Math.sin(a) * q * h * 0.26, rx * 1.5, rx * 0.75, r.r(0.4, 1), 0.2);
}
flush(D * 0.85, { warm, blur: 0.6 });
}
// ---- 点景 -----------------------------------------------------------
function hut(x, y, w, D, warm) {
const h = w * 0.78;
stroke([[x - w * 0.62, y - h * 0.5, 1.6], [x - w * 0.05, y - h, 2.4], [x + w * 0.66, y - h * 0.46, 1.4]], { dry: 0.15 });
stroke([[x - w * 0.62, y - h * 0.5, 1.4], [x + w * 0.66, y - h * 0.46, 1.6]], { dry: 0.3 });
stroke([[x - w * 0.42, y - h * 0.48, 1.3], [x - w * 0.42, y, 1.2]]);
stroke([[x + w * 0.44, y - h * 0.45, 1.3], [x + w * 0.44, y, 1.2]]);
flush(D, { warm });
dab(x, y - h * 0.72, w * 0.5, h * 0.2, 0.5, 0.2); flush(D * 0.3, { warm, blur: 0.8 });
}
function boat(x, y, D) {
stroke([[x - 38, y - 4, 1.4], [x - 20, y + 3, 4.4], [x + 14, y + 3.5, 4.8], [x + 40, y - 5, 1.3]], { dry: 0.12 });
stroke([[x - 14, y, 1.8], [x - 3, y - 12, 2.6], [x + 10, y, 1.8]]);
stroke([[x + 22, y - 1, 3.2], [x + 23, y - 14, 2.6]]);
stroke([[x + 28, y - 26, 0.9], [x + 46, y + 14, 0.8]]);
flush(D, { warm: 0.6 });
dab(x + 23, y - 18, 3.3, 3.3, 1.2, 0.5);
stroke([[x + 15, y - 21, 1.2], [x + 23, y - 24.5, 1.5], [x + 31, y - 21, 1.2]]);
flush(D, { warm: 0.6 });
for (let j = 0; j < 4; j++) {
const yy = y + 10 + j * 7, l = 46 - j * 8;
stroke([[x - l + R.r(-6, 6), yy, 0.9], [x, yy + R.r(-1, 1), 1.9], [x + l + R.r(-6, 6), yy, 0.8]], { dry: 0.5 });
}
flush(D * 0.3, { warm: 0.3, grain: 0.5 });
}
function traveller(x, y, D) {
stroke([[x + 1, y, 11], [x - 1, y - 16, 12], [x - 2, y - 31, 7.5]], { dry: 0.12, ramp: 0 });
flush(D * 0.8, { warm: 0.6 });
dab(x - 3, y - 37, 4.6, 4.6, 1.3, 0.5);
stroke([[x - 17, y - 39, 1.3], [x - 4, y - 46.5, 2.4], [x + 10, y - 39.5, 1.3]]);
stroke([[x - 17, y - 39, 1.2], [x + 10, y - 39.5, 1.2]]);
stroke([[x + 13, y + 2, 1.4], [x + 11.5, y - 24, 1.3], [x + 10, y - 52, 1]]);
flush(D, { warm: 0.6 });
}
function birds(list) {
for (const [x, y, k] of list) {
const a = R.r(-0.25, 0.25), c = Math.cos(a), sn = Math.sin(a);
const P = (u, v) => [x + (u * c - v * sn) * k, y + (u * sn + v * c) * k];
const l = P(-11, -5), m = P(0, 0), r2 = P(12, -4), l2 = P(-5, -5.5), r3 = P(6, -5);
stroke([[l[0], l[1], 0.6], [l2[0], l2[1], 1.5 * k], [m[0], m[1], 1.9 * k]]);
stroke([[m[0], m[1], 1.9 * k], [r3[0], r3[1], 1.5 * k], [r2[0], r2[1], 0.6]]);
}
flush(0.62, { warm: 0.3 });
}
// ---- 書と印 ---------------------------------------------------------
// 100×100 の枡に [x, y, 筆圧]。末尾の数は掠れ
const GLYPH = {
土: [
[[[30, 38.5, 10.5], [50, 36.5, 8.5], [70, 34.5, 10.5]], 0.22],
[[[50, 11, 10.5], [50.3, 45, 9], [50, 80, 10]], 0.3],
[[[12, 83, 11], [50, 80.5, 9], [88, 78, 11.5]], 0.42],
],
谷: [
[[[38.5, 8.5, 9.5], [33, 17, 8], [23, 28.5, 0.8]], 0.3],
[[[60, 8.5, 4], [66, 15.5, 12], [73, 24, 15], [75.5, 27, 9]], 0.2],
[[[50, 27, 9.5], [43, 39, 8.5], [29, 51.5, 6], [9, 61.5, 0.8]], 0.5],
[[[51, 29, 4], [61, 40, 10], [75, 51, 18], [88, 57.5, 17], [96, 58, 1]], 0.46],
[[[31, 64, 10], [32, 77, 8.5], [33.5, 91, 8]], 0.2],
[[[31, 65, 9.5], [50, 63, 8.5], [69, 61.5, 10.5], [69.7, 63, 10.5], [68.5, 77, 8.5], [66.5, 92, 9]], 0.36],
[[[34, 89.5, 8], [50, 89, 8], [67, 88.5, 8.5]], 0.2],
],
大: [
[[[13, 42, 11], [50, 38, 9], [87, 34.5, 11.5]], 0.4],
[[[51, 9, 10.5], [51.5, 37, 9], [45, 61, 8.5], [31, 80, 6], [9, 93.5, 0.8]], 0.52],
[[[52, 39, 4], [60, 56, 10], [73, 75, 18], [87, 87.5, 18], [96, 88.5, 1]], 0.5],
],
地: [
[[[7, 42, 9.5], [20, 39.5, 8], [33.5, 37, 9]], 0.2],
[[[21, 17, 10], [21, 50, 8.5], [21, 75, 9]], 0.26],
[[[4, 81.5, 9.5], [15, 75.5, 7], [38, 61.5, 0.8]], 0.4],
[[[38, 46, 9], [56, 39.5, 8], [84, 30, 10], [85, 31.5, 10], [82, 47, 8.5], [78.5, 60.5, 8.5], [70, 55, 0.8]], 0.4],
[[[59, 11, 10.5], [59, 40, 9], [59, 69, 10]], 0.28],
[[[42, 21, 10.5], [42, 55, 9], [43, 75, 9.5], [50, 85.5, 10.5], [70, 87, 10], [88, 86, 11.5], [92.5, 80, 9], [92.5, 64, 0.8]], 0.54],
],
};
function glyph(ch, cx, cy, size, rot) {
const c = Math.cos(rot || 0), sn = Math.sin(rot || 0), k = size / 100;
for (const [st, dry] of GLYPH[ch]) {
const pts = st.map((p) => {
const u = (p[0] - 50) * k, v = (p[1] - 50) * k;
return [cx + u * c - v * sn, cy + u * sn + v * c, p[2] * k];
});
stroke(pts, { dry, ramp: 0.9, nib: [0.61, 0.55] });
flush(1.6, { warm: 0.5, k: 3.2, grain: 0.1 });
}
}
function seal(cx, cy, size) {
const k = size / 100, P = (x, y) => [cx + (x - 50) * k, cy + (y - 50) * k, 11 * k];
// 白文「土」
stroke([P(29, 37), P(71, 37)], { bd: 1.3 });
stroke([P(50, 17), P(50, 80)], { bd: 1.3 });
stroke([P(17, 80), P(83, 80)], { bd: 1.3 });
const x0 = Math.max(0, Math.floor((cx - size * 0.56) * s)), x1 = Math.min(W - 1, Math.ceil((cx + size * 0.56) * s));
const y0 = Math.max(0, Math.floor((cy - size * 0.56) * s)), y1 = Math.min(H - 1, Math.ceil((cy + size * 0.56) * s));
boxBlur(tmp, x0, y0, x1, y1, 1);
const hs = size * 0.5, cr = size * 0.09;
for (let py = y0; py <= y1; py++) for (let px = x0; px <= x1; px++) {
const i = py * W + px, x = px / s, y = py / s;
const carve = sstep(0.25, 0.6, tmp[i]); tmp[i] = 0;
const qx = Math.max(0, Math.abs(x - cx) - (hs - cr)), qy = Math.max(0, Math.abs(y - cy) - (hs - cr));
const d = Math.hypot(qx, qy) - cr + (vn(x * 0.16, y * 0.16, 91) - 0.5) * 3.4;
const body = 1 - sstep(-1.2, 1.2, d);
const worn = 0.62 + 0.38 * sstep(0.2, 0.52, fbm(x * 0.09, y * 0.09, 3, 93)) - 0.3 * sstep(0.6, 0.85, vn(x * 0.45, y * 0.45, 95));
red[i] = Math.max(red[i], body * (1 - carve) * clamp(worn, 0, 1) * (0.86 + 0.14 * grain[i]));
}
bx0 = W; by0 = H; bx1 = -1; by1 = -1;
}
// ---- 画面構成 -------------------------------------------------------
const farA = { ridge: ridgeOf(1560, [[1300, 560, 130, 160], [1560, 470, 125, 155], [1090, 390, 115, 115], [1810, 430, 155, 155], [1450, 260, 500, 500]], 20, 101, { terrace: 12 }), mist: 1470, mistVar: 90, D: 0.115, warm: 0, sd: 101, x0: 780, soft: 0.5, rimK: 0.2 };
const farB = { ridge: ridgeOf(1640, [[1180, 470, 110, 130], [1450, 420, 115, 130], [1690, 380, 140, 155], [1930, 470, 115, 115], [1500, 250, 500, 500]], 22, 111, { terrace: 14 }), mist: 1570, mistVar: 100, D: 0.17, warm: 0, sd: 111, x0: 860, soft: 0.6, rimK: 0.25 };
const M1 = { ridge: ridgeOf(1900, [[760, 1250, 180, 200, 2.3], [905, 1010, 90, 150, 1.8], [500, 1010, 150, 130, 1.7], [1040, 830, 110, 170, 2.2], [225, 800, 190, 150, 1.8], [620, 640, 640, 620]], 44, 121), mist: 1720, D: 0.36, warm: 0.12, sd: 121, x1: 1500 };
const M2 = { ridge: ridgeOf(1950, [[640, 1000, 150, 185, 2.2], [915, 830, 120, 185, 1.7], [335, 780, 190, 160, 2.4], [70, 700, 170, 170, 1.8], [560, 520, 600, 640]], 40, 131), mist: 1830, D: 0.47, warm: 0.25, sd: 131, x1: 1450 };
const M3 = { ridge: ridgeOf(2040, [[430, 690, 200, 220, 2.3], [770, 560, 140, 235, 1.7], [100, 640, 200, 180, 2], [450, 360, 560, 640]], 38, 141), mist: 1970, D: 0.6, warm: 0.38, sd: 141, x1: 1400 };
const R1 = { ridge: ridgeOf(1950, [[1660, 640, 230, 200, 1.8], [1965, 700, 190, 160, 2.3], [1385, 330, 130, 160], [1750, 360, 520, 400]], 38, 151), mist: 1850, D: 0.4, warm: 0.2, sd: 151, x0: 1000 };
const R2 = { ridge: ridgeOf(2030, [[1800, 560, 230, 230, 2.3], [1485, 400, 160, 180, 1.7], [2010, 500, 160, 160], [1760, 300, 560, 400]], 36, 161), mist: 1990, D: 0.54, warm: 0.36, sd: 161, x0: 1050 };
function dress(o, f) {
contour(o, f.w || 1, f.line || 1.3);
shadeStrokes(o, f.shade || 0, f.w || 1);
spines(o, f.w || 1, (f.line || 1.3) * 0.75);
cun(o, f.cun || 0, f.w || 1);
moss(o, f.dots || 0, f.dot || 4, f.dotD || 1.2);
if (f.trees) ridgeTrees(o, f.trees, f.tree || 20, f.treeD || 1.3);
}
function waterfall() {
const x = 806;
const top = M3.r(x) + 70;
stroke([[x, top, 2.5], [x - 4, top + 60, 4.5], [x + 3, top + 130, 6], [x - 1, top + 185, 7.5]], { v: 1.5 });
stroke([[x + 15, top + 190, 6], [x + 19, top + 250, 8], [x + 13, top + 320, 11], [x + 17, top + 390, 14]], { v: 1.5 });
flush(0.94, { lift: true, blur: 1.4 });
for (const sg of [-1, 1]) {
stroke([[x + sg * 7, top - 6, 1.5], [x + sg * 9 - 4, top + 60, 3.2], [x + sg * 11 + 3, top + 130, 2.2], [x + sg * 12, top + 186, 3.4]], { dry: 0.4 });
stroke([[x + 15 + sg * 10, top + 192, 2], [x + 19 + sg * 13, top + 250, 3], [x + 13 + sg * 16, top + 310, 1.6]], { dry: 0.5 });
}
flush(0.55, { warm: 0.38, grain: 0.4 });
}
function water() {
// 遠い岸の線
for (let k = 0; k < 9; k++) {
const x = R.r(250, 1750), y = 2030 + R.r(-10, 22) + Math.abs(x - 1150) * 0.02, l = R.r(120, 380);
stroke([[x - l / 2, y + R.r(-2, 2), 0.9], [x, y, R.r(1.6, 3.4)], [x + l / 2, y + R.r(-2, 2), 0.8]], { dry: 0.55, ramp: 0.3 });
flush(R.r(0.1, 0.2), { warm: 0.2, grain: 0.5 });
}
const S1 = spit({ xa: -20, xb: 900, y: 2112, th: 30, D: 0.3, warm: 0.3, sd: 201, hatch: 8 });
for (let k = 0; k < 26; k++) {
const x = R.r(60, 700); if (vn(x * 0.012, 0.7, 207) < 0.42) continue;
leafTree(x, S1.top(x) + 2, R.r(22, 44), 0.34, 0.3, 300 + k);
}
hut(388, S1.top(388) + 1, 30, 0.42, 0.3); hut(432, S1.top(432) + 2, 24, 0.4, 0.3); hut(336, S1.top(336) + 2, 22, 0.36, 0.3);
const S2 = spit({ xa: 2020, xb: 1040, y: 2240, th: 44, D: 0.42, warm: 0.42, sd: 211, hatch: 12, tilt: -0.012 });
for (let k = 0; k < 22; k++) {
const x = R.r(1230, 1980); if (vn(x * 0.012, 0.7, 217) < 0.4) continue;
leafTree(x, S2.top(x) + 3, R.r(38, 74), 0.5, 0.42, 340 + k);
}
boat(1108, 2334, 0.72);
const S3 = spit({ xa: -20, xb: 830, y: 2428, th: 58, D: 0.52, warm: 0.5, sd: 221, hatch: 16, tilt: 0.014 });
pine(236, S3.top(236) + 6, 300, 0.12, 0.4, 71, 0.4);
pine(338, S3.top(338) + 8, 215, -0.1, 0.3, 72, 0.4);
for (let k = 0; k < 16; k++) {
const x = R.r(420, 790);
grass(x, S3.top(x) + 3, 5 + ((R() * 5) | 0), R.r(22, 46), 0.6, 0.5);
}
// さざなみ
for (let k = 0; k < 26; k++) {
const x = R.r(120, 1500), y = R.r(2150, 2760), l = R.r(60, 240);
stroke([[x - l / 2, y + R.r(-1.5, 1.5), 0.7], [x, y, R.r(1.2, 2.4)], [x + l / 2, y + R.r(-1.5, 1.5), 0.6]], { dry: 0.62, ramp: 0.3 });
flush(R.r(0.05, 0.12), { warm: 0.2, grain: 0.6 });
}
}
function bank(pts, D, sd, f) {
const o = mountain({ ridge: curveOf(pts, f.rough || 14, sd), x0: pts[0][0], x1: pts[pts.length - 1][0], mist: 3420, mistVar: 0, D, warm: 0.6, sd, rimK: 0.55, rim: 30 });
shadeStrokes(o, f.shade, 1.5);
contour(o, 2.4, 1.7);
spines(o, 1.7, 1.25);
cun(o, f.cun, 1.6);
moss(o, f.moss, 5.5, 1.7);
return o;
}
function foreground() {
const RL = bank([[-10, 2850], [70, 2800], [180, 2812], [262, 2870], [330, 2940], [410, 3040]], 0.78, 231, { shade: 260, cun: 60, moss: 130 });
for (let k = 0; k < 14; k++) { const x = R.r(10, 300); grass(x, RL.r(x) + 4, 6, R.r(40, 96), 0.95, 0.6); }
const RA = bank([[1000, 3040], [1075, 2900], [1150, 2800], [1225, 2740], [1310, 2706], [1390, 2690], [1455, 2644], [1535, 2628], [1615, 2640], [1690, 2592], [1770, 2563], [1860, 2578], [1940, 2522], [2010, 2500]], 0.7, 241, { shade: 900, cun: 200, moss: 300 });
pine(1580, RA.r(1580) + 14, 760, -0.2, 1.08, 81, 0.62);
pine(1772, RA.r(1772) + 12, 520, 0.13, 1.0, 82, 0.62);
bank([[1330, 3040], [1425, 2920], [1545, 2866], [1645, 2820], [1795, 2804], [1905, 2752], [2010, 2726]], 0.92, 251, { shade: 600, cun: 130, moss: 220 });
for (let k = 0; k < 22; k++) { const x = R.r(1120, 1980); grass(x, RA.r(x) + 4, 5, R.r(20, 52), 0.95, 0.6); }
traveller(1338, RA.r(1338) + 3, 1.1);
}
function inscription() {
const x = 1748, sz = 172;
glyph('土', x + 2, 286, sz * 0.94, -0.012);
glyph('谷', x - 2, 462, sz, 0.01);
glyph('大', x + 3, 642, sz * 1.03, -0.008);
glyph('地', x, 826, sz, 0.012);
seal(x + 4, 978, 84);
}
const steps = [
['紙', makePaper],
['空', sky],
['遠山', () => { mountain(farA); miDots(farA, 200, 9, 0.4); mountain(farB); miDots(farB, 240, 9, 0.42); contour(farB, 0.7, 0.9); mist(1230, 1520, 0.5, 51); }],
['主峰', () => { mountain(M1); dress(M1, { w: 1, line: 1.3, shade: 1500, cun: 260, dots: 420 }); }],
['雲帯', () => { mist(1020, 1400, 0.72, 52); }],
['中景', () => {
mountain(M2); dress(M2, { w: 1.1, line: 1.35, shade: 1600, cun: 300, dots: 480, trees: 150, tree: 17, treeD: 1.2 });
mountain(R1); dress(R1, { w: 1, line: 1.3, shade: 1100, cun: 220, dots: 400, trees: 110, tree: 16, treeD: 1.2 });
mist(1380, 1740, 0.62, 53);
}],
['前山', () => {
mountain(M3); dress(M3, { w: 1.25, line: 1.45, shade: 1600, cun: 320, dots: 560, dot: 4.6, trees: 260, tree: 25, treeD: 1.35 });
waterfall();
mountain(R2); dress(R2, { w: 1.2, line: 1.45, shade: 1300, cun: 260, dots: 480, dot: 4.6, trees: 220, tree: 24, treeD: 1.35 });
}],
['谷の霞', () => { mist(1640, 2080, 0.9, 54); mist(1500, 2000, 0.75, 55, 1170, 330); }],
['水辺', water],
['近景', foreground],
['鳥', () => birds([[1040, 1436, 1], [1088, 1408, 0.85], [1131, 1452, 0.9], [1186, 1420, 0.7], [1232, 1447, 0.6]])],
['題と印', inscription],
];
// ---- 仕上げ:紙と墨を色にする ---------------------------------------
const LUT = new Float32Array(4097);
for (let i = 0; i <= 4096; i++) LUT[i] = Math.exp(-i / 512);
const ex = (d) => LUT[d >= 8 ? 4096 : (d * 512) | 0];
function composite(out) {
let sd = 12345;
for (let i = 0, j = 0; i < N; i++, j += 4) {
const p = paper[i], a = age[i];
let r = (246 - 9 * a) * p, g = (240 - 13 * a) * p, b = (226 - 22 * a) * p;
const c = inkC[i], w = inkW[i];
if (c + w > 0.0004) {
const gr = 1 + (grain[i] - 0.5) * 0.46;
const cc = c * gr, ww = w * gr;
r *= ex(cc * 2.62 + ww * 2.42); g *= ex(cc * 2.5 + ww * 2.48); b *= ex(cc * 2.3 + ww * 2.6);
}
const q = red[i];
if (q > 0) {
const m = q * 0.93;
r = lerp(r, 186 * p, m); g = lerp(g, 44 * p, m); b = lerp(b, 34 * p, m);
}
sd = (Math.imul(sd, 1664525) + 1013904223) | 0;
const dz = ((sd >>> 9) / 8388608) - 0.5;
out[j] = r + dz; out[j + 1] = g + dz; out[j + 2] = b + dz; out[j + 3] = 255;
}
}
return { W, H, steps: steps.map((x) => x[0]), run: (i) => steps[i][1](), composite };
}
G.Sumi = { create, DW, DH };
})(globalThis);