Three.js From Zero · Article s16-08
Julia Sets — Dragging a Knob Through Infinite Universes
Two lines turn the Mandelbrot shader into a Julia explorer: drag c on a picture-in-picture Mandelbrot map and morph through connected and shattered universes live. Part of the MasterAllArts Creative 3D Roadmap.
Part of the MasterAllArts Creative 3D Roadmap .
Season 16 · Article 08 · Beautiful Math
Last article, every pixel asked "does my own address escape?" Change two lines — let the pixel be the starting point and make the constant a knob — and the same iteration produces the Julia sets: a different infinite fractal for every value of c. The Mandelbrot set turns out to be the map of all of them. So that's the interface: a little Mandelbrot chart in the corner, a marker you drag across it, and the main canvas morphing through universes under your finger.
The inset map (top right) is the Mandelbrot set with a white ring on it — drag the ring and the main view morphs live (dragging pauses the tour). Drag the main canvas to pan, scroll to zoom. Watch what happens as the marker crosses the Mandelbrot's boundary: inside, the Julia set is one connected piece; the instant you step outside, it shatters into a dust of infinitely many disconnected points. That's a real theorem — the marker is your finger on the Fatou–Julia dichotomy.
One map, infinitely many territories
Both fractals run the identical iteration z → z² + c. The only question is what varies per pixel:
// Mandelbrot (S16-07): the pixel is the CONSTANT
z = 0; c = pixel;
// Julia (this article): the pixel is the STARTING POINT
z = pixel; c = uC; // one constant for the whole image — a uniform
So a Mandelbrot image answers one question about every c at once, while a Julia image explores a single c in full depth. The deep fact connecting them: the Julia set of c is connected exactly when c lies inside the Mandelbrot set. The Mandelbrot set is literally the index of the Julia catalog — which is why it keeps appearing when you zoom into Julia-like regions of anything.
Step 1 — The two-line diff
In the shader from last article, this is the entire change:
vec2 z = uCenter + vec2((vUv.x - 0.5) * uScale * uAspect,
(vUv.y - 0.5) * uScale); // was: c = ...
vec2 c = uC; // was: z = vec2(0.0)
Everything else — escape loop, smooth coloring, cosine palette, pan/zoom camera — transfers untouched. This is the payoff of building rigs instead of demos: article seven's machinery renders article eight's mathematics with a two-line patch and one new uniform.
Step 2 — Picture-in-picture without a second renderer
The corner map is not a second canvas, scene, or render pass. It's a second quad in the same scene wearing a Mandelbrot material, parked in a corner of clip space. The vertex shader applies the mesh's transform and nothing else:
// vertex shader for BOTH quads:
gl_Position = vec4((modelMatrix * vec4(position, 1.0)).xy, 0.0, 1.0);
// JS: park the inset top-right, keep it square on resize
inset.scale.set(H / aspect, H, 1); // H ≈ 0.62 of clip height
inset.position.set(1 - 0.04 - H / aspect / 2, 1 - 0.06 - H / 2, 0);
modelMatrix is one of the uniforms Three.js gives every ShaderMaterial for
free. The main quad's transform is identity, so it still fills the screen; the inset's
transform shrinks it into a HUD element. One scene, two materials, two draw calls — that's
the whole compositing system.
The inset shader is a mini-Mandelbrot (fixed window, lower iteration cap) plus a marker —
a white ring drawn with smoothstep at the current uC:
float d = abs(length(plane - uC) - 0.06); // distance to a circle
col = mix(vec3(1.0), col, smoothstep(0.012, 0.025, d));
Step 3 — Dragging a complex number
The pointer logic is a tiny state machine: on pointer-down, test whether you're inside the
inset's screen rectangle. If yes, the drag writes uC (mapping inset UV to the
complex window); if no, it pans the main view. The mapping is the same
center-plus-scaled-offset formula as everything else in these two articles — by now you've
written it four times and it's yours.
Step 4 — The grand tour
The opening animation drives c in a circle of radius 0.7885 around the origin — a path that repeatedly grazes the Mandelbrot boundary, so the Julia set cycles through connected → shatter → reconnect, sprouting and retracting dendrites as it goes:
theta += dt * 0.15;
uC.value.set(0.7885 * Math.cos(theta), 0.7885 * Math.sin(theta));
A parametric path through parameter space is a movie of universes — one float of "time" driving two floats of "where." When you build your own generative pieces, this is the cheapest possible choreography: don't animate the artwork, animate its parameters.
Famous addresses
- rabbit (−0.123 + 0.745i) — Douady's rabbit, three ears forever.
- dragon (−0.8 + 0.156i) — a seahorse-dragon hybrid, barely connected.
- dendrite (0 + 1i) — pure branching skeleton, zero interior: c sits exactly on the Mandelbrot boundary.
- spiral (−0.7269 + 0.1889i) — galaxies inside galaxies; zoom in.
The skeleton
<script type="importmap">
{ "imports": {
"three": "https://unpkg.com/three@0.170.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/" } }
</script>
<script type="module">
import * as THREE from 'three';
// main quad: Julia material (S16-07 rig, two-line diff) (Step 1)
// inset quad: Mandelbrot material + uC marker, parked
// in clip space via modelMatrix (Step 2)
// pointer: inset hit-test → drag uC, else pan/zoom (Step 3)
// grand tour: c = 0.7885 · e^(iθ) (Step 4)
</script>
Next: S16-09 — Wave Interference. A GPU ripple tank: tap to drop sources, watch circular waves superpose into moiré cathedrals — the physics of "adding sines" made touchable.