#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - \
            smoothstep((b)-(fuzz),(b),(x)))
#define blend(a,b,x) ((a) * (1 - (x)) + (b) * (x))

#define odd(x)            (mod((x), 2) == 1)
#define even(x)           (mod((x), 2) == 0)
#define union(a,b)        ((a) + (b) - (a) * (b))


/* hlayer_color = color of horizontal lines
   vlayer_color = color of vertical lines
   bgcolor = color of background
   freq = number of lines on one dimesion of the surface
   Ka, Kd = coefficient of ambient and diffusse ligt */

displacement towel( float freq = 100, depth = 0.1)
{
  
  float ss, tt,sss,ttt;
  point Nf, V;
  float surface_mag, layer_mag;
   point PP;
  float col, row;
 

  ss = mod(s*freq,1);
  tt = mod(t*freq,1);
    
  col = floor(s* freq);
  row = floor(t* freq);
  PP = transform("shader", P);
  Nf = faceforward(normalize(N), I);
 
   if (even(row) && odd(col) || even(col) && odd(row)) {


  /* layer #1 */

  layer_mag = pulse(0.35, 0.45, 0.02, ss);
  layer_mag *= 0.5; 
  surface_mag = max(surface_mag, layer_mag) +(depth+noise(s,t))+2*layer_mag;
  PP += surface_mag*Nf;    

  /* layer #2 */
  
  layer_mag = pulse(0.35, 0.75, 0.02, tt);
  layer_mag *= 0.5; 
  surface_mag = max(surface_mag, layer_mag) +(depth+noise(s,t))+layer_mag;
  PP -= surface_mag*Nf;    

  }
 
  else {
  /* layer #2 */
  
  layer_mag = pulse(0.35, 0.45, 0.02, tt);
  layer_mag *= 0.5; 
  surface_mag = max(surface_mag, layer_mag) +(depth+noise(s,t))+2*layer_mag;
  PP += surface_mag*Nf;    

  /* layer #1 */

  layer_mag = pulse(0.35, 0.75, 0.02, ss);
  layer_mag *= 0.5; 
  surface_mag = max(surface_mag, layer_mag) +(depth+noise(s,t))+layer_mag;
  PP -= surface_mag*Nf;    

 }
  
 N=calculatenormal(PP);  

}