Maths for shaders
glint · Examples · Tutorials · Gallery
From first principles, each idea drawn on screen: pixels and coordinates, vectors, length, dot and cross products, projection, reflection, matrices built up gently (rules that move points, where the axes go, by hand, working backwards), rotation in 2D and 3D, perspective, a camera, and the shaping functions.
A free interactive course of 40 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. A shader is a function of the pixel
Everything in main() runs once for every pixel on the screen, all at once on the graphics card. It has one job: write this pixel's colour into fragColor. It can't see the other pixels, so every picture in this course is built by answering one question: what colour is this one pixel?
A colour is four numbers from 0 to 1: red, green, blue and alpha (opacity). vec4(1.0, 1.0, 1.0, 1.0) is white, vec4(0.0, 0.0, 0.0, 1.0) is black, vec4(1.0, 0.0, 0.0, 1.0) is pure red. A vec4 is simply a bundle of four numbers.
GLSL is strict about numbers: 1.0 is a decimal (a float), 1 is a whole number (an int), and mixing them is an error. Write the .0.
Changes run as you type (no need to compile or refresh). A mistake keeps the last working version on screen and shows the error on its line.
Your turn: Make the whole screen orange: fragColor = vec4(1.0, 0.5, 0.0, 1.0);
2. Every pixel knows where it is
gl_FragCoord.xy is this pixel's position in pixels, counted from the bottom-left corner. resolution is the size of the screen in pixels. Divide one by the other and you get uv: 0 at the left/bottom edge, 1 at the right/top, whatever the screen size.
uv is a vec2: two numbers in one variable, uv.x and uv.y. Maths on a vector works on each number, so gl_FragCoord.xy / resolution divides x by the width and y by the height in one go.
The easiest way to see numbers in a shader is to use them as a colour. Here red is uv.x, so the screen fades from black on the left to red on the right.
Your turn: Show uv.y too, as green: fragColor = vec4(uv.x, uv.y, 0.0, 1.0); (top-right becomes yellow: red + green).
3. Centre and aspect: the coordinates we'll use
uv has two problems for drawing. (0, 0) is in a corner, and a unit across is more pixels than a unit up (screens are wider than tall), so this circle comes out squashed and off in the corner.
The fix: subtract half the resolution, so (0, 0) is the middle, and divide both x and y by the height, so a unit is the same number of pixels in both directions. Then y runs from -0.5 (bottom) to 0.5 (top), and x a bit further on a wide screen.
The circle is drawn with length and step, both explained soon: for now, it's everything within 0.3 of (0, 0). Every step from here starts with this p.
Your turn: Use centred, square coordinates: vec2 p = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
4. Vectors: a point is two numbers
A vec2 is two numbers, and you can read it two ways: as a position (a point on the screen) or as an arrow (go this far right, this far up). The maths is the same either way, and shaders switch between the two readings all the time.
Here p is this pixel's position and c is a point. The grid lines are 0.1 apart and the bright lines are the axes, x = 0 and y = 0, crossing at the middle. The orange dot is drawn at c.
Your turn: Move the dot up and right: set c to vec2(0.3, 0.2) (3 grid squares right, 2 up).
5. Components and swizzles
The numbers inside a vector are its components: v.x, v.y, v.z, v.w. For colours the same slots are also called .r, .g, .b, .a: col.r is col.x.
You can pick several at once, in any order, which GLSL calls swizzling: p.yx swaps x and y, col.bgr reverses a colour, v.xxx repeats x three times. And you can build bigger vectors out of smaller ones: vec3(p, 0.0), vec4(col, 1.0).
Here the colour is built from the position: red grows to the right, green upwards.
Your turn: Swap red and blue with a swizzle: change col = col.rgb; to col = col.bgr;.
6. Length: Pythagoras
How long is the arrow p? It goes p.x across and p.y up: the two sides of a right-angled triangle. Pythagoras says the long side is sqrt(p.x * p.x + p.y * p.y). That's length(p), built into GLSL.
Since p is this pixel's position, length(p) is how far the pixel is from the middle. Shown as brightness it's a smooth glow outwards, and the rings mark every 0.1. Everything within 0.25 is drawn orange: a circle is just 'the pixels whose length is less than the radius'.
Your turn: Prove it's the same thing: replace length(p) with sqrt(p.x * p.x + p.y * p.y). The picture shouldn't change.
7. Adding vectors: arrows head to tail
Vectors add one component at a time: a + b is vec2(a.x + b.x, a.y + b.y). As arrows, that means: follow a, then follow b from where you ended up. The sum is the arrow straight from the start to the finish.
Orange is a, blue is b drawn from the tip of a. The green arrow s should be the total trip, but it's zero for now.
Your turn: Set vec2 s = a + b;: the green arrow lands exactly on the blue tip, however b turns.
8. Subtracting: the arrow between two points
Subtraction is the other way round: b - a is the arrow that goes from a to b (whatever you add to a to get to b). This one is everywhere in shaders.
The most important case is p - c: the arrow from a point c to this pixel. Drawing with p - c instead of p moves the origin to c, which is how you place anything anywhere.
distance(a, b) is just length(b - a).
Your turn: Make the green arrow go from the orange dot to the blue one: vec2 d = b - a;
9. Distance: circles anywhere
Put the last two steps together: length(p - c) is how far this pixel is from the point c. That's distance(p, c). Every pixel gets its own distance, so the rings below are drawn around wherever the point is.
A circle of radius r around c is distance(p, c) < r. Subtract the radius, distance(p, c) - r, and you get a signed distance: negative inside, 0 on the edge, positive outside. That idea (a shape described by the distance to it) is the basis of 2D shapes and of raymarching.
Your turn: Measure from a moving point: replace length(p) with distance(p, vec2(0.3 * sin(time), 0.0)).
10. Scaling: multiplying by a number
Multiplying a vector by a single number multiplies each component: v * 2.0 points the same way and is twice as long. v * 0.5 is half as long, and a negative number flips it round: v * -1.0 points the opposite way. Dividing by k is multiplying by 1/k.
Scaling p itself zooms the whole picture. With p * 2.0, twice as much of the plane fits on screen, so everything looks half the size: coordinates and shapes go opposite ways, which will keep coming up.
Your turn: Make the blue arrow half as long and pointing backwards: vec2 b = a * -0.5;
11. Multiplying vectors: one component at a time
a * b with two vectors multiplies matching components: vec2(a.x * b.x, a.y * b.y). (It is not the dot product, which comes next.) It's how you scale differently in each direction.
On colours it's tinting: col * vec3(1.0, 0.5, 0.2) keeps the red, halves the green and mostly removes the blue. On coordinates it stretches space: p * vec2(1.0, 2.0) doubles every y, so shapes come out half as tall.
Your turn: Squash the circle into an ellipse: vec2 q = p * vec2(1.0, 2.0);
12. Normalize: direction without length
normalize(v) keeps an arrow's direction but makes its length exactly 1: it's v / length(v). A length-1 vector is called a unit vector, and most maths with directions (angles, lighting, reflections, rays) expects them.
Here each pixel is coloured by the direction from the middle to it: normalize(p) has x and y between -1 and 1, and * 0.5 + 0.5 maps that to 0..1 for colour. Every pixel on a line out from the centre has the same colour, however far out it is: the length has gone.
Mapping -1..1 to 0..1 with x * 0.5 + 0.5 (and back with x * 2.0 - 1.0) is something you'll do constantly.
Your turn: The length is still there in length(p): draw a dark circle of radius 0.3 over the colours with col *= smoothstep(0.0, 0.02, abs(length(p) - 0.3)); (the colour doesn't change as you cross it).
13. The dot product: how much along
dot(a, b) multiplies the components and adds them up: a.x * b.x + a.y * b.y. The answer is a single number, not a vector.
When b is a unit vector, dot(a, b) answers: how far does a go in the direction of b? So dot(p, dir) is each pixel's position measured along dir. It's 0 on the line through the middle at right angles to dir, positive on the side the arrow points to and negative behind it. The stripes are drawn from it, so they always run across the arrow.
With dir = vec2(1.0, 0.0), dot(p, dir) is just p.x: measuring along a direction is a generalisation of reading off a coordinate.
Your turn: Make the direction turn: set dir to vec2(cos(time), sin(time)) (always length 1).
14. Dot and angles
For two unit vectors, dot(a, b) is the cosine of the angle between them: 1 when they point the same way, 0 at right angles, -1 when they point opposite ways. Nothing else about the vectors matters, only the angle.
So dot(normalize(p), dir) compares the direction to each pixel with dir. The faint half is where it's above 0 (less than 90° away: 'in front'), and the bright wedge is where it's above 0.95 (within about 18°: a spotlight cone).
cos 45° ≈ 0.7, cos 60° = 0.5, cos 90° = 0. Tests like dot(a, b) > 0.0 ('is it in front of me?') are used everywhere, because they avoid ever computing an angle.
Your turn: Widen the cone to about 45°: change step(0.95, c) to step(0.7, c).
15. Projection: the closest point on a line
Where on the line from a to b is closest to a point? Two arrows start at a: ba = b - a (orange, along the line to b) and pa = p - a (pink, out to the point; here the wandering pink dot). Measure how far the pink arrow goes along the orange one: dot(pa, ba) / dot(ba, ba). That's 0 at a and 1 at b (dividing by dot(ba, ba), the length squared, makes it a fraction of the way). The closest point is a + ba * h.
dot(v, v) is the length squared: it saves a square root when you only need to compare or divide.
The rings show the distance to the line, and the pink dot is projected onto it (dashed across to its shadow on the line). Right now h can be anything, so it's an endless line: watch the shadow run past b and behind a.
Your turn: Limit it to the segment between the dots: float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0); The rings become a capsule, the shape of a line segment.
16. Reflection
A ball bouncing off a wall: the wall is described by its normal n, the unit vector sticking straight out of it. The incoming direction d splits into two parts: along the normal, dot(d, n) * n (into the wall, since d heads towards it), and along the wall (the rest).
Bouncing keeps the part along the wall and reverses the part into it. Subtract the normal part twice (once to cancel it, once to send it back out): r = d - 2.0 * dot(d, n) * n. That's what GLSL's reflect(d, n) does, and it's how mirrors, shiny highlights and bouncing rays work.
Your turn: Bounce it: vec2 r = d - 2.0 * dot(d, n) * n; (or reflect(d, n)).
17. Dot for lighting
A surface facing a light is lit; one turned away is dark. The direction a surface faces is its normal n, a unit vector. With l the unit vector towards the light, dot(n, l) is 1 facing the light, 0 edge-on, negative facing away. max(dot(n, l), 0.0) is the diffuse (Lambert) lighting used almost everywhere.
This disc pretends to be a ball, in 3D now (vec3: x right, y up, z out of the screen towards you). On a sphere of radius 1 the normal at (x, y) is vec3(x, y, sqrt(1 - x² - y²)): pointing straight at you in the middle and sideways at the rim.
Your turn: Light it from the left instead of the right: make the x of l negative, e.g. normalize(vec3(-1.0, 1.0, 1.0)).
18. The cross product: at right angles to both
cross(a, b) (3D only) gives a vector at right angles to both a and b: vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x). Its length is the area of the parallelogram they span, so it's zero when they're parallel. Its direction follows the right-hand rule: swap a and b and it points the other way.
Its main job in shaders is building normals and camera axes. Here's a bumpy height map h(x, y): stepping a little in x climbs by some amount, stepping in y by another. Both little arrows tx and ty lie along the surface, so cross(tx, ty) sticks straight out of it: the normal, ready for the lighting from the last step.
Right now the normal is always vec3(0, 0, 1) (flat), so the lighting is flat too.
Your turn: Compute the real normal: vec3 n = normalize(cross(tx, ty));
19. Before matrices: moving every point
Matrices are next, and they trip almost everyone up, so here's the idea underneath them first.
A transformation is just a rule: give it a point, it gives back a new point. apply below is one. The picture runs it on a whole grid of dots (and the lines between them), so you see what it does to everything at once. Orange dots are the top-right quarter, so you can follow them; the white one is (0, 0).
Right now it hands every point back unchanged. Rules you could try: v * 2.0 (twice as big), vec2(-v.x, v.y) (a mirror), vec2(v.y, v.x) (swap x and y).
The ones a matrix can do all have one shape: each new number is some amount of x plus some amount of y, and nothing else. The mirror is: new x = -1 × x + 0 × y. So is a lean, where points slide right by half their height:
new x = 1 × x + 0.5 × y
new y = 0 × x + 1 × yRules like that keep straight lines straight, keep evenly spaced dots evenly spaced, and leave (0, 0) where it is.
Your turn: Make the lean: return vec2(v.x + 0.5 * v.y, v.y);
20. Every point is built from the axes
Why does 'some x plus some y' matter? Because every point is built that way. Call one step along x the arrow X = (1, 0) (red), and one step up Y = (0, 1) (green). The point (3, 2) is 3 steps of X then 2 of Y: 3 × X + 2 × Y.
apply here does exactly that, v.x * X + v.y * Y, but with X and Y as variables. The pink path is the recipe for the orange dot 3 across and 2 up: 3 dot-steps along X, then 2 along Y.
So if you change X, every point changes with it, because every point is made of X. Point X a little upwards, to (1, 0.5), and each step to the right now also climbs half a step. The dot is wherever its path ends.
Your turn: Change X: vec2 X = vec2(1.0, 0.5); and follow the pink path.
21. A matrix is where the axes go
A mat2 is nothing more than the X and Y from the last step, kept together: two columns of two numbers. The first column is where one step along x goes, the second where one step along y goes. m[0] and m[1] read them back.
m * v is the same recipe: v.x * m[0] + v.y * m[1], v.x steps of the first column plus v.y steps of the second. The red and green arrows are the two columns; the grid and the L show the whole plane after m.
The four numbers go in a column at a time, which is the part everyone gets wrong:
mat2(1.0, 0.0, // column 0: x goes here
0.0, 1.0) // column 1: y goes hereThat one leaves both axes where they are: the identity, which changes nothing. Change the first column to (2, 0) and one step along x becomes two: everything is twice as wide, and nothing happens to heights.
Your turn: Send the x axis to (2, 0), doubling everything's width: mat2 m = mat2(2.0, 0.0, 0.0, 1.0);
22. Shear: moving one axis
Matrices can be built from their columns directly: mat2(vec2(...), vec2(...)). It's clearer when you're thinking about axes.
Leave x alone and tip the y axis over to (0.5, 1): every point moves right by half its height. Squares become parallelograms and the L leans. This is a shear, and it can't be made from rotating or stretching alone.
Any arrangement of the two columns is a valid matrix. Rotation, next, is just a particular choice of columns.
Your turn: Tip the y axis: mat2 m = mat2(vec2(1.0, 0.0), vec2(0.5, 1.0));
23. Working one out by hand
Doing one on paper, the way the GPU does, makes it stick. A worked example first: mat2(2.0, 1.0, -1.0, 1.0) * vec2(3.0, 2.0). The columns are (2, 1) and (-1, 1), so it's 3 lots of the first plus 2 lots of the second:
3 × ( 2, 1) = ( 6, 3)
2 × (-1, 1) = (-2, 2)
added: ( 4, 5)Now yours. Here m = mat2(1.0, 1.0, -1.0, 1.0) and v = (2, 1), and one grid square is 1. The arrows draw the recipe (red: 2 steps of column 0; green: 1 of column 1), and the blue dot is the GPU's answer.
Work it out yourself before you look at where the dot is, then type it into guess: the orange ring lands on the blue dot when you're right.
Your turn: Put your answer for m * v in guess (written like vec2(1.0, 2.0)).
24. Rotation, built from the axes
Turn the x axis (1, 0) by an angle a. It lands on (cos a, sin a): that's what cos and sin are, the x and y of a point on a circle of radius 1. (Angles are in radians: a full turn is 2π ≈ 6.283, a quarter turn π/2 ≈ 1.571.)
The y axis is a quarter turn further round than x. A quarter turn takes (x, y) to (-y, x), so y lands on (-sin a, cos a).
Those are the two columns: mat2(vec2(c, s), vec2(-s, c)) with c = cos(a), s = sin(a). That's the rotation matrix, and it came from nothing but 'where do the axes go'.
Your turn: Fill in the columns: mat2 m = mat2(vec2(c, s), vec2(-s, c)); The whole plane turns.
25. Shaders work backwards
Here's the twist behind most matrix confusion in shaders. A shader can't move a shape: each pixel can only ask 'what colour am I?'. So to draw an L that m has moved, each pixel works backwards: which point of the original L would m have sent here? If that point is in the L, colour the pixel.
Working backwards is the inverse, inverse(m), the matrix that undoes m. The code uses m * p instead, running the move forwards on the pixels, and the L comes out moved the opposite way: narrower instead of wider, leaning the wrong way. Compare it with the orange dots, which were moved properly.
That's why shader code so often seems backwards: making p bigger makes shapes smaller, adding to p moves shapes the other way, and turning p turns the picture the other way.
Your turn: Work backwards: vec2 q = inverse(m()) * p; The L's corners land on the orange dots.
26. Rotating shapes: turn the coordinates
Same idea with turning: to draw a box turned by some angle, turn the pixel's coordinates by the opposite angle, then ask 'is this in the (unturned) box?'.
For rotations the inverse is simple: rotating back by -a. So rot(a) * p draws shapes turned by -a, clockwise. Swap to rot(-a) if the direction matters.
GLSL shortcut: p * m (vector on the left) multiplies by the transpose (the matrix flipped across its diagonal), and for a rotation the transpose is the inverse.
Your turn: Spin the box: use rot(time) instead of rot(0.5).
27. Turning around a point
A matrix always keeps (0, 0) where it is, so rotations turn around the origin. Here the box's centre c is subtracted after rotating, so the whole picture turns around the middle and the box orbits it.
To turn around c instead, move c to the origin first (p - c), then rotate. The order of operations is the whole trick: translate, then rotate, then draw.
(Moving isn't a matrix in 2D because it would move the origin. 3D graphics fixes that with an extra coordinate always equal to 1, which is why vertex shaders use mat4 for 3D.)
Your turn: Spin the box in place: vec2 q = rot(time) * (p - c);
28. Combining matrices: order matters
Matrices multiply into one: m = a * b does b first, then a (m * v = a * (b * v): read right to left). So rot * shear and shear * rot are different transforms.
Grid lines here are drawn in the transformed space, so you can see what the matrix does to the whole plane.
Your turn: Add a shear: mat2 m = rot(time * 0.3) * mat2(1.0, 0.0, 0.5, 1.0);
29. Determinant and inverse
determinant(m) is how much m scales areas: m[0].x * m[1].y - m[1].x * m[0].y. Rotations have 1 (areas unchanged); doubling x gives 2. At 0 the matrix squashes the whole plane onto a line: information is lost, and there's no way back.
A negative determinant means the plane has been flipped over, like in a mirror: the L comes out backwards. The background turns red when that happens.
inverse(m) undoes m: inverse(m) * (m * v) == v. It exists whenever the determinant isn't 0. We've been using it to draw the transformed grid all along.
Your turn: Make a mirror, sending x to (-1, 0): mat2 m = mat2(-1.0, 0.0, 0.0, 1.0);
30. 3D points and perspective
In 3D a point has three numbers: x right, y up, z into the screen (away from the camera). This cube's 8 corners are all the combinations of ±1, and its 12 edges join corners that differ in just one coordinate.
The screen is flat, so every 3D point has to be projected to 2D. Ignoring z (v.xy) is an orthographic view: the back face sits exactly behind the front one, so you see a single square.
Real perspective divides by the distance: v.xy / v.z. Things twice as far away are drawn half the size. That one division is all perspective is. The front corners are orange, the back ones blue.
Your turn: Turn on perspective: in project, change return v.xy * 0.25; to return v.xy / v.z;
31. Rotation in 3D: about the y axis
A mat3 has three columns: where x, y and z go. Same idea as mat2, one more axis.
Turning about the y axis (like a record on a turntable, seen from the side): y doesn't move, so the middle column is (0, 1, 0). x and z turn like the 2D rotation: x goes to (cos a, 0, -sin a) and z to (sin a, 0, cos a).
rotY is written out below. Apply it to every corner before projecting.
Your turn: Spin the cube: change transform to return rotY(time) * v;
32. Combining 3D rotations
Turning about x (nodding) leaves x alone and turns y and z: rotX. There's a rotZ too (tilting your head), which leaves z alone and is exactly the 2D rotation.
Combine them by multiplying, right to left like before: rotX(b) * rotY(a) * v turns about y first, then about x. Swapping the order gives a different result: rotations in 3D don't commute (try it with a book: turn it about two axes in different orders).
Your turn: Tumble it: return rotX(time * 0.7) * rotY(time) * v;
33. The shorthand: p.xz *= rot(a)
Open almost any raymarcher and you'll find lines like p.xz *= rot(a); with a 2D rot. It's a 3D turn written as a 2D one: p.xz is two of p's numbers, x and z, treated as a 2D point and turned. y isn't touched, so it turns about the y axis, like rotY.
The pair you pick sets the axis: the one left out is the one it turns about. .xz turns about y, .yz about x, .xy about z.
p.xz *= m means p.xz = p.xz * m, with the vector on the left. That multiplies by the transpose, which for a rotation turns the other way. Nobody minds: if it spins the wrong way, flip the sign of the angle.
Your turn: Tumble about x instead: v.yz *= rot(time);
34. Rotation about any axis
To turn about any unit axis k, split the point v in two, using everything so far. The part along the axis, k * dot(k, v) (projection), doesn't move. The rest goes round in a circle, in the plane at right angles to k.
In that plane, the rest of v and cross(k, v) are like the x and y axes of a 2D rotation (the cross product is exactly a quarter turn of it). So: v * c + cross(k, v) * s + k * dot(k, v) * (1 - c). It's called Rodrigues' formula, and rotAxis below is it.
The pink line is the axis.
Your turn: Turn about a diagonal: make axis() return normalize(vec3(1.0, 1.0, 0.0)).
35. A camera from cross products
A camera needs three unit axes: forward f (towards what it looks at), right r and up u. Cross products build them: r = normalize(cross(f, worldUp)) is at right angles to forward and to the sky, then u = cross(r, f) is at right angles to both.
Now use them as a matrix's columns, where the axes go: mat3(r, u, f) sends the screen's x to the camera's right, y to its up and z to its forward. mat3(r, u, f) * vec3(p, 1.5) turns a pixel into a direction in the world: its ray. That's exactly how a raymarching camera aims (see the Raymarching course).
Here the rays are just coloured by direction (sky gradient, sun, ground), and the camera looks around as target moves.
Your turn: Look further round: change the target's x from sin(time * 0.3) to sin(time * 0.3) * 3.0.
36. Interpolation: mix, step, smoothstep, clamp
mix(a, b, t) blends: a at t = 0, b at t = 1, in between on a straight line (a + (b - a) * t). It works on numbers, vectors and colours.
step(edge, x) is 0 below the edge and 1 above: a hard cut. smoothstep(e0, e1, x) goes from 0 to 1 smoothly between e0 and e1: soft edges, anti-aliasing, fades. clamp(x, lo, hi) keeps x in range.
The graph shows the curve y = f(x) for x across the screen; the background uses the same value as the mix between two colours.
Your turn: Soften the hard edge: replace step(0.0, x) with smoothstep(-0.2, 0.2, x).
37. abs: symmetry for free
abs(x) makes negatives positive. Applied to a coordinate, it folds space: abs(p.x) makes the left half a copy of the right, a mirror down the middle. abs(p) folds both ways: whatever you draw in the top-right quarter appears in all four.
It works on distances too: abs(d) turns a filled shape (negative inside) into its outline, and abs(d) - w into an outline of thickness w (the blue ring).
Your turn: Mirror everything into all four quarters: vec2 q = abs(p);
38. fract and floor: repetition
floor(x) rounds down to a whole number; fract(x) is what's left, x - floor(x): it climbs from 0 to 1 and jumps back, over and over. So fract(p * 4.0) chops the plane into cells, 4 per unit, each with its own 0..1 coordinates. Subtract 0.5 and each cell is centred: draw one shape and you get a whole grid of them.
floor(p * 4.0) tells you which cell you're in (an id), so every copy can be a bit different. mod(x, n) is the same as fract but repeating every n.
Your turn: Tile it: vec2 q = fract(p * 4.0) - 0.5;
39. Trigonometry and polar coordinates
sin and cos go smoothly between -1 and 1 and repeat every 2π, which makes them the source of most waves, pulses and loops in shaders (0.5 + 0.5 * sin(time) goes 0..1 and back).
atan(y, x) goes the other way from vec2(cos(a), sin(a)): the angle of a point, from -π to π. Together with length, it gives polar coordinates (angle, radius), the natural space for anything round.
Here the radius of the shape changes with the angle, r = 0.25 + 0.08 * cos(n * a): n bumps around the circle.
Your turn: Give it 7 petals and let it turn: cos(7.0 * a + time).
40. Falloff: glows, fades and highlights
Three curves turn a distance into a brightness. 1.0 / d shoots up near 0 and fades slowly: glows and lights (0.01 / d for a small bright one). exp(-k * d) fades smoothly, faster for bigger k: fog, soft shadows. pow(x, n) on a 0..1 value sharpens it: pow(dot(r, l), 32.0) is a shiny highlight.
Glows add up past 1, which the screen can't show, so the last line squashes brightness with 1.0 - exp(-col): a simple tone map.
Your turn: Swap the hard dot for a glow: float glow = 0.01 / d;
Other courses: Basics · Medium · Advanced · 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