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_eyeball.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  5.2 KB  |  138 lines

  1. /*
  2.  * eyeball.sl -- RenderMan compatible shader for an eyeball.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a plastic-like surface which looks like an eyeball.  It's meant
  6.  *   for use on a sphere.  The center of the pupil is at the "north pole",
  7.  *   i.e. where the t parameter is 1.  The colors of the pupil, iris, white
  8.  *   part (eyeball), and blood vessels can be set individually.  Fractal
  9.  *   functions are used for the veining and the iris mottling.
  10.  * 
  11.  * PARAMETERS:
  12.  *   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic shader
  13.  *   iriscolor - color of the iris
  14.  *   eyeballcolor - color of the white part of the eyeball
  15.  *   bloodcolor - color of the blood vessels
  16.  *   pupilcolor - color of the pupil (opening)
  17.  *   pupilsize - size of pupil (in "t" space)
  18.  *   irissize - size of iris (in "t" space), must be larger than pupilsize
  19.  *   bloodshot - controls how bloodshot the eye is (0=no blood, 1=very ugly)
  20.  *   veinfreq, veinlevel - control the formation of the blood vessels
  21.  *   index - set between 0 and 1, lets you use this shader to generate
  22.  *           non-identical eyeballs.
  23.  *
  24.  * ANTIALIASING: basic antialiasing of the boundaries between tissue types
  25.  *               is performed.
  26.  *
  27.  * AUTHOR: written by Larry Gritz
  28.  *
  29.  * HISTORY:
  30.  *      Nov 1991 - crude written of "eye" by lg for Herman's eyes for
  31.  *                 "Graphic Violence".  Original version hard coded in C.
  32.  *      Dec 1993 - "eye" modified by lg to clean up a bit.
  33.  *      10 Jan 1994 - recoded by lg in correct shading language.
  34.  *      28 Jun 94 (lg) - revamped to add veins and iris mottling, renamed
  35.  *                       "eyeball"
  36.  *       7 Jan 95 (wave) - changed name to LGEyeBall for namespace reasons...
  37.  *       8 Jan 95 (wave) - changed Ciris line to fix bug Larry figured out and changed defaults
  38.  *       27 Feb 95 (wave) - changed PO line to fix bug Larry figured out to actually *use* index
  39.  *
  40.  * last modified  8 Jan 95 by Michael B. Johnson (wave)
  41.  */
  42.  
  43. #include "k3d_noises.h" 
  44. #include "k3d_rmannotes.h"
  45.  
  46. surface
  47. k3d_eyeball (float Ka = .75, Kd = 0.75, Ks = 0.4, roughness = 0.1;
  48.      color specularcolor = 1;
  49.      color iriscolor = color (.135289, .084323, .372417);
  50.      color irisoutercolor = color (.403882, .343944, .68276);
  51.      color irisinnercolor = color (.065142, .040605, .179311);
  52.      color eyeballcolor = color(1,1,1);
  53.      color bloodcolor = color(.8,.05,.05);
  54.      color pupilcolor = 0;
  55.      float pupilsize = 0.05, irissize = 0.12;
  56.      float bloodshot = 1.0;
  57.      float veinfreq = 8, veinlevel = 4;
  58.      float index = 0;
  59.         )
  60. {
  61.   color Ct;
  62.   point Nf;
  63.   point PP, PO;
  64.   float i, turb, newturb, freq, f2;
  65.   float displayed, newdisp;
  66.   color Cball, Ciris;
  67.   float irisstat, pupilstat;
  68.   float bloody, tt;
  69.   float ks, rough;
  70.   float twidth, cutoff;
  71.  
  72.   /* Calculate an appropriate filter width for antialiasing */
  73.   twidth = max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH);
  74.   PO = transform ("object", P) + index;
  75.  
  76.   /* Figure out where we are in the eyeball.  Use the following variables:
  77.    * irisstat: 0 inside the iris/white boundary, 1 outside
  78.    * pupilstat: 0 inside the pupil/iris boundary, 1 outside
  79.    * bloody: how potentially bloody it is (fade as we get away from iris)
  80.    */
  81.   tt = 1-t;
  82.   irisstat = smoothstep (irissize, irissize+twidth, tt);
  83.   pupilstat = smoothstep (pupilsize, pupilsize+twidth, tt);
  84.   bloody = bloodshot * (smoothstep (-irissize, 2.5*irissize, tt));
  85.  
  86.   /* If we're somewhere in the white part and it's potentially bloody,
  87.    * then calculate the veining pattern.  Otherwise, just use the color
  88.    * of the whites.  The veining pattern is essentially summed zero sets
  89.    * of turbulence functions.  Some stretching is done to get it to look
  90.    * just right.
  91.    */
  92.   if (irisstat * bloody > 0.001) {
  93.       turb = bloody;  freq = veinfreq;
  94.       displayed = 0;
  95.       for (i = 1;  (i <= veinlevel) && (turb > 0.1);  i += 1) {
  96.       newturb = 1 - abs (snoise(PO*freq + point(0,0,20*freq)));
  97.       newdisp = pow (smoothstep (.85, 1, newturb), 10);
  98.       displayed += (1-displayed) * newdisp * smoothstep (.1, .85, turb * turb);
  99.       turb *= newturb;
  100.       freq *= 2;
  101.         }
  102.       Cball = mix (eyeballcolor, bloodcolor, smoothstep(0,.75,displayed));
  103.     }
  104.   else Cball = eyeballcolor;
  105.  
  106.   Ciris = mix (iriscolor, irisoutercolor, smoothstep (irissize*.8, irissize, tt));
  107.   /* If we're somewhere in the iris, calculate the iris pattern, which is
  108.    * just a stretched turbulence function.
  109.    */
  110.   if (irisstat < 0.9999 && pupilstat > 0.0001) {
  111.       turb = 0;  freq = 1;  f2 = 30;
  112.       for (i = 1;  i <= 4;  i += 1) {
  113.       turb += snoise (PO*f2 + point(0,0,20*f2)) / freq;
  114.       freq *= 2;  f2 *= 2;
  115.         }
  116.       Ciris *= (1-clamp(turb/2,0,1));
  117.     }
  118.  
  119.   /* OK, now calculate a surface texture color (Ct) based on where we are
  120.    * and what patterns we calculated.
  121.    */
  122.   Ct = mix (Ciris, Cball, irisstat);
  123.   Ct = mix (pupilcolor, Ct, pupilstat);
  124.  
  125.   /* Make the eye a little glossier on the iris and pupil */
  126.   ks = Ks * (1+2*(1-irisstat));
  127.   rough = roughness * (1-.75*(1-irisstat));
  128.  
  129.   /* Now shade like plastic, but using our calculated surface color and
  130.    * our modified values for roughness and Ks.
  131.    */
  132.   Oi = Os;
  133.   Nf = faceforward (normalize(N),I);
  134.   Ci = Os * ( Ct * (Ka*ambient() + Kd*diffuse(Nf)) +
  135.           specularcolor * ks*specular(Nf,-normalize(I),rough));
  136. }
  137.  
  138.