Skip to content

Plasma Effect

Several sine waves running in different directions, added together: the demoscene's plasma.

How it works

One wave runs across x, one down y, one along the diagonal x + y, and one outwards from a centre point. Each is simple, but their speeds don't divide into each other, so the sum never quite repeats. Dividing the total by 4 keeps it between −1 and 1.

The helper does exactly this:

plasma(x, y, cx, cy) = (sin(x × 0.04) + sin(y × 0.03) + sin((x + y) × 0.03) + sin(dist(x, y, cx, cy) × 0.05)) / 4

0.5 + 0.5 × plasma(...) moves it to 0..1 for a brightness or a palette. The classic colouring sends the same value through three sine waves shifted a little from each other, one each for red, green and blue.

The C is for the raylib pixels starter.

Plasma in grey

static RGB shade(float x, float y, float t)
{
    return grey(0.5f + 0.5f * plasma(x, y, 150, 150));
}
function shade(x, y, t) {
  return grey(0.5 + 0.5 * plasma(x, y, 150, 150));
}

Shifted sine colours

static RGB shade(float x, float y, float t)
{
    float v = plasma(x, y, 150, 150) * PI;
    return rgb(wave(v), wave(v + 2), wave(v + 4));
}
function shade(x, y, t) {
  let v = plasma(x, y, 150, 150) * PI;
  return rgb(wave(v), wave(v + 2), wave(v + 4));
}

Through a palette

static RGB shade(float x, float y, float t)
{
    return rainbow(0.5f + 0.5f * plasma(x, y, 150, 150));
}
function shade(x, y, t) {
  return rainbow(0.5 + 0.5 * plasma(x, y, 150, 150));
}

Moving plasma

Give each wave its own speed and direction in time, move the centre round a circle, and let the palette drift.

static RGB shade(float x, float y, float t)
{
    float cx = 150 + 80 * sinf(t * 0.7f);
    float cy = 150 + 80 * cosf(t * 0.5f);
    float v = sinf(x * 0.04f + t) + sinf(y * 0.03f - t) + sinf((x + y) * 0.03f + t * 0.5f)
            + sinf(dist(x, y, cx, cy) * 0.05f);
    return rainbow(0.5f + v / 8 + t * 0.05f);
}
function shade(x, y, t) {
  let cx = 150 + 80 * sinf(t * 0.7);
  let cy = 150 + 80 * cosf(t * 0.5);
  let v = sinf(x * 0.04 + t) + sinf(y * 0.03 - t) + sinf((x + y) * 0.03 + t * 0.5)
        + sinf(dist(x, y, cx, cy) * 0.05);
  return rainbow(0.5 + v / 8 + t * 0.05);
}