home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Stuff / 3D_Reality / Shaders / Shader_Source / spasm.sl < prev    next >
Encoding:
Text File  |  1992-07-15  |  3.5 KB  |  114 lines

  1. surface
  2. spasm(
  3.     float     frequency = 5,
  4.                 pbm       = 0.5,
  5.                 pg        = 0.5;      
  6.  
  7.       /* granite stuff */
  8.     float    g_Kd    = .8,
  9.         g_Ka    = .2;
  10.  
  11.        /* blue marble stuff */
  12.        float    bm_Ks    = .4,
  13.        bm_Kd    = .6, 
  14.        bm_Ka    = .1,
  15.        bm_roughness = .1,
  16.        bm_txtscale = 1;
  17.        color   bm_specularcolor = 1)
  18. {
  19.  
  20.   float    smod = mod(s*frequency,1),
  21.   tmod = mod(t*frequency,1);
  22.  
  23.   point NN;
  24.   float y, z, r;
  25.   float sum = 0;
  26.   float i, freq = 7.0;
  27.   color bm_Ci, g_Ci;
  28.  
  29.   point PP;            /* scaled point in shader space */
  30.   float csp;           /* color spline parameter */
  31.   point Nf;            /* forward-facing normal */
  32.   point V;             /* for specular() */
  33.   float pixelsize, twice, scale, weight, turbulence;
  34.  
  35.   /* do blue_marble */
  36.   {
  37.     /* Obtain a forward-facing normal for lighting calculations. */
  38.     Nf = faceforward( normalize(N), I);
  39.     V = normalize(-I);
  40.     
  41.     /*
  42.      * Compute "turbulence" a la [PERLIN85]. Turbulence is a sum of 
  43.      * "noise" components with a "fractal" 1/f power spectrum. It gives the
  44.      * visual impression of turbulent fluid flow (for example, as in the 
  45.      * formation of blue_marble from molten color splines!). Use the 
  46.      * surface element area in texture space to control the number of 
  47.      * noise components so that the frequency content is appropriate 
  48.      * to the scale. This prevents aliasing of the texture.
  49.      */
  50.     PP = transform("shader", P) * bm_txtscale;
  51.     pixelsize = sqrt(area(PP));
  52.     twice = 2 * pixelsize;
  53.     turbulence = 0;
  54.     for (scale = 1; scale > twice; scale /= 2) 
  55.       turbulence += scale * noise(PP/scale);
  56.     
  57.     /* Gradual fade out of highest-frequency component near limit */
  58.     if (scale > pixelsize) {
  59.       weight = (scale / pixelsize) - 1;
  60.       weight = clamp(weight, 0, 1);
  61.       turbulence += weight * scale * noise(PP/scale);
  62.     }
  63.     
  64.     /*
  65.      * Magnify the upper part of the turbulence range 0.75:1
  66.      * to fill the range 0:1 and use it as the parameter of
  67.      * a color spline through various shades of blue.
  68.      */
  69.     csp = clamp(4 * turbulence - 3, 0, 1);
  70.     bm_Ci = color spline(csp,
  71.              color (0.25, 0.25, 0.35),      /* pale blue        */
  72.              color (0.25, 0.25, 0.35),  /* pale blue        */
  73.              color (0.20, 0.20, 0.30),  /* medium blue      */
  74.              color (0.20, 0.20, 0.30),  /* medium blue      */
  75.              color (0.20, 0.20, 0.30),  /* medium blue      */
  76.              color (0.25, 0.25, 0.35),  /* pale blue        */
  77.              color (0.25, 0.25, 0.35),  /* pale blue        */
  78.              color (0.15, 0.15, 0.26),  /* medium dark blue */
  79.              color (0.15, 0.15, 0.26),  /* medium dark blue */
  80.              color (0.10, 0.10, 0.20),  /* dark blue        */
  81.              color (0.10, 0.10, 0.20),  /* dark blue        */
  82.              color (0.25, 0.25, 0.35),  /* pale blue        */
  83.              color (0.10, 0.10, 0.20)   /* dark blue        */
  84.              );
  85.     
  86.     /* Multiply this color by the diffusely reflected light. */
  87.     bm_Ci *= bm_Ka*ambient() + bm_Kd*diffuse(Nf);
  88.     
  89.     /* Adjust for opacity. */
  90.     Oi = Os;
  91.     bm_Ci = bm_Ci * Oi;
  92.     
  93.     /* Add in specular highlights. */
  94.     bm_Ci += bm_specularcolor * bm_Ks * specular(Nf,V,bm_roughness);
  95.   }
  96.   
  97.   /* do wood */
  98.   {
  99.     for (i = 0; i < 6; i = i + 1) {
  100.       sum = sum + abs(.5 - noise( 4 * freq * I))/freq ;
  101.       freq *= 2;
  102.     }
  103.     g_Ci = Cs * sum * (g_Ka + g_Kd *diffuse(faceforward( normalize(N),I )) ) ;
  104.   }
  105.  
  106.   /* combine the two */
  107.   if(((smod < pg) && (tmod<pbm)) || ((smod > pg) && (tmod>pbm)))
  108.     Ci=g_Ci;
  109.   else
  110.     Ci=bm_Ci;
  111. }
  112.  
  113.   
  114.