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_toonmap.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.8 KB  |  183 lines

  1. /*
  2.  * 2006-03-13 dan@3-e.net
  3.  *
  4.  */
  5.  
  6. #include "k3d_rmannotes.h"
  7.  
  8. surface k3d_toonmap (
  9.  
  10. float Ka=0.327; 
  11. float Kd=0.336; 
  12. float Ks=0.929; 
  13. float roughness=0.107; 
  14. string Highlight_Map="";
  15. color Highlight_Color=(1, 1, 1); 
  16. float channel4=0; 
  17. float swidth4=1; 
  18. float twidth4=1; 
  19. float samples4=1; 
  20. float ss4=1; 
  21. float tt4=1; 
  22. string Paint_Map="";
  23. color Paint_Color=(0.5, 0.5, 0.5); 
  24. float channel3=0; 
  25. float swidth3=1; 
  26. float twidth3=1; 
  27. float samples3=1; 
  28. float ss3=1; 
  29. float tt3=1; 
  30. float paint_spec=0.0888; 
  31. float paint_trans=0.539; 
  32. float paint_fuzz=0.274; 
  33. float ink_thresh=0.389; 
  34. float ink_fuzz=0.765; 
  35. string Ink_Map=""; 
  36. color Ink_Color=(0, 0, 0);
  37. float channel2=0; 
  38. float swidth2=1; 
  39. float twidth2=1; 
  40. float samples2=1; 
  41. float ss2=1; 
  42. float tt2=1; ) 
  43. {
  44. /** Surface main-code start **/
  45.  
  46. /** Texture_Highlight_Map **/
  47. color temt_c4;
  48. float temp_ss4;
  49. float temp_tt4;
  50. if (ss4 ==1) {temp_ss4=s;} else {temp_ss4=ss4;}
  51. if (tt4 ==1) {temp_tt4=t;} else {temp_tt4=tt4;}
  52. if ( Highlight_Map=="" ) 
  53. {
  54. temt_c4=Highlight_Color;
  55. else
  56. {
  57. if (channel4 < 0 || channel4 > 3)
  58. {
  59. temt_c4=texture(Highlight_Map,temp_ss4,temp_tt4,"swidth",swidth4,"twidth",twidth4,"samples",samples4);
  60. else 
  61. {
  62. temt_c4=texture(Highlight_Map[channel4],temp_ss4,temp_tt4,"swidth",swidth4,"twidth",twidth4,"samples",samples4);
  63. }
  64. }
  65. /** Texture_Paint_Map **/
  66. color temt_c3;
  67. float temp_ss3;
  68. float temp_tt3;
  69. if (ss3 ==1) {temp_ss3=s;} else {temp_ss3=ss3;}
  70. if (tt3 ==1) {temp_tt3=t;} else {temp_tt3=tt3;}
  71. if ( Paint_Map=="" ) 
  72. {
  73. temt_c3=Paint_Color;
  74. else
  75. {
  76. if (channel3 < 0 || channel3 > 3)
  77. {
  78. temt_c3=texture(Paint_Map,temp_ss3,temp_tt3,"swidth",swidth3,"twidth",twidth3,"samples",samples3);
  79. else 
  80. {
  81. temt_c3=texture(Paint_Map[channel3],temp_ss3,temp_tt3,"swidth",swidth3,"twidth",twidth3,"samples",samples3);
  82. }
  83. }
  84. /** Texture_Ink_Map **/
  85. color temt_c2;
  86. float temp_ss2;
  87. float temp_tt2;
  88. if (ss2 ==1) {temp_ss2=s;} else {temp_ss2=ss2;}
  89. if (tt2 ==1) {temp_tt2=t;} else {temp_tt2=tt2;}
  90. if ( Ink_Map=="" ) 
  91. {
  92. temt_c2=Ink_Color;
  93. else
  94. {
  95. if (channel2 < 0 || channel2 > 3)
  96. {
  97. temt_c2=texture(Ink_Map,temp_ss2,temp_tt2,"swidth",swidth2,"twidth",twidth2,"samples",samples2);
  98. else 
  99. {
  100. temt_c2=texture(Ink_Map[channel2],temp_ss2,temp_tt2,"swidth",swidth2,"twidth",twidth2,"samples",samples2);
  101. }
  102. }
  103. #define blend(a,b,x) ((a) * (1 - (x)) + (b) * (x))
  104. #define union(a,b) ((a) + (b) - (a) * (b))
  105.  
  106. float
  107. toonspec(vector N, V; float roughness)
  108. {
  109. float C = 0;
  110. vector H;
  111.  
  112. illuminance(P, N, PI/2) {
  113. H = normalize(normalize(L)+V);
  114. C += pow(N.H, 1/roughness);
  115. }
  116. return C;
  117. }
  118.  
  119. float cos_here;
  120. float diff, spec;
  121. normal Nf;
  122. vector normI, half;
  123.  
  124. color toon_color, layer_color;
  125. color toon_opac, layer_opac;
  126.  
  127. Nf = faceforward(normalize(N), I);
  128. normI = normalize(I);
  129.  
  130. /* background layer (layer 0) */
  131. toon_color = color (0, 0, 0);
  132. toon_opac = Os;
  133.  
  134. /* toon paint (layer 1) */
  135.  
  136. /* only care about percentage illumination */
  137. diff = 0;
  138. illuminance(P, Nf, PI/2)
  139. diff += normalize(L).Nf; 
  140.  
  141. /* antialias the transition */
  142. diff = smoothstep(paint_trans - paint_fuzz/2, paint_trans + paint_fuzz/2, diff);
  143.  
  144. /* specular -- transition also antialiased */ 
  145. spec = toonspec(Nf, -normalize(I), roughness);
  146. spec = smoothstep(paint_spec - paint_fuzz/2, paint_spec + paint_fuzz/2, spec);
  147.  
  148. layer_color = Cs*(Kd*diff*temt_c3 + Ka*ambient()) + temt_c4*Ks*spec;
  149. layer_opac = Os;
  150.  
  151. toon_color = blend(toon_color, layer_color, layer_opac);
  152. toon_opac = union(toon_opac, layer_opac);
  153.  
  154. /* toon ink (layer 3) */
  155.  
  156. cos_here = normalize(Nf).normI;
  157.  
  158. /* antialias ink lines */
  159. layer_opac = 1 - smoothstep(ink_thresh - ink_fuzz/2, ink_thresh + ink_fuzz/2, abs(cos_here));
  160.  
  161. if (abs(cos_here) < ink_thresh)
  162. layer_color = temt_c2;
  163. else
  164. layer_color = 0;
  165.  
  166. toon_color = blend(toon_color, layer_color, layer_opac);
  167. toon_opac = union(toon_opac, layer_opac);
  168.  
  169. color toonOi = toon_opac;
  170. color toonCi = toon_opac * toon_color;
  171.  
  172. /** Surface main-code end **/
  173.  
  174. Ci = toonCi;
  175. Oi = toon_opac;
  176. }
  177.