Plasma
Paint a classic animated plasma with a few lines of NumPy and show it live on a display block.
Blocks used
Overview
The plasma is a classic effect of computer graphics demos, a soft and endlessly shifting field of color that looks far more complex than the code behind it. It is usually built as a sum of sine functions evaluated at every pixel.[1] Each sine on its own draws plain stripes or rings. Added together, they reinforce in some places and cancel in others, and the result drifts and swirls.
This flowgraph paints a new plasma on every update. A Python block adds four sine waves over a 760 by 400 pixel canvas with NumPy and writes the result as an RGB image into a tensor, and a Frame block displays it. No radio is involved, which makes it a good first look at how a Python block produces data for a native display.
How it works
The canvas is 760 by 400 pixels (width by height). Each pixel gets coordinates and that run from about to across and to down. On every update the block advances time by 0.05 and adds four waves,
where is roughly the distance from the pixel to a center that wanders slowly around the canvas. The first term draws vertical stripes, the second horizontal stripes twice as wide, the third diagonal stripes, and the fourth rings around the moving center. Adding inside each sine shifts its phase, and that is what makes the bands move. In the painter, the s = ... line is this equation written in NumPy, with the same names.
The sum stays between and . A cosine palette turns it into color,[2]
Offsetting the phase between the three channels is what spreads the pattern across so many hues. The output is a CPU tensor of 32-bit floats with shape [400, 760, 3], holding red, green, and blue between 0 and 1. The Frame block shows those values as is, with auto range turned off. With auto range on, it would stretch each frame between its own minimum and maximum, and the brightness would pump from frame to frame.
What to look for
Watch the rings first. They come from the fourth wave, and their center drifts along a slow loop, dragging them across the canvas. The three straight waves sweep through in different directions, bending the rings into ovals and pinching them into islands where the waves cancel.
The palette repeats as the sum rises, so a single ridge of the pattern shows up as several bands of color, running through orange, red, purple, blue, cyan, green, and yellow. Every term repeats within of time, so the animation comes back to where it started after about 754 updates. For more on building plasmas and on cosine palettes, see the sources below.
Going further
To see what one wave contributes on its own, replace the s = ... line in the painter with a single term, such as the rings,
s = np.sin(r + t)
and put the other terms back one at a time. With fewer waves the sum covers a narrower slice of the palette, so the colors also get calmer.
Three constants at the top of the painter control the rest. Raising SCALE packs more stripes into the canvas, STEP sets the speed, and PHASE is the palette phase . Values such as give dark red and teal, and gives blue and yellow. The canvas size is tied to the output tensor, so changing H or W means updating the painter's declared output shape to match. The Python block reference explains how output shapes are declared.
The painter recomputes every pixel in NumPy on each update, which favors readability over speed.
References
References
-
L. Vandevenne, "Plasma," Lode's Computer Graphics Tutorial, 2004. lodev.org/cgtutor/plasma.html ↩
-
I. Quilez, "Simple color palettes," iquilezles.org. iquilezles.org/articles/palettes ↩