Fractals 2: further in

glint · Examples · Tutorials · Gallery

After Fractals, step by step: other powers, skipping the inside, stripe colouring, lighting the set, the inside's cycles, the Phoenix and Lyapunov fractals, the bifurcation diagram, the chaos game on any polygon, the dragon, fractal flames, the Buddhabrot, a photocopier fractal, trees by folding, biomorphs, spheres inside spheres, hybrids, glowing fractals and fractals that listen.

A free interactive course of 20 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. Other powers

Fractals, step by step left the Mandelbrot set squaring z. Any power works: z³ + c, z⁴ + c… the Multibrot sets. Raising to the n-th power is easiest in polar form: the length to the power n, the angle times n (cpow).

Power n gives n − 1 big lobes, arranged round the middle with (n − 1)-fold symmetry. Non-whole powers work too, though a branch cut (a seam) appears where the angle jumps from −π to π.

Your turn: Cube it: const float POWER = 3.0;

2. Skip the inside

The black inside of the Mandelbrot set is the most expensive part: those pixels never escape, so they always run all MAX rounds. SHOW_COST is on: the bright areas are where the work goes, and here that's 1,000 rounds each.

Its two biggest parts, the heart-shaped main cardioid and the round disc on its left, have exact formulas. Testing for them first skips all that work for most of the inside. It's the cheapest, biggest speed-up any Mandelbrot viewer has.

Your turn: Test first: bool inside = q * (q + (c.x - 0.25)) < 0.25 * c.y * c.y || dot(c + vec2(1.0, 0.0), c + vec2(1.0, 0.0)) < 0.0625;

3. Stripes from the orbit

Another way to colour the outside: as z flies off, it turns round the middle. Add up something that depends on its angle each round (sin(5 * angle)) and take the average over the orbit. Neighbouring points' orbits wind round in nearly the same way, so the average changes smoothly and draws flowing stripes that follow the set's structure, like contour lines on a map.

This is stripe-average colouring. avg is worked out; it's just not used yet.

Your turn: Use it: col = mix(col * 0.15, vec3(1.0, 0.9, 0.7), avg);

4. Lighting the set

With the derivative dz (Fractals, step by step used it for the distance), z / dz points down the slope of the escape speed, like the normal of a landscape. Light it like a surface: brighter where it faces the light, darker where it faces away.

t is that light, from a light going round. The flat colours become embossed ridges and valleys, with no 3D at all.

Your turn: Light it: col *= t;

5. What's inside: cycles

Inside the set, z doesn't fly off, but it doesn't wander either: it settles into a loop, visiting the same few points over and over. The main cardioid's points settle on one point (a period of 1), the big disc's swap between two, and each smaller bulb has its own number: 3 for the top and bottom ones, and so on.

After the main loop, period counts how many more steps it takes z to come back to where it was. Colour the inside by it and the bulbs light up in families.

Your turn: Colour the inside: col = palette(float(period) / 7.0);

6. The Phoenix

The Phoenix fractal remembers: the next z is z² + c plus some of the z before last. That bit of memory turns the Julia set's islands into feathery wings.

prev is z one round ago, and p_ is how much of it is fed back. At 0 it's an ordinary Julia set (c = 0.5667, outside the Mandelbrot set: a dust of points). The classic value is −0.5.

Your turn: Feed the past back: float p_ = -0.5;

7. Lyapunov: order and chaos

The logistic map, x = r·x·(1 − x), is a famous model of a population. For small r it settles down; past about 3.57 it goes chaotic. The Lyapunov fractal switches between two values of r (a across the screen, b up) in a repeating sequence of As and Bs, and colours each (a, b) by whether the result is stable (gold) or chaotic (blue).

The measure (the Lyapunov exponent) is how fast two nearly equal starting points fly apart: negative means they come together (order). The sequence is stored as bits: AB is 0b10 = 2, two steps long. AABAB is 0b10100 = 20, five long, and gives Markus's famous swallow-like shapes.

Your turn: Use the sequence AABAB: const int SEQ = 20, LEN = 5;

8. The road to chaos

The logistic map again, drawn another way: one column for each r, and in it every value x keeps coming back to once it has settled. 120,000 points (a vertex shader: Vertices only, Points), each letting x settle then stepping a little further.

On the left, one value: stable. Then it splits in two, four, eight, faster and faster, until chaos at about 3.57. Inside the chaos are windows of order (look for the clear band with three branches near 3.83), and each window holds the whole picture again, smaller.

Your turn: In the Vertex tab, zoom into the chaos: const vec2 R_RANGE = vec2(3.5, 4.0);

9. The chaos game, on any polygon

The chaos game from Fractals, step by step, simplified: corners of a polygon, and each step jumps towards a random one. With a triangle and half-way jumps, the points can only ever land on the Sierpinski triangle.

With more corners, the jump has to be longer for the copies to just touch instead of overlapping into a blur: ratio works it out from N (1/2 for 3, about 0.38 for 5, 1/3 for 6). Each point is coloured by the last corner it jumped to, which shows the copies.

Your turn: In the Vertex tab, a pentagon: const int N = 5;

10. The dragon

