# Rimas Avizienis cs184-bs Project #2 shader
# Surface shader to generate a tile pattern

surface
tile ( float Ka = 0.5, Kd = 0.75, Ks=.5, roughness=.1;
                    color tilecolor = color "rgb" (0.1,0.1,1);
                    float numtiles = 4;
                    float tilesize = 0.3; )
{
  normal Nf, V;
  color Ct;
  float ss,tt,tilespacing,spot;

/* first normalize s & t so they are between 0 and 1
 * instead of -1 and 1 */

  tilespacing = 1/numtiles;
  ss = (s + 1) / 2; tt = (t + 1) / 2;

/* now we determine how close we are to the nearest dot by making ss and tt
 * the coordinates relative to the nearest dot (with the nearest dot
 * centered at 0) */

  ss = mod(ss, tilespacing);
  tt = mod(tt, tilespacing);
  ss = ss - tilespacing/2; tt = tt - tilespacing/2;

/* if we are within a tile, use the tilecolor, otherwise use the background
 * color */

  if (ss < tilesize * tilespacing/2 && tt < tilesize * tilespacing/2)
     spot = 1;
  else
     spot = 0;

  Ct = mix (Cs, tilecolor, spot);

  if (spot = 1)

  Nf = faceforward (normalize(N),I);
  V = -normalize(I);

  Oi = Os;
  Ci = Os *  Ct * (Ka*ambient() + Kd*diffuse(Nf));
}