Task 1 of 4
Diffusion is gossip: every cell drifts toward the average of its neighbors.
The operator that measures "how far am I from my neighbors' average" is the
Laplacian, and on a grid it's a five-read gather:
left + right + up + down − 4·center. Positive means the neighbors are
higher and stuff will flow in; negative means it flows out.
One wrinkle: simulations hate edges. Instead of clamping like the filters in Convolution & Filters, we wrap around — the left neighbor of column 0 is column 31. The world becomes a torus and every cell has exactly four neighbors, no special cases.
field with wrap-around edges.0 becomes size − 1, past size − 1 becomes 0left + right + up + down − 4·centerSame trick as clamping, different else:
let xr = this.thread.x + 1;
if (xr > this.constants.size - 1) xr = 0;
The starter already wrote xl for you — mirror it three times.
The neighbors sit at field[y][xl], field[y][xr],
field[yd][x] and field[yu][x] — only ever vary
one coordinate at a time. The center is field[y][x].
return field[y][xl] + field[y][xr] + field[yd][x]
+ field[yu][x] - 4 * field[y][x];This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.