The Heighway dragon (the curve on the cover of Jurassic Park's novel) is two maps: both shrink by √2 and turn 45°, and the second is flipped round and moved along. Each is a complex multiplication. The fractal is exactly two half-size copies of itself, joined.

which says which map a point went through last, so it tells which of the two copies the point is in. All one colour, the two copies can't be told apart.

Your turn: In the Vertex tab, colour the copies: vec3 colour = mix(vec3(1.0, 0.4, 0.2), vec3(0.2, 0.6, 1.0), which);

11. Fractal flames

Scott Draves's fractal flames (the Electric Sheep screensaver) add one thing to the chaos game: after each jump, the point is bent by a non-linear function, a variation. Straight copies become curling, smoky shapes.

This is the triangle game with each point's colour drifting towards the colour of the corners it visits. swirl turns points by an angle that grows with their distance from the middle. Apply it after each jump.

Your turn: In the Vertex tab, bend it: replace the comment with x = swirl(x);

12. The Buddhabrot

Turn the Mandelbrot set inside out: instead of colouring c by how fast it escapes, follow the orbits of escaping points and light up everywhere they go. Added up over millions of orbits, a seated Buddha appears (it's turned upright here).

Pass 0 draws points along orbits of random c (each vertex one point of one orbit), with Clear off and additive blending, so every frame adds to the last. Pass 1 divides by the number of frames and shows it. It gets smoother the longer it runs.

Right now it draws the orbits that never escape: the anti-Buddhabrot, a ghostly cloud. The famous one keeps only those that escape (n < MAX).

Your turn: In the Orbits pass's Vertex tab, keep the escapers: bool keep = n < MAX;

13. The photocopier

A fractal can grow by itself through feedback. Every few frames (EVERY) this pass reads its own last picture (feedback), shrinks it to half size and pastes it into MAPS places: one round of copying. It starts from a rainbow; within a few rounds that has shrunk away and only the shape of the copying is left.

With two copies, one above and to the side of the other, all that's left is a line. With three, in a triangle, it's the Sierpinski triangle again, from a completely different direction: whatever goes in, the same shape comes out. That's what makes it the copying's own fractal (its attractor).

Your turn: Add the third copy: const int MAPS = 3;

14. Trees by folding

A tree is self-similar too: every branch is a smaller tree. Folding draws it with one segment: from the top of the trunk, mirror (abs(p.x): left and right the same), turn by the branching angle, shrink, and measure the same segment again. Ten rounds: 2,047 branches.

With an angle of 0 the branches all go straight up, inside each other: a pole. Turn them and the tree grows; animate the angle and it sways and opens.

Your turn: Branch out: float angle = 0.45 + 0.08 * sin(time);

15. Biomorphs

In 1986 Clifford Pickover was drawing Julia sets of z³ + c and made a test that looked wrong: a point counts as inside if either part of z stayed small, real or imaginary, not both. What came out looked like single-celled creatures, with bodies and hairs: biomorphs.

The loop stops when either part passes 10. Colouring by the test, abs(z.x) < 10.0 || abs(z.y) < 10.0, turns the plain shape into an organism.

Your turn: Use Pickover's test: bool body = abs(z.x) < 10.0 || abs(z.y) < 10.0;

16. Spheres inside spheres

The Apollonian circles of Fractals, step by step, in 3D: fold space into a repeating cell, invert it in a sphere, again and again. It never ends: the camera drifts through a sponge of spheres packed between spheres, at every size.

S is the inversion's strength, as in 2D. Small changes repack everything at once, at every scale.

Your turn: Let it breathe: S = 1.1 + 0.08 * sin(time * 0.3);

17. Hybrids

Fractal formulas can be mixed: do one kind of fold on some rounds and another on the rest. This is the Mandelbox (Fractals, step by step), and on every other round it can also fold into a corner (abs) and turn.

Hybrids are where most of the strange architecture in fractal art comes from. i % 2 == 1 picks the odd rounds; try i % 3 == 0, or i < 3, after.

Your turn: Mix in a fold and turn on odd rounds: replace the comment with if (i % 2 == 1) { p = abs(p); p.xz = turn * p.xz; }

18. Glowing fractals

Glow from near misses (Raymarching 2) is made for fractals: their surfaces are full of fine detail that rays skim past, so the glow traces it in light.

Add up a little light at every step of the march, more the closer the ray is (d) to the fractal, weighted by the step so it doesn't depend on the step count.

Your turn: In the march, gather glow: glow += d * 0.004 / (0.0004 + d * d);

19. Fractals that listen

Julia sets are very sensitive to c: moving it by a hundredth changes the shape. That makes them perfect for music: nudge c by the bass and the set breathes with it.

spectrum is bass, mid and treble (Inputs panel: turn on audio). With no audio it's 0, so add time for a slow drift as well.

Your turn: Drive c with the music: vec2 c = vec2(-0.8, 0.156) + 0.03 * vec2(sin(time * 0.3), cos(time * 0.2)) + 0.06 * vec2(spectrum.x, -spectrum.y);

20. Pull it apart: the vanishing copier

The photocopier with four tinted copies, each slowly turning: it should settle into a coloured fractal and stay. Instead, it appears for a moment and fades away to black.

Every frame is made from the last one. Look at what happens to the colour on its way through.

Your turn: Fix it so the fractal stays.

Other courses: Basics · 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 · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment · Blur and glow