Vertex shaders 3: living geometry
glint · Examples · Tutorials · Gallery
After Vertex shaders 2: a flag, ocean waves, grass and gusts, a snake, bones, pushing with the mouse, point clouds that change shape, a photo in 3D, voxels, the superformula, Unknown Pleasures from the music, shadows and reflections on the floor, a kaleidoscope of meshes, a tunnel, ribbons that face you, a forest and fur.
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. A flag in the wind
A plane (Shape › Plane, 64 segments) as cloth. flag(uv) says where the cloth is at each point: flat for now. A travelling wave, sin(uv.x * 8.0 - time * 4.0), rolls along it from the pole.
Multiplying by uv.x pins it: at the pole (u = 0) the wave is 0, and it grows towards the free end, as a real flag does. The normals come from the shape itself (Vertex shaders 2's trick), so the folds catch the light, and the fragment shader lights both sides.
Your turn: Wave it: float wave = 0.15 * uv.x * sin(uv.x * 8.0 - time * 4.0 + uv.y * 2.0);
2. Ocean waves
Four sine waves in different directions, each moving at the speed real water waves of that length do (sqrt(9.8 * k): long waves are faster). But they're too smooth: sine waves are as round on top as underneath, and real waves have sharp crests and wide troughs.
In a real wave each bit of water goes round in a circle: forwards at the crest, backwards in the trough. Moving the points sideways as well as up (Gerstner waves) bunches them up at the crests and spreads them out in the troughs. STEEP is how much: at 1 the crests are as sharp as they can get before they'd fold over.
Your turn: Sharpen the crests: const float STEEP = 0.8;
3. Grass
4,000 instances of one blade: two quads and a tip, 15 corners, each corner knowing how far up the blade it is (t, 0 at the root) and which side. Every blade gets its own place, height and turn from its hash.
They stand bolt upright, like pins. Real grass leans: push each corner downwind by bend * t * t, so the root stays put and the tip moves most (t² makes it curve rather than tilt), and dip it a little as it leans.
Your turn: Let it lean in the wind: float bend = 0.25 + 0.1 * sin(time * 2.0);
4. Gusts
The whole field bends together, like a single sheet: real wind comes in gusts that roll across the grass in waves.
Read noise at each blade's place, scrolling with time (base * 0.6 - vec2(time * 1.5, 0.0)): it moves across the field like a gust, and nearby blades get similar values while distant ones don't. That's the whole trick behind the grass in games.
Your turn: Gusts of wind: float bend = 0.1 + 0.6 * noise(base * 0.6 - vec2(time * 1.5, 0.0));
5. A snake
A tube (Vertex shaders, step by step built one) along a path that circles and wiggles. spine says where the body's middle is; right now it's a fixed stretch of the path, path(along * 3.0), so the snake is a still shape.
A snake's body follows its head: every part goes where the head went a moment before. So the body at along is at the path's point time - along * 3.0: the head is at now, the tail three seconds of path behind. The whole body slithers along the curve.
Your turn: In spine, follow the head: return path(time - along * 3.0);
6. Bones
Characters bend with skinning: a skeleton of bones, each a transform, and every corner of the skin following a mix of the bones near it. This arm has two bones: the lower one still, the upper one turning about the elbow.
w is how much a corner follows the upper bone. At 0 below the elbow and 1 above, with a hard switch, the arm folds like a hinge with the skin crushed into a crease. Blending the weights over a stretch round the elbow makes it bend smoothly, like an arm.
Your turn: Blend round the elbow: float w = smoothstep(JOINT - 0.3, JOINT + 0.3, y);
7. Pushing with the mouse
A grid of dots that shy away from the pointer (mouse: move it over the picture; with the code hidden (Ctrl .) it's easier). Each dot works out its distance from the pointer and moves directly away from it by push.
The push should be strong close by and nothing far away: 0.12 * exp(-r * r * 40.0) is a smooth bump, 0.12 at the pointer and almost 0 by 0.3 away. The dots move further the nearer they are, and the grid bulges round the pointer like a lens.
Your turn: Push them: float push = 0.12 * exp(-r * r * 40.0);
8. Clouds of points that change shape
30,000 points, each with a place on a sphere (A) and a place near a knot (B). Mixing by m morphs between them, but all together, like a mesh.
Points are free to go one by one. Give each its own delay from its hash h: smoothstep(h * 0.5, h * 0.5 + 0.5, wave) turns from 0 to 1 at a different moment for each point, so the shape dissolves and reassembles, streaming across.
Your turn: Stagger it: float m = smoothstep(h * 0.5, h * 0.5 + 0.5, 0.5 + 0.5 * sin(time * 0.6));
9. A photo in 3D
90,000 points in a grid, each reading its colour from a photo (the stock street texture) in the vertex shader: a picture made of dots, flat, turning with the camera.
Use what the vertex shader read: push each point out of the picture by its brightness, l. Bright parts come forward, dark ones stay back, and turning, the photo becomes a relief: a cheap depth effect used in music videos and on album art.
Your turn: Lift by brightness: float z = l * 0.6;
10. Voxels
4,096 instanced cubes in a 16 × 16 × 16 block, one per cell. Any shape described by a distance function (the raymarching courses' map) can be built from them: keep the cubes whose middle is inside it (distance below 0) and shrink the others to nothing.
Animating the shape makes cubes pop in and out: the look of voxel games and LED cubes.
Your turn: Keep only the inside: float keep = step(field(centre), 0.0);
11. Shapes from a formula
Johan Gielis's superformula gives a radius for every angle, and with a few numbers makes stars, flowers, shells and seed pods. Doing it once round (r1) and once up (r2), and multiplying like a globe's latitude and longitude, wraps the sphere mesh into a 3D one.
m is the number of lobes. At 0 every radius is 1: a sphere. The other numbers (animated here) round or sharpen them.
Your turn: Give it lobes: float m = 6.0;
12. Unknown Pleasures
Joy Division's album cover: pulses from a star, stacked, each line hiding the ones behind it. Pass 0 (History) keeps the music's recent past: its bottom row is the spectrum now, and every frame each row copies the one below, so the rows scroll up through time. (Without audio it makes up the famous shape.)
Pass 1 (Lines) draws 50 lines, each a strip of quads, plus a black curtain under each: drawn in front of the lines behind, it hides them (depth test). Every line's height is flat for now. history(u, L) reads line L's moment in the past at u across.
Your turn: In the Lines pass's Vertex tab: float h = history(u, L);
13. Shadows on the floor
Proper shadows need a whole extra render from the light's point of view. On a flat floor there's a classic shortcut: draw the object again, squashed flat onto the floor along the light's direction, in a dark colour. That's the shadow, exactly, for nothing but a second instance.
Instance 1 is the copy. Each of its corners slides along the light direction (LIGHT) until it reaches the floor: by (world.y - FLOOR) / LIGHT.y of it. Right now the copy is drawn where the object is, so it's hidden inside it.
Your turn: Flatten the copy onto the floor: after vec3 n = …, add if (copy) world -= LIGHT * (world.y - FLOOR) / LIGHT.y;
14. Reflections in the floor
A glossy floor reflects what's above it upside down. Another second instance: flip it about the floor's height (world.y = 2 * FLOOR - world.y) and flip its normals too, so it's lit as the reflection would be. Dimmed, it reads as a polished floor.
Games used this for decades for mirrors and water. (The copy is inside out after a flip, which matters when back faces are hidden; glint draws both sides, so it doesn't here.)
Your turn: Mirror the copy: after vec3 n = …, add if (copy) { world.y = 2.0 * FLOOR - world.y; n.y = -n.y; }
15. A kaleidoscope of meshes
Twelve copies of one petal (a stretched torus), every other one mirrored. All twelve are drawn in the same place, so they look like one.
Turn each copy round the middle by its share of the circle: copy i by i × 30°. With every other one mirrored, pairs of petals face each other, and the twelve make a 3D kaleidoscope that moves as one petal moves.
Your turn: Spread them round: float a = float(i / 2) * 6.2832 / float(instanceCount / 2);
16. A tunnel to fly through
A tube built from gl_VertexID (24 sides, 60 rings), with perspective by hand (w = distance), its rings sliding towards the camera to give the feeling of flying.
It's dead straight. Make its middle wander with distance: at distance z, the centre is off by sin(z * 0.2 + time) across and cos(z * 0.15 + time * 0.7) up, scaled by z so the bend starts from where you are. The tunnel snakes away ahead, and since the bend moves with time, it turns as you fly.
Your turn: Bend it: vec2 centre = vec2(sin(z * 0.2 + time), cos(z * 0.15 + time * 0.7) - 1.0) * z * 0.12;
17. Ribbons that face you
The thick lines of Vertex shaders 2 were flat, in 2D. In 3D a ribbon needs an "across" direction at every point, and a fixed one (here straight up) fails: where the curve itself runs up and down, the ribbon is edge-on and vanishes, and elsewhere it twists.
Work in the camera's space, where the camera is at the origin: the direction to the camera is -e. Across should be at right angles to both the curve (T) and that, which is their cross product. Then the ribbon always shows its full width, like a billboard stretched along a curve.
Your turn: Face the camera: vec3 side = normalize(cross(T, normalize(-e)));
18. A forest on the hills
Instance 0 is the ground (the sphere mesh flattened and lifted by ground, a noise height), and instances 1 to 300 are trees, each the same mesh pulled into a cone, with its own place and size.
But the trees all stand at height 0: some float in the air, others are buried in the hills. Each tree should stand on the ground where it is: ground(at), the same function the ground used.
Your turn: Plant them: float y = ground(at);
19. Fur from shells
Fur in real time: draw the object many times, each copy (a shell) pushed a little further out along its normals. In each shell, the fragment shader keeps only the pixels where a hair reaches that high: each hair is a cell of a fine grid with a random length, so higher shells have fewer and fewer hairs.
Stacked, the shells look like fur, dark at the roots and light at the tips, drooping and swaying. 24 shells (instances) are drawn, but with LEN at 0 they all sit on the skin.
Your turn: Grow the fur: const float LEN = 0.25;
20. Pull it apart: skating grass
The meadow with gusts again. But something's off: the blades don't bend. Watch closely: whole blades slide sideways and back, roots and all, as if the grass were skating over the ground.
The push is the same for every corner of a blade. Look at how the push is applied, and at what should make the root stay put while the tip moves most.
Your turn: Make the grass bend again.
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 · Fractals 2: further in · Post-processing · Surfaces: from vertex to fragment · Blur and glow