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_brick.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.3 KB  |  136 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, gritzl@acm.org
  24.  */
  25.  
  26. #include "k3d_noises.h"
  27. #include "k3d_patterns.h"
  28. #include "k3d_functions.h"
  29.  
  30. surface k3d_brick(float Ka = 1, Kd = 1;
  31.           color brickcolor = color "rgb"(.6, .1, .1);
  32.           color mortarcolor = color "rgb"(.6, .6, .6);
  33.           float raggedamp = 0.04, raggedfreq = 12;
  34.           float jagged = 0.006, brickvary = 0.3;
  35.           float brickwidth = .28, brickheight = .07;
  36.           float mortarthickness = .014;
  37.           float rowvary = .5;
  38.           float pitting = 0.01;
  39.           float pockfrequency = 10, groovedepth = 0.01;)
  40. {
  41.   color bcolor, Ct;
  42.   normal Nf;
  43.   float sbrick, tbrick, w, h;
  44.   float ss, tt;
  45.   float swidth, twidth;
  46.   uniform float BMWIDTH = (brickwidth + mortarthickness);
  47.   uniform float BMHEIGHT = (brickheight + mortarthickness);
  48.   uniform float MWF = (mortarthickness * 0.5 / BMWIDTH);
  49.   uniform float MHF = (mortarthickness * 0.5 / BMHEIGHT);
  50.   float whichbrick;
  51.   float fact, disp;
  52.  
  53.   /* Determine how wide in s-t space one pixel projects to, relative
  54.    * the the width and height of a brick.  Overestimate the filter
  55.    * size by a bit -- it makes the transitions between brick and mortar
  56.    * a bit smoother.
  57.    */
  58.   swidth = 1.5 * max(filterwidth(s), MINFILTWIDTH) / BMWIDTH;
  59.   twidth = 1.5 * max(filterwidth(t), MINFILTWIDTH) / BMHEIGHT;
  60.  
  61.   basicbrick(s, t, BMWIDTH, BMHEIGHT, 0.5, 0.2, 1, jagged, sbrick, tbrick, ss,
  62.          tt);
  63.  
  64.   /* Make the edges ragged, but different for each brick */
  65.   whichbrick = 103 * sbrick + tbrick;
  66.   ss +=
  67.     raggedamp * snoisexy((s + tbrick * 5.15) * raggedfreq,
  68.              (t + sbrick * 23.8) * raggedfreq);
  69.   tt +=
  70.     raggedamp * snoisexy((s + tbrick * 11.4) * raggedfreq,
  71.              (t + sbrick * 7.2) * raggedfreq);
  72.   ss +=
  73.     raggedamp / 2 * snoisexy((s + tbrick * 5.15) * raggedfreq * 2,
  74.                  (t + sbrick * 23.8) * raggedfreq * 2);
  75.   tt +=
  76.     raggedamp / 2 * snoisexy((s + tbrick * 11.4) * raggedfreq * 2,
  77.                  (t + sbrick * 7.2) * raggedfreq * 2);
  78.  
  79.   /* Choose a color for the surface */
  80.   if(swidth >= 1)
  81.     w = 1 - 2 * MWF;
  82.   else
  83.     w =
  84.       clamp(filteredpulse(MWF, 1 - MWF, ss, swidth), max(1 - MWF / swidth, 0),
  85.         1);
  86.   if(twidth >= 1)
  87.     h = 1 - 2 * MHF;
  88.   else
  89.     h =
  90.       clamp(filteredpulse(MHF, 1 - MHF, tt, twidth), max(1 - MHF / twidth, 0),
  91.         1);
  92.  
  93.   fact = 1;
  94.   disp = 0;
  95.   if(tt < MHF)
  96.     {
  97.       /* We're in the top horizontal groove */
  98.       disp = groovedepth * (sqr((tt) / MHF) - 1);
  99.     }
  100.   else if(tt > (1.0 - MHF))
  101.     {
  102.       /* Bottom horizontal groove */
  103.       disp = groovedepth * (sqr((1 - tt) / MHF) - 1);
  104.     }
  105.   if(ss < MWF)
  106.     {
  107.       disp = min(disp, 0.85 * groovedepth * (sqr(ss / MWF) - 1));
  108.     }
  109.   else if(ss > (1.0 - MWF))
  110.     {
  111.       disp = min(disp, 0.85 * groovedepth * (sqr((1 - ss) / MWF) - 1));
  112.     }
  113.  
  114.   fact = smoothstep(0, 1.3 * MHF, tt) - smoothstep(1.0 - 1.3 * MHF, 1, tt);
  115.   fact *= (smoothstep(0, 1.3 * MWF, ss) - smoothstep(1.0 - 1.3 * MWF, 1, ss));
  116.   fact = pitting * (0.5 * fact + 0.5);
  117.   disp -=
  118.     fact *
  119.     pow(noise
  120.     ((ss + sbrick) * pockfrequency / BMHEIGHT,
  121.      (tt + tbrick) * pockfrequency / BMWIDTH), 0.25);
  122.  
  123.   P += disp * normalize(N);
  124.   N = calculatenormal(P);
  125.   Nf = faceforward(normalize(N), I);
  126.  
  127.  
  128.   /* Choose a brick color that varies from brick to brick */
  129.   bcolor = brickcolor * (1 + (brickvary * snoise(whichbrick + 0.5)));
  130.  
  131.   Ct = mix(mortarcolor, bcolor, w * h);
  132.  
  133.   Oi = Os;
  134.   Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(Nf));
  135. }
  136.