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_luna.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.5 KB  |  184 lines

  1. /*
  2.  * luna.sl -- surface shader for the moon
  3.  *
  4.  * DESCRIPTION:
  5.  *    Makes a surface that looks sort of like Earth's moon.  It doesn't really
  6.  *    have craters, so it isn't good for closeups.  But it's pretty good at about
  7.  *    the scale for human naked-eye viewing from earth.
  8.  *
  9.  * AUTHOR:
  10.  *    C language version by F. Kenton Musgrave
  11.  *    Translation to Shading Language by Larry Gritz.
  12.  *
  13.  * REFERENCES:
  14.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  15.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  16.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  17.  *
  18.  * HISTORY:
  19.  *    ??? - original C language version by Ken Musgrave
  20.  *    Apr 94 - translation to Shading Language by L. Gritz
  21.  *
  22.  * this file last updated 18 Apr 1994
  23.  */
  24. #include "k3d_functions.h"
  25. #include "k3d_noises.h"
  26. #include "k3d_constants.h"
  27.  
  28. surface k3d_luna(float Ka = .5, Kd = 1;
  29.          float lacunarity = 2;
  30.          float octaves = 8;
  31.          float H = .3;
  32.          color highland_color = .7;
  33.          float maria_basecolor = .7, maria_color = .1;
  34.          float arg22 = 1, arg23 = .3;
  35.          float highland_threshold = -0.2;
  36.          float highland_altitude = 0.001, maria_altitude = 0.0004;
  37.          float peak_rad = .0075, inner_rad = .01, rim_rad =
  38.          .02, outer_rad = .05; float peak_ht = 0.005, rim_ht = 0.003;
  39.          float numrays = 8;    /* arg10 */
  40.          float rayfade = 1;    /* arg11 */
  41.   )
  42. {
  43.   float radial_dist;
  44.   point PP, PQ;
  45.   float l, a, o, i, omega;
  46.   float chaos;
  47.   color Ct;
  48.   float temp1;
  49.   point vv;
  50.   float uu, ht, freq, scale;
  51.   float lighten;
  52.   point NN;
  53.   float pd;            /* pole distance */
  54.   float raydist;
  55.  
  56.   PQ = P;
  57.   PP = transform("shader", P);
  58.   NN = normalize(N);
  59.   radial_dist = sqrt(xcomp(PP) * xcomp(PP) + ycomp(PP) * ycomp(PP));
  60.   omega = pow(lacunarity, (-.5) - H);
  61.  
  62.   /* bumpy = fBm (PP, omega, lacunarity, octaves); */
  63.   l = 1;
  64.   o = 1;
  65.   a = 0;
  66.   for(i = 0; i < octaves; i += 1)
  67.     {
  68.       a += o * snoise(l * PP);
  69.       l *= lacunarity;
  70.       o *= omega;
  71.     }
  72.   chaos = a;
  73.  
  74.   Ct = Cs;
  75.  
  76.   /* Insure that the crater is in one of the maria */
  77.   temp1 = radial_dist * arg22;
  78.   if(temp1 < 1)
  79.     chaos -= arg23 * (1 - smoothstep(0, 1, temp1));
  80.  
  81.   if(chaos > highland_threshold)
  82.     {
  83.       PQ += chaos * highland_altitude * NN;
  84.       Ct += highland_color * chaos;
  85.     }
  86.   else
  87.     {
  88.       PQ += chaos * maria_altitude * NN;
  89.       Ct *= maria_basecolor + maria_color * chaos;
  90.     }
  91.  
  92.  
  93.     /***********************************************************************/
  94.   /* Add crater */
  95.   /* get normalized vector "v" */
  96.   pd = 1 - v;
  97.   vv = point(xcomp(PP) / radial_dist, 0, zcomp(PP) / radial_dist);
  98.   lighten = 0;
  99.   if(pd < peak_rad)
  100.     {                /* central peak */
  101.       uu = 1 - pd / peak_rad;
  102. /*      lighten = uu*uu; */
  103.       ht = peak_ht * smoothstep(0, 1, uu);
  104.     }
  105.   else if(pd < inner_rad)
  106.     {                /* crater floor */
  107.       ht = 0;
  108.     }
  109.   else if(pd < rim_rad)
  110.     {                /* inner rim */
  111.       uu = (pd - inner_rad) / (rim_rad - inner_rad);
  112.       lighten = .75 * uu;
  113.       ht = rim_ht * smoothstep(0, 1, uu);
  114.     }
  115.   else if(pd < outer_rad)
  116.     {                /* outer rim */
  117.       uu = 1 - (pd - rim_rad) / (outer_rad - rim_rad);
  118.       lighten = .75 * uu * uu;
  119.       ht = rim_ht * smoothstep(0, 1, uu * uu);
  120.     }
  121.   else
  122.     ht = 0;
  123.   PQ += ht * NN;
  124.   lighten *= 0.2;
  125.   Ct += color(lighten, lighten, lighten);
  126.  
  127.   /* Add some noise */
  128.   if(uu > 0)
  129.     {
  130.       if(pd < peak_rad)
  131.     {            /* if on central peak */
  132.       vv = 5 * PP + 3 * vv;
  133.       freq = 1;
  134.       scale = 1;
  135.       ht = 0;
  136.       for(i = 0; i < 4; i += 1)
  137.         {
  138.           ht += scale * snoise(freq * vv);
  139.           freq *= 2;
  140.           scale *= 0.833;
  141.         }
  142. /*      ht = wrinkled (vv, 2, .833, 4); */
  143.       PQ += 0.0025 * uu * ht * NN;
  144.     }
  145.       else
  146.     {
  147.       vv = 6 * PP + 3 * vv;
  148.       freq = 1;
  149.       scale = 1;
  150.       ht = 0;
  151.       for(i = 0; i < 4; i += 1)
  152.         {
  153.           ht += scale * snoise(freq * vv);
  154.           freq *= 2;
  155.           scale *= 0.833;
  156.         }
  157. /*      ht = wrinkled (vv, 2, .833, 4); */
  158.       if(radial_dist > rim_rad)
  159.         uu *= uu;
  160.       PQ += 0.0025 * (0.5 * uu + 0.5 * ht) * NN;
  161.     }
  162.     }
  163.  
  164.  
  165.   /* Make crater rays (PP, arg10, arg11, arg12, arg15, arg24, arg25, radial_dist);, yielding temp1 */
  166.   lighten = 0;
  167.   if(pd >= rim_rad && pd < 0.4)
  168.     {
  169.       lighten = smoothstep(.15, .5, snoise(62 * u));
  170.       raydist = 0.2 + 0.2 * snoise(20 * mod(u + 0.022, 1));
  171.       lighten *= (1 - smoothstep(raydist - .2, raydist, pd));
  172.     }
  173.   lighten = 0.2 * clamp(lighten, 0, 1);
  174.   Ct += color(lighten, lighten, lighten);
  175.  
  176.  
  177.   /* Recalc normal since we changed P a whole bunch. */
  178. /*  N = normalize (calculatenormal (PQ)); */
  179.  
  180.   /* Shade like matte */
  181.   Oi = 1;
  182.   Ci = Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  183. }
  184.