HDRI reflections
glint · Examples · Tutorials · Gallery
Three balls lit by an HDR sky: chrome, rough gold and red plastic, each a mix of the sky's blurred light and its reflection, by Fresnel. The Sky pass draws an HDR sky into a cube map, or copies your own: load an .hdr (polyhaven.com/hdris, free) into texture0 in Inputs. The Skies and reflections tutorial builds it step by step.

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.
The code
2 passes, drawn in this order each frame, in GLSL.
Sky: fragment shader
#version 300 es
precision highp float;
uniform mat3 cubeFace;
uniform vec2 resolution;
uniform sampler2D texture0;
uniform float time;
in vec2 v_texcoord;
out vec4 fragColor;
// The environment, as a cube map: this runs for every pixel of each of the six faces, and cubeFace turns the
// pixel's place on its face into a direction. Load an .hdr into texture0 (Inputs) and it's copied in instead.
// Where a direction is in an equirectangular picture: longitude across, latitude up.
vec2 equirect(vec3 d) {
return vec2(atan(d.x, -d.z) / 6.2832 + 0.5, asin(clamp(d.y, -1.0, 1.0)) / 3.1416 + 0.5);
}
// A made-up HDR sky: the sun is 60 times brighter than white, which only a float picture can keep.
vec3 sky(vec3 d) {
vec3 sun = normalize(vec3(0.6, 0.3, -0.5));
vec3 col = mix(vec3(0.9, 0.75, 0.6), vec3(0.2, 0.4, 0.85), smoothstep(-0.05, 0.5, d.y));
col += vec3(1.0, 0.6, 0.3) * pow(max(dot(d, sun), 0.0), 6.0) * 0.6;
col += vec3(60.0, 52.0, 42.0) * smoothstep(0.9990, 0.9994, dot(d, sun));
if (d.y < 0.0) {
vec2 q = d.xz / -d.y * 1.5;
float check = mod(floor(q.x) + floor(q.y), 2.0);
col = mix(col, vec3(0.25, 0.22, 0.2) * (0.6 + 0.4 * check), smoothstep(0.0, -0.08, d.y));
}
return col;
}
void main() {
vec3 d = normalize(cubeFace * vec3(gl_FragCoord.xy / resolution * 2.0 - 1.0, 1.0));
vec3 col = sky(d);
if (textureSize(texture0, 0).x > 1) col = textureLod(texture0, equirect(d), 0.0).rgb;
fragColor = vec4(col, 1.0);
}Picture: fragment shader
#version 300 es
precision highp float;
uniform samplerCube pass0;
uniform vec2 resolution;
uniform float time;
uniform vec2 mouse;
uniform float exposure;
uniform float goldRoughness;
in vec2 v_texcoord;
out vec4 fragColor;
// Image-based lighting: each surface mixes the sky's light from all round (a tiny, blurred mip along the normal)
// with its reflection (sharp or blurred by roughness, along reflect()), by Fresnel.
struct Ball { vec3 c; vec3 base; float rough; float metal; };
Ball ball(int i) {
if (i == 0) return Ball(vec3(-2.2, 0.0, 0.0), vec3(0.95), 0.0, 1.0); // chrome
if (i == 1) return Ball(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.75, 0.3), goldRoughness, 1.0); // gold
return Ball(vec3(2.2, 0.0, 0.0), vec3(0.8, 0.1, 0.08), 0.15, 0.0); // red plastic
}
float hitBall(vec3 ro, vec3 rd, vec3 c) {
vec3 oc = ro - c;
float b = dot(oc, rd), h = b * b - dot(oc, oc) + 1.0;
return h < 0.0 ? -1.0 : -b - sqrt(h);
}
// The nearest ball along the ray (index, or -1) and how far.
int trace(vec3 ro, vec3 rd, out float t) {
t = 1e9;
int hit = -1;
for (int i = 0; i < 3; i++) {
float d = hitBall(ro, rd, ball(i).c);
if (d > 0.001 && d < t) { t = d; hit = i; }
}
return hit;
}
vec3 sky(vec3 d, float rough) { return textureLod(pass0, d, rough * 8.0).rgb; }
// One surface: metals reflect in their own colour, plastics in white over their colour.
vec3 shade(Ball b, vec3 n, vec3 rd, vec3 refl) {
vec3 f0 = mix(vec3(0.04), b.base, b.metal);
float cosv = max(dot(n, -rd), 0.0);
vec3 F = f0 + (1.0 - f0) * pow(1.0 - cosv, 5.0) * (1.0 - b.rough * 0.7);
vec3 diffuse = textureLod(pass0, n, 7.0).rgb * b.base * (1.0 - b.metal);
return diffuse * (1.0 - F) + refl * F;
}
void main() {
vec2 p = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float a = time * 0.1 + mouse.x * 6.2832;
vec3 ro = vec3(sin(a) * 9.0, 1.4 + (mouse.y - 0.5) * 5.0, cos(a) * 9.0);
vec3 fw = normalize(-ro), rt = normalize(cross(vec3(0.0, 1.0, 0.0), fw)), up = cross(fw, rt);
vec3 rd = normalize(fw * 1.8 + rt * p.x + up * p.y);
vec3 col = sky(rd, 0.0);
float t;
int i = trace(ro, rd, t);
if (i >= 0) {
Ball b = ball(i);
vec3 pos = ro + rd * t, n = normalize(pos - b.c);
vec3 r = reflect(rd, n);
// The reflection: the sky, or another ball (lit by the sky only, one bounce).
vec3 refl = sky(r, b.rough);
float t2;
int j = trace(pos + n * 0.002, r, t2);
if (j >= 0) {
Ball b2 = ball(j);
vec3 n2 = normalize(pos + r * t2 - b2.c);
refl = mix(refl, shade(b2, n2, r, sky(reflect(r, n2), b2.rough)), 1.0 - b.rough);
}
col = shade(b, n, rd, refl);
}
col = 1.0 - exp(-col * exposure); // tone mapping: any brightness into 0..1
fragColor = vec4(pow(col, vec3(1.0 / 2.2)), 1.0);
}More Games, sound & cube maps examples
Cube map reflections · Textured room · Stand inside (VR) · Video wall