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_rustymetal.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.4 KB  |  102 lines

  1. /*
  2.  * rustymetal.sl -- metal with specks of rust
  3.  *
  4.  * DESCRIPTION:
  5.  *   A rough metal surface with controllable rust spots.  The rust pattern
  6.  *   is basically thresholded turbulence (summed abs(snoise)).  Where it's
  7.  *   rusty, shade like rust colored matte, and also make it bumpy (like
  8.  *   the corrosion is kind of grainy).  Where there is no rust, shade like
  9.  *   regular metal.  All computations are done in shader space.
  10.  *
  11.  * PARAMETERS
  12.  *   metalKa, metalKs, metalroughness - control the appearance of the metal.
  13.  *   rustKa, rustKd, rustcolor - control the appearance of the rust.
  14.  *   txtscale - overall scaling factor of the rust pattern.
  15.  *   rusty - 0=no rust, larger for more rust, 1=completely rusty
  16.  *   rustbump - controls the "bumpiness" of the rusty areas.
  17.  *
  18.  * ANTIALIASING:
  19.  *   The fractal sum used to determine the rust pattern chooses a number of
  20.  *   octaves to sum based on the shader sampling rate.  This helps to keep
  21.  *   aliasing under control.
  22.  *
  23.  * AUTHOR: Larry Gritz, gritz@SpamSucks_seas.gwu.edu
  24.  *         The George Washington University
  25.  *
  26.  * HISTORY:
  27.  *   19 Jan 1995 - gritz - created
  28.  *
  29.  *   last modified 19 Jan 95 
  30.  */
  31.  
  32.  
  33.  
  34. #include "k3d_noises.h"
  35.  
  36. /* Maximum number of octaves */
  37. #define MAXOCTAVES 8
  38.  
  39.  
  40.  
  41.  
  42. surface
  43. k3d_rustymetal (float metalKa = 1, metalKs = 1, metalroughness = .1;
  44.         float rustKa = 1, rustKd = 1;
  45.         color rustcolor = color (.437, .084, 0);
  46.         float txtscale = 1;
  47.         float rusty = 0.2;
  48.         float rustbump = 0.035;
  49.     )
  50. {
  51.   point Nf, V;                 /* normal and view vector used for shading */
  52.   point Nrust;                 /* perturbed normal for the rusty areas */
  53.   point PP;                    /* shade space point */
  54.   float i, sum = 0, a = 1;     /* Loop control for fractal sum */
  55.   float alimit;                /* Limit sum to do simple antialiasing */
  56.   float rustiness;             /* Result: how rusty is this point? */
  57.   color Cmetal = 0, Crust = 0; /* Computed colors of metal & rust */
  58.  
  59.   /* Sum several octaves of abs(snoise), i.e. turbulence.  Limit the
  60.    * number of octaves by the estimated change in PP between adjacent
  61.    * shading samples.
  62.    */
  63.   PP = txtscale * transform ("shader", P);
  64.   alimit = sqrt (area(PP));
  65.   for (i = 0;  i < MAXOCTAVES  &&  a > alimit;  i += 1) {
  66.       sum += a * abs(snoise(PP));
  67.       PP *= 2;
  68.       a /= 2;
  69.     }
  70.   /* If it's rusty, also add a high frequency bumpiness to the normal */
  71.   Nrust = calculatenormal (P + rustbump * snoise(PP) * normalize(N));
  72.  
  73.   /* Scale the rust appropriately, modulate it by another noise 
  74.    * computation, then sharpen it by squaring its value.
  75.    */
  76.   rustiness = step (1-rusty, clamp (sum,0,1));
  77.   rustiness *= clamp (abs(snoise(PP)), 0, .08) / 0.08;
  78.   rustiness *= rustiness;
  79.  
  80.   /* If we have any rust, calculate the color of the rust, taking into
  81.    * account the perturbed normal and shading like matte.
  82.    */
  83.   if (rustiness > 0) {
  84.       Nf = faceforward (normalize(Nrust),I);
  85.       Crust = rustcolor * (rustKa*ambient() + rustKd*diffuse(Nf));
  86.     }
  87.   /* If we have any metal, calculate the color of the metal, using the
  88.    * original (smooth) normal and the usual metal illumination model.
  89.    */
  90.   if (rustiness < 1) {
  91.       Nf = faceforward (normalize(N),I);
  92.       V = -normalize(I);
  93.       Cmetal = Cs * (metalKa*ambient() + metalKs*specular(Nf,V,metalroughness));
  94.     }
  95.  
  96.   /* Now blend the metal and rust colors depending on the computed value
  97.    * of the rustiness.
  98.    */
  99.   Oi = Os;
  100.   Ci = Oi * mix (Cmetal, Crust, rustiness);
  101. }
  102.