Skip to content

Plasma Effect

You'll write the oldest demo effect there is, a shimmering field of summed sine waves (or fire, or a starfield), and get it running smoothly on your own screen.

Quest board
This badge is on the quest board this week, so every requirement you clear pays 1.5x XP until Sunday.

About

Most sceners' first program was a plasma, a fire or a starfield, because each one teaches the three things a demo is made of: a pixel buffer you write into, a palette that turns numbers into colour, and a loop that runs once per frame. Any language counts, from JavaScript on a canvas to C with SDL, as long as your code sets every pixel. A shader pasted from a tutorial does not count, and neither does a GIF. The trap in a scripting language is calling sin() for every pixel of every frame, so you precompute a table and look values up instead.

At a glance

Per requirement
15 XP (23 this week)
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Open a window and write pixels into a buffer by hand. Make a 320 by 240 array of pixels that you copy to the screen each frame. In JavaScript that is a canvas with ImageData, in Python it is pygame with a surface, in C it is SDL and a texture. Fill it with a gradient from left to right first, so you know x and y are the right way round before anything else goes wrong. Take a screenshot of the gradient next to the line of code that writes one pixel. Photo
  2. 2
    Build a 256-entry palette and draw it as a strip. A plasma is a number from 0 to 255 looked up in a palette, so make the palette first. Three sine waves offset by a third of a turn give you a smooth rainbow; a ramp from black through red to yellow to white gives you fire. The trap is a palette whose first and last colours don't match, which puts a hard line through everything. Take a screenshot of the strip. Photo
  3. 3
    Write the effect itself and get it moving. For a plasma, add three or four sines of x, y, x plus y and the distance from the centre, shift the result by time, and scale it into the palette. For fire, keep a heat buffer where each pixel is the average of the three below it minus a little cooling, and seed the bottom row with random heat. For a starfield, keep a list of points with x, y and z, move z toward you and project with x divided by z. Film it running for ten seconds. Video
  4. 4
    Measure the frame rate and get it above 30 frames a second. Draw the frames per second in a corner. If it crawls, you are probably calling sin() per pixel, so precompute a sine table of 256 entries and index into it, or halve the resolution and scale the buffer up. Film the effect with the counter showing. Video
  5. 5
    Add palette cycling or a second effect, record 30 seconds and post the source. Rotating the palette by one entry per frame costs almost nothing and changes the whole feel, which is the first lesson of the scene: cheap tricks that look expensive. Record a 30-second clip, link your source in the write-up, and say which part was slower than you expected. Video