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_crayon.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.1 KB  |  92 lines

  1. /* Renamed shader to PQCrayon for RMR -- talrmr@SpamSucks_pacbell.net */
  2.  
  3. /* crayon.sl - a surface shader making crayon like marks
  4.  
  5.     DESCRIPTION
  6.  
  7.     This shader makes the surface look as if it had been shaded with a pastel crayon.
  8.     It makes an attempt at antaliasing.
  9.     
  10.     PARAMETERS
  11.     
  12.     Ka, Kd, Ks, roughness, specularcolor - work as in the plastic shader
  13.     txtscale  - an overall scaling factor
  14.     width - the width of the crayon strokes - this is scaled by txtscale
  15.     micro - the size of the dots that make up a crayon stroke, relative to the size of
  16.         the stroke. By default they are about 15 times smaller
  17.     stretch - the length of the stroke relative to its width;
  18.     density0 - controls the amount of topcolor seen - measured as a proportion - 
  19.         this should vary between 0 (no topcolor) to 1, .3 would give 30% topcolor;
  20.     density1 - if different density0 this is the density when t = 1, with a smooth
  21.         interpolation of values for density0 when t = 0, thus allowing a 
  22.         graduation of shading from top to bottom of the object
  23.     color topcolor, basecolor - the color of the crayon strokes and the color of the ground
  24.     
  25.     AUTHOR
  26.     Peter Quint - Revised Monday, January 10, 2000 
  27.  
  28.  */
  29.  
  30.  
  31. #include "k3d_filterwidth.h"
  32. #include "k3d_noises.h"
  33.  
  34. float
  35. aanoise(float sp, tp, width)
  36. {
  37.     /* an antaliased noise function, which returns noise of a wavelenth always greater than
  38.        twice the micropolygon width */
  39.     float f, mag, ns;
  40.     /* calculate smallest integer f for which width / f < .5 */
  41.     f = ceil(width /.5);
  42.     mag = max(pow(0.85, f - 1),.1);
  43.     /*(printf("f = %f, mag = %f\n",f,mag)*/;
  44.     ns  = mag * snoise(sp / f, tp / f) * (1 - smoothstep(0, .5, width / f))
  45.         + snoise(sp / (f * 1.33), tp / (f * 1.33)) * mag * .25 * smoothstep(0, .5, width / f);
  46.     return ns;
  47. }
  48.  
  49.  
  50. surface
  51. k3d_crayon (    float Ka = 1;
  52.             float Kd = .5;
  53.              float Ks = .5;
  54.              float roughness = .1;
  55.              color specularcolor = 1;
  56.             float txtscale = 1;
  57.             float width = .05;
  58.             float micro = 15.32;
  59.             float stretch = 10;
  60.             float density0 = .5;
  61.             float density1 = .5;
  62.             color topcolor = 1;
  63.             color basecolor = 0;
  64.         )
  65. {
  66.   color Csurf;
  67.     float density = density0 + t * (density1 - density0);
  68.     /* work out the density for the current t */
  69.     float trs = spline(1 - density, 0 , -0.195997, -0.128361, -0.0738346,    -0.0316483,
  70.             0.00807387,    0.0445915, 0.084543, 0.150693, 0.2198, 0.527474);
  71.     /*  use a spline to read across to the appropriate noise value - this equalisation
  72.         process is described by Steven Worley in Ch 3 of "Texturing and Modelling a
  73.         procedural approach */ 
  74.     normal Nf = faceforward (normalize(N),I);
  75.     float m;
  76.     float fw = max(filterwidth(s), filterwidth(t)); /* the size of the micropolygon */
  77.     float smks = aanoise(txtscale * s * micro / width, txtscale * t * micro / width,
  78.              txtscale * fw * micro / width);
  79.     float lmks = (aanoise(txtscale * s / width, txtscale * t / (width * stretch), 
  80.         txtscale * fw / width) + 1) / 2;
  81.     smks = (smks + 1) / 2;
  82.     lmks = lmks - smks;
  83.     m = smoothstep(trs - .1, trs + .3, lmks);    
  84.     m = clamp(m, 0, 1);
  85.     Csurf = mix(basecolor, topcolor, m);
  86.     Oi = Os;
  87.     Ci = Os * ( Csurf * (Ka*ambient() + Kd*diffuse(Nf)) +
  88.         specularcolor * Ks*specular(Nf,-normalize(I),roughness));
  89. }
  90.  
  91.  
  92.