LearnFirst steps

Lesson 9 of 30 · 50 min

Repetition and tiles

Split the screen into cells with fract and floor, and repeat patterns

In this lesson you will

  • Split the screen into cells with fract(p * n) - 0.5 and get a coordinate inside each cell
  • Get each cell's index with floor(p * n) and vary color, size or motion per cell
  • Make checkerboards, brickwork and mirrored repeats
  • Draw smooth grid lines of even thickness
  • Rotate and move shapes inside a repeated pattern

01Turning the screen into a grid with fract

In Time and motion we used fract(time) to repeat time. fract keeps only the fractional part, so whatever you feed it, the answer stays between 0 and 1 and wraps back to 0 every time the input advances by 1. Apply it to coordinates instead of time and you repeat space.

1

2

3

4

Starts when it scrolls into view
Open in /edit ↗
▸Change 2.0 to 5.0 for smaller cells. What about vec2(4.0, 1.0)?

p * 2.0 puts y in the range -2 to 2. Take fract of that and it wraps to 0 every time it crosses a whole number, splitting the screen into four rows of cells. Each cell is 1 ÷ 2 = 0.5 wide in p units. Inside every cell, the bottom-left is black (0, 0), red grows to the right and green grows upward — the same as uv in Knowing where you are. You can think of each cell as a tiny screen of its own.

The repetition stays seamless for negative numbers too. fract(x) is x - floor(x) (x minus the largest whole number not greater than x), so fract(-0.6) is -0.6 - (-1.0) = 0.4. For p = (0.8, -0.3), p * 2.0 = (1.6, -0.6), and fract gives (0.6, 0.4).

02Putting the origin at each cell's center

For drawing shapes, it's handier to have the origin at the center, just as we built p from uv in Knowing where you are. fract(p * n) - 0.5 runs from -0.5 to 0.5 inside every cell, with the cell's center at (0, 0). Draw one circle in that coordinate and every cell gets a circle.

1

2

3

4

5

6

7

8

9

10

11

12

Starts when it scrolls into view
Open in /edit ↗
▸Set n to 6.0 for more, smaller circles. At a radius of 0.5 instead of 0.3 the circles touch. What happens at 0.6?

/ n is the same correction as the * s from scaling in Moving space. p * n equals p / (1 / n), so the shape has been shrunk to 1 / n of its size, and the distance must be multiplied by 1 / n — that is, divided by n. With n = 3 a cell is about 0.333 across on screen, and a circle of radius 0.3 in cell units has a radius of 0.1 on screen.

Watch out

A shape too big for its cell gets sliced off at the cell border, because each cell only knows about the one shape inside it. Keep shapes within the cell (within 0.5 of its center). A way to check neighboring cells and draw shapes that spill over comes up in Cellular patterns (Voronoi).

03Finding the cell's index with floor

While fract tells you where you are inside a cell, floor tells you which cell you're in. floor(x) rounds x down to a whole number (as a float): floor(2.7) is 2.0 and floor(-0.4) is -1.0. If p * n is (2.7, -0.4), floor gives (2.0, -1.0) and fract gives (0.7, 0.6). It's always true that p * n = index + position inside the cell.

Every point in the same cell shares the same index. So anything you compute from the index stays constant inside a cell and changes from cell to cell — perfect for varying color or size per cell.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Starts when it scrolls into view
Open in /edit ↗
▸Set r to 0.25 + 0.15 * sin(id.x + id.y * 2.0 + time * 2.0) and each cell pulses at its own size. What if id.y * 0.25 becomes 0.0?

Tip

Here we fed the index into sin and the palette to make each cell different. But the variation is regular, so you can see diagonal bands in it. For variation that looks scattered, you need random numbers, which we build in Making randomness.

04Checkerboards

The classic index-based pattern is the checkerboard. mod(id.x + id.y, 2.0) is the remainder when the sum of the indices is divided by 2, so it's either 0 or 1. Step one cell right or up and the sum grows by 1, flipping the remainder between 0 and 1. mod(3.0, 2.0) is 1 and mod(4.0, 2.0) is 0. GLSL's mod stays non-negative even for negative numbers (mod(-1.0, 2.0) is 1), so the pattern doesn't break in the lower-left of the screen.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
▸Use only id.x instead of id.x + id.y for vertical stripes. Change p * 4.0 to (p + vec2(time * 0.2, 0.0)) * 4.0 — which way does it flow?

05Shifting rows: brickwork

In a brick wall, each course is shifted by half a brick. You can make this by adding 0.5 to x in odd rows before dividing x into cells with fract and floor. The row index is floor(g.y), and mod(floor(g.y), 2.0) tells you whether it's odd (0 in even rows, 1 in odd rows).

Bricks are wide, so the cells should be too. Dividing by size in p / size makes one brick fill one cell. Multiplying the in-cell coordinate by size again brings it back to screen units, so the sdBox distance comes out in screen units as well.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.5 * to 0.25 * for a staircase-like offset. Set size to vec2(0.2) for square tiles

