The sphere
2026-03-07
The homepage has a lumpy sphere on it that breathes and rotates. I put it there because I wanted something to look at, and because I wanted to learn a little more about shaders. The math turned out to be more interesting than I expected. Here is how it got built.
Start with the right shape
The obvious way to make a sphere is a UV sphere โ stack latitude rings, connect them with triangles. It works, but it has a problem. The triangles pinch together at the poles. You get dense clusters at the top and bottom and wide, stretched faces at the equator. For a textured globe that is fine, but for a low-poly look where you can actually see the geometry, it is ugly.
A better starting point is an icosahedron. Twenty faces, every one an equilateral triangle. Subdivide each face and project the new vertices back onto the sphere, and you get roughly uniform triangles everywhere. No pole artifacts, no equator stretching.
Look at the top and bottom of the UV sphere on the left. Triangles converge to a point. The icosahedron on the right distributes them evenly. Three.js gives you this for free with IcosahedronGeometry.
Fake noise with sine waves
The terrain displacement uses a noise function, but not Perlin or Simplex. You could import a library for those. I wanted something simpler. It sums five sine waves with irrational-ish frequency multipliers: things like 1.27, 3.43, 2.17. Because the multipliers avoid small rational relationships, the waves never quite line up. The sum looks and behaves like noise: continuous, bounded, no obvious repetition. Cheaper than real noise and no dependencies. It also ports trivially to GLSL, because it is just sin() calls.
Push the vertices around
A noise function on its own is just a number. To turn it into terrain you sample it at every vertex of the sphere and use the result to push the vertex outward. The base mesh is a unit sphere, which means the direction outward from any vertex is just the vertex itself. Position and normal are the same vector. The whole displacement step is one line:
new_position = unit_position * (radius + noise * amplitude)
Vertices grow taller where the noise is high and sink where it is low. Do this for thousands of vertices at once and the smooth sphere turns into a lumpy, continent-looking thing. No UV mapping, no displacement textures, no heightfield. Just a function sampled at a point.
Fractal brownian motion
One layer of noise gives you smooth hills. To get terrain that looks interesting you layer multiple octaves at different scales. This is fractal brownian motion. Each octave doubles the frequency and halves the amplitude. Click through to see how detail builds up.
The faint lines are individual octaves. The bold line is the sum. With one octave you get gentle rolling hills. By four there is detail at every scale, big features and fine texture coexisting, like real terrain.
Time as a fourth dimension
A static lumpy sphere is fine, but the one on the homepage breathes. The obvious ways to animate it โ keyframed vertex positions, tweening between two noise fields, lerping parameters โ are all fiddly and most look bad. The trick is that the noise function takes a fourth argument, w, treated exactly like x, y, and z. Feed it the clock time. Every frame, w ticks forward a little, and the noise values at every vertex shift to whatever they would have been one slice later in a 4D field. The terrain morphs continuously. No keyframes, no tweens, no discontinuities. From the outside it looks alive. Under the hood it is one extra argument.
The orbital rotation on top of that is unremarkable. A rotateOnWorldAxis call on the parent group every frame, with some friction on the velocity when you let go after dragging.
Wireframe without a wireframe
Instead of drawing a separate wireframe on top of the mesh (which has z-fighting issues and doubles the draw calls), each triangle's vertices get barycentric coordinates: (1,0,0), (0,1,0), (0,0,1). In the fragment shader, you check how close the current pixel is to an edge by taking the minimum of the three values. Near an edge, at least one approaches zero. A smoothstep on that minimum gives you clean anti-aliased lines in a single draw call. Toggle between the raw coordinates and the edge detection.
The coordinate view shows each vertex's contribution as an RGB channel. The edge view runs smoothstep on the minimum coordinate. That is the entire edge detection algorithm. One line of shader code.
All on the GPU
The entire displacement runs in the vertex shader. The CPU never touches the geometry after creation. It uploads the base icosahedron once and the GPU handles noise evaluation, FBM layering, and vertex displacement every frame. Drag the detail slider to see what that means in practice.
At detail 20 you are pushing around 8,000 triangles with FBM noise computed per vertex per frame. Crank it to 80 and you are north of 100,000 triangles, and the frame rate barely drops. That is the GPU doing its thing.
The same noise function exists in both TypeScript and GLSL so the CPU can sample terrain heights when needed without diverging from what the shader produces.
That is the sphere. A subdivided icosahedron, sine-wave noise, four octaves of FBM, time as a fourth dimension, barycentric wireframe, all on the GPU. Simpler than it looks once you break it down.