Invaders
glint · Examples · Tutorials · Gallery
A marching fleet of procedural space invaders: each row's sprite is random bits in an 11x8 grid mirrored left to right, with two frames of animation, stepping sideways and down like the arcade.

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))); }
// One sprite: 6 columns of random bits mirrored to 11, per row type and animation frame.
float sprite(vec2 p, float kind, float frame) {
vec2 c = floor(p);
if (c.x < 0.0 || c.y < 0.0 || c.x > 10.0 || c.y > 7.0) return 0.0;
float x = min(c.x, 10.0 - c.x);
float bit = hash(vec2(x + kind * 13.0, c.y + (c.y < 3.0 ? frame * 7.0 : 0.0)));
float shape = 1.0 - smoothstep(0.0, 6.0, abs(c.y - 4.0) * 1.2 + x * 0.5); // rounder in the middle
return step(0.55 - shape * 0.4, bit);
}
void main() {
vec2 p = gl_FragCoord.xy / resolution.y * 100.0;
float w = resolution.x / resolution.y * 100.0;
float stepT = floor(time * 2.0);
float frame = mod(stepT, 2.0);
// March: 16 steps right, down a row, 16 steps left...
float leg = floor(stepT / 16.0), s = mod(stepT, 16.0);
float x0 = mod(leg, 2.0) < 1.0 ? s * 2.0 : 32.0 - s * 2.0;
float y0 = 90.0 - mod(leg, 8.0) * 3.0;
vec3 col = vec3(0.0);
vec2 q = p - vec2(w * 0.5 - 70.0 + x0, y0);
vec2 cell = floor(vec2(q.x / 14.0, -q.y / 12.0));
if (cell.x >= 0.0 && cell.x < 8.0 && cell.y >= 0.0 && cell.y < 5.0) {
vec2 local = vec2(q.x - cell.x * 14.0, -q.y - cell.y * 12.0);
float kind = floor(cell.y / 2.0 + 0.5);
float on = sprite(vec2(local.x, 8.0 - local.y), kind, frame);
col = on * (cell.y < 1.0 ? vec3(1.0, 0.3, 0.8) : cell.y < 3.0 ? vec3(0.3, 1.0, 1.0) : vec3(0.4, 1.0, 0.3));
}
// The player's cannon, sliding along the bottom.
vec2 c = p - vec2(w * 0.5 + sin(time * 0.7) * w * 0.35, 8.0);
float cannon = step(abs(c.x), 6.0) * step(abs(c.y), 1.5) + step(abs(c.x), 1.0) * step(abs(c.y - 2.5), 1.5);
col += vec3(0.3, 1.0, 0.3) * min(cannon, 1.0);
col *= 0.8 + 0.2 * sin(gl_FragCoord.y * 3.14159); // scanlines
fragColor = vec4(col, 1.0);
}More More GLSL examples
Tesseract · Black hole · Low poly · 10 PRINT · Gears · Phyllotaxis · Pong · Jellyfish