/** * Poisson-disc sampling (Bridson), seeded. * * Blue-noise points: no two closer than `r`, none further than about * `2r` from a neighbour, so the field reads as a cloud rather than as a * grid or as static. Seeded so a given hero looks the same on every * visit and every mount; the seed also keys the per-point randoms the * shader uses for twinkle and arrival order. *//** mulberry32 — small, fast, good enough for layout. */exportfunction rng(seed:number) {
let a = seed >>>0;
return () => {
a = (a +0x6d2b79f5) >>>0;
let t = a;
t =Math.imul(t ^ (t >>>15), t |1);
t ^= t +Math.imul(t ^ (t >>>7), t |61);
return ((t ^ (t >>>14)) >>>0) /4294967296;
};
}
exportinterface PoissonOptions {
/** Half-extents of the domain, centred on the origin. */ halfW:number;
halfH:number;
/** Minimum spacing between points. */ r:number;
seed?:number;
/** Candidates tried around each active point before it retires. */ k?:number;
}
/** * Points as a flat `[x0, y0, x1, y1, …]` array in the domain's units. * Count is roughly `0.7 · area / r²`; `minDistFor` inverts that. */exportfunction poisson({ halfW, halfH, r, seed =1, k =30 }: PoissonOptions):Float32Array {
const rand = rng(seed);
const W = halfW *2;
const H = halfH *2;
const cell = r /Math.SQRT2;
const gw =Math.ceil(W / cell);
const gh =Math.ceil(H / cell);
const grid =newInt32Array(gw * gh).fill(-1);
const xs:number[] = [];
const ys:number[] = [];
const active:number[] = [];
const r2 = r * r;
const put = (x:number, y:number) => {
const i = xs.length;
xs.push(x);
ys.push(y);
grid[Math.floor((y + halfH) / cell) * gw +Math.floor((x + halfW) / cell)] = i;
active.push(i);
return i;
};
const fits = (x:number, y:number) => {
if (x <-halfW || x >= halfW || y <-halfH || y >= halfH) returnfalse;
const gx =Math.floor((x + halfW) / cell);
const gy =Math.floor((y + halfH) / cell);
for (let j =Math.max(0, gy -2); j <=Math.min(gh -1, gy +2); j++) {
for (let i =Math.max(0, gx -2); i <=Math.min(gw -1, gx +2); i++) {
const n = grid[j * gw + i];
if (n <0) continue;
const dx = xs[n] - x;
const dy = ys[n] - y;
if (dx * dx + dy * dy < r2) returnfalse;
}
}
returntrue;
};
put((rand() -0.5) * W, (rand() -0.5) * H);
while (active.length) {
const ai =Math.floor(rand() * active.length);
const p = active[ai];
let placed =false;
for (let n =0; n < k; n++) {
const a = rand() *Math.PI *2;
const d = r * (1+ rand());
const x = xs[p] +Math.cos(a) * d;
const y = ys[p] +Math.sin(a) * d;
if (fits(x, y)) {
put(x, y);
placed =true;
break;
}
}
if (!placed) {
active[ai] = active[active.length -1];
active.pop();
}
}
const out =newFloat32Array(xs.length *2);
for (let i =0; i < xs.length; i++) {
out[i *2] = xs[i];
out[i *2+1] = ys[i];
}
return out;
}
/** The spacing that lands about `count` points on a `w × h` domain. */exportfunction minDistFor(count:number, w:number, h:number) {
returnMath.sqrt((0.7* w * h) / count);
}