Soap bubbles
glint · Examples · Tutorials · Gallery
Thin-film interference: a bubble's colour is light reflected off the front and back of a film a few hundred nanometres thick, so it shifts with thickness (thinner at the top, swirling) and viewing angle.

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
One pass, in GLSL.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
in vec2 v_texcoord;
out vec4 fragColor;
float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + 17.0; a *= 0.5; }
return v;
}
vec3 cameraRay(vec3 ro, vec3 target, vec2 uv, float zoom) {
vec3 f = normalize(target - ro);
vec3 r = normalize(cross(vec3(0.0, 1.0, 0.0), f));
vec3 u = cross(f, r);
return normalize(uv.x * r + uv.y * u + zoom * f);
}
vec3 room(vec3 d) {
vec3 c = mix(vec3(0.08, 0.1, 0.15), vec3(0.45, 0.55, 0.65), d.y * 0.5 + 0.5);
c += vec3(1.0, 0.95, 0.85) * pow(max(dot(d, normalize(vec3(0.5, 0.6, -0.6))), 0.0), 30.0) * 2.0; // a window
c += vec3(1.0, 0.7, 0.4) * pow(max(dot(d, normalize(vec3(-0.7, 0.2, -0.5))), 0.0), 60.0); // a lamp
return c;
}
float sphere(vec3 ro, vec3 rd, vec4 s) {
vec3 oc = ro - s.xyz;
float b = dot(oc, rd), h = b * b - dot(oc, oc) + s.w * s.w;
return h < 0.0 ? -1.0 : -b - sqrt(h);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 ro = vec3(0.0, 0.0, -3.0), rd = normalize(vec3(uv, 1.4));
vec3 col = room(rd) * 0.6;
for (int i = 0; i < 3; i++) {
float fi = float(i);
vec4 s = vec4(sin(time * 0.3 + fi * 2.1) * 0.9, sin(time * 0.4 + fi * 1.3) * 0.4, fi * 0.8, 0.75 - fi * 0.2);
float t = sphere(ro, rd, s);
if (t < 0.0) continue;
vec3 n = normalize(ro + rd * t - s.xyz);
float cosT = abs(dot(n, rd));
float fresnel = 0.04 + 0.96 * pow(1.0 - cosT, 5.0);
// Film thickness: drains downwards, swirls.
float thick = 0.3 + 0.35 * (0.5 - 0.5 * n.y) + 0.35 * fbm(n.xy * 2.5 + vec2(time * 0.15, fi));
// The path difference between the two reflections decides which colours add up and which cancel.
vec3 film = 0.5 + 0.5 * cos(6.28318 * (thick * 3.0 * cosT + vec3(0.0, 0.33, 0.67)));
col = col * (1.0 - fresnel * 0.5) + room(reflect(rd, n)) * film * (fresnel + 0.12);
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More More GLSL examples
Apollonian · Digital rain · Bokeh · ASCII · Sierpinski tetrahedron · CRT television · Lightning · Candle