Cellular Automata in ShoggothOx

Published:

← Back to Blog


At first glance, a music visualizer seems like a simple mapping tool: sound goes in, waves are plotted on screen. But if you look at the fluid, organic, and shifting patterns of ShoggothOx, you will realize that there is a deeper computational engine at work. The secret to these morphing visual textures lies in Cellular Automata (CA) — a branch of computer science and mathematical physics that simulates complex systems through simple, local interactions.

What is a Cellular Automaton?

A cellular automaton consists of a regular grid of "cells," each of which resides in one of a finite number of states. The grid evolves through discrete timesteps. In each step, every cell determines its new state based on its current state and the states of its immediate neighbors, governed by a set of mathematical transition rules.

The Discrete Paradigm: Conway's Game of Life

The most famous cellular automaton is John Conway's Game of Life (1970). It is a binary, discrete system:

Conway's Game of Life simulates biological systems. It displays emergent behaviors like "gliders" (oscillating structures that travel across the grid) and "glider guns" that create them, proving that complex, symbolic self-replication can emerge from a few simple rules.

Interactive: Conway's Game of Life
Click or drag on the grid to toggle cells or draw new structures!

The Continuous Paradigm: ShoggothOx's Flame Engine

While Conway's system simulates biology, ShoggothOx simulates physical thermodynamics and fluid dynamics. To achieve this, it replaces discrete, binary thresholds with a Continuous Cellular Automaton (CCA) framework:

ct+1 = max(0, ⌊ s / 4 ⌋ - 1)

Where s is the sum of selected neighbor cells.

The lookup-table bounds behind this equation are examined in Proving Pixel Math with Verus.

This formula is a discrete approximation of the physical heat equation:

∂u/∂t = α∇2u

The Laplacian operator (∇2) measures the difference between a point's value and the average value of its surrounding neighborhood (local spatial curvature). If a cell is brighter than its neighbors, the Laplacian is negative, causing it to cool down; if it is darker, it heats up.

Solving the continuous heat equation exactly requires expensive numerical integration. Instead, ShoggothOx approximates the Laplacian using a discrete finite-difference stencil (averaging neighboring pixels). This discrete approximation allows the engine to compute diffusion using simple, local array operations, which run extremely fast on both the CPU and GPU to maintain 60 FPS.

Convection and Directional Drift

In physics, heat does not just diffuse; it also flows via convection. ShoggothOx models this by shifting the neighborhood convolution kernel. For example, in the Upward Flame mode, the cell at index i gathers neighbor values from the rows directly below it (at offsets +W-1, +W, +W+1, and +2W, where W is the row width).

Because each pixel draws its energy from below, the brightness appears to rise upward. This directional convolution, combined with the constant decay term (the -1 in the equation), creates the organic, rising, and fading trails of fire.

To combine convection with more complex movement (like spirals and zooms), ShoggothOx layers these drift stencils on top of precomputed warp tables.

Decay Stencil Variants

By tweaking the convolution stencil and the decay rate, we can alter the visual "physics" of the simulation:

  1. divsub (Standard): The sum of 4 neighbors is divided by 4, and 1 is subtracted. This creates moderate, organic trails that dissolve smoothly.
  2. divsub2 (Amplification): The sum is divided by 2, and 1 is subtracted. This allows neighbors to amplify the signal, creating a high-contrast, binary-like clustering where bright areas saturate rapidly to maximum value.
  3. divsub4 (Linear Decay): The current cell value is simply decreased by 2 per frame without sampling neighbors. This removes spatial diffusion entirely, leaving sharp, stationary lines that fade out.
Interactive: Continuous Decay Flame CA
Drag on the grid to inject heat/energy (255 brightness) into the automaton!

Scaling to Millions of Cells: WebGPU Shaders

Historically, Cthugha was limited to small 160×100 grids because 90s CPUs had to compute these loops in raw assembly. ShoggothOx compiles native Rust to WebAssembly to perform audio processing, but it pushes the cellular simulation boundary into hardware-accelerated GPUs.

Using WebGPU and WebGL fragment shaders, ShoggothOx runs the neighbor stencil averages and coordinate warps directly on graphics hardware. The entire simulation grid is stored in GPU memory as a texture, allowing us to evaluate millions of cell transitions in parallel via compute shaders. What once required assembly optimization on a 486 PC now runs effortlessly at 60 FPS at native high-DPI monitor resolutions. For environments without hardware acceleration, ShoggothOx falls back to a highly optimized WebAssembly CPU path, executing the cellular transition loops in memory using flat 1D arrays and pointer swaps.