ASCII

glint · Examples · Tutorials · Gallery

A raymarched scene printed in characters: each 16-pixel cell's brightness picks a 5x5 glyph from . : * o & 8 @ #, stored as bits in an int.

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

precision highp int;

float sdTorus(vec3 p, vec2 t) { return length(vec2(length(p.xz) - t.x, p.y)) - t.y; }
float map(vec3 p) {
    vec3 q = p;
    q.xy = rot(time * 0.6) * q.xy;
    q.yz = rot(time * 0.4) * q.yz;
    return min(sdTorus(q, vec2(1.0, 0.35)), length(p) - 0.45 - 0.1 * sin(time * 2.0));
}
vec3 normal(vec3 p) {
    vec2 e = vec2(0.002, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}

// Glyph n (25 bits, 5 per row) at p in -1..1: the middle 5/8 of the cell, so there's a gap between characters.
float glyph(int n, vec2 p) {
    p = floor(p * vec2(-4.0, 4.0) + 2.5);
    if (p.x < 0.0 || p.y < 0.0 || p.x > 4.0 || p.y > 4.0) return 0.0;
    int bit = int(p.x) + 5 * int(p.y);
    return float((n >> bit) & 1);
}

void main() {
    // Shade the cell's centre, not every pixel.
    vec2 cell = floor(gl_FragCoord.xy / 16.0) * 16.0 + 8.0;
    vec2 uv = (cell - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.0, -4.0);
    vec3 rd = cameraRay(ro, vec3(0.0), uv, 1.6);
    float t = 0.0;
    for (int i = 0; i < 80; i++) {
        float d = map(ro + rd * t);
        if (d < 0.001 || t > 10.0) break;
        t += d;
    }
    float g = 0.0;
    vec3 tint = vec3(0.3, 1.0, 0.5);
    if (t < 10.0) {
        vec3 p = ro + rd * t;
        vec3 n = normal(p);
        g = 0.1 + 0.9 * max(dot(n, normalize(vec3(0.5, 0.8, -0.6))), 0.0);
        tint = mix(tint, pal(length(p) * 0.3 + time * 0.05), 0.5);
    }
    int ch = 0;
    if (g > 0.1) ch = 4096;       // .
    if (g > 0.2) ch = 65600;      // :
    if (g > 0.3) ch = 332772;     // *
    if (g > 0.4) ch = 15255086;   // o
    if (g > 0.5) ch = 23385164;   // &
    if (g > 0.6) ch = 15252014;   // 8
    if (g > 0.7) ch = 13199452;   // @
    if (g > 0.8) ch = 11512810;   // #
    vec2 p = mod(gl_FragCoord.xy / 8.0, 2.0) - 1.0;
    fragColor = vec4(tint * g * glyph(ch, p), 1.0);
}

More More GLSL examples

Desert dunes · Apollonian · Digital rain · Bokeh · Soap bubbles · Sierpinski tetrahedron · CRT television · Lightning