Reaction-Diffusion
初出 1952 · Alan Turing — Alan Turing, "The Chemical Basis of Morphogenesis" (1952); Gray–Scott model, 1980s. · 年表 ↗
コミュニティ・通称 名称 — 化学・物理由来の記述的用語(反応+拡散)。Turing の 1952 年機構は別途「チューリング・ パターン」とエポニム化される。単一著者がアルゴリズム名として命名したものではない。
ひとことで 2 種類の物質が拡散しながら反応する偏微分方程式系。Gray-Scott モデルが代表的で、生物模様のような有機的なパターンが現れる。
コード
上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。
reaction-diffusion/index.html
const SEED = Math.floor(Math.random()*1e9);
// Gray-Scott reaction-diffusion on a small grid, scaled up to the canvas.
// Two chemicals A and B diffuse and react; B is rendered.
const W = 150, H = 150;
let A, B, A2, B2, dA = 1.0, dB = 0.5, feed, kill, ITER = 20;
const DT = 1.0;
function setup() {
randomSeed(SEED);
const s = Math.min(windowWidth, windowHeight);
pixelDensity(1);
const cnv = createCanvas(W, H);
cnv.elt.style.width = s + 'px';
cnv.elt.style.height = s + 'px';
A = new Float32Array(W * H).fill(1);
B = new Float32Array(W * H).fill(0);
A2 = new Float32Array(W * H);
B2 = new Float32Array(W * H);
// Curated feed/kill pairs from Pearson's classification — each yields a
// distinct, lively pattern (coral, mitosis, worms, spots, maze).
const PAIRS = [
[0.0545, 0.062], // coral
[0.0367, 0.0649], // mitosis
[0.039, 0.058], // worms
[0.030, 0.062], // spots/holes
[0.029, 0.057], // maze
[0.062, 0.0609], // u-skate
];
[feed, kill] = PAIRS[Math.floor(random(PAIRS.length))];
// Seed a few CHUNKY B squares. Small/dispersed seeds disperse below the
// reaction threshold before a front can establish (especially in the worms
// regime), so use solid blocks ~18-26px wide.
const seeds = 6 + Math.floor(random(5));
for (let k = 0; k < seeds; k++) {
const half = Math.floor(random(8, 12));
const cx = Math.floor(random(half + 4, W - half - 4));
const cy = Math.floor(random(half + 4, H - half - 4));
for (let y = -half; y <= half; y++)
for (let x = -half; x <= half; x++) {
B[(cy + y) * W + (cx + x)] = 1;
}
}
noStroke();
}
function laplA(x, y) {
const i = y * W + x;
return (
A[i] * -1 +
(A[((y-1+H)%H)*W+x] + A[((y+1)%H)*W+x] + A[y*W+((x-1+W)%W)] + A[y*W+((x+1)%W)]) * 0.2 +
(A[((y-1+H)%H)*W+((x-1+W)%W)] + A[((y-1+H)%H)*W+((x+1)%W)] + A[((y+1)%H)*W+((x-1+W)%W)] + A[((y+1)%H)*W+((x+1)%W)]) * 0.05
);
}
function laplB(x, y) {
const i = y * W + x;
return (
B[i] * -1 +
(B[((y-1+H)%H)*W+x] + B[((y+1)%H)*W+x] + B[y*W+((x-1+W)%W)] + B[y*W+((x+1)%W)]) * 0.2 +
(B[((y-1+H)%H)*W+((x-1+W)%W)] + B[((y-1+H)%H)*W+((x+1)%W)] + B[((y+1)%H)*W+((x-1+W)%W)] + B[((y+1)%H)*W+((x+1)%W)]) * 0.05
);
}
function step() {
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
const i = y * W + x;
const a = A[i], b = B[i];
const abb = a * b * b;
// dt < 1 + clamp to [0,1] keeps the explicit Euler integration stable
// (otherwise hot seeds blow up and collapse to a dead A=1, B=0 state).
let na = a + (dA * laplA(x, y) - abb + feed * (1 - a)) * DT;
let nb = b + (dB * laplB(x, y) + abb - (kill + feed) * b) * DT;
A2[i] = na < 0 ? 0 : na > 1 ? 1 : na;
B2[i] = nb < 0 ? 0 : nb > 1 ? 1 : nb;
}
}
[A, A2] = [A2, A];
[B, B2] = [B2, B];
}
function draw() {
for (let k = 0; k < ITER; k++) step();
loadPixels();
for (let i = 0; i < W * H; i++) {
const v = Math.max(0, Math.min(1, A[i] - B[i]));
const idx = i * 4;
// teal → cream ramp
pixels[idx] = 20 + v * 235;
pixels[idx + 1] = 60 + v * 195;
pixels[idx + 2] = 70 + v * 160;
pixels[idx + 3] = 255;
}
updatePixels();
}仕組み
濃度場 A・B をグリッド上に持ち、毎ステップ Gray–Scott 方程式で更新する:A += Dₐ∇²A − A·B² + f(1−A)、B += D_b∇²B + A·B² − (k+f)B。∇² は拡散(近傍との小カーネル畳み込み)、A·B² は A→B の反応、f は A の供給率、k は B の除去率。A が B より速く拡散する(Dₐ>D_b)ため微小な揺らぎが安定パターンに育つ。f と k を変えるだけで斑点・縞・迷路・自己複製ブロブへと連続的に変化する。
パラメータ
feedRate: number — Rate at which chemical A is replenished (the Gray–Scott f).killRate: number — Rate at which chemical B is removed (the Gray–Scott k).diffusionA: number — Diffusion coefficient of chemical A.diffusionB: number — Diffusion coefficient of chemical B (commonly about half of A).
系譜
よく組み合わされるアルゴリズム
- Fragment Shader (GLSL) · 1 作品
- Perlin / Simplex Noise · 1 作品
このアルゴリズムを使う作品 (2)
最終更新 2026-07-16