Skies and reflections
glint · Examples · Tutorials · Gallery
Cube maps and HDR skies: a sky all around, a mirror ball, Fresnel, rough reflections from mipmaps, light from the whole sky, tone mapping, and your own .hdr photo (Poly Haven) as the sky and its reflections.
A free interactive course of 7 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 sky all around
A cube map is six square pictures folded into a box around you. You don't read it at a place on a picture, like a texture: you read it in a direction, and get what you'd see looking that way. That makes it just right for a sky, and for anything that reflects one.
This shader works out rd, the direction the camera looks through each pixel (camera() is at the top: drag across the picture to turn it). For now it shows the direction itself as a colour: x as red, y as green, z as blue.
glint has a few cube maps built in. uniform samplerCube forest; is one of them: a clearing in a wood.
Your turn: Show the forest instead: vec3 col = texture(forest, rd).rgb;
2. A mirror ball
Now a ball. sphere() says how far along the ray it is (or -1 if the ray misses), so ro + rd * t is the point on its surface, and for a ball at the origin that point is also its normal n, the way the surface faces. For now the ball shows its normal as a colour.
A mirror sends the ray on, bounced off the surface: reflect(rd, n) is that new direction (it's rd - 2.0 * dot(rd, n) * n: the part of rd going into the surface turned round). Whatever the sky is in that direction is what the mirror shows.
Your turn: Make it a mirror: col = texture(forest, reflect(rd, n)).rgb;
3. Fresnel: shiny at the edges
Most things aren't mirrors. A red plastic ball shows its own colour where you look straight at it, but towards its edges, where you see the surface at a glancing angle, it reflects more and more, until right at the rim it's nearly a mirror. That's Fresnel, and it's much of what makes things look real.
Schlick's formula is the usual way to write it. f0 is how much the surface reflects head on (4% for plastic, glass and water), and dot(n, -rd) is the cosine of the angle you see it at: 1 head on, 0 at the rim.
F = f0 + (1 - f0) * (1 - cos)^5Right now F is just f0, so the reflection is a faint 4% everywhere and the ball looks flat.
Your turn: Write Schlick's formula: float F = f0 + (1.0 - f0) * pow(1.0 - max(dot(n, -rd), 0.0), 5.0);
4. Rough surfaces
A rough surface is covered in tiny bumps facing every which way, so it reflects a whole cone of directions at once: the reflection is blurred, more the rougher it is.
Blurring the sky for every pixel would be slow, but a cube map comes with its blur ready-made: mipmaps, smaller and smaller copies (each pixel the average of four). textureLod(map, dir, level) reads a chosen one: level 0 is the sharp sky, and each level up is half the size and twice as blurred. The forest is 256 across, so level 8 is a single pixel, the whole face averaged.
The roughness slider (Params) is already there, 0 to 1. Turn it into a level and read the reflection from that.
Your turn: Blur the reflection: vec3 spec = textureLod(forest, reflect(rd, n), roughness * 8.0).rgb;
5. Light from the whole sky
The ball's own colour is still lit by a made-up 0.5. Really, a matte surface is lit by everything it can see: the whole half of the sky its normal faces, averaged. Facing up it gets the bright sky; facing down, the dark ground.
That average is a blur so wide it's a soft glow, and glint has it ready: forestBlur is the forest blurred that much. Read it along the normal (not the reflection: a matte surface doesn't care where you're looking from).
Together, that's the whole trick of image-based lighting: the blurred sky along n for the colour underneath, the sharp (or rough) sky along reflect for the shine, mixed by Fresnel.
Your turn: Light it with the sky: vec3 diffuse = texture(forestBlur, n).rgb;
6. Brighter than white: HDR
A real sky isn't between 0 and 1. The sun is thousands of times brighter than a white wall, and that matters for reflections: in a rough ball, the sun blurs into a glow that's still bright, not a grey smudge. A sky that keeps those values is an HDR (high dynamic range) sky.
This project now draws its own sky in a cube map pass (the Sky tab), with a sun 60 times brighter than white. Cube map passes store floats, so that survives. The Picture pass reads it as pass0: the same ball as before. For a sky of your own there's no ready-blurred twin, so the light underneath comes from a tiny mip (level 7) instead.
But a screen still stops at 1, so the sky looks blown out and the sun's glow clips to a hard white. Tone mapping squeezes any brightness into 0..1, gently: 1.0 - exp(-col * exposure) is nearly col for dim colours and approaches 1 for bright ones. exposure (Params) is the camera's: try it after.
Your turn: In Picture, tone map before the gamma: col = 1.0 - exp(-col * exposure);
7. Your own HDRI
Real HDR skies are photographs: a camera on a tripod taking the whole sphere at many exposures. Poly Haven (polyhaven.com/hdris) has hundreds, free to use for anything (CC0). Download one as .hdr (1K or 2K is plenty), then in Inputs › texture0 press the image button and pick the file. glint keeps it as floats, so the sun stays as bright as it was.
Those files are equirectangular: the whole sphere unrolled into a 2:1 rectangle, like a world map, longitude across and latitude up. equirect(d) in the Sky pass (already written) turns a direction into that map's coordinates.
So the Sky pass can copy the picture into its cube map: each face pixel looks up its direction in the map. Why not read the map straight from the Picture pass? A cube map has even pixels all round (the map squashes the poles) and proper mipmaps for the roughness, with no seam where the map's edges meet.
With no picture in texture0, textureSize(texture0, 0).x is 1, so the line below keeps the made-up sky until you load one.
Your turn: In Sky, use the picture: if (textureSize(texture0, 0).x > 1) col = textureLod(texture0, equirect(d), 0.0).rgb;
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 · 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