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_lunette.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  6.9 KB  |  175 lines

  1. /* renamed JMlinette.sl for RMR.  -- tal */
  2.  
  3. /******************************************************************************
  4.  * lunette.sl -- A pretty surface shader ideal for "ground planes".
  5.  *
  6.  * Description:
  7.  *   A grid pattern overlaid upon an fBm noise.  This shader is good for
  8.  *   surfaces placed beneath your scene which act as the ground.  The default
  9.  *   colors are a bland pastel blue and yellow, which should complement many
  10.  *   hero objects that you might wish to show off.  The shader is a copy of a
  11.  *   pattern I saw in the background of another image entitled "Lunettes",
  12.  *   which I believe has something to do with spectacles in French.  Hence the
  13.  *   name, anyway.  The pattern is calculated in 2D u,v coordinates.
  14.  *
  15.  * Parameters:
  16.  *   Ka, Kd, Ks, roughness, specularcolor - work like the plastic shader
  17.  *   urepeats, vrepeats - number of repeats of the grid pattern in u and v
  18.  *   ulinewidth, vlinewidth - proportional width of the grid lines
  19.  *   colorA, colorB - colors used in the background fBm noise
  20.  *   baselinecolor - base color of the grid lines
  21.  *   colorBenhance - amount to enhance colorB's presence over colorA
  22.  *   gridDensity - effective opacity of the grid lines
  23.  *   noiseScale - scale factor for the fBm noise relative to the grid size
  24.  *   noiseRandom - randomization factor for the fBm noise
  25.  *   maxOctaves, lunacrity, gain - work as in the fBm function
  26.  *
  27.  * Antialiasing:
  28.  *   The shader should antialias itself quite well, since it uses antialiased
  29.  *   noise functions and an box-filter antialiased grid function.
  30.  *
  31.  * Author: written by Jonathan Merritt (jmerritt@warpax.com), 5th October 2002
  32.  *
  33.  * License: This shader is distributed under the GNU General Public License.
  34.  *
  35.  *****************************************************************************/
  36.  
  37. #include "k3d_patterns.h"
  38. #include "k3d_noises.h"
  39.  
  40.  
  41. /**
  42.  * Base color for the "lunette" pattern.  The pattern is calculated in a two-
  43.  * dimensional fashion using the coordinates ss and tt as texture / pattern
  44.  * coordinates.
  45.  *
  46.  * Parameters:
  47.  *   ss, tt - 2D pattern coordinates.
  48.  *   ssrepeats, ttrepeats - Number of repeats of the grid pattern in both ss
  49.  *                           and tt directions.  Also scales the noise in both
  50.  *                           directions appropriately.
  51.  *   sslinewidth, ttlinewidth - Width of the grid lines in ss and tt
  52.  *                              respectively; expressed as a fraction of the
  53.  *                              cell over which the grid repeat occurs.
  54.  *   colorA, colorB - Two colors for the background pattern.
  55.  *   noiseScale - Scale of the background fBm noise (relative to the size of
  56.  *                a grid cell).  Increasing this value makes a finer, smaller
  57.  *                noise; decreasing it makes a broader noise.
  58.  *   noiseRandom - Randomization value for the noise.
  59.  *   maxOctaves - Maximum number of octaves for the fBm noise.
  60.  *   lunacrity - Lunacrity of the fBm noise.
  61.  *   gain - Gain of the fBm noise.
  62.  *   colorBenhance - After the noise has been calculated, there exists the
  63.  *                   possibility to increase the amount of colorB present in
  64.  *                   the output.  A value of colorBenhance = 1 means that
  65.  *                   colorA and colorB are present in equal amounts.
  66.  *                   Increasing this value puts more colorB in the output.
  67.  *   gridDensity - Effective opacity of the grid pattern over the underlying
  68.  *                   noise.
  69.  *   baselinecolor - Base color of the grid lines.
  70.  */
  71. color lunetteColor (
  72.     float ss;        /* ss coordinate for the lunette pattern */
  73.     float tt;        /* tt coordinate for the lunette pattern */
  74.     float ssrepeats;    /* number of repeats in the ss direction */
  75.     float ttrepeats;    /* number of repeats in the tt direction */
  76.     float sslinewidth;    /* proportional width of lines in ss */
  77.     float ttlinewidth;    /* proportional width of lines in tt */
  78.     color colorA;        /* first color in the pattern */
  79.     color colorB;        /* second color in the pattern */
  80.     float noiseScale;    /* scale of the noise relative to the grid */
  81.     float noiseRandom;    /* randomization for the noise */
  82.     uniform float maxOctaves;    /* maximum number of octives for the noise */
  83.     uniform float lunacrity;    /* lunacrity of the noise */
  84.     uniform float gain;        /* gain for the noise */
  85.     float colorBenhance;    /* amount to enhance color B */
  86.     float gridDensity;    /* density of the grid pattern */
  87.     color baselinecolor;    /* base color for the lines */
  88. ) {
  89.  
  90.     /*
  91.      * Calculate pulsegrid, which is a variable indicating the value of
  92.      * the grid.  pulsegrid = 0 means that there is no grid at the point
  93.      * whereas pulsegrid = 1 means that there IS a grid at the point.
  94.      * pulsegrid varies smoothly between the two values to cope with
  95.      * antialising at the edges of the grid.
  96.      */
  97.     float pulsess = filteredpulsetrain(
  98.         1/ssrepeats, sslinewidth, ss, filterwidth(ss)
  99.     );
  100.     float pulsett = filteredpulsetrain(
  101.         1/ttrepeats, ttlinewidth, tt, filterwidth(tt)
  102.     );
  103.     float pulsegrid = 1 - min(pulsess, pulsett); 
  104.  
  105.     /*
  106.      * Find the base color for the pattern.  The base color is a mix
  107.      * between colorA and colorB, created using noise and other mixing
  108.      * parameters.
  109.      */
  110.     point noisePt = noiseScale * 
  111.         point(ss*ssrepeats, tt*ttrepeats, noiseRandom);
  112.     float noisefilterwidth = filterwidthp(noisePt);
  113.     float noiseamt = (fBm(
  114.         noisePt, noisefilterwidth, maxOctaves, lunacrity, gain
  115.     )+1) / 2;
  116.     noiseamt = pow(noiseamt, colorBenhance);
  117.     color basecolor = mix(colorB, colorA, noiseamt);
  118.  
  119.     /*
  120.      * find the base line color for the grid pattern
  121.      */
  122.     color linecolor = mix(basecolor, baselinecolor, gridDensity);
  123.  
  124.     /*
  125.      * return the mix between the base grid and the colorful noise
  126.      */
  127.     return mix(basecolor, linecolor, pulsegrid);
  128.  
  129. }
  130.  
  131. surface k3d_lunette (
  132.     /* Plastic illumination model parameters. */
  133.     float Ka = 1;
  134.     float Kd = .5;
  135.     float Ks = .5;
  136.     float roughness = .1;
  137.     color specularcolor = 1;
  138.     /* Grid pattern parameters. */    
  139.     float urepeats = 30;        /* Number of repeats in u */
  140.     float ulinewidth = 0.125;    /* Relative width of grid lines in u */
  141.     float vrepeats = 30;        /* Number of repeats in v */
  142.     float vlinewidth = 0.12;    /* Relative width of grid lines in v */
  143.     /* Colors used. */
  144.     color colorA = color(.36, .58, .67);
  145.     color colorB = color(.88, .88, .71);
  146.     color baselinecolor = color(.51, .48, .52);
  147.     float colorBenhance = 1.6;    /* Enhancement of colorB in output */
  148.     float gridDensity = .5;        /* Effective opacity of the grid */
  149.     /* fBm noise parameters */
  150.     float noiseScale = .3;
  151.     float noiseRandom = 0;
  152.     float maxOctaves = 5;
  153.     float lunacrity = 1.3;
  154.     float gain = .7;
  155. ) {
  156.     /*
  157.      * fetch the lunette color
  158.      */
  159.     color cc = lunetteColor(
  160.         u, v, urepeats, vrepeats, ulinewidth, vlinewidth,
  161.         colorA, colorB, noiseScale, noiseRandom, maxOctaves,
  162.         lunacrity, gain, colorBenhance, gridDensity, baselinecolor
  163.     );
  164.  
  165.     /*
  166.      * apply a plastic illumination model
  167.      */
  168.     normal Nf = faceforward (normalize(N),I);
  169.     Oi = Os;
  170.  
  171.     Ci = Os * ( cc * (Ka*ambient() + Kd*diffuse(Nf)) +
  172.         specularcolor * Ks*specular(Nf,-normalize(I),roughness));
  173.  
  174. }
  175.