Documentation

Shader Example: Numbers Display

Display numbers in fragment shader.

Header

Your content here

Snippets

Shadertoy

// Displays grid coordinates as numbers in each cell.
// Change GRID_DENSITY to control how many numbered cells are shown.

#define PI 3.14159265359

// ======================================================
// Config
// ======================================================
const vec2 GRID_DENSITY = vec2(8.0, 5.0); // cells across x/y
// const vec2 GRID_DENSITY = vec2(4.0, 3.0);   // bigger cells, fewer labels
// const vec2 GRID_DENSITY = vec2(12.0, 8.0);  // denser grid
// const vec2 GRID_DENSITY = vec2(20.0, 12.0); // very dense
const float GRID_LINE_W = 0.025;
const float DIGIT_SCALE = 0.75;

// ======================================================
// Helpers
// ======================================================
float sdBox(vec2 p, vec2 b)
{
    vec2 d = abs(p) - b;
    return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
}

float segmentH(vec2 p, vec2 c, vec2 size, float thick)
{
    return 1.0 - smoothstep(thick, thick + 0.01, sdBox(p - c, vec2(size.x, thick)));
}

float segmentV(vec2 p, vec2 c, vec2 size, float thick)
{
    return 1.0 - smoothstep(thick, thick + 0.01, sdBox(p - c, vec2(thick, size.y)));
}

// 7-segment digit in local space p ~ [-0.5..0.5]
float digit7(vec2 p, int d)
{
    float s = 0.0;
    vec2 hs = vec2(0.22, 0.0);
    vec2 vs = vec2(0.0, 0.18);
    float t = 0.055;

    // segment positions
    float A = segmentH(p, vec2( 0.0,  0.36), hs, t);
    float B = segmentV(p, vec2( 0.23,  0.18), vs, t);
    float C = segmentV(p, vec2( 0.23, -0.18), vs, t);
    float D = segmentH(p, vec2( 0.0, -0.36), hs, t);
    float E = segmentV(p, vec2(-0.23, -0.18), vs, t);
    float F = segmentV(p, vec2(-0.23,  0.18), vs, t);
    float G = segmentH(p, vec2( 0.0,  0.00), hs, t);

    bool a=false,b=false,c=false,dn=false,e=false,f=false,g=false;

    if (d == 0) { a=true; b=true; c=true; dn=true; e=true; f=true; }
    if (d == 1) { b=true; c=true; }
    if (d == 2) { a=true; b=true; dn=true; e=true; g=true; }
    if (d == 3) { a=true; b=true; c=true; dn=true; g=true; }
    if (d == 4) { b=true; c=true; f=true; g=true; }
    if (d == 5) { a=true; c=true; dn=true; f=true; g=true; }
    if (d == 6) { a=true; c=true; dn=true; e=true; f=true; g=true; }
    if (d == 7) { a=true; b=true; c=true; }
    if (d == 8) { a=true; b=true; c=true; dn=true; e=true; f=true; g=true; }
    if (d == 9) { a=true; b=true; c=true; dn=true; f=true; g=true; }

    s += a  ? A : 0.0;
    s += b  ? B : 0.0;
    s += c  ? C : 0.0;
    s += dn ? D : 0.0;
    s += e  ? E : 0.0;
    s += f  ? F : 0.0;
    s += g  ? G : 0.0;

    return clamp(s, 0.0, 1.0);
}

float colonMark(vec2 p)
{
    float a = 1.0 - smoothstep(0.06, 0.07, length(p - vec2(0.0,  0.12)));
    float b = 1.0 - smoothstep(0.06, 0.07, length(p - vec2(0.0, -0.12)));
    return max(a, b);
}

float drawNumber2(vec2 p, int value)
{
    value = clamp(value, 0, 99);
    int tens = value / 10;
    int ones = value % 10;

    float s = 0.0;

    // left digit
    if (tens > 0)
        s = max(s, digit7((p - vec2(-0.28, 0.0)) / 0.42, tens));

    // right digit
    s = max(s, digit7((p - vec2(0.18, 0.0)) / 0.42, ones));

    return s;
}

float drawCoordLabel(vec2 p, ivec2 cell)
{
    // local label composition: "x:y"
    float s = 0.0;
    vec2 q = p * DIGIT_SCALE;

    // x
    s = max(s, drawNumber2(q + vec2(0.48, 0.0), cell.x));
    // colon
    s = max(s, colonMark((q - vec2(0.02, 0.0)) / 0.45));
    // y
    s = max(s, drawNumber2(q - vec2(0.42, 0.0), cell.y));

    return s;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    // aspect-corrected centered coords
    vec2 uv = fragCoord / iResolution.xy;
    vec2 gv = uv * GRID_DENSITY;      // grid space
    ivec2 cell = ivec2(floor(gv));    // integer grid coords
    vec2 f = fract(gv) - 0.5;         // local cell coords [-0.5,0.5]

    // base background
    vec3 col = vec3(0.05, 0.06, 0.08);

    // alternating subtle cell tint
    float checker = mod(float(cell.x + cell.y), 2.0);
    col += mix(vec3(0.02, 0.025, 0.03), vec3(0.00), checker);

    // grid lines
    vec2 af = abs(f);
    float line = max(
        1.0 - smoothstep(0.5 - GRID_LINE_W, 0.5, af.x),
        1.0 - smoothstep(0.5 - GRID_LINE_W, 0.5, af.y)
    );
    col = mix(col, vec3(0.18, 0.22, 0.28), line);

    // label area
    float label = drawCoordLabel(f * 2.0, cell);
    vec3 textCol = vec3(0.95, 0.95, 0.90);

    // small center highlight behind text
    float plate = 1.0 - smoothstep(0.30, 0.34, length(f * vec2(1.2, 0.9)));
    col += vec3(0.08, 0.09, 0.11) * plate * 0.5;

    col = mix(col, textCol, label);

    fragColor = vec4(col, 1.0);
}