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_tooledsteel.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.3 KB  |  169 lines

  1. /*  tooledsteel.sl written 9/99 by Ivan DeWolf
  2.  *  ivan@SpamSucks_martian-tools.com
  3.  *  feel free to copy, distribute, hack and/or abuse this code 
  4.  *  in any way you see fit, but please leave my name near the top
  5.  *
  6.  *  a combination of IDbrushedmetal.sl and the Worley-esque
  7.  *  F1 from bubbly.sl adapted to happen in parameter space.
  8.  *  A vector is computed from the gradient of the distance function,
  9.  *  and then used as the vector of anisotropy. works best on parametric
  10.  *  surfaces where you don't care about the seams. 
  11.  *  (like that'll ever happen...)
  12.  *  thanks to Jos Stam whose animated cube motivated this thing.
  13.  *
  14.  *
  15.  *  PARAMETERS:
  16.  *  mult            - multiples of swirls per unit
  17.  *  Nzscale         - scale of the noise that randomizes the location of 
  18.  *                    the swirls. set this to zero for a perfect grid.
  19.  *  Ks            - coefficient of specular
  20.  *  Kd            - coefficient of diffuse
  21.  *  Ka            - coefficient of ambient
  22.  *  Kr            - coefficient of reflectivity
  23.  *  specwidth        - the width of the specular stripe
  24.  *               maxes out at 10
  25.  *  specspread        - the spread of the specular stripe
  26.  *              maxes out at .5
  27.  *  mapspread        - the spread of the reflection map streaking
  28.  *              maxes out at .5
  29.  *  twist        - allows you to twist the direction of anisotropy
  30.  *              angle in radians (i.e. PI*.5 = 90 degrees)
  31.  *  mapname        - name of the environment map
  32.  *  specularcolor    - color of the specular hilight
  33.  */
  34.  
  35. vector
  36. getvec(float  mult; float Nzscale)
  37. {
  38.   extern point P;
  39.   extern vector dPdu;
  40.   extern vector dPdv;
  41.   extern float s;
  42.   extern float t;
  43.   vector udir = vector(1,0,0);
  44.   vector vdir = vector(0,1,0);
  45.  
  46.   float a,b,c;
  47.   float dist, shortest=10000;
  48.   float valu,valv;
  49.   point Po = point(s*mult,t*mult,0);
  50.   point Pou = Po +(udir*.01);
  51.   point Pov = Po +(vdir*.01);
  52.  
  53.   /*true cell center, surrounding cell centers, noised cell center*/
  54.   point trucell, surrcell, nzcell;
  55.   vector offset, nzoff, out;
  56.   float chu, chv;
  57.   
  58.   float ncells = floor(mult);
  59.   float cellsize = 1/ncells;
  60.   
  61.   setxcomp(trucell,floor(xcomp(Po))+.5);
  62.   setycomp(trucell,floor(ycomp(Po))+.5);
  63.   setzcomp(trucell,0);
  64.   c = 0;
  65.                  
  66.   /*what is the shortest distance to a noised cell center?*/
  67.   for(a = -1; a<= 1; a+=1){
  68.     for(b = -1; b<=1; b += 1){
  69.     offset = vector(a,b,c);
  70.     surrcell = trucell+offset;
  71.     nzoff = ((vector cellnoise(surrcell)-.5)*Nzscale);
  72.     setzcomp(nzoff,0);
  73.     nzcell = surrcell + nzoff;
  74.     dist = distance(Po,nzcell);
  75.     if(dist<shortest){
  76.       shortest = dist;
  77.       valu = distance(Pou,nzcell);
  78.       valv = distance(Pov,nzcell);
  79.     }
  80.     }
  81.   }
  82.   chu = valu - shortest;
  83.   chv = valv - shortest;
  84.   out  = normalize((udir*chu)+(vdir*chv));
  85.   return out;
  86. }
  87.  
  88. color
  89. anisospecular (vector VA; float specspread; float specwidth)
  90. {
  91.  
  92.     extern vector I;
  93.     extern normal N;
  94.     extern point P;
  95.  
  96.     float nonspec;
  97.     vector V = normalize(-I);
  98.     normal NN = normalize(N);
  99.     normal Nf = faceforward(NN,-V);
  100.     color pixbrdf, C = 0;
  101.     point zro = 0;
  102.  
  103.     illuminance (P, Nf, PI*.5) {
  104.         extern vector L;  
  105.     extern color Cl;
  106.  
  107.         nonspec = 0;
  108.         lightsource ("__nonspecular", nonspec);
  109.         if (nonspec < 1) {
  110.             vector LN = normalize (L);
  111.         vector H = normalize (V + LN);
  112.  
  113.         pixbrdf  = specularbrdf(LN, Nf,V,specspread);
  114.             C += Cl * pixbrdf * pow( 1-abs(VA.H), 1/specwidth );
  115.  
  116.         }
  117.     }
  118.     return C;
  119. }
  120.  
  121. surface
  122. k3d_tooledsteel(
  123.     float    mult        =  3,
  124.         Nzscale        =  1.5,
  125.         Ks        =  1,
  126.         Kd        = .01,
  127.         Ka        = .001,
  128.         Kr        = .6,
  129.         specwidth    = .5,
  130.         specspread     = .5,
  131.         mapspread     = .2,
  132.         twist        =  0;
  133.     string    mapname     = "";
  134.     color    specularcolor    =  1)
  135. {
  136.         vector VA = getvec(mult,Nzscale);
  137.     point Po = transform("object",P);
  138.     color ev = 0;
  139.     vector D, V= normalize(-I);
  140.     vector Nf, Ntmp;
  141.         point zro = 0;
  142.     float i, numsamples = 20;
  143.     float angle, jitter;
  144.     float Jspread = PI*(1/numsamples)*mapspread;
  145.  
  146.     VA = rotate(VA, twist, zro, normalize(N));
  147.     
  148.  
  149.     Nf = faceforward(normalize(N), -I);
  150.         Ntmp = Nf;
  151.  
  152.     if( mapname != "" ) {
  153.         for(i=0;i<=numsamples;i=i+1){
  154.           jitter = (random()-.5)*Jspread;
  155.           angle = PI*((i/numsamples)-.5)*mapspread;
  156.           Ntmp = rotate(Nf,angle+jitter,zro,VA);
  157.           D = reflect(-V, Ntmp);
  158.           D = vtransform("world", D);
  159.           ev += environment(mapname, D)*(.5-abs((i/numsamples)-.5))*.25;
  160.         }
  161.     } else
  162.         ev = 0;
  163.  
  164.     Oi = Os;
  165.     Ci = Oi * (Cs * (Ka * ambient() + Kd * diffuse(-Nf)) + 
  166.     specularcolor * Ks * anisospecular(VA, specspread*2, specwidth*.1)) +
  167.     ev * Kr;
  168. }
  169.