Clock
glint · Examples · Tutorials · Gallery
A real clock from the date uniform (seconds since midnight), drawn with signed distances: marks, hands, their shadows, and a second hand that springs into place.

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 vec4 date;
in vec2 v_texcoord;
out vec4 fragColor;
float segment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a, ba = b - a;
return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0));
}
// Clockwise from twelve.
vec2 dir(float turns) { return vec2(sin(turns * 6.28318), cos(turns * 6.28318)); }
// The hands at p: x = distance to hour/minute hands, y = to the second hand.
vec2 hands(vec2 p, float s) {
float tick = floor(s) + 1.0 - exp(-fract(s) * 12.0) * cos(fract(s) * 25.0); // snaps, overshoots, settles
float h = segment(p, -dir(s / 43200.0) * 0.1, dir(s / 43200.0) * 0.5) - 0.035;
float m = segment(p, -dir(s / 3600.0) * 0.1, dir(s / 3600.0) * 0.78) - 0.022;
float sec = min(segment(p, -dir(tick / 60.0) * 0.2, dir(tick / 60.0) * 0.85) - 0.006, length(p) - 0.04);
return vec2(min(h, m), sec);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec2 p = uv / 0.42;
float px = 2.0 / (resolution.y * 0.42);
float s = date.w;
// Background tinted by the hour: night blue, day sky.
float day = smoothstep(0.2, 0.35, s / 86400.0) * smoothstep(0.85, 0.75, s / 86400.0);
vec3 col = mix(vec3(0.05, 0.06, 0.12), vec3(0.45, 0.6, 0.75), day) * (1.0 - 0.25 * length(uv));
float r = length(p);
// Case and face.
col = mix(col, vec3(0.1), smoothstep(px, 0.0, r - 1.08) * 0.5 * smoothstep(1.2, 1.0, length(p + vec2(0.04, 0.06))));
col = mix(col, vec3(0.75, 0.72, 0.68), smoothstep(px, 0.0, r - 1.06));
col = mix(col, vec3(0.97, 0.95, 0.9), smoothstep(px, 0.0, r - 1.0));
// Sixty marks: find the nearest one and measure in its frame.
float notch = 6.28318 / 60.0;
float i = floor(atan(p.x, p.y) / notch + 0.5);
vec2 d = dir(i / 60.0);
vec2 q = vec2(dot(p, vec2(d.y, -d.x)), dot(p, d));
bool hour = mod(i, 5.0) == 0.0;
float len = hour ? 0.1 : 0.04, wid = hour ? 0.022 : 0.008;
float mark = max(abs(q.x) - wid, abs(q.y - 0.9 + len) - len);
col = mix(col, vec3(0.1), smoothstep(px, 0.0, mark));
// Hands: a soft shadow first, then the hands themselves.
vec2 sh = hands(p - vec2(0.03, -0.04), s);
col *= 1.0 - 0.3 * smoothstep(0.05, 0.0, min(sh.x, sh.y));
vec2 hd = hands(p, s);
col = mix(col, vec3(0.08), smoothstep(px, 0.0, hd.x));
col = mix(col, vec3(0.85, 0.1, 0.1), smoothstep(px, 0.0, hd.y));
// Glass: a soft highlight.
col += 0.08 * smoothstep(0.9, 0.0, length(p - vec2(-0.4, 0.5))) * step(r, 1.0);
fragColor = vec4(col, 1.0);
}More More GLSL examples
Dithered retro · Chrome balls · Stained glass · DNA helix · Desert dunes · Apollonian · Digital rain · Bokeh