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_tile.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  5.6 KB  |  172 lines

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