It matters that the g.x += … line comes before id is computed. The index is taken from the shifted x, so each shifted brick gets its own proper index. The mortar (the gaps between bricks) comes from making the sdBox slightly smaller than size * 0.5.

06Mirrored repetition

Repeat a shape that isn't symmetric and the pattern breaks at the cell borders. Instead, flip the in-cell x in odd columns and the in-cell y in odd rows: neighbors then face each other like mirror images and the borders join perfectly. mix(f, 1.0 - f, mod(id, 2.0)) keeps the axes where mod(id, 2.0) is 0 and takes 1.0 - f (flipped) where it's 1. As this shows, the mix from Mixing colors also accepts separate amounts for x and y.

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
▸Put // in front of the f = mix(…); line to stop the flipping: the lines break at the cell borders. What if the 1.0 in length(f) - 1.0 becomes 0.5?

Every cell draws only a quarter ring centered on its lower-left corner. A cell's lower-left corner is its left neighbor's lower-right corner. Flipping makes the neighbor draw its ring around that very same corner (and likewise up and down), so the four cells around a corner each contribute a quarter and together form one complete circle. Mirrored repetition lets a single small piece grow into a large, connected pattern.

07Grid lines

To draw graph-paper lines, compute the distance to the nearest grid line. abs(fract(g) - 0.5) is how far you are from the cell center along x and along y (0 to 0.5). The distance to the cell's edge — the line — is 0.5 minus that, and picking the nearer line gives 0.5 - max(a.x, a.y). Divide by n to get screen units, and the lines come out the same thickness everywhere.

With n = 10, each cell is 0.1 across. For p.x = 0.23, p.x * n = 2.3, fract gives 0.3, which is 0.2 from the center, so the line is 0.5 - 0.2 = 0.3 cells away — 0.03 in screen units. Sure enough, the nearest vertical line, x = 0.2, is 0.03 away.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Change grid(p, 10.0) to grid(p, 20.0) for a finer grid. Change - 0.002 to - 0.01 to make the bold lines bolder

Tip

In other people's code you'll also see repetition written with mod, like mod(p, 0.5) - 0.25. mod(p, s) equals fract(p / s) * s: a form where the cell size s is written directly in screen units. Either way gives the same result.

08Rotating and moving inside the repeat

The in-cell coordinate q works with the transforms from Moving space just like any other coordinate. Rotate it with rot(-a) * q and the shape in every cell turns. Mix the cell's index into the angle and, just like staggering the phase in Time and motion, the motion shifts slightly from cell to cell and ripples across like a wave.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Starts when it scrolls into view
Open in /edit ↗
▸Set 0.3 to 0.0 and they all turn together. Change vec2(0.22) to vec2(0.35) and the corners get clipped at the cell borders as they turn

id.x + id.y stays constant along diagonals running up and to the right, so the rotation wave travels diagonally. A box's corners reach farther from the center as it turns. With a half-size of 0.22 plus 0.06 of rounding, the corners reach about 0.37 from the center, which fits within the cell's half-width of 0.5, so nothing is clipped. When rotating inside a repeat, check that the farthest-reaching point still fits in the cell.

You can shift as well as rotate. In the example below, a sin is added to y before taking fract, with its phase offset by the column index id.x. Whole columns slide up and down together, so nothing is clipped and the columns of dots ripple.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Starts when it scrolls into view
Open in /edit ↗
▸Change column * 0.6 to column * 0.0 and everything moves in unison. Turn g.y += … into g.x += … and set column to floor(g.y) to make each row slide sideways instead

Watch out

Base the amount of shift only on the index of the other axis. If the amount you shift y by depends on the y position or the y index, it varies inside a cell, and shapes get distorted or clipped at the borders.

09Exercises

Use grids to make patterns. It's reassuring to first display the fract and floor values as colors and check that the cells are split the way you expect.

Exercise 1

Gingham

Make a gingham pattern like a picnic cloth. Where the vertical and horizontal stripes overlap it's darkest, where only one passes it's medium, and where neither passes it's white: three levels in all.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

Rows flowing both ways

Make rows of rounded boxes flow sideways, with every other row going the opposite way: even rows flow right, odd rows flow left.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

Halftone

Make a halftone like a newspaper photo. The size of each dot follows a wave spreading out from the center of the screen, so from a distance the dots form concentric rings that flow outward.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗

10Recap

  • fract(p * n) - 0.5 gives a coordinate centered in each cell; divide distances by n to get back to screen units
  • floor(p * n) gives the cell index; anything computed from it is constant within a cell
  • mod(id.x + id.y, 2.0) makes a checkerboard; shifting rows before fract makes brickwork
  • mix(f, 1.0 - f, mod(id, 2.0)) mirrors the repeat so the borders join
  • Keep shapes inside their cells; anything that spills over gets clipped
  • Rotations and shifts work inside cells too; offsetting the phase by index makes motion ripple across

Grids divide the screen by x and y. Next time we look at the screen in terms of angle and distance with polar coordinates. Apply repetition to the angle and you get rays, flowers and kaleidoscopes.

Finished lessons get a mark in the course contents