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_plank.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.3 KB  |  150 lines

  1. /*
  2.  * plank.sl -- another surface shader for wood.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes texture of wooden planks in s-t space.  This wood looks rather
  6.  *   like oak planks.
  7.  *
  8.  * PARAMETERS:
  9.  *   Ka, Kd, Ks, specular, roughness - work just like the plastic shader
  10.  *   txtscale - overall scaling factor for the texture
  11.  *   ringscale - scaling for the ring spacing
  12.  *   grainscale - scaling for the fine grain
  13.  *   plankwidth - width of each plank (in terms of s/t)
  14.  *   planklength - length of each plank (in terms of s/t)
  15.  *   groovewidth - width of the grooves between the planks (in terms of s/t)
  16.  *   lightwood, darkwood - surface colors for the wood itself
  17.  *   groovecolor - the color of the "grooves" between the planks
  18.  *   plankvary - controls how much wood color varies from plank to plank
  19.  *   grainy - relative graininess (0 = no fine grain)
  20.  *   wavy - relative wavyness of the ring pattern
  21.  *
  22.  * ANTIALIASING: this shader does a pretty good job of antialiasing itself,
  23.  *   even with low sampling densities.
  24.  *
  25.  * AUTHOR: Larry Gritz, lg@bmrt.org
  26.  *
  27.  */
  28.  
  29. #include "k3d_noises.h"
  30. #include "k3d_rmannotes.h"
  31.  
  32. surface k3d_plank(float Ka = 1, Kd = 0.75, Ks = .15, roughness = .05;
  33.           color specularcolor = 1;
  34.           float ringscale = 15, grainscale = 60;
  35.           float txtscale = 1;
  36.           color lightwood = color(0.57, 0.292, 0.125);
  37.           color darkwood = color(0.275, 0.15, 0.06);
  38.           color groovecolor = color(.05, .04, .015);
  39.           float plankwidth = .05, planklength = .75, groovewidth =
  40.           0.001; float plankvary = 0.8;
  41.           float grainy = 1, wavy = 0.08;)
  42. {
  43.  
  44.   float r, r2;
  45.   normal Nf;
  46.   float whichrow, whichplank;
  47.   float swidth, twidth, fwidth, ss, tt, w, h, fade, ttt;
  48.   color Ct, woodcolor;
  49.   float groovy;
  50.   float PGWIDTH, PGHEIGHT, GWF, GHF;
  51.  
  52.   PGWIDTH = plankwidth + groovewidth;
  53.   PGHEIGHT = planklength + groovewidth;
  54.   GWF = groovewidth * 0.5 / PGWIDTH;
  55.   GHF = groovewidth * 0.5 / PGHEIGHT;
  56.  
  57.   /* Determine how wide in s-t space one pixel projects to */
  58.   swidth =
  59.     max(abs(Du(s) * du) + abs(Dv(s) * dv),
  60.     MINFILTERWIDTH) / PGWIDTH * txtscale;
  61.   twidth =
  62.     max(abs(Du(t) * du) + abs(Dv(t) * dv),
  63.     MINFILTERWIDTH) / PGHEIGHT * txtscale;
  64.   fwidth = max(swidth, twidth);
  65.  
  66.   Nf = faceforward(normalize(N), I);
  67.  
  68.   ss = txtscale * s / PGWIDTH;
  69.   whichrow = floor(ss);
  70.   ss -= whichrow;
  71.   /* Jiggle each row */
  72.   tt = (txtscale * t / PGHEIGHT) + 10 * snoise(0.5 + whichrow);
  73.   whichplank = floor(tt);
  74.   tt -= whichplank;
  75.   whichplank += 20 * whichrow;
  76.  
  77.   /*
  78.    * Figure out where the grooves are.  The value groovy is 0 where there
  79.    * are grooves, 1 where the wood grain is visible.  Do some simple
  80.    * antialiasing.
  81.    */
  82.   if(swidth >= 1)
  83.     w = 1 - 2 * GWF;
  84.   else
  85.     w =
  86.       clamp(boxstep(GWF - swidth, GWF, ss), max(1 - GWF / swidth, 0),
  87.         1) - clamp(boxstep(1 - GWF - swidth, 1 - GWF, ss), 0,
  88.                2 * GWF / swidth);
  89.   if(twidth >= 1)
  90.     h = 1 - 2 * GHF;
  91.   else
  92.     h =
  93.       clamp(boxstep(GHF - twidth, GHF, tt), max(1 - GHF / twidth, 0),
  94.         1) - clamp(boxstep(1 - GHF - twidth, 1 - GHF, tt), 0,
  95.                2 * GHF / twidth);
  96.   /* This would be the non-antialiased version:
  97.    * w = step (GWF,ss) - step(1-GWF,ss);
  98.    * h = step (GHF,tt) - step(1-GHF,tt);
  99.    */
  100.   groovy = w * h;
  101.  
  102.  
  103.   /*
  104.    * Add the ring patterns
  105.    */
  106.   fade = smoothstep(1 / ringscale, 8 / ringscale, fwidth);
  107.   if(fade < 0.999)
  108.     {
  109.       ttt = tt + whichplank / 28.38 + wavy * noise(8 * ss, tt);
  110.       r = ringscale * noise(ss - whichplank, ttt);
  111.       r -= floor(r);
  112.       r =
  113.     0.3 + 0.7 * smoothstep(0.2, 0.55, r) * (1 - smoothstep(0.75, 0.8, r));
  114.       r = (1 - fade) * r + 0.65 * fade;
  115.  
  116.       /*
  117.        * Multiply the ring pattern by the fine grain
  118.        */
  119.       fade = smoothstep(2 / grainscale, 8 / grainscale, fwidth);
  120.       if(fade < 0.999)
  121.     {
  122.       r2 = 1.3 - noise(ss * grainscale, (tt * grainscale));
  123.       r2 = grainy * r2 * r2 + (1 - grainy);
  124.       r *= (1 - fade) * r2 + (0.75 * fade);
  125.     }
  126.       else
  127.     r *= 0.75;
  128.     }
  129.   else
  130.     r = 0.4875;
  131.  
  132.  
  133.   /* Mix the light and dark wood according to the grain pattern */
  134.   woodcolor = mix(lightwood, darkwood, r);
  135.  
  136.   /* Add plank-to-plank variation in overall color */
  137.   woodcolor *=
  138.     (1 - plankvary / 2 + plankvary * (float noise(whichplank + 0.5)));
  139.  
  140.   Ct = mix(groovecolor, woodcolor, groovy);
  141.  
  142.   /*
  143.    * Use the plastic illumination model
  144.    */
  145.   Oi = Os;
  146.   Ci =
  147.     Os * (Ct * (Ka * ambient() + Kd * diffuse(Nf)) +
  148.       specularcolor * Ks * specular(Nf, -normalize(I), roughness));
  149. }
  150.