Basics

glint · Examples · Tutorials · Gallery

From a single colour to audio-reactive visuals: coordinates, shapes, time, repetition, palettes, parameters.

A free interactive course of 10 steps in glint, a live shader editor in the browser: each step changes a running shader, and ticks itself when you've done its task.

1. One job: a colour

A fragment shader is a tiny program that runs once for every pixel, all at the same time on the GPU. Its only job is to set fragColor: red, green, blue and alpha, each from 0.0 to 1.0.

Right now every pixel is told to be pure red: vec4(1.0, 0.0, 0.0, 1.0).

There's no compile button to press and no need to refresh: glint compiles as you type, a moment after you stop. If there's a mistake, the last working version keeps running and the error shows on its line.

Your turn: Change the first three numbers to make the screen your favourite colour. It updates as you type.

2. Every pixel knows where it is

gl_FragCoord.xy is the pixel's position in pixels. Dividing by resolution (the screen size) gives uv, which runs from 0.0 at the bottom-left to 1.0 at the top-right.

Using uv.x as red and uv.y as green paints a gradient: redder to the right, greener towards the top.

Your turn: Flip it so green is strongest at the bottom: use 1.0 - uv.y for green.

3. Distance makes a circle

Shift uv so (0, 0) is the centre and divide by the height so circles stay round: (gl_FragCoord.xy - 0.5 * resolution) / resolution.y.

length(uv) is each pixel's distance from the centre. step(d, 0.3) is 1.0 where the distance is less than 0.3 and 0.0 outside: a circle.

Your turn: Move the circle to the right: measure the distance from a different point, e.g. length(uv - vec2(0.4, 0.0)).

4. Soft edges and mixing

step gives a hard edge: each pixel is either in or out, so the rim looks jagged. smoothstep gives a soft one.

smoothstep(a, b, x) turns x into a number from 0 to 1: 0 while x is at a, 1 once x reaches b, and a smooth ramp in between. Here x is the distance d, with a = 0.3 and b = 0.29: 0 on the rim (d = 0.3), 1 just a hundredth further in. So it's 1 inside and 0 outside, with a soft rim one hundredth wide. (The bigger number comes first so that inside is 1.)

mix(a, b, t) is the other way round, and easy to mix up with it: it goes from a to b as t goes from 0 to 1. So mix(background, ink, circle) paints ink where the circle is 1 and background where it's 0.

Your turn: Make the edge a big soft glow: spread the two smoothstep numbers further apart, like smoothstep(0.3, 0.1, d).

5. Bring in time

time counts seconds since the start. sin(time) swings smoothly between -1 and 1, so 0.2 + 0.1 * sin(time) breathes between 0.1 and 0.3.

Anything can move this way: size, position, colour. Play/pause and rewind are in the top bar (Alt+P, Alt+R).

Your turn: Make it pulse faster: multiply time inside the sine, e.g. sin(time * 4.0).

6. Repeat space

fract(x) keeps only the part after the decimal point, so it counts 0→1, 0→1, 0→1… fract(uv * 4.0) turns the screen into a grid of cells, each with its own 0→1 coordinates.

Subtract 0.5 to centre each cell, and one circle becomes a whole grid of them, with no loops needed. floor(uv * 4.0) gives each cell an id, used here to offset the timing.

Your turn: Change the number of repeats (the 4.0).

7. Angles: polar coordinates

Instead of x and y, describe each pixel by its angle around the centre, atan(uv.y, uv.x), and its distance, length(uv).

sin(angle * 6.0) goes up and down six times around the circle: six petals. Adding time spins them, and adding the distance twists them into a spiral.

Your turn: Change the number of petals (the 6.0).

8. Colour from a number

A cosine palette turns any number into a colour: 0.5 + 0.5 * cos(6.28 * (t + phase)). Each channel has its own phase, so as t changes, red, green and blue rise and fall out of step: a rainbow.

Here the palette is fed the distance from the centre plus time, which makes rings that flow outwards.

Your turn: Change the palette: edit the three phase numbers vec3(0.0, 0.33, 0.67), e.g. to vec3(0.0, 0.1, 0.2) for a fiery one.

9. Knobs: parameters

Parameters are your own uniforms with controls. This step added two, rings and tint, in the Parameters panel (now open on the right). The shader declares them with uniform float rings; and uniform vec3 tint;.

Every parameter has a keyboard button next to it for MIDI learn: click it, move a knob on a controller, and that knob drives the parameter.

Your turn: Drag the rings slider in the Parameters panel.

10. Make it listen

With audio input on, spectrum holds the bass, mid and treble levels (0 to 1), beat jumps to 1 on every kick and fades, and spectrumIntegrated is a clock that runs faster when the music is loud.

Here the rings spin with spectrumIntegrated.x, the circle swells with the bass, and it flashes on the beat. The fft texture has the full spectrum if you want more detail.

Your turn: Turn on audio in the Inputs panel (microphone, line-in or system audio) and play some music.

Other courses: Medium · Advanced · Maths for shaders · Raymarching, step by step · Matrices, from scratch · Sound you can see · Noise from scratch · Colour and light · Passes and feedback · Skies and reflections · Vertex shaders, step by step · Raymarching 2: glow, fog and light · Raymarching 3: worlds · Raymarching 4: modelling, materials and motion · Fractals, step by step · Vertex shaders 2: lines, particles and moving meshes · Fractals 2: further in · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment · Blur and glow