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_fur2.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.6 KB  |  161 lines

  1. /* Renamed to SIG2k_srf_fur to be consistent with the RMR 
  2.    -- tal@SpamSucks_renderman.org
  3.  */
  4.  
  5. /* fur surface shader
  6.  
  7.    with clumping and specular model
  8.  
  9.    by Clint Hanson and Armin Bruderlin
  10. */
  11.  
  12. color 
  13. fnc_diffuselgt (color Cin;       /* Light Colour */
  14.                 point Lin;       /* Light Position */
  15.                 point Nin;       /* Surface Normal */
  16.     )
  17. {
  18.     color Cout = Cin;
  19.     vector LN, NN;
  20.     float Atten;
  21.  
  22.     /* normalize the stuff */
  23.     LN = normalize(vector(Lin));
  24.     NN = normalize(vector(Nin));
  25.  
  26.     /* diffuse calculation */
  27.     Atten = max(0.0,LN.NN);
  28.  
  29.     Cout *= Atten;
  30.  
  31.     return (Cout);
  32. }
  33.  
  34.  
  35. #define luminance(c) comp(c,0)*0.299 + comp(c,1)*0.587 + comp(c,2)*0.114
  36.  
  37.  
  38. surface 
  39. k3d_fur2( /* Hair Shading... */
  40.          float Ka   = 0.0287;
  41.      float Kd   = 0.77;
  42.      float Ks   = 1.285;
  43.      float roughness1  = 0.008;
  44.      float SPEC1  = 0.01;
  45.      float roughness2  = 0.016;
  46.      float SPEC2  = 0.003;
  47.      float   start_spec = 0.3;
  48.      float   end_spec = 0.95;
  49.      float spec_size_fade  = 0.1;
  50.      float illum_width  = 180;
  51.      float var_fade_start = 0.005;
  52.      float var_fade_end = 0.001;
  53.      float clump_dark_strength = 0.0;
  54.               
  55.      /* Hair Color */
  56.      color rootcolor  = color (.9714, .9714, .9714);
  57.      color tipcolor = color (.519, .325, .125);
  58.      color specularcolor  = (color(1) + tipcolor) / 2;
  59.      color static_ambient  = color (0.057,0.057,0.057);
  60.      
  61.      /* Variables Passed from the rib... */
  62.      uniform float hair_col_var  = 0.0;
  63.      uniform float hair_length = 0.0;
  64.      uniform normal surface_normal  = normal 1;
  65.      varying vector clump_vect  = vector 0;
  66.      uniform float hair_id   = 0.0; /* Watch Out... Across Patches */
  67.      
  68.     )
  69. {
  70.     vector T = normalize (dPdv); /* tangent along length of hair */
  71.     vector V = -normalize(I);    /* V is the view vector */
  72.     color Cspec = 0, Cdiff = 0;  /* collect specular & diffuse light */
  73.     float Kspec = Ks;
  74.     vector nL;
  75.     varying normal nSN = normalize( surface_normal );
  76.     vector S = nSN^T;     /* Cross product of the tangent along the hair and surface normal */
  77.     vector N_hair = (T^S); /* N_hair is a normal for the hair oriented "away" from the surface */
  78.     vector norm_hair;
  79.     float  l = clamp(nSN.T,0,1);  /* Dot of surface_normal and T, used for blending */
  80.     float clump_darkening = 1.0;
  81.     float T_Dot_nL = 0;
  82.     float T_Dot_e = 0;
  83.     float Alpha = 0;
  84.     float Beta = 0;
  85.     float Kajiya = 0;
  86.     float darkening = 1.0;
  87.     varying color final_c;
  88.  
  89.     /* values from light */
  90.     uniform float nonspecular = 0;
  91.     uniform color SpecularColor = 1;
  92.  
  93.     /* When the hair is exactly perpendicular to the surface, use the
  94.        surface normal, when the hair is exactly tangent to the
  95.        surface, use the hair normal Otherwise, blend between the two
  96.        normals in a linear fashion 
  97.     */
  98.     norm_hair = (l * nSN) + ( (1-l) * N_hair);
  99.     norm_hair = normalize(norm_hair);
  100.     
  101.     /* Make the specular only hit in certain parts of the hair--v is
  102.        along the length of the hair 
  103.     */
  104.     Kspec *= min( smoothstep( start_spec, start_spec + spec_size_fade, v),  
  105.           1 - smoothstep( end_spec, end_spec - spec_size_fade, v ) );
  106.  
  107.  
  108.     /* Loop over lights, catch highlights as if this was a thin cylinder,
  109.  
  110.        Specular illumination model from:
  111.        James T. Kajiya and Timothy L.  Kay (1989) "Rendering Fur with Three 
  112.        Dimensional Textures", Computer Graphics 23,3, 271-280  
  113.     */
  114.  
  115.     illuminance (P, norm_hair, radians(illum_width)) {
  116.     nL = normalize(L);
  117.     
  118.     T_Dot_nL = T.nL;
  119.     T_Dot_e = T.V;
  120.     Alpha = acos(T_Dot_nL);
  121.     Beta = acos(T_Dot_e);
  122.  
  123.     Kajiya = T_Dot_nL * T_Dot_e + sin(Alpha) * sin(Beta);
  124.  
  125.     /* calculate diffuse component */
  126.     if ( clump_dark_strength > 0.0 ) {
  127.         clump_darkening = 1 - ( clump_dark_strength * 
  128.                                     abs(clamp(nL.normalize(-1*clump_vect), -1, 0)));
  129.     } else {
  130.         clump_darkening = 1.0;
  131.     }
  132.     
  133.     /* get light source parameters */
  134.     if ( lightsource("__nonspecular",nonspecular) == 0)
  135.         nonspecular = 0;
  136.     if ( lightsource("__SpecularColor",SpecularColor) == 0)
  137.         SpecularColor = color 1;
  138.     
  139.     Cspec += (1-nonspecular) * SpecularColor * clump_darkening * 
  140.         ((SPEC1*Cl*pow(Kajiya, 1/roughness1)) + 
  141.          (SPEC2*Cl*pow(Kajiya, 1/roughness2)));
  142.     
  143.     Cdiff += clump_darkening * fnc_diffuselgt(Cl, L, norm_hair);
  144.     }
  145.  
  146.     darkening = clamp(hair_col_var, 0, 1);
  147.     
  148.     darkening = (1 - (smoothstep( var_fade_end, var_fade_start, 
  149.                   abs(luminance(Kd*Cdiff))) * darkening));
  150.     
  151.     final_c = mix( rootcolor, tipcolor, v ) * darkening;
  152.     
  153.     Ci =  ((Ka*ambient() + Kd*Cdiff + static_ambient) * final_c
  154.        + ((v) * Kspec * Cspec * specularcolor));
  155.     
  156.     Ci = clamp(Ci, color 0, color 1 );
  157.     
  158.     Oi = Os;
  159.     Ci = Oi * Ci;
  160. }
  161.