/* fallingwater.sl
 * Water falls from one end to another of the qubert surface.  The water is also
 *weighted by the z component of the partial derivatives along the surface, so that
 *the water falls faster vertically.
 *
 */
#define snoise(x)    (2*noise(x) - 1)
surface fallingwater(
   float Kd = .6;
   float Ka = .2;
   float roughness = .01;
   float time = 8;
   float size = 1;
   float border = .5;
   float excited = .2;
   color ambientcolor = color(1);
   color specularcolor = color(1);)
{

float water;
color blue = color(0,0,.5);
float sfar = s+5;
float tfar = t+5;

/*Compute the zcomp of the derivative of the surface.*/
float ds = -sin(s/4);
float dt = -sin(t/4);

float sofar = sqrt(sfar*sfar + tfar*tfar)+ds+dt;
sofar += border*snoise(s*s+t*t);
water = sofar < time ? 1 : 0;
 
point Nf=normalize(N);
point NN = Nf + size*noise(P*noise(time));

float nearline = time - sofar;
nearline = nearline < 0 ? -nearline : nearline;
float n = excited*snoise(s*t);
blue = nearline <  n ? color(.9,.9,.9) : blue;

NN = normalize(NN);
point V=normalize(-I);
Nf = water > 0 ? NN : Nf;

  Ci = water*blue + noise(s,t)*Cs*(Ka * ambientcolor+ Kd * diffuse(Nf)+ Kd * diffuse(-Nf))+
    water*specularcolor*specular(NN,V,roughness)+ water*specularcolor*specular(-NN,V,roughness);


}
