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_parquet_plank.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.9 KB  |  161 lines

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