home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / displacemnt / k3d_hexatile_bump.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.3 KB  |  138 lines

  1. /****************************************************************************
  2. * FGHexaTile_bump.sl -- "Simple shader to do "Hexagon" displacement"
  3. *
  4. * Description:
  5. * A simple bump shader that tile a "Hexagon" pattern
  6. *
  7. *
  8. * Parameters:
  9. *     string Name     = "FGHexaTile_bump"    ---    Name of the shader "do nothing".
  10. *    float Edge1    =.2             ---    Minvalue of Edge.
  11. *    float Edge2    =.5             ---    Maxvalue of Edge.
  12. *    float ScaleSS    = 3             ---    Scale the "s" direction.
  13. *    float ScaleTT    = 3             ---    Scale the "t" direction.
  14. *    float Rotate    = 180             ---    Rotate the tile default "Z-direction"
  15. *    point P1    = point(0,0,0)         ---    Center point for Rotation.
  16. *    point P2    = point(0,0,1)         ---    The x,y,z direction for Rotate.
  17. *    string Projection = "planar"         ---    Projection "preoject.h"
  18. *    string Space      = "shader"         ---    Space for projection.
  19. *    float mx[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}  --- 16 float for scale,rotate,translate.
  20. *    float C1    = 1             ---    Multiply of f1 the closes future point.
  21. *    float C2    = 2             ---    Multiply of f2 the second closes future point.
  22. *    float Km    = -.015         ---    Diplacement amplitude.
  23. *    float Truedisp    = 1             ---    True Displacement 0 = off, 1 = on.
  24. *
  25. *
  26. ***************************************************************************
  27. *
  28. * Author: Fredrik Gustafsson, gusten@SpamSucks_algonet.se
  29. *
  30. * Reference:
  31. * Larry Gritz "noises.h", "project.h", "patterns.h"
  32. * Arman "Advanced Renderman-"Creating CGI for Motion Picture.
  33. *
  34. * Revision:1.0 Date: 2002/02/25
  35. *
  36. ****************************************************************************/
  37.  
  38. #include "k3d_project.h"
  39. #include "k3d_patterns.h"
  40.  
  41. void
  42. Worley2D_f1f2 (float ss, tt; float jitter, seed, c1, c2, hexagon, scale_ss, scale_tt; 
  43.         output float f1; output float spos1, tpos1; output float f2; output float spos2, tpos2;)
  44. {
  45.  
  46. float sthiscell = floor(ss)+0.5, tthiscell = floor(tt)+0.5;
  47.  
  48. f1 = f2 = 1000;
  49.  
  50. uniform float i, j;
  51.     for (i = -1; i <= 1; i += 1) {
  52.         float stestcell = sthiscell + i;
  53.             for (j = -1; j <= 1; j += 1) {
  54.                 float ttestcell = tthiscell + j;
  55.  
  56. /* Hexagon code */
  57. if ((hexagon != 0) && abs(mod(stestcell, 2)) < 1)
  58.     ttestcell += 0.5;
  59.  
  60.     float spos = stestcell + jitter * (float cellnoise(stestcell,seed+ttestcell) - 0.5);
  61.     float tpos = ttestcell + jitter * (float cellnoise(stestcell+23,seed+ttestcell-87) - 0.5);
  62.     float soffset = (spos - ss) * scale_ss;
  63.     float toffset = (tpos - tt) * scale_tt;
  64.     float dist = soffset*soffset + toffset*toffset;
  65.  
  66. if (dist < f1) {
  67.     f2 = f1; spos2 = spos1; tpos2 = tpos1;
  68.     f1 = dist; spos1 = spos; tpos1 = tpos;
  69. } else if (dist < f2) {
  70.     f2 = dist;
  71.     spos2 = spos; tpos2 = tpos;
  72.     }
  73.   }
  74. }
  75. f1 = f1 * c1; f2 = f2 * c2;
  76. }
  77.  
  78. displacement k3d_hexatile_bump (
  79.     string Name     = "FGHexaTile_bump";
  80.     float Edge1    =.2; 
  81.     float Edge2    =.5; 
  82.     float ScaleSS    = 3; 
  83.     float ScaleTT    = 3; 
  84.     float Rotate    = 180; 
  85.     point P1    = point(0,0,0); 
  86.     point P2    = point(0,0,1); 
  87.     string Projection = "planar"; 
  88.     string Space      = "shader"; 
  89.     float mx[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; 
  90.     float C1    = 1; 
  91.     float C2    = 2; 
  92.     float Km    = -.015; 
  93.     float Truedisp    = 1; ) 
  94.  
  95. float SS,TT,Ds,Dt;
  96. ProjectTo2D (Projection,rotate(P,radians(Rotate),P1,P2),Space,array_to_mx(mx),SS,TT,Ds,Dt);
  97.  
  98. float f1_out_15 = 0,f2_out_15 = 0,spos1_out_15 = 0,tpos1_out_15 = 0,spos2_out_15 = 0,tpos2_out_15 = 0;
  99.  
  100. float scale1 = ScaleSS * SS;
  101. float scale2 = ScaleTT * TT;
  102.  
  103. Worley2D_f1f2 (scale1,scale2,/* Jitter */ 0,/* Seed */ 0,C1,C2,/* Hexagon */ 1,/* TileScaleSS */ 1,
  104.             /* TileScaleTT */ 1,/* f1_result */ f1_out_15,spos1_out_15, tpos1_out_15,
  105.                 /* f2_result */ f2_out_15,spos2_out_15,tpos2_out_15);
  106.  
  107. /*** ----- Exponential "sqrt(float X)" ----- ***/
  108. float SQRT_f2 = sqrt(f2_out_15);
  109.  
  110. /*** ----- SmoothPulse macro ----- ***/
  111. float smoothp_20 = smoothpulse (0,Edge1,Edge2,1,SQRT_f2);
  112.  
  113. /*** ----- Exponential "sqrt(float X)" ----- ***/
  114. float SQRT_f1 = sqrt(f1_out_15);
  115.  
  116. /*** ----- SmoothPulse macro ----- ***/
  117. float smoothp_22 = smoothpulse (0,Edge1,Edge2,1,SQRT_f1);
  118.  
  119. /*** ----- True Displacement and bump ----- ***/
  120. normal 
  121. Displace (normal dir; vector space; float amp; float truedisp;)
  122. {
  123.     extern point P;
  124.     float spacescale = length(space);
  125.     vector Ndisp = dir * (amp / max(spacescale,1e-6));
  126. P += truedisp * Ndisp;
  127. return normalize (calculatenormal (P + (1-truedisp)*Ndisp));
  128. }
  129.  
  130. vector VShd = vtransform("shader", vector(normalize(N)));
  131.  
  132. float disp = clamp(smoothp_20*smoothp_22,0,0.95);
  133.  
  134. N = Displace(normalize(N),VShd,Km*disp,Truedisp); 
  135. }
  136.  
  137.