Diffusion with framebuffer objects
Each step draws a full-screen triangle into one texture while reading the other, then the two swap roles: the ping-pong of render3, render4, render5 and particleDiffusion. This page combines them with the image filters of hatImage1, hawaiiImage and hawaiiIsobel, and can run two simulations side by side that differ in one setting.
left (or only)right
Point at the simulation to read a texel.
What the samples do, and what this page changes
- The 4-neighbour average never uses the centre. On a grid, a texel's 4 neighbours all have the other parity of i + j, so the even and odd texels trade values every step and never mix: a single lit texel stays on one parity at a time. In the heat step u + r(sum − 4u), the checkerboard pattern is multiplied by 1 − 8r, which is −1 for the average (r = 1/4) and −3 for render5's r = 1/2. Stable smoothing needs r < 1/4.
- render5 at r = 0.5 is unstable. In RGBA8 the growth is clamped to [0, 1], so it shows up as stripes of alternating 0 and 1 spreading from every edge, not as an overflow.
- 8 bits. Every step rounds to 1/255: diffusion stalls once neighbours differ by less than that, and the total changes. In a CPU simulation of a 16 × 16 white square on a 64 × 64 grid, 2000 stable steps took the total from 256 to 189 in 8 bits and kept it at 256 in floats; the 8 bits vs half float comparison shows the same drift live.
- Wrap modes. render3 and render4 set
CLAMP_TO_EDGEonly ontexture2;texture1keepsREPEAT, and the two alternate as the source. This page reads withtexelFetchand applies the edge rule in the shader, so it is explicit. - The displayed image is one step ahead. render3–render5 draw the screen with the diffusion shader too, so what you see is the state after one more step, not the state. Here the display pass only shows.
- particleDiffusion sets
uTexture, but the shader's sampler isuTextureMap(it works because samplers default to unit 0), callsvertexAttribPointerwith an undefined location, and uses green (0, 9, 0). - Edge kernels. hawaiiIsobel's weights are (1, 3, 1); Sobel's are (1, 2, 1). Neither sample normalizes, so a full step reads 5 (hawaiiIsobel) and saturates.
- Reading back. The probe calls
readPixelsdirectly, which makes the CPU wait until the GPU has finished drawing (Chrome logs this as a "GPU stall"). The chart instead reads into a pixel-pack buffer, sets a fence, and copies the data out on a later frame, once the fence has passed: a WebGL2 way to read back without stalling.