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_stones.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  5.5 KB  |  180 lines

  1. /* SHADER INFO 
  2.  *
  3.  * RudyCstones.sl
  4.  * 
  5.  * Copyright (C) 2002, Rudy Cortes   rcortes@hntb.com
  6.  * created  06/12/2002
  7.  * 
  8.  * This software is placed in the public domain and is provided as is 
  9.  * without express or implied warranty.
  10.  *
  11.  * Shader that creates a surface covered with stones of different sizes,
  12.  * trying to replicate the shader usr for the ground in "A BUGS LIFE".
  13.  * Uses st to create the rocks and "shader" space to create the grunge.
  14.  *
  15.  * Feel free to use this shader to create skin for any character, anywhere and
  16.  * everywhere, Just list me on the credits under "Shading Team" if you use the
  17.  *  hader as is, or under "shader info" if you do any minor modifications to
  18.  * this code.
  19.  ********************************************
  20.  *
  21.  * ka, Kd, Ks, roughness = the usual
  22.  * Km = amount of displacement
  23.  * displace = should the surface be bumped(0) or displaced (1)?
  24.  * minfreq & maxfreq = limits to the rock loop excecution
  25.  * grungefreq, grunge_Pow, grunginess = freqeuncy, power and depth of grunge
  26.  * stonecolor, groundcolor = this is obvious, isn't it?
  27.  * varyhue, varysat,varylum  = how much will the color change?
  28.  *
  29.  ********************************************
  30.  * ENJOY!
  31.  * modified 10/17/02 Changed algorithms arround to make it render a little faster.
  32.  *
  33.  * NOTE .- This shader is VERY SLOW when you enable bumping, even SLOWER with
  34.  * displacements.
  35.  *
  36.  */
  37.  
  38. #include "k3d_noises.h"
  39. #include "k3d_rmannotes.h"
  40.  
  41.  
  42. /* varyEach takes a computed color, then tweaks each indexed item
  43.  * separately to add some variation.  Hue, saturation, and lightness
  44.  * are all independently controlled.  Hue adds, but saturation and
  45.  * lightness multiply.
  46.  * Original by Larry Gritz. Modified to "hsv" by Rudy Cortes
  47.  */
  48. color varyEach (color Cin; float index, varyhue, varysat, varyval;)
  49. {
  50.     /* Convert to "hsv" space, it's more convenient */
  51.     color Chsv = ctransform ("hsv", Cin);
  52.     float h = comp(Chsv,0), s = comp(Chsv,1), v = comp(Chsv,2);
  53.     /* Modify Chsv by adding Cvary scaled by our separate h,s,v controls */
  54.     h += varyhue * (cellnoise(index+3)-0.5);
  55.     s *= 1 - varysat * (cellnoise(index-14)-0.5);
  56.     v *= 1 - varyval * (cellnoise(index+37)-0.5);
  57.     Chsv = color (mod(h,1), clamp(s,0,1), clamp(v,0,1));
  58.     /* Clamp hsl and transform back to rgb space */
  59.     return ctransform ("hsv", "rgb", clamp(Chsv,color 0, color 1));
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. surface k3d_stones (
  68.     float Ka = .6, Kd = .85,
  69.           Ks = 0, roughness = 1,
  70.           Km = 0.01,
  71.           displace = 0;
  72.     float minfreq = 1,
  73.           maxfreq = 10,
  74.           tilefreq = 6,
  75.           grungefreq = 30, grunge_Pow = 3, grunginess = - 0.8;
  76.    color  stonecolor = color(.7,.6,.4);
  77.    color  groundcolor = color (.45,0.35,0.2);
  78.    float varyhue = .03, varysat = .2, varylum = .25;)
  79.  
  80. {
  81.  color surface_color, layer_color;
  82.  float layer_opac;
  83.  float ss, tt, stile,ttile;
  84.  float freq,mag;
  85.  float r,theta,angle;
  86.  float d,d0,d1;
  87.  float cx,cy;
  88.  float noifreq = 10, noiscale = 0.3;
  89.  float bub;
  90.  vector V;
  91.  normal Nf;
  92.  float grunge;
  93.  
  94.  surface_color = groundcolor;
  95.  float surface_mag = 0;
  96.  
  97.  /*loop for creating color layers of rocks*/
  98.  for (freq = maxfreq ; freq>minfreq;freq -=0.5)
  99.   {
  100.    angle = PI * snoise(freq * 16.31456);  /*randomize angle index*/
  101.   
  102.    rotate2d(s,t,angle,0.5,0.5,cx,cy);    /*randomize rotations*/
  103.  
  104.    /*repeat tiles and find out in which tile we are at?*/
  105.    ss = repeat(cx,freq * tilefreq);
  106.    tt = repeat(cy,freq * tilefreq);
  107.    stile = whichtile(cx,freq * tilefreq);
  108.    ttile = whichtile(cy,freq * tilefreq);
  109.  
  110.    /*tile index to be use in vary each*/
  111.    float stoneindex = stile + 13 * ttile;
  112.  
  113.    /*create buble shapes*/
  114.    bub = 0.5 + snoisexy(10 * freq,10 * freq);
  115.    ss += noiscale * snoise(snoisexy(s * noifreq, t * noifreq) + 912);
  116.    tt += noiscale * snoise(snoisexy(s * noifreq, t * noifreq) + 333);
  117.    /*cx = 0.5 + 0.1 * snoisexy( 12.312,  21.773);
  118.    cy = 0.5 + 0.1 * snoisexy( 28.398, 62.112); */
  119.    cx = 0.5 + 0.1 * snoisexy( freq * 8.456, freq * 18.773);
  120.    cy = 0.5 + 0.1 * snoisexy( freq * 28.398, freq * 42.112);
  121.    point p1 =(cx,cy,0);
  122.    point p2 = (ss,tt,0);
  123.    d = distance(p1, p2);
  124.    /*mag= ((0.5 * .8 - abs(bub - 0.5)) / .8) * 60 *(.09 - d * d) *
  125.               ((maxfreq - freq)/maxfreq); */
  126.    mag= (0.5 - abs(bub - 0.5)) * 90 *(.09 - d * d)*((maxfreq - freq)/maxfreq);
  127.  
  128.    layer_opac = clamp( mag,0,1);
  129.    /*create a diferent color for each rock*/
  130.    layer_color = varyEach(stonecolor, stoneindex,varyhue,varysat,varylum);
  131.    surface_color = mix(surface_color,layer_color,layer_opac);
  132.  
  133.  
  134.    /*calculate displacement if Km > .01*/
  135.      if(Km != 0)
  136.    {
  137.     surface_mag = max(surface_mag,mag);
  138.     }
  139. }
  140.  
  141.    /*apply if grunginess != 0 */
  142.    if(grunginess != 0)
  143.       {
  144.       /* compute turbulence  for grunge*/
  145.        point PP = transform("shader", P) * grungefreq;
  146.        float width = filterwidth_point(PP);
  147.        float cutoff = clamp(0.5 / width, 0, maxfreq);
  148.  
  149.         float turb = 0, f;
  150.         for (f = 1; f < 0.5 * cutoff; f *= 2)
  151.         turb += abs(snoise(PP * f)) / f;
  152.         float fade = clamp(2 * (cutoff - f) / cutoff, 0, 1);
  153.         turb += fade * abs(snoise(PP * f)) / f;
  154.  
  155.         grunge = pow(turb, grunge_Pow);
  156.         surface_mag += grunge * grunginess;
  157.         
  158.         }
  159.  
  160.   if (displace == 1)
  161.   {
  162.   P += Km * surface_mag * normalize(N);
  163.   N = normalize(calculatenormal(P)); 
  164.   }
  165.   else {
  166.   N = normalize(calculatenormal(P + Km * surface_mag * normalize(N)));
  167.   }
  168.  /*compute normals and vectors for shading*/
  169.   Nf = faceforward(normalize(N),I);
  170.   V = - normalize(I);
  171.  
  172. /*color output*/
  173.  
  174. Oi = Os;
  175. Ci = surface_color * Oi * (Ka * ambient() + Kd * diffuse(Nf))+
  176.       Ks * specular(Nf,V,roughness);
  177.  
  178. }
  179.  
  180.