Pong
glint · Examples · Tutorials · Gallery
Pong playing itself: the ball's x and y are triangle waves (so it bounces off every wall at once for free), and each paddle follows the ball, a little late.

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 tri(float x) { return abs(fract(x) - 0.5) * 2.0; } // 0..1..0
void main() {
vec2 st = gl_FragCoord.xy / resolution;
float aspect = resolution.x / resolution.y;
vec2 p = vec2(st.x * aspect, st.y);
vec2 ball = vec2(0.08 + tri(time * 0.35) * (aspect - 0.16), 0.05 + tri(time * 0.47 + 0.3) * 0.9);
float by = 0.05 + tri(time * 0.47 + 0.3 - 0.08) * 0.9; // where the ball was a moment ago
vec2 left = vec2(0.05, clamp(by, 0.1, 0.9)), right = vec2(aspect - 0.05, clamp(by, 0.1, 0.9));
float on = 0.0;
on += step(abs(p.x - left.x), 0.012) * step(abs(p.y - left.y), 0.08);
on += step(abs(p.x - right.x), 0.012) * step(abs(p.y - right.y), 0.08);
on += step(max(abs(p.x - ball.x), abs(p.y - ball.y)), 0.013);
on += step(abs(p.x - aspect * 0.5), 0.004) * step(0.5, fract(p.y * 16.0)); // the net
on += step(p.y, 0.01) + step(0.99, p.y);
vec3 col = vec3(min(on, 1.0)) * vec3(0.85, 1.0, 0.9);
col += vec3(0.1, 0.15, 0.1) * 0.01 / (length(p - ball) + 0.01);
col *= 0.85 + 0.15 * sin(gl_FragCoord.y * 3.14159);
fragColor = vec4(col, 1.0);
}More More GLSL examples
10 PRINT · Invaders · Gears · Phyllotaxis · Jellyfish · Fireflies · Newton fractal · Burning ship