Keyboard maze
glint · Examples · Tutorials · Gallery
A little game: find your way out with the arrow keys or WASD (R starts again). The keyboard is a texture (uniform sampler2D keyboard), where you are lives in a buffer pass that reads its own last frame, the maze is Common code both passes share, and the text uses the stock font. Click the picture first, so the keys go to the game and not the code.

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
3 passes, drawn in this order each frame, in GLSL.
State: fragment shader
#version 300 es
precision highp float;
uniform sampler2D feedback;
uniform sampler2D keyboard;
uniform int frame;
in vec2 v_texcoord;
out vec4 fragColor;
// The keyboard texture: column = key code, row 1 = pressed this frame (row 0 held, row 2 toggled).
bool pressed(int key) { return texelFetch(keyboard, ivec2(key, 1), 0).x > 0.5; }
// Every pixel holds the game: xy = where you are, z = moves, w = 1 once you're out.
void main() {
vec4 s = texelFetch(feedback, ivec2(0), 0);
if (frame < 2 || pressed(82)) s = vec4(vec2(START), 0.0, 0.0); // R: start again
ivec2 p = ivec2(s.xy);
ivec2 dir = ivec2(0);
if (pressed(37) || pressed(65)) dir = ivec2(-1, 0); // left, A
if (pressed(39) || pressed(68)) dir = ivec2(1, 0); // right, D
if (pressed(38) || pressed(87)) dir = ivec2(0, 1); // up, W
if (pressed(40) || pressed(83)) dir = ivec2(0, -1); // down, S
if (dir != ivec2(0) && s.w < 0.5 && !wall(p + dir)) {
p += dir;
s.z += 1.0;
}
if (p == GOAL) s.w = 1.0;
fragColor = vec4(vec2(p), s.zw);
}Image: fragment shader
#version 300 es
precision highp float;
uniform sampler2D pass0;
uniform sampler2D font;
uniform vec2 resolution;
uniform float time;
in vec2 v_texcoord;
out vec4 fragColor;
// One character from the stock font: ASCII code c at (c % 16, 15 - c / 16) in a 16x16 grid,
// a distance field in alpha (0.5 on the edge). p is 0..1 across the character.
float glyph(vec2 p, int c) {
if (any(lessThan(p, vec2(0.0))) || any(greaterThan(p, vec2(1.0)))) return 0.0;
vec2 cell = vec2(float(c % 16), float(15 - c / 16));
float d = textureLod(font, (cell + p) / 16.0, 0.0).a;
return smoothstep(0.56, 0.44, d);
}
const int HELP[22] = int[](65, 82, 82, 79, 87, 83, 47, 87, 65, 83, 68, 32, 32, 32, 82, 61, 65, 71, 65, 73, 78, 32);
const int WIN[12] = int[](79, 85, 84, 33, 32, 32, 77, 79, 86, 69, 83, 58);
// Text along a line from 'at', 'size' pixels per character.
float help(vec2 px, vec2 at, float size) {
vec2 q = (px - at) / size;
int i = int(floor(q.x));
if (i < 0 || i >= 22) return 0.0;
return glyph(vec2(fract(q.x) * 0.8 + 0.1, q.y), HELP[i]);
}
float win(vec2 px, vec2 at, float size, int moves) {
vec2 q = (px - at) / size;
int i = int(floor(q.x));
if (i < 0 || i >= 16) return 0.0;
int c = 32;
if (i < 12) c = WIN[i];
else {
// The move count, three digits.
int k = i - 12, d = moves / (k == 1 ? 100 : k == 2 ? 10 : 1) % 10;
c = k == 0 ? 32 : 48 + d;
}
return glyph(vec2(fract(q.x) * 0.8 + 0.1, q.y), c);
}
void main() {
vec4 s = texelFetch(pass0, ivec2(0), 0);
vec2 px = gl_FragCoord.xy;
// The maze fitted in the picture, with room for a line of text above.
float cs = floor(min(resolution.x / float(MAZE.x + 2), (resolution.y * 0.86) / float(MAZE.y + 1)));
vec2 origin = floor((resolution - vec2(MAZE) * cs) * vec2(0.5, 0.4));
vec2 m = (px - origin) / cs;
ivec2 c = ivec2(floor(m));
vec2 f = fract(m);
vec3 col = vec3(0.05, 0.06, 0.09);
if (all(greaterThanEqual(c, ivec2(0))) && all(lessThan(c, MAZE))) {
if (wall(c)) {
// Walls: raised blocks, lit from the top left.
float edge = min(min(f.x, f.y), min(1.0 - f.x, 1.0 - f.y));
col = vec3(0.22, 0.26, 0.38) * (0.8 + 0.4 * smoothstep(0.0, 0.2, edge)) + 0.08 * (f.y - f.x);
} else {
col = vec3(0.09, 0.1, 0.14) + 0.03 * float((c.x + c.y) & 1);
}
}
// The way out: a pulsing green diamond.
vec2 g = (m - vec2(GOAL) - 0.5);
float pulse = 0.3 + 0.05 * sin(time * 5.0);
col = mix(col, vec3(0.3, 1.0, 0.5), smoothstep(pulse, pulse - 0.06, abs(g.x) + abs(g.y)));
// You: a glowing orange dot.
float d = length(m - s.xy - 0.5);
col += vec3(1.0, 0.55, 0.15) * (smoothstep(0.32, 0.26, d) + 0.25 * exp(-d * d * 3.0));
// Text above the maze; and the count once you're out.
float size = cs * 0.9;
vec2 at = vec2(origin.x, origin.y + float(MAZE.y) * cs + cs * 0.3);
col = mix(col, vec3(0.75, 0.8, 0.95), help(px, at, size * 0.8));
if (s.w > 0.5) {
vec2 w = vec2(resolution.x * 0.5 - size * 8.0, resolution.y * 0.5 - size * 0.5);
float box = step(abs(px.y - resolution.y * 0.5), size) * step(abs(px.x - resolution.x * 0.5), size * 9.0);
col = mix(col, vec3(0.0), box * 0.75);
col = mix(col, vec3(0.4, 1.0, 0.6), win(px, w, size, int(s.z)));
}
fragColor = vec4(col, 1.0);
}Common: shared by every pass
// Common: shared by the State and Image passes. The maze is a binary-tree maze:
// every room opens north or east (by a hash), so there's always exactly one way through.
const ivec2 MAZE = ivec2(21, 13); // cells, walls included; rooms are at odd x, y
const ivec2 START = ivec2(1, 1);
const ivec2 GOAL = MAZE - 2;
float roomHash(ivec2 r) { return fract(sin(float(r.x) * 127.1 + float(r.y) * 311.7) * 43758.5453); }
// Which way a room opens: 1 north, 2 east, 0 neither (the top right corner).
int carve(ivec2 r) {
bool top = r.y >= MAZE.y - 2, right = r.x >= MAZE.x - 2;
if (top && right) return 0;
if (top) return 2;
if (right) return 1;
return roomHash(r) < 0.5 ? 1 : 2;
}
bool wall(ivec2 c) {
if (c.x <= 0 || c.y <= 0 || c.x >= MAZE.x - 1 || c.y >= MAZE.y - 1) return true;
bool ox = (c.x & 1) == 1, oy = (c.y & 1) == 1;
if (ox && oy) return false; // a room
if (!ox && !oy) return true; // a pillar
if (ox) return carve(ivec2(c.x, c.y - 1)) != 1; // between a room and the one above it
return carve(ivec2(c.x - 1, c.y)) != 2; // between a room and the one to its right
}More Games, sound & cube maps examples
Little synth · Cube map reflections · Textured room · Stand inside (VR)