Cube map reflections
glint · Examples · Tutorials · Gallery
A cube map pass draws a whole sky (sun, clouds, a horizon) into the six faces of a cube map every frame; the picture is a chrome blob raymarched in front of it, reflecting it with texture(pass0, direction). Turn on Forest to reflect the stock forest cube map instead.

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 float time;
uniform sampler2D grayNoise;
in vec2 v_texcoord;
out vec4 fragColor;
// A cube map pass: this runs for every pixel of each of the six faces.
// cubeFace turns the pixel's place on its face into a direction.
float clouds(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) {
v += a * texture(grayNoise, p).x;
p = p * 2.03 + 0.17;
a *= 0.5;
}
return v;
}
void main() {
vec3 d = normalize(cubeFace * vec3(gl_FragCoord.xy / resolution * 2.0 - 1.0, 1.0));
vec3 sun = normalize(vec3(cos(time * 0.15), 0.35, sin(time * 0.15)));
vec3 col = mix(vec3(1.0, 0.7, 0.45), vec3(0.2, 0.45, 0.9), smoothstep(-0.02, 0.45, d.y));
col += vec3(1.0, 0.75, 0.45) * pow(max(dot(d, sun), 0.0), 8.0) * 0.5;
col += vec3(4.0, 3.2, 2.2) * smoothstep(0.9975, 0.999, dot(d, sun));
if (d.y > 0.0) {
vec2 p = d.xz / (d.y + 0.15) * 0.08 + time * 0.004;
float c = smoothstep(0.45, 0.8, clouds(p));
col = mix(col, vec3(1.0, 0.95, 0.9) * (0.7 + 0.5 * max(dot(d, sun), 0.0)), c * smoothstep(0.0, 0.15, d.y));
} else col = mix(vec3(0.15, 0.13, 0.12), col, exp(d.y * 40.0));
fragColor = vec4(col, 1.0);
}Image: fragment shader
#version 300 es
precision highp float;
uniform samplerCube pass0;
uniform samplerCube forest;
uniform vec2 resolution;
uniform float time;
uniform vec2 mouse;
uniform bool useForest;
in vec2 v_texcoord;
out vec4 fragColor;
vec3 env(vec3 d) { return useForest ? texture(forest, d).rgb * 1.2 : texture(pass0, d).rgb; }
float map(vec3 p) {
float t = time * 0.7;
float d = length(p) - 0.8;
for (int i = 0; i < 4; i++) {
float fi = float(i);
vec3 c = 0.65 * vec3(sin(t + fi * 1.7), cos(t * 1.3 + fi * 2.3), sin(t * 0.9 + fi * 0.8));
float b = length(p - c) - 0.38;
float k = 0.45;
float h = clamp(0.5 + 0.5 * (b - d) / k, 0.0, 1.0);
d = mix(b, d, h) - k * h * (1.0 - h);
}
return d;
}
vec3 normalAt(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float a = time * 0.2 + (mouse.x - 0.5) * 6.0;
vec3 ro = vec3(sin(a) * 3.5, 0.6 + (mouse.y - 0.5) * 2.0, cos(a) * 3.5);
vec3 fw = normalize(-ro), rt = normalize(cross(vec3(0, 1, 0), fw)), up = cross(fw, rt);
vec3 rd = normalize(fw * 1.6 + rt * uv.x + up * uv.y);
float t = 0.0;
bool hit = false;
for (int i = 0; i < 96; i++) {
float d = map(ro + rd * t);
if (d < 0.001) { hit = true; break; }
t += d;
if (t > 10.0) break;
}
vec3 col = env(rd);
if (hit) {
vec3 p = ro + rd * t, n = normalAt(p);
float fres = 0.6 + 0.4 * pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
col = env(reflect(rd, n)) * vec3(0.95, 0.93, 0.9) * fres;
}
col = col / (1.0 + col * 0.25);
fragColor = vec4(pow(col, vec3(0.9)), 1.0);
}More Games, sound & cube maps examples
Keyboard maze · Little synth · Textured room · Stand inside (VR) · Video wall · HDRI reflections