home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / displacemnt / k3d_brickbump3.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.8 KB  |  116 lines

  1. /*
  2.  * brickbump.sl -- displacement shader for bricks.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes displacements for a wall of bricks.  This is the companion
  6.  *   shader to the surface "brick" shader.  The parameters work exactly
  7.  *   the same.  Of course, you can use it with any surface shader, and
  8.  *   in fact matte or plastic gives those nice white cinder block walls.
  9.  *   However, if you do use it with "brick", the parameters MUST match,
  10.  *   or your bricks will look very strange.
  11.  * 
  12.  * PARAMETERS:
  13.  *    brickwidth                Width of a brick (in st space)
  14.  *    brickheight               Height of a brick (in st space)
  15.  *    mortarthickness           Thickness of the mortar (in st space)
  16.  *    rowvary                   How much does each row shift?
  17.  *    jagged                    How much do bricks deviate from squares?
  18.  *    pitting                   The amplitude of the "pits" on the face of
  19.  *                                 the bricks.
  20.  *    pockfrequency             The st frequency of the pits.
  21.  *    groovedepth               The depth of the grooves between bricks.
  22.  *
  23.  * AUTHOR: written by Larry Gritz, 1992
  24.  *
  25.  * HISTORY:
  26.  *      28 May 1992 -- written by lg for the "Timbre Trees" video (saucer)
  27.  *      12 Jan 1994 -- recoded by lg in correct shading language.
  28.  *
  29.  * last modified  12 Jan 1994 by Larry Gritz
  30.  */
  31.  
  32. /* note from Larry:
  33.  
  34.   You may note the companion shaders "brick" and "brickbump".
  35.  
  36.   They're meant to go together, but I like to use the brickbump
  37.   displacement shader with the matte surface shader.  With appropriate
  38.   parameters, it looks *exactly* like those painted cinderblock walls
  39.   (we have a wall in our lab like this).  
  40.   ~gritz/brick.tif.
  41.  
  42.   I think good parameters for this look are:
  43.  
  44.   Displacement "brickbump" "brickwidth" 0.5 "brickheight" 0.25 
  45.            "mortarthickness" 0.02 "pitting" 0.015 "pockfrequency" 12
  46.               "groovedepth" 0.015
  47. */
  48. #include "k3d_functions.h"
  49. #include "k3d_noises.h"
  50.  
  51. displacement
  52. k3d_brickbump3 ( float jagged = 0.006;
  53.             float brickwidth = .25, brickheight = .08;
  54.         float mortarthickness = .01;
  55.         float rowvary = .25, pitting = 0.01;
  56.         float pockfrequency = 10, groovedepth = 0.01; )
  57. {
  58. #define BMWIDTH (brickwidth+mortarthickness)
  59. #define BMHEIGHT (brickheight+mortarthickness)
  60. #define MWF (mortarthickness*0.5/BMWIDTH)
  61. #define MHF (mortarthickness*0.5/BMHEIGHT)
  62.   point PP2;
  63.   float sbrick, tbrick, w, h;
  64.   float scoord, tcoord, ss, tt;
  65.   float fact, disp;
  66.  
  67.   scoord = s;  tcoord = t;
  68.  
  69.   /* Make the shapes of the bricks vary just a bit */
  70.   PP2 = point noise (s/BMWIDTH, t/BMHEIGHT);
  71.   scoord = s + jagged * xcomp (PP2);
  72.   tcoord = t + jagged * ycomp (PP2);
  73.  
  74.   ss = scoord / BMWIDTH;
  75.   tt = tcoord / BMHEIGHT;
  76.  
  77.   /* shift alternate rows */
  78.   if (mod (tt*0.5, 1) > 0.5)
  79.       ss += 0.5;
  80.  
  81.   tbrick = floor (tt);   /* which brick row? */
  82.   /* Shift the columns randomly by row */
  83.   ss += rowvary * (noise (tbrick+0.5) - 0.5);
  84.  
  85.   sbrick = floor (ss);   /* which brick column? */
  86.   ss -= sbrick;          /* Now ss and tt are coords within the brick */
  87.   tt -= tbrick;
  88.  
  89.   fact = 1;
  90.   disp = 0;
  91.   if (tt < MHF) {
  92.       /* We're in the top horizontal groove */
  93.       disp = groovedepth * (sqr((tt)/MHF) - 1);
  94.     }
  95.   if (tt > (1.0-MHF)) {
  96.       /* Bottom horizontal groove */
  97.       disp = groovedepth * (sqr((1-tt)/MHF) - 1);
  98.     }
  99.   if (ss < MWF) {
  100.       disp = 0.75 * groovedepth * (sqr(ss/MWF) - 1);
  101.     }
  102.   if (ss > (1.0-MWF)) {
  103.       disp = 0.75 * groovedepth * (sqr((1-ss)/MWF) - 1);
  104.     }
  105.  
  106.   fact = smoothstep (0, 1.3*MHF, tt) - smoothstep (1.0-1.3*MHF, 1, tt);
  107.   fact *= (smoothstep (0, 1.3*MWF, ss) - smoothstep (1.0-1.3*MWF, 1, ss));
  108.   fact = pitting * (0.75 * fact + 0.25);
  109.   disp -= fact * pow(noise ((ss+sbrick)*pockfrequency/BMHEIGHT,
  110.                     (tt+tbrick)*pockfrequency/BMWIDTH), 0.25);
  111.  
  112.   P += disp * normalize(N);
  113.   N = calculatenormal (P);
  114.  
  115. }
  116.