Home

Awesome

<center><a href="https://jasonwebb.github.io/reaction-diffusion-playground" title="Go to the playground!"><img src="https://raw.githubusercontent.com/jasonwebb/reaction-diffusion-playground/master/images/social-media-preview.jpg" alt="The text 'Reaction Diffusion Playground' in a wavy font over a black background with small green dots."></a></center>

<a href="https://raw.githubusercontent.com/jasonwebb/reaction-diffusion-playground/master/images/screenshots/all-screenshots.png" title="Sample screenshots"><img src="https://raw.githubusercontent.com/jasonwebb/reaction-diffusion-playground/master/images/screenshots/all-screenshots.png" alt="Grid of 12 screenshots, 4 columns and 3 rows, with the text Reaction Diffusion Playground overlaid on top."></a>

What is reaction-diffusion?

Reaction-diffusion is a mathematical model describing how two chemicals might react to each other as they diffuse through a medium together. It was proposed by Alan Turing in 1952 as a possible explanation for how the interesting patterns of stripes and spots that are seen on the skin/fur of animals like giraffes and leopards form.

The reaction-diffusion equations really only describes how the concentrations of the chemicals change over time, which means that all of the interesting patterns and behaviors that we see are emergent phenomena.

Here's what the equations look like:

The two reaction-diffusion differential equations with boxes around each part, explained in text bubbles nearby.

Credit to Karl Sims for the equation illustration above.

The most important terms, the ones we'll want to turn into tweakable parameters, are:

The other terms in the equation are usually kept constant, but might be worth playing around with once you are comfortable with the core parameters:

Learn more about reaction-diffusion in my morphogenesis-resources repo.

How does this project work?

Most reaction-diffusion simulations store values representing the concentrations of the two chemicals (A and B) in a 2D grid format, then applies the reaction-diffusion equations to each cell in the grid. In this simulation, data textures matching the size of the screen (canvas) are used for the 2D grid, and custom shaders are used to apply the reaction-diffusion equations to each pixel (texel) of these textures.

Each pixel/texel of the simulation data texture encodes the A and B concentrations for that location in the R and G channels as a normalized float value ([0.0-1.0]).

In each frame of the simulation, a custom fragment shader (./glsl/simulationFrag.glsl) reads the values of the last frame's data texture as an input and applies the reaction-diffusion equation to every pixel. Data textures are rendered back and forth between two render targets many times per frame with a technique called ping-pong to speed things up.

Once the simulation has been run enough times, another fragment shader (./glsl/displayFrag.glsl) reads the latest data texture and maps the chemical concentration data to color values (configurable in the UI).

Just about every option you see in the UI controls one or more uniforms that get passed to these fragment shaders to influence the reaction-diffusion equations or the way the data texture information is translated into colors.

Using the app

Annotated screenshot of the UI

Equation parameters

Each of the four fundamental parameters of the reaction-diffusion equation (f, k, dA, dB), along with the timestep increment, can be changed using the sliders found at the top of the right panel. Change them gradually to avoid collapsing the system.

Interactive parameter map

<a href="https://raw.githubusercontent.com/jasonwebb/reaction-diffusion-playground/master/images/parameter-map-screenshot.png"><img src="https://raw.githubusercontent.com/jasonwebb/reaction-diffusion-playground/master/images/parameter-map-screenshot.png" alt="Screenshot of interactive parameter map" align="right" width="200px"></a>Use this map to navigate through the parameter space easily and find areas with interesting patterns and behaviors. k values are plotted along the X axis, f values along the Y axis.

Use the crosshairs attached to the mouse position to navigate to a region you're interested in, then click to set the main f and k values to match where you clicked.

This map is inspired by the work of Robert Munafo.

Seed pattern

Choose a pattern to use in the first frame of the simulation to seed the reaction. Each pattern has some additional options you can play with for different effects.

Also provides buttons to reset the simulation with the selected pattern or clear the canvas completely.

Rendering style

Control how the chemical concentration data is translated into visuals.

Canvas size

Set the width and height of the simulation area.

You can also maximize the canvas to fit the entire viewport.

Global actions

Buttons to pause/play or export an image of the current state of the simulation area.

Style map

Upload an image from your computer to vary the f, k, dA, and dB values based on the brightness value of each pixel. The secondary values you choose here will become endstops in an interpolation calculation with the primary values (on the right UI pane). In other words, the four equation parameters will be interpolated to be between the original parameter values (on the right pane) and these secondary parameter values (left pane) using the brightness value of each pixel.

In addition to the reaction-diffusion equation parameters, you can also adjust the uniform scale, rotation, and X/Y offset of the image for different effects.

Your image will automatically be scaled to fit the entire canvas, so it may be stretched. Resize your image to match the width and height of the canvas (or at least its aspect ratio) to minimize distortion.

Bias

Normally diffusion occurs evenly in all directions due to the default radially symmetrical Laplacian stencil. Using this X/Y pad you can "weight" this stencil to cause diffusion to favor one particular direction.

Keyboard controls

Mouse controls

Click and drag anywhere on the canvas to increase the concentration of the B chemical around the mouse position.

Use your mouse wheel to change the diameter of the area affected by clicking / dragging. The size of the brush is indicated with a circle that follows the mouse.

MIDI controls

If you have an Akai LPD8 Wireless or a Novation Launch Control XL, mappings are provided for the various pads and knobs. See ./js/midi.js for details.

Technical notes

Setting up and running locally

  1. Run npm install to get all packages.
  2. Run npm run serve to start up Webpack and launch the application in a live-reloading browser window.

Technologies used

Architecture and file structure

JavaScript

The most important file is entry.js - that's where the environment, UI, and the simulation itself are set up, and where the main update loop is. It sets up a ThreeJS environment with an orthographic camera and a scene containing a plane mesh that is oriented perpendicular to the camera so that it appears 2D.

In the ./js folder are a bunch of modules split into files (all referenced in one way or another through entry.js):

GLSL

In the ./glsl folder are:

References

Fundamentals

These papers, articles, and videos helped me understand how the reaction-diffusion algorithm works.

Code

These articles, repos, and project pages helped me figure out how to build my implementation.

Creative

These projects inspired me to explore some of the creative possibilities of reaction-diffusion.