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_hextile.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.6 KB  |  123 lines

  1. /*
  2.  * hextile.sl -- surface shader for hexagonal tiles in st space
  3.  *
  4.  * DESCRIPTION
  5.  *       This surface shader operates in s-t space and gives a pattern of
  6.  *    hexagonal tiles, similar to that found as floor patterns in public
  7.  *    places and such.
  8.  *       The basic pattern is a hexagonal tiling, with a little bit of
  9.  *    color variation from tile to tile.  On top of that is some staining
  10.  *    (presumably due to water or something), which darkens the tile or
  11.  *    mortar underneath it.  Finally, there is scuffing due to people's
  12.  *    shoes, which really only affects the tile part not the mortar part.
  13.  *
  14.  *
  15.  * PARAMTERS
  16.  *    Ka, Kd, Ks, roughness, specularcolor - work just like plastic
  17.  *    tilecolor - the color of the tiles
  18.  *    mortarcolor - the color of the mortar (space between the tiles)
  19.  *    tileradius - the "radius" (in s-t units) of a single tile
  20.  *    mortarwidth - the width of the mortar (in s-t units)
  21.  *    tilevary - the color variance from tile to tile
  22.  *
  23.  * ANTIALIASING
  24.  *    Some rudimentary antialiasing is performed on the borders between
  25.  *    tile and mortar.
  26.  *
  27.  * HINTS & APPLICATIONS
  28.  *    If all of the default parameters are used, the tiles look just like
  29.  *    the floors in the public areas of the Washington DC subway system.
  30.  *
  31.  * AUTHOR: written by Larry Gritz, 1994
  32.  *
  33.  * HISTORY:
  34.  *    15 Feb 1994 -- written by lg
  35.  *
  36.  * last modified 15 Feb 94 by Larry Gritz
  37.  */
  38.  
  39.  
  40. #include "k3d_noises.h"
  41.  
  42. surface
  43. k3d_hextile (float Ka = .5;
  44.          float Kd = .5;
  45.          float Ks = .2;
  46.          float roughness = .1;
  47.      color specularcolor = 1;
  48.      color tilecolor = color(.55,0,0);
  49.      color mortarcolor = color(.5,.5,.5);
  50.      float tileradius = 0.2;
  51.      float mortarwidth = 0.02;
  52.      float tilevary = 0.15;
  53.      float scuffing = 0.5;
  54.      float stains = 0.4;
  55.          float stainfrequency = 2;
  56.          float scufffrequency = 4;
  57.      color scuffcolor = color (.05,.05,.05))
  58. {
  59.   point Nf;
  60.   color Ct, Ctile;
  61.   float tilewidth;
  62.   float ss, tt;
  63.   float ttile, stile;
  64.   float x, y;
  65.   float mortar;
  66.   float swidth, twidth, sfuzz, tfuzz, fuzzmax;
  67.   float mw2;
  68.   float tileindex;
  69.   float stain, scuff;
  70.   float ks;
  71.  
  72.   /* Determine how wide in s-t space one pixel projects to */
  73.   swidth = abs(Du(s)*du) + abs(Dv(s)*dv);
  74.   twidth = abs(Du(t)*du) + abs(Dv(t)*dv);
  75.   sfuzz = 0.5 * swidth;
  76.   tfuzz = 0.5 * twidth;
  77.   fuzzmax = max (sfuzz, tfuzz);
  78.  
  79.   tilewidth = tileradius * 1.7320508;  /* sqrt(3) */
  80.   tt = mod (t, 1.5*tileradius);
  81.   ttile = floor (t/(1.5*tileradius));
  82.   if (mod (ttile/2, 1) == 0.5)
  83.        ss = s + tilewidth/2;
  84.   else ss = s;
  85.   stile = floor (ss / tilewidth);
  86.   ss = mod (ss, tilewidth);
  87.   mortar = 0;
  88.   mw2 = mortarwidth/2;
  89.   if (tt < tileradius) {
  90.       mortar =  1 - (smoothstep(mw2,mw2+sfuzz,ss) *
  91.              (1 - smoothstep(tilewidth-mw2-sfuzz,tilewidth-mw2,ss)));
  92.     }
  93.   else {
  94.       x = tilewidth/2 - abs (ss - tilewidth/2);
  95.       y = 1.7320508 * (tt - tileradius);
  96.       if (y > x) {
  97.       if (mod (ttile/2, 1) == 0.5)
  98.           stile -= 1;
  99.       ttile += 1;
  100.       if (ss > tilewidth/2)
  101.           stile += 1;
  102.     }
  103.       mortar = (smoothstep (x-1.73*mw2-tfuzz, x-1.73*mw2, y) *
  104.         (1 - smoothstep (x+1.73*mw2, x+1.73*mw2+tfuzz, y)));
  105.     }
  106.  
  107.   tileindex = stile+41*ttile;
  108.   Ctile = tilecolor * (1 + tilevary * snoise(tileindex+0.5));
  109.  
  110.   stain = stains * smoothstep (.5,1, noise(s*stainfrequency,t*stainfrequency));
  111.  
  112.   scuff = scuffing * smoothstep (.6,1, noise(t*scufffrequency-90.26,
  113.                          s*scufffrequency+123.82));
  114.  
  115.   ks = Ks * (1-scuff/2);
  116.   Ct = (1-stain) * mix (mix (Ctile, scuffcolor, scuff), mortarcolor, mortar);
  117.   Oi = Os;
  118.   Nf = faceforward (normalize(N),I);
  119.   Ci = Os * ( Ct * (Ka*ambient() + Kd*diffuse(Nf)) +
  120.           specularcolor * ks*specular(Nf,-normalize(I),roughness));
  121. }
  122.  
  123.