#define repeat(x,freq)    (mod((x) * (freq), 1.0))
#define pulse(a,b,fuzz,x) (smoothstep((a)-(fuzz),(a),(x)) - smoothstep((b)-(fuzz),(b),(x)))

displacement tile3(float xfreq =20, yfreq= 10, xmin=0, xmax=1, ymin=0,ymax=1, Km = 0.01)
{
  float surface_mag, layer_mag;
  float ss, tt;
  float fuzz = 0.02;
  float x,y,z;
   
  /* background layer */

  surface_mag = 0;

  /* rotate & repeat layers */
/*
  ss = repeat(s, xfreq);
  tt = repeat(t, yfreq);
*/
  
  ss = repeat((s-xmin)/(xmax-xmin), xfreq);
  tt = repeat((t-ymin)/(ymax-ymin), yfreq);

  
   
  /* layer 1 */

  layer_mag = pulse(0.48, 0.52, fuzz, ss);
  surface_mag += layer_mag;

  /* layer 2 */

  layer_mag = pulse(0.48, 0.52, fuzz, tt);
  surface_mag = max(surface_mag, layer_mag);

  /* displace */

  P += Km * surface_mag * normalize(N);
  N = calculatenormal(P);
}