home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / WWShaders / WWGrid.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  2.4 KB  |  81 lines

  1. /* adapted from Larry Gritz's antialiased screen shader (see BMRTShaders/BMAntialiasedScreen)
  2.  */
  3. /*
  4.  * WWGrid.sl -- shader that makes a metal screen with potentially different grids in s and t
  5.  *
  6.  * DESCRIPTION:
  7.  *   Makes a surface that looks like a metal screen.  Strips of metal run
  8.  *   parallel to lines of s and t.  You can adjust the Ka, Kd, Ks, etc.
  9.  *   to change the material appearance.  This texture antialiases pretty
  10.  *   well, even with only one sample per pixel.
  11.  *
  12.  * PARAMETERS:
  13.  *   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic shader
  14.  *   frequency - how many cycles of screen in st space
  15.  *   density - how much of each cycle is opaque?
  16.  *
  17.  * AUTHOR: hacked by wave (again, from an original shader by Larry Gritz)
  18.  *
  19.  */
  20.  
  21.  
  22.  
  23. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  24. #define MINFILTERWIDTH 1.0e-7
  25.  
  26.  
  27.  
  28. surface
  29. WWGrid (float Ka = 1, Kd = 0.75, Ks = 0.4, roughness = 0.1;
  30.                  color specularcolor = 1;
  31.                  float sDensity = 0.2, sFrequency = 2, 
  32.                            tDensity = .1, tFrequency = 10;)
  33. {
  34.   point Nf;     /* Forward facing Normal vector */
  35.   point IN;     /* normalized incident vector */
  36.   float d;      /* Density at the sample point */
  37.   float ss, tt; /* s,t, parameters in phase */
  38.   float swidth, twidth, sGWF, tGWF, w, h;
  39.  
  40.  
  41.   /* Compute a forward facing normal */
  42.   IN = normalize (I);
  43.   Nf = faceforward (normalize(N), I);
  44.  
  45.   /* Determine how wide in s-t space one pixel projects to */
  46.   swidth = max (abs(Du(s)*du) + abs(Dv(s)*dv), MINFILTERWIDTH) * sFrequency;
  47.   twidth = max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH) * tFrequency;
  48.  
  49.   /* Figure out where in the pattern we are */
  50.   ss = mod (sFrequency * s, 1);
  51.   tt = mod (tFrequency * t, 1);
  52.  
  53.   /* Figure out where the strips are. Do some simple antialiasing. */
  54.   sGWF = sDensity*0.5;
  55.   if (swidth >= 1)
  56.   {  w = 1 - 2*sGWF;
  57.   }
  58.   else 
  59.   {  w =   clamp (boxstep(sGWF-swidth, sGWF, ss), max(1-sGWF/swidth, 0), 1)
  60.        - clamp (boxstep(1-sGWF-swidth, 1-sGWF, ss), 0, 2*sGWF/swidth);
  61.   }
  62.  
  63.   tGWF = tDensity*0.5;
  64.   if (twidth >= 1)
  65.   {  h = 1 - 2*tGWF;
  66.   }
  67.   else 
  68.   {  h =   clamp (boxstep(tGWF-twidth, sGWF, tt), max(1-tGWF/twidth, 0), 1)
  69.      - clamp (boxstep(1-tGWF-twidth, 1-tGWF, tt), 0, 2*tGWF/twidth);
  70.   }
  71.  
  72.   d = 1 - w*h;
  73.   Oi = d;
  74.   if (d > 0) {
  75.       Ci = Oi * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
  76.          specularcolor * Ks*specular(Nf,-IN,roughness));
  77.     }
  78.   else
  79.       Ci = 0;
  80. }
  81.