home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / RCShaders / RCWood.sl < prev   
Encoding:
Text File  |  1995-03-22  |  1.2 KB  |  50 lines

  1. /* Listing 16.15  Surface shader providing  wood-grain texture*/
  2. /*
  3.  *  wood(): calculate a solid wood texture using noise()
  4.  */
  5. surface
  6. RCWood(
  7.     float    ringscale    = 10;
  8.     color    lightwood    = color (0.3, 0.12, 0.03),
  9.         darkwood    = color (0.05, 0.01, 0.005);
  10.     float    Ka        = 0.2,
  11.         Kd        = 0.4,
  12.         Ks        = 0.6,
  13.         roughness    = 0.1 )
  14. {
  15.      point NN, V;
  16.      point PP;
  17.      float y, z, r;
  18.     /*
  19.      * Compute the forward-facing normal NN and the vector
  20.      * toward the ray origin V, both normalized.
  21.      * These vectors are used by "specular" and "diffuse". */
  22.     NN = faceforward(normalize(N),I);
  23.     V = -normalize(I);
  24.  
  25.     /* put point in shader space and perturb it to add irregularity */
  26.     PP = transform("shader", P);
  27.     PP += noise(PP);
  28.  
  29.     /* compute radial distance r from PP to axis of "tree" */
  30.     y = ycomp(PP);
  31.     z = zcomp(PP);
  32.     r = sqrt(y*y + z*z);
  33.  
  34.     /* map radial distance r into ring position [0,1] */
  35.     r *= ringscale;
  36.     r += abs(noise(r));
  37.     r -= floor(r);            /* == mod(r,1) */
  38.  
  39.     /* use ring position r to select wood color */
  40.     r = smoothstep(0, 0.8, r) - smoothstep(0.83, 1.0, r);
  41.     Ci = mix(lightwood, darkwood, r);
  42.  
  43.     /* shade using r to vary shininess */
  44.     Oi = Os;
  45.     Ci = Oi * Ci * (Ka * ambient() + Kd * diffuse(NN))
  46.          + (0.3*r + 0.7) * Ks * specular(NN, V, roughness);
  47. }
  48.  
  49.  
  50.