TExcelTAD
Home › Shaders & Graphics Programming › Patterns and Tiling

Patterns and Tiling

Draw one shape well, and you can draw a thousand for free. The trick is to fold space: instead of computing a thousand shapes per pixel, you change the coordinates so every pixel thinks it is in the first tile, then draw one shape. The workhorse is fract.

fract: repeat everything

fract(x) keeps only the fractional part: fract(2.3) = 0.3, fract(-0.25) = 0.75. Multiply coordinates by n, take fract, and the 0..1 range repeats n times. floor of the same value tells you which tile you are in, an integer cell ID you can use to vary each tile:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = fragCoord / iResolution.y;          // square units, 1.0 = frame height
    vec2 grid = p * 6.0;
    vec2 cell = floor(grid);                     // which tile (integer)
    vec2 local = fract(grid) - 0.5;              // position inside the tile, -0.5..0.5

    float d = length(local) - 0.3;
    float px = 6.0 / iResolution.y;              // one pixel, in tile units
    float dot_ = 1.0 - smoothstep(-px, px, d);

    vec3 tint = 0.5 + 0.5 * cos(vec3(0.0, 2.0, 4.0) + cell.x * 0.7 + cell.y * 1.3);
    fragColor = vec4(mix(vec3(0.08), tint, dot_), 1.0);
}

Note that the pixel width scales too: after multiplying by 6, one pixel is 6 / iResolution.y tile units. Forgetting this makes small repeated shapes blurry or aliased.

Stripes, checkers and grids

Most classic patterns are one line once you have fract, floor and step:

Pattern Expression
stripes step(0.5, fract(p.x * n))
diagonal stripes step(0.5, fract((p.x + p.y) * n))
checkerboard mod(floor(p.x * n) + floor(p.y * n), 2.0)
grid lines min(abs(fract(p.x*n) - 0.5), abs(fract(p.y*n) - 0.5)) near 0.5
brick offset shift p.x by 0.5 on odd rows: p.x += 0.5 * mod(floor(p.y*n), 2.0)
glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = fragCoord / iResolution.y;
    vec3 color;
    float third = iResolution.x / iResolution.y / 3.0;

    if (p.x < third) {
        float checker = mod(floor(p.x * 8.0) + floor(p.y * 8.0), 2.0);
        color = vec3(0.2 + 0.6 * checker);
    } else if (p.x < 2.0 * third) {
        vec2 b = p * vec2(6.0, 12.0);
        b.x += 0.5 * mod(floor(b.y), 2.0);                       // offset every other row
        vec2 f = fract(b);
        float mortar = step(0.06, f.x) * step(0.1, f.y);
        color = mix(vec3(0.85), vec3(0.7, 0.3, 0.2), mortar);
    } else {
        float s = step(0.5, fract((p.x + p.y) * 10.0 - iTime));  // moving stripes
        color = mix(vec3(0.1), vec3(1.0, 0.8, 0.1), s);
    }
    fragColor = vec4(color, 1.0);
}

Polar coordinates

Converting to angle and radius turns radial patterns into straight ones. atan(y, x) gives the angle in -π..π, length(p) the radius. Repeat the angle and you get petals, gears, sunbursts and loading spinners:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float r = length(p);
    float a = atan(p.y, p.x);                    // -PI..PI

    float petals = 6.0;
    float edge = 0.25 + 0.08 * cos(a * petals + iTime);   // wobbly flower radius
    float flower = 1.0 - smoothstep(edge - 0.005, edge + 0.005, r);

    float rays = step(0.5, fract(a / 6.28318 * 24.0 + iTime * 0.2));
    vec3 color = mix(vec3(0.15, 0.2, 0.4), vec3(0.25, 0.3, 0.55), rays);
    color = mix(color, vec3(1.0, 0.75, 0.2), flower);
    color = mix(color, vec3(0.5, 0.25, 0.1), 1.0 - smoothstep(0.07, 0.075, r));
    fragColor = vec4(color, 1.0);
}

GLSL's two-argument atan(y, x) is what other languages call atan2. The atan2 and Aiming lesson explains why it beats atan(y / x).

Random per tile

Identical tiles look artificial. A hash turns the integer cell ID into a pseudo-random number, the same every frame for the same cell. This tiny one (from the Book of Shaders tradition) is good enough for visuals, though its quality varies between GPUs; the Noise lesson uses it as the basis for smooth noise.

glsl · try it yourself
float hash(vec2 c) { return fract(sin(dot(c, vec2(127.1, 311.7))) * 43758.5453); }

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 g = fragCoord / iResolution.y * 10.0;
    vec2 cell = floor(g);
    vec2 f = fract(g);

    // Truchet tiles: each cell draws one of two diagonal quarter-circle pairs.
    if (hash(cell) > 0.5) f.x = 1.0 - f.x;
    float d1 = abs(length(f) - 0.5);
    float d2 = abs(length(f - 1.0) - 0.5);
    float d = min(d1, d2);
    float line = 1.0 - smoothstep(0.06, 0.06 + 10.0 / iResolution.y, d);

    fragColor = vec4(mix(vec3(0.05, 0.1, 0.15), vec3(0.3, 0.9, 0.8), line), 1.0);
}

Every cell draws one of two tile variants, picked by its hash, and the arcs still connect at the cell edges. That is a Truchet pattern, and it shows the general recipe: fold space, identify the cell, randomize per cell, draw one simple shape.

Note: mod(x, y) in GLSL is x - y * floor(x / y), so it is never negative for positive y. That differs from % in C, C# and JavaScript, which keeps the sign of x. Code ported from the CPU side often breaks on negative coordinates for this reason.

Test yourself

What is fract(-0.25) in GLSL?

  1. -0.25
  2. 0.25
  3. 0.75
  4. 0.0

After vec2 g = p * 6.0;, what does floor(g) give you?

  1. The position inside the tile
  2. An integer cell ID that is the same for every pixel in a tile
  3. A random number
  4. The tile size

Which GLSL call gives the angle of p around the origin, handling all four quadrants?

  1. atan(p.y / p.x)
  2. atan(p.y, p.x)
  3. acos(p.x)
  4. angle(p)