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

  1. /*
  2.  * brick.sl -- Surface shader for a bricks.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a wall of bricks.  Need more be said?  OK.  It makes a good
  6.  *   looking staggered brick masonry.  It is especially convincing when
  7.  *   used in conjunction with the "brickbump" displacement shader (and
  8.  *   identical parameters).  Every other row of bricks is staggered.
  9.  *   The staggering isn't exact, however, and this variance is controlled
  10.  *   by the "rowvary" parameter.
  11.  * 
  12.  * PARAMETERS:
  13.  *    Ka, Kd            The usual
  14.  *    brickcolor, mortarcolor    Pretty obvious (default is red bricks)
  15.  *    brickvary                 How much does the brick color vary from
  16.  *                        brick to brick?
  17.  *    brickwidth                Width of a brick (in st space)
  18.  *    brickheight               Height of a brick (in st space)
  19.  *    mortarthickness           Thickness of the mortar (in st space)
  20.  *    rowvary                   How much does each row shift?
  21.  *    jagged                    How much do bricks deviate from squares?
  22.  *
  23.  * AUTHOR: written by Larry Gritz, 1992
  24.  *     This shader is very similar to (and based upon) brick shaders
  25.  *    by Darwyn Peachey.
  26.  *
  27.  * 
  28.  * HISTORY:
  29.  *      28 May 1992 -- written by lg for the "Timbre Trees" video (saucer)
  30.  *      12 Jan 1994 -- recoded by lg in correct shading language.
  31.  *
  32.  * last modified  12 Jan 1994 by Larry Gritz
  33.  */
  34. #include "k3d_noises.h"
  35.  
  36.  
  37. surface
  38. k3d_brick3 ( float Ka = 1, Kd = 1;
  39.     color brickcolor = color "rgb" (.6,.1,.1);
  40.     color mortarcolor = color "rgb" (.6,.6,.6);
  41.         float jagged = 0.006, brickvary = 0.3;
  42.         float brickwidth = .25, brickheight = .08;
  43.         float mortarthickness = .01;
  44.         float rowvary = .25; )
  45. {
  46. #define BMWIDTH (brickwidth+mortarthickness)
  47. #define BMHEIGHT (brickheight+mortarthickness)
  48. #define MWF (mortarthickness*0.5/BMWIDTH)
  49. #define MHF (mortarthickness*0.5/BMHEIGHT)
  50. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  51. #define MINFILTERWIDTH 1.0e-7
  52.   color bcolor, Ct;
  53.   point PP2, Nf;
  54.   float sbrick, tbrick, w, h;
  55.   float scoord, tcoord, ss, tt;
  56.   float swidth, twidth;
  57.   float Nfactor;
  58.  
  59.   /* Determine how wide in s-t space one pixel projects to */
  60.   swidth = max (abs(Du(s)*du) + abs(Dv(s)*dv), MINFILTERWIDTH);
  61.   twidth = max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH);
  62.  
  63.   Nf = faceforward (normalize(N),I);
  64.  
  65.   /* Make the shapes of the bricks vary just a bit */
  66.   PP2 = point noise (s/BMWIDTH, t/BMHEIGHT);
  67.   scoord = s + jagged * xcomp (PP2);
  68.   tcoord = t + jagged * ycomp (PP2);
  69.  
  70.   ss = scoord / BMWIDTH;   /* Determine which brick the point is in */
  71.   tt = tcoord / BMHEIGHT;  /*                   "                   */
  72.   swidth /= BMWIDTH;
  73.   twidth /= BMHEIGHT;
  74.  
  75.   /* shift alternate rows */
  76.   if (mod (tt*0.5, 1) > 0.5)
  77.       ss += 0.5;
  78.  
  79.   tbrick = floor (tt);   /* which brick row? */
  80.   /* Shift the columns randomly by row */
  81.   ss += rowvary * (noise (tbrick+0.5) - 0.5);
  82.  
  83.   sbrick = floor (ss);   /* which brick column? */
  84.   ss -= sbrick;          /* Now ss and tt are coords within the brick */
  85.   tt -= tbrick;
  86.  
  87.   /* Choose a color for the surface */
  88.   if (swidth >= 1)
  89.       w = 1 - 2*MWF;
  90.   else w = clamp (boxstep(MWF-swidth,MWF,ss), max(1-MWF/swidth,0), 1)
  91.      - clamp (boxstep(1-MWF-swidth,1-MWF,ss), 0, 2*MWF/swidth);
  92.  
  93.   if (twidth >= 1)
  94.       h = 1 - 2*MHF;
  95.   else h = clamp (boxstep(MHF-twidth,MHF,tt), max(1-MHF/twidth,0),1)
  96.      - clamp (boxstep(1-MHF-twidth,1-MHF,tt), 0, 2*MHF/twidth);
  97.  
  98.   /* Choose a brick color that varies from brick to brick */
  99.   bcolor = brickcolor * (1 + (brickvary * snoise (tbrick+(100*sbrick)+0.5)));
  100.  
  101.   Ct = mix (mortarcolor, bcolor, w*h);
  102.  
  103.   Oi = Os;
  104.   Ci = Os * Ct * (Ka * ambient() + Kd*diffuse(Nf));
  105. }
  106.