home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / surface / k3d_antialiasedchecks.sl next >
Encoding:
Text File  |  2008-01-23  |  2.2 KB  |  67 lines

  1. /*
  2.  * checks_aa.sl -- RenderMan compatible shader for checks.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a checkered surface, fully antialiased!  This sucker should
  6.  *   look great even at only one sample per pixel.
  7.  * 
  8.  * PARAMETERS:
  9.  *   Ka, Kd            work just like the matte shader
  10.  *   color1, color2     these are the colors which make the pattern
  11.  *   frequency        determines the frequency (in s-t space) of the checks
  12.  *
  13.  *
  14.  * AUTHOR: written by Larry Gritz
  15.  *
  16.  * HISTORY:
  17.  *      27 Jan 1994 -- written by lg
  18.  *
  19.  * last modified 27 Jan 1994 by Larry Gritz
  20.  */
  21.  
  22. surface
  23. k3d_antialiasedchecks (float Ka = 1, Kd = 1, frequency = 10;
  24.        color color1 = 0, color2 = 1; )
  25. {
  26.   point Nf;             /* Forward facing surface normal */
  27.   float smod, tmod;     /* Texture position within the pattern */
  28.   color checkcolor;     /* Color of the checks */
  29.   float x, y;           /* Used to determine pattern */
  30.   float swidth, twidth, sfuzz, tfuzz;  /* Antialiasing */
  31.   float Nfactor;        /* Multiplicative factor for AA due to normal */
  32.   float fuzzmax;        /* max of (sfuzz, tfuzz) */
  33.  
  34.   Nf = faceforward (normalize(N), I);
  35.  
  36.   /* Determine how wide in s-t space one pixel projects to */
  37.   swidth = abs(Du(s)*du) + abs(Dv(s)*dv);
  38.   twidth = abs(Du(t)*du) + abs(Dv(t)*dv);
  39.  
  40.   /* Figure out amount of fuzziness, taking normal into account */
  41.   Nfactor = abs (Nf . I) / (length(Nf) * length(I));
  42.   sfuzz = .5 * swidth * frequency / Nfactor;
  43.   tfuzz = .5 * twidth * frequency / Nfactor;
  44.   fuzzmax = max (sfuzz, tfuzz);
  45.  
  46.   /* Get the place in the pattern where we're sampling */
  47.   smod = mod (s*frequency, 1);
  48.   tmod = mod (t*frequency, 1);
  49.  
  50.   /* If the filter width is small enough, compute the pattern color */
  51.   if (fuzzmax <= 0.5) {
  52.       x = ((smoothstep (.5,.5+sfuzz,smod)) + (1 - smoothstep (0,sfuzz,smod)));
  53.       y = ((smoothstep (.5,.5+tfuzz,tmod)) + (1 - smoothstep (0,tfuzz,tmod)));
  54.       checkcolor = mix (color1, color2, x*y + (1-x)*(1-y));
  55.       /* Gradually fade in the average color when we get close to the limit */
  56.       Ci = mix (checkcolor, (color1+color2)/2, smoothstep (.125, .5, fuzzmax));
  57.     }
  58.   else { /* otherwise, only use the average color */
  59.       Ci = (color1 + color2) / 2;
  60.     }
  61.  
  62.   /* Use the matte reflectance formula */
  63.   Oi = Os;
  64.   Ci *= Os * (Ka*ambient() + Kd*diffuse(Nf));
  65. }
  66.  
  67.