Chrome balls

glint · Examples · Tutorials · Gallery

Ray tracing, not marching: spheres and a floor hit exactly with the quadratic formula, bouncing four times for reflections of reflections, with hard shadows.

Chrome balls: a 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;

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))); }

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);
}

vec4 S[5]; // centre, radius
const vec3 SUN = normalize(vec3(0.6, 0.8, -0.3));

// Where the ray first meets the sphere (a quadratic in t), or -1.
float sphere(vec3 ro, vec3 rd, vec4 s) {
    vec3 oc = ro - s.xyz;
    float b = dot(oc, rd), c = dot(oc, oc) - s.w * s.w, h = b * b - c;
    return h < 0.0 ? -1.0 : -b - sqrt(h);
}

// Nearest hit: t, normal, and what it was (0-4 a sphere, 5 the floor, -1 nothing).
float trace(vec3 ro, vec3 rd, out vec3 n, out int id) {
    float t = 1e9;
    id = -1;
    for (int i = 0; i < 5; i++) {
        float h = sphere(ro, rd, S[i]);
        if (h > 0.001 && h < t) { t = h; id = i; n = normalize(ro + rd * h - S[i].xyz); }
    }
    if (rd.y < 0.0) {
        float h = -ro.y / rd.y;
        if (h > 0.001 && h < t) { t = h; id = 5; n = vec3(0.0, 1.0, 0.0); }
    }
    return t;
}

vec3 sky(vec3 rd) {
    vec3 c = mix(vec3(0.9, 0.85, 0.8), vec3(0.2, 0.4, 0.8), clamp(rd.y * 1.5, 0.0, 1.0));
    return c + vec3(1.0, 0.9, 0.7) * pow(max(dot(rd, SUN), 0.0), 200.0) * 4.0;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    S[0] = vec4(0.0, 0.8, 0.0, 0.8);
    for (int i = 1; i < 5; i++) {
        float a = float(i) * 1.5708 + time * 0.5;
        S[i] = vec4(cos(a) * 1.9, 0.4 + 0.6 * abs(sin(time * 2.0 + float(i))), sin(a) * 1.9, 0.4);
    }
    float ca = time * 0.15;
    vec3 ro = vec3(sin(ca) * 5.5, 2.0, cos(ca) * 5.5);
    vec3 rd = cameraRay(ro, vec3(0.0, 0.6, 0.0), uv, 1.8);
    vec3 col = vec3(0.0), through = vec3(1.0);
    for (int bounce = 0; bounce < 4; bounce++) {
        vec3 n; int id;
        float t = trace(ro, rd, n, id);
        if (id < 0) { col += through * sky(rd); break; }
        vec3 p = ro + rd * t;
        vec3 sn; int sid;
        float lit = trace(p, SUN, sn, sid) < 1e8 ? 0.2 : 1.0;
        vec3 albedo; float mirror;
        if (id == 5) {
            float c = mod(floor(p.x) + floor(p.z), 2.0);
            albedo = mix(vec3(0.1), vec3(0.8), c);
            mirror = 0.25;
            albedo *= exp(-0.02 * dot(p.xz, p.xz)) + 0.3;
        } else {
            albedo = id == 0 ? vec3(0.9) : pal(float(id) * 0.25);
            mirror = 0.8;
        }
        col += through * (1.0 - mirror) * albedo * (0.15 + 0.85 * lit * max(dot(n, SUN), 0.0));
        through *= mirror * (id == 5 ? vec3(1.0) : albedo);
        ro = p;
        rd = reflect(rd, n);
    }
    fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}

More More GLSL examples

Ink in water · Infinite spheres · Night sky · Dithered retro · Stained glass · DNA helix · Clock · Desert dunes