Glass orbs

glint · Examples · Tutorials · Gallery

Raymarched glass: refraction through spheres with dispersion (a separate ray per colour), Fresnel reflections and a studio backdrop, orbiting to the beat.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Glass orbs: an audio-reactive GLSL shader

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;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float ior;
uniform float dispersion;

in vec2 v_texcoord;
out vec4 fragColor;

mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }

// Ray from an orbiting camera: yaw from mouse.x (plus slow drift), pitch from mouse.y.
void orbit(vec2 uv, float dist, float drift, out vec3 ro, out vec3 rd) {
    float yaw = (mouse.x - 0.5) * 6.2831 + time * drift;
    float pitch = clamp((mouse.y - 0.5) * 2.5, -1.4, 1.4);
    ro = vec3(0.0, 0.0, dist);
    ro.yz *= rot(pitch);
    ro.xz *= rot(yaw);
    vec3 fw = normalize(-ro), rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
    rd = normalize(uv.x * rt + uv.y * up + 1.8 * fw);
}

vec3 orbCenter(int i) {
    float fi = float(i);
    float a = time * 0.4 + fi * 2.094 + spectrumIntegrated.x * 0.3;
    return vec3(cos(a) * 0.9, 0.15 * sin(time * 1.3 + fi) + beat * 0.1, sin(a) * 0.9);
}
float map(vec3 p) {
    float d = 1e9;
    for (int i = 0; i < 3; i++) d = min(d, length(p - orbCenter(i)) - 0.45);
    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)));
}
// The room: a gradient backdrop with bright stripe lights to refract and reflect.
vec3 env(vec3 rd) {
    vec3 c = mix(vec3(0.05, 0.05, 0.08), vec3(0.25, 0.25, 0.3), rd.y * 0.5 + 0.5);
    c += vec3(1.0, 0.9, 0.8) * smoothstep(0.02, 0.0, abs(fract(atan(rd.z, rd.x) * 1.2) - 0.5) - 0.44) * smoothstep(-0.2, 0.4, rd.y) * 1.5;
    c += vec3(0.4, 0.2, 1.0) * pow(max(rd.y, 0.0), 8.0);
    float checker = mod(floor(rd.x / (abs(rd.y) + 0.1) * 3.0) + floor(rd.z / (abs(rd.y) + 0.1) * 3.0), 2.0);
    return mix(c, vec3(0.15 + 0.1 * checker), step(rd.y, -0.05));
}
float march(vec3 ro, vec3 rd, float sgn) {
    float t = 0.0;
    for (int i = 0; i < 80; i++) {
        float d = sgn * map(ro + rd * t);
        if (d < 0.001 || t > 12.0) break;
        t += max(d, 0.002);
    }
    return t;
}
// Trace one colour channel through the glass: enter, refract, exit, refract.
vec3 traceChannel(vec3 ro, vec3 rd, float eta) {
    float t = march(ro, rd, 1.0);
    if (t > 12.0) return env(rd);
    vec3 p = ro + rd * t, n = normalAt(p);
    float fres = 0.04 + 0.96 * pow(1.0 - max(dot(n, -rd), 0.0), 5.0);
    vec3 refl = env(reflect(rd, n));
    vec3 rin = refract(rd, n, 1.0 / eta);
    vec3 p2 = p - n * 0.005;
    float t2 = march(p2, rin, -1.0);
    vec3 q = p2 + rin * t2, n2 = -normalAt(q);
    vec3 rout = refract(rin, n2, eta);
    if (dot(rout, rout) < 0.01) rout = reflect(rin, n2);   // total internal reflection
    return mix(env(rout) * vec3(0.92, 0.97, 1.0), refl, fres);
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro, rd;
    orbit(uv, 3.2, 0.05, ro, rd);
    vec3 col = vec3(traceChannel(ro, rd, ior - dispersion).r, traceChannel(ro, rd, ior).g, traceChannel(ro, rd, ior + dispersion).b);
    fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}

More Showpieces examples

Glitch · Quaternion Julia · Aurora · Caustics · Four to the floor