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_screen_aa.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  2.5 KB  |  83 lines

  1. /*
  2.  * screen_aa.sl -- RenderMan compatible shader for a metalic screen.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a surface that looks like a metal screen.  Strips of metal run
  6.  *   parallel to lines of s and t.  You can adjust the Ka, Kd, Ks, etc.
  7.  *   to change the material appearance.  This texture antialiases pretty
  8.  *   well, even with only one sample per pixel.
  9.  *
  10.  * PARAMETERS:
  11.  *   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic shader
  12.  *   frequency - how many cycles of screen in st space
  13.  *   density - how much of each cycle is opaque?
  14.  *
  15.  * AUTHOR: written by Larry Gritz
  16.  *
  17.  * last modified  31 Jan 1994 by Larry Gritz
  18.  *
  19.  *
  20.  * The RenderMan (R) Interface Procedures and RIB Protocol are:
  21.  *     Copyright 1988, 1989, Pixar.  All rights reserved.
  22.  * RenderMan (R) is a registered trademark of Pixar.
  23.  */
  24.  
  25. #include "k3d_rmannotes.h"
  26.  
  27.  
  28.  
  29. surface k3d_screen_aa(float Ka = 1, Kd = 0.75, Ks = 0.4, roughness = 0.1;
  30.               color specularcolor = 1;
  31.               float density = 0.25, frequency = 20;)
  32. {
  33.   normal Nf;            /* Forward facing Normal vector */
  34.   vector IN;            /* normalized incident vector */
  35.   float d;            /* Density at the sample point */
  36.   float ss, tt;            /* s,t, parameters in phase */
  37.   float swidth, twidth, GWF, w, h;
  38.  
  39.   /* Compute a forward facing normal */
  40.   IN = normalize(I);
  41.   Nf = faceforward(normalize(N), I);
  42.  
  43.   /* Determine how wide in s-t space one pixel projects to */
  44.   swidth = max(abs(Du(s) * du) + abs(Dv(s) * dv), MINFILTERWIDTH) * frequency;
  45.   twidth = max(abs(Du(t) * du) + abs(Dv(t) * dv), MINFILTERWIDTH) * frequency;
  46.  
  47.   /* Figure out where in the pattern we are */
  48.   ss = mod(frequency * s, 1);
  49.   tt = mod(frequency * t, 1);
  50.  
  51.   /* Figure out where the strips are. Do some simple antialiasing. */
  52.   GWF = density * 0.5;
  53.   if(swidth >= 1)
  54.     w = 1 - 2 * GWF;
  55.   else
  56.     w =
  57.       clamp(boxstep(GWF - swidth, GWF, ss), max(1 - GWF / swidth, 0),
  58.         1) - clamp(boxstep(1 - GWF - swidth, 1 - GWF, ss), 0,
  59.                2 * GWF / swidth);
  60.   if(twidth >= 1)
  61.     h = 1 - 2 * GWF;
  62.   else
  63.     h =
  64.       clamp(boxstep(GWF - twidth, GWF, tt), max(1 - GWF / twidth, 0),
  65.         1) - clamp(boxstep(1 - GWF - twidth, 1 - GWF, tt), 0,
  66.                2 * GWF / twidth);
  67.   /* This would be the non-antialiased version:
  68.    *    w = step (GWF,ss) - step(1-GWF,ss);
  69.    *    h = step (GWF,tt) - step(1-GWF,tt);
  70.    */
  71.   d = 1 - w * h;
  72.  
  73.   Oi = d;
  74.   if(d > 0)
  75.     {
  76.       Ci =
  77.     Oi * (Cs * (Ka * ambient() + Kd * diffuse(Nf)) +
  78.           specularcolor * Ks * specular(Nf, -IN, roughness));
  79.     }
  80.   else
  81.     Ci = 0;
  82. }
  83.