RManNotes

previous | next | regular patterns: contents | rmannotes: top

Tiling Using Mod

The mod function is probably the most important building block for generating periodic patterns. mod produces a sawtooth pattern which repeats from 0 to a based on the variable x.


mod(x, a), where a = 1

When mod is used to modify the input to another function, f, the result is that function f gets repeated.


(a) shows a simple function, f(x). (b) shows f(mod(x * 5, 1.0))
demonstrating the use of mod to repeat a function.
Adapted from [Apodaca92].

Tile Texture Coordinates

mod can also be used to repeat tiles over an entire surface. Instead of using texture coordinates (s, t) which vary from 0 to 1 over the entire surface to compute a tile pattern, we use tile texture coordinates (ss, tt) which vary from 0 to 1 repeatedly over the surface.


(a) Texture coordinates (s,t) over entire surface;
(b) tile texture coordinates (ss, tt) within a single tile

These coordinates can be computed using mod directly or through the RManNotes convenience function called repeat.

RManNotes function

float repeat(float x, freq)

      Repeats the parameter x over the interval 0 to 1 freq number of times. In other words, it returns the value of mod(x * freq, 1);

"Cross" Pattern

In the original crosstile shader which generates 1 tile, s and t were used to generate a horizontal and a vertical bar.


crosstile.sl

By using repeat to compute ss and tt and then using ss and tt instead of s and t to generate the bars, the bars are repeated over the surface.


cross.sl

Which Tile Are We Shading?

When repeat is used, each tile can be considered in the same way since each tile will have the same tile texture coordinates (ss and tt vary from 0 to 1 for every tile). In fact, one tile cannot be distinguished from another using ss and tt alone. (That's why it's easy to make patterns using repeat.)

Sometimes however, it is useful to determine "which" tile we are currently shading. For example, in a "brick" shader we might want to shift every other row horizontally by half a tile to give a staggered tiling. In this case, we need to know if the tile currently being shaded lies in an odd or even row. Fortunately, computing the tile coordinates (the row and column) of the current tile is easy using the floor function.

In RManNotes, we'll use a function called whichtile instead of using floor directly because it's easier to remember and parallels the syntax of the repeat function.

RManNotes function

float whichtile(float x, freq)

      Returns the integer tile coordinate for the parameter x which is being repeated freq times. This is computed as floor(x * freq).

The code fragment below shows a typical usage of whichtile with repeat. Note that the frequency passed to whichtile should be the same as the frequency passed to repeat for the same dimension (s and/or t).

  ss = repeat(s, sfreq);
  tt = repeat(t, tfreq);
  col = whichtile(s, sfreq);
  row = whichtile(t, tfreq);


"Tile coordinates" (col,row) over entire surface
where the tile is repeated 4 times in both s and t.

Two associated RManNotes convenience functions are odd and even.

RManNotes function

boolean odd(float x)
boolean even(float x)

      odd returns true if x is an odd integer and false otherwise. Likewise, even returns true if x is an even integer and false otherwise.

The cross shader can be modified to produce a "weaving" effect by alternating (by row and column) which bar is on top -- the horizontal bar or the vertical bar.


crossweave.sl

previous | next | regular patterns: contents | rmannotes: top

RManNotes is Copyright © 1995, 1996 Stephen F. May

Any comments or suggestions appreciated.

Steve May (smay@pixar.com)

Last Modified: 4/15/96