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_oakplank.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  6.6 KB  |  170 lines

  1. /****************************************************************************
  2.  * oakplank.sl
  3.  *
  4.  * Description: makes procedural varnished wood planks.  The planks
  5.  *    are projected onto the x-y plane, with the length aligned with
  6.  *    the y axis.  The subpattern within each individual plank is just
  7.  *    a shifted version of the oaktexture function from oak.h.
  8.  *   
  9.  * Parameters for the coordinate mapping: 
  10.  *   shadingspace - space in which the pattern is laid out
  11.  *   shadingfreq - overall scaling factor for the pattern
  12.  *   Pref - if supplied, gives the reference pose
  13.  *
  14.  * Parameters for the pattern of the plank structure: 
  15.  *   plankwidth, planklength - size of the planks
  16.  *   groovewidth, grooveheight - width of the grooves between planks
  17.  *   Cgroove - color of the grooves between the planks
  18.  *   groovedepth - how far down do the grooves displace?
  19.  *   edgewidth - how close to the plank border does the wood start to curl?
  20.  *   varyhue, varysat, varylum - control plank-to-plank color variation
  21.  * 
  22.  * Parameters for the color and pattern of the wood grain: 
  23.  *   Clightwood - the light, "background" wood color
  24.  *   Cdarkwood - the darker color in the ring/grain
  25.  *   ringfreq - mean frequency of ring spacing
  26.  *   ringunevenness - 0=equally spaced rings, larger is unequally spaced
  27.  *   grainfreq - frequency of the fine grain
  28.  *   ringnoise, ringnoisefreq - general warping of the domain
  29.  *   trunkwobble, trunkwobblefreq - controls noise which wobbles the
  30.  *       axis of the trunk so that it's not perfectly on the z axis.
  31.  *   angularwobble, angularwobblefreq - warping indexed by angle about
  32.  *       the z axis.
  33.  *   ringy, grainy - overall scale on the degree to which rings and
  34.  *       grain are weighted.  0 turns one off, 1 makes full effect.
  35.  *   divotdepth - depth (in shader units) of the displacement due to
  36.  *       ring or grain.
  37.  *   truedisp - 1 for true displacement, 0 for bump mapping
  38.  *
  39.  * Parameters for illumination model:
  40.  *   Ka, Kd, Ks, roughness - the usual meaning
  41.  *   Kr, blur, eta - reflection parameters for the tile
  42.  *   envname, envspace, envrad - environment mapping controls
  43.  *   rayjitter, raysamples - ray tracing controls
  44.  *   varnishlump, arnishlumpfreq - amp & freq of lumpiness in the varnish
  45.  *
  46.  ***************************************************************************
  47.  *
  48.  * Author: Larry Gritz, 1999
  49.  *
  50.  * Contacts:  lg@pixar.com
  51.  *
  52.  * $Revision: 1.2 $    $Date: 2006/03/08 16:01:22 $
  53.  *
  54.  ****************************************************************************/
  55.  
  56.  
  57. #include "k3d_project.h"
  58. #include "k3d_pshad.h"
  59. #include "k3d_material.h"
  60. #include "k3d_noises.h"
  61. #include "k3d_displace.h"
  62. #include "k3d_patterns.h"
  63.  
  64. #include "k3d_oak.h"
  65.  
  66.  
  67. /* Given 2-D texture coordinates ss,tt, filter widths ds, dt, and the
  68.  * width and height of the grooves between tiles, figure out which
  69.  * (integer indexed) plank we are on and what coordinates within our
  70.  * individual plank we are shading.
  71.  */
  72. float plankpattern(float ss, tt, ds, dt;
  73.            float plankwidth, planklength;
  74.            float groovewidth, grooveheight;
  75.            output float swhichplank, twhichplank;
  76.            output float splank, tplank;)
  77. {
  78.   /* Find which s plank we're on and our s coordinate within it */
  79.   swhichplank = floor(ss / plankwidth);
  80.   splank = ss - swhichplank * plankwidth;
  81.   /* Shift in t a random amount for each plank column */
  82.   float newt = tt + planklength * cellnoise(swhichplank);
  83.   /* Find which t plank we're on and our t coordinate within it */
  84.   twhichplank = floor(newt / planklength);
  85.   tplank = newt - twhichplank * planklength;
  86.   /* Calculate our "in-plank" value by multiplying two perpendicular
  87.    * filtered pulsetrain functions.
  88.    */
  89.   return filteredpulsetrain(groovewidth, plankwidth, ss + groovewidth / 2,
  90.                 ds) * filteredpulsetrain(grooveheight,
  91.                              planklength,
  92.                              newt + grooveheight / 2,
  93.                              dt);
  94. }
  95.  
  96.  
  97.  
  98. surface k3d_oakplank(float Ka = 1, Kd = 1, Ks = .75, roughness = 0.1;
  99.              float Kr = 1, blur = 0, eta = 1.5;
  100.              DECLARE_DEFAULTED_ENVPARAMS;
  101.              DEFAULT_PSHAD_PARAMS;
  102.              float ringfreq = 8, ringunevenness = 0.5;
  103.              float ringnoise = 0.02, ringnoisefreq = 1;
  104.              float grainfreq = 25;
  105.              float trunkwobble = 0.15, trunkwobblefreq = 0.025;
  106.              float angularwobble = 1, angularwobblefreq = 1.5;
  107.              float divotdepth = 0.012, truedisp = 0;
  108.              color Clightwood = color(.5, .2, .067);    /* light wood color */
  109.              color Cdarkwood = color(0.15, 0.077, 0.028);
  110.              color Cgroove = color(0.02, 0.02, 0.02);
  111.              float ringy = 1, grainy = 1;
  112.              float plankwidth = 2, planklength = 30;
  113.              float groovewidth = 0.05, grooveheight = 0.05;
  114.              float varyhue = 0.015, varysat = 0.1, varylum = 0.5;
  115.              float groovedepth = 0.03, edgewidth = 0.1;
  116.              float varnishlump = 0.01, varnishlumpfreq = 0.5;
  117.   )
  118. {
  119.   GET_PSHAD;
  120.   float ss = xcomp(Pshad), tt = ycomp(Pshad), height = zcomp(Pshad);
  121.   float dss = filterwidth(ss), dtt = filterwidth(tt);
  122.  
  123.   /*
  124.    * Find out where in the pattern we are: which plank we're on, and
  125.    * the (splank,tplank) coordinates (both on [0,1]) within our tile.
  126.    */
  127.   float swhichplank, twhichplank, splank, tplank;
  128.   float inplank = plankpattern(ss, tt, dss, dtt, plankwidth, planklength,
  129.                    groovewidth, grooveheight,
  130.                    swhichplank, twhichplank, splank, tplank);
  131.   float plankindex = swhichplank + 13 * twhichplank;
  132.   point Ppat =
  133.     point(splank - 0.5, height - 0.01 * tplank, tplank) + vector(1, 5,
  134.                                  10) *
  135.     (vector cellnoise(swhichplank, twhichplank) - 0.5);
  136.  
  137.   float wood = oaktexture(Ppat, dPshad, ringfreq, ringunevenness, grainfreq,
  138.               ringnoise, ringnoisefreq, trunkwobble,
  139.               trunkwobblefreq, angularwobble,
  140.               angularwobblefreq, ringy, grainy);
  141.   color Cwood = mix(Clightwood, Cdarkwood, wood);
  142.  
  143.   Cwood = varyEach(Cwood, plankindex, varyhue, varysat, varylum);
  144.   Cwood = mix(Cgroove, Cwood, inplank);
  145.  
  146.   /* Displacement: the edges of the planks displace down a bit, as do
  147.    * the grooves between planks. 
  148.    */
  149.   float edgedisp = smoothpulse(0, edgewidth, plankwidth - edgewidth,
  150.                    plankwidth, splank);
  151.   edgedisp *=
  152.     smoothpulse(0, edgewidth, planklength - edgewidth, planklength, tplank);
  153.   normal Nf = faceforward(normalize(N), I);
  154.   float disp = -wood * divotdepth + groovedepth * (edgedisp - 1);
  155.   disp +=
  156.     varnishlump * filteredsnoise(Pshad * varnishlumpfreq,
  157.                  dPshad * varnishlumpfreq);
  158.   Nf = faceforward(Displace(Nf, "shader", disp, truedisp), I);
  159.  
  160.   /* Illumination model
  161.    * Less specular in the grooves, more specular in the dark wood. 
  162.    */
  163.   float specadjusted = 1 + .3 * wood - 0.8 * (1 - inplank);
  164.   Ci =
  165.     MaterialShinyPlastic(Nf, Cwood, Ka, Kd, specadjusted * Ks, roughness,
  166.              specadjusted * Kr, blur, eta, 1, ENVPARAMS);
  167.   Oi = Os;
  168.   Ci *= Oi;
  169. }
  170.