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_mondometal.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  6.7 KB  |  246 lines

  1. /* MondoMetal.sl   Felipe Esquivel, july 2001
  2.  * felipe@siggraph.org.mx
  3.  * 
  4.  * Layered shader with mondo as a base and a metal modulated with a matte file.
  5.  *
  6.  * Version ampliada de Mondo.sl. Recibe un archivo B/N como mate, donde
  7.  * las partes blancas producen un efecto metalico y donde lo oscuro 
  8.  * se comporta igual que mondo simple.
  9.  */
  10.  
  11. surface
  12. k3d_mondometal(
  13.     string abColorMap = "";
  14.     float abColorMapBlur = 1;
  15.     float abUseColorMapAlpha = 0;
  16.     float abDiffuse = .8;
  17.     string abDiffuseMap = "";
  18.     float abDiffuseMapBlur = 1;
  19.     float abSpecular = .3;
  20.     string abSpecularMap = "";
  21.     float abSpecularMapBlur = 1;
  22.     color abSpecularColor = color(1,1,1);
  23.     string abSpecularColorMap = "";
  24.     float abSpecularColorMapBlur = 1;
  25.     float abRoughness = .3;
  26.     string abRoughnessMap = "";
  27.     float abRoughnessMapBlur = 1;
  28.     string abTransparencyMap = "";
  29.     float abInvertTransparencyMap = 0;
  30.     float abTransparencyMapBlur = 1;
  31.     color abIncandescence = color(0,0,0);
  32.     string abIncandescenseMap = "";
  33.     float abIncandescenseMapBlur = 1;
  34.     string abReflectionMap = "";
  35.     float abReflectionUp = 0;
  36.     float abReflectivity = .1;
  37.     float abReflectionMapBlur = 1;
  38.     string abBumpMap = "";
  39.     float abBumpMapBlur = 1;
  40.     float abBumpScale = 1.0;
  41.     float abDoDisplacement = 0.0;
  42.     float abUseNormals = 0;
  43.     float Ka = 0.05;
  44.     float Kd = 0.80;
  45.     float Ks = 0.80;
  46.     float roughness = 0.25;
  47.     color Cmetal = color(0.0,0.76,0.79);
  48.     string metalMatte = "";
  49.     point STMatrix0 = point "shader" (1,0,0);
  50.     point STMatrix1 = point "shader" (0,1,0);
  51. )
  52. {
  53.  
  54.  
  55.  
  56.  
  57.     extern vector I;
  58.  
  59.     normal Nf;
  60.     vector V, D;
  61.     point PP;
  62.     color Csurf, Cspec, Copac, Cincand, Crefl;
  63.     color Cmatte = 0;
  64.     float ss, tt, roughness, diff, spec, bmp;
  65.  
  66.     /* apply STMatrix to texture coordinates **/
  67.     ss = vector(s, t, 1) . vector(transform("shader", STMatrix0));
  68.     tt = vector(s, t, 1) . vector(transform("shader", STMatrix1));
  69.     
  70.     /* determine color **/
  71.     if(abColorMap != "")
  72.     {
  73.         Csurf = Cs * color texture(abColorMap, ss, tt,
  74.                             "swidth", abColorMapBlur,
  75.                             "twidth", abColorMapBlur );
  76.         if(abUseColorMapAlpha != 0)
  77.         {
  78.             Copac = float texture(abColorMap[3], ss, tt, 
  79.                             "swidth", abColorMapBlur,
  80.                             "twidth", abColorMapBlur );
  81.             /* need to un-premultiply the color in this case */
  82.             Csurf = Csurf / Copac;
  83.         }
  84.         else
  85.         {
  86.             Copac = color(1, 1, 1);
  87.         }
  88.     }
  89.     else
  90.     {
  91.         Csurf = Cs;
  92.         Copac = color(1);
  93.     }
  94.  
  95.     /* determine diffuse **/
  96.     if(abDiffuseMap != "")
  97.     {
  98.         diff = abDiffuse * float texture(abDiffuseMap, ss, tt, 
  99.                             "swidth", abDiffuseMapBlur,
  100.                             "twidth", abDiffuseMapBlur );
  101.     }
  102.     else
  103.     {
  104.         diff = abDiffuse;
  105.     }
  106.     
  107.     /* determine specular */
  108.     if(abSpecularMap != "")
  109.     {
  110.         spec = abSpecular * float texture(abSpecularMap, ss, tt, 
  111.                             "swidth", abSpecularMapBlur,
  112.                             "twidth", abSpecularMapBlur );
  113.  
  114.     }
  115.     else
  116.     {
  117.         spec = abSpecular;
  118.     }
  119.     
  120.     /* determine specular color */
  121.     if(abSpecularColorMap != "")
  122.     {
  123.         Cspec = abSpecularColor * color texture(abSpecularColorMap, ss, tt,
  124.                       "swidth", abSpecularColorMapBlur, 
  125.                       "twidth", abSpecularColorMapBlur );
  126.     }
  127.     else
  128.     {
  129.         Cspec = abSpecularColor;
  130.     }
  131.     /* determine roughness */
  132.     if(abRoughnessMap != "")
  133.     {
  134.         roughness = abRoughness * float texture(abRoughnessMap, ss, tt,
  135.                             "swidth", abRoughnessMapBlur, 
  136.                             "twidth", abRoughnessMapBlur );
  137.     }
  138.     else
  139.     {
  140.         roughness = abRoughness;
  141.     }
  142.     
  143.     /* determine opacity */
  144.     if(abTransparencyMap != "")
  145.     {
  146.         if(abInvertTransparencyMap != 0)
  147.         {
  148.             Copac = Copac * color texture(abTransparencyMap, ss, tt,
  149.                             "swidth", abTransparencyMapBlur, 
  150.                             "twidth", abTransparencyMapBlur );
  151.         }
  152.         else
  153.         {
  154.             Copac = Copac * (color(1) - color texture(abTransparencyMap, ss, tt,
  155.                             "swidth", abTransparencyMapBlur, 
  156.                             "twidth", abTransparencyMapBlur ));
  157.         }
  158.     }
  159.     
  160.     /* determine incandescence */
  161.     if(abIncandescenseMap != "")
  162.     {
  163.         Cincand = abIncandescence * color texture(abIncandescenseMap, ss, tt,
  164.                             "swidth", abIncandescenseMapBlur, 
  165.                             "twidth", abIncandescenseMapBlur );
  166.     }
  167.     else
  168.     {
  169.         Cincand = abIncandescence;
  170.     }
  171.     
  172.     
  173.     /* do the bump */
  174.     if( abBumpMap != "" )
  175.     {
  176.         bmp = abBumpScale * float texture( abBumpMap, ss, tt,
  177.                             "swidth", abBumpMapBlur,
  178.                             "twidth", abBumpMapBlur );
  179.         PP = transform("shader", P);
  180.         Nf = normalize( ntransform("shader", N) );
  181.         PP += bmp * Nf;
  182.         PP = transform("shader", "current", PP);
  183.         Nf = calculatenormal(PP);
  184.     
  185.     if (abUseNormals == 1) {
  186.         normal deltaN = normalize(N) - normalize(Ng);
  187.         Nf = normalize(Nf) + deltaN;
  188.     }
  189.     
  190.         if( abDoDisplacement == 1.0 )
  191.             P = PP; /* usually it's better to do displacements in displacement shader */
  192.     }
  193.     else
  194.     {
  195.         Nf = N;
  196.     }
  197.  
  198.     /* compute shading variables **/
  199.     Nf = faceforward(normalize(Nf), I , normalize(Nf));
  200.     V = -normalize(I);
  201.  
  202.     /* get reflection from environment map **/
  203.     if( abReflectionMap != "" )
  204.     {
  205.         D = reflect(-V, Nf);
  206.         D = vtransform("worldspace", D);
  207.         if( abReflectionUp != 0 )
  208.         {
  209.             D = vector(-zcomp(D), xcomp(D), ycomp(D));
  210.         }
  211.         Crefl = abReflectivity * 
  212.                 color environment(abReflectionMap, D,
  213.                                     "swidth", abReflectionMapBlur,
  214.                                     "twidth", abReflectionMapBlur );
  215.     }
  216.     else
  217.         Crefl = color(0);
  218.  
  219.     /* Averiguamos si la textura es metßlica o no */
  220.     if(metalMatte != "")
  221.     {
  222.         Cmatte = color texture(metalMatte, ss, tt,
  223.                             "swidth", abColorMapBlur,
  224.                             "twidth", abColorMapBlur);
  225.     }
  226.  
  227.  
  228.  
  229.     /* now add it all together */
  230.     Oi = Os * Copac;
  231.     
  232.     /* Textura normal de Mondo*/
  233.     if(comp(Cmatte, 1) < 0.5){
  234.       Ci = Os * Copac * (Csurf * (Cincand + ambient() + diff * diffuse(Nf)) + 
  235.            (spec * Cspec * (specular(Nf, V, roughness) + Crefl) ));
  236.     }
  237.     /* Textura metßlica */
  238.     else{
  239.       Ci = Cmetal * Oi * (Ka * ambient() + Kd * diffuse(Nf) +
  240.             Ks * specular(Nf,-normalize(I),roughness));
  241.     }
  242. }
  243.  
  244.  
  245.  
  246.