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_graphic_lines.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  12.7 KB  |  388 lines

  1. /*
  2.  * 2006-03-13 dan@3-e.net
  3.  * need to clean up and rewrite this code to be faster and more compact.
  4.  */
  5.  
  6. #include "k3d_rmannotes.h"
  7.  
  8. surface k3d_graphic_lines (
  9.  
  10.         float Ka=1.0;
  11.         float Kd=1.0;
  12.         float Ks=0.6;
  13.         float roughness=1.0;
  14.         float paint_spec=0.6;
  15.         float paint_trans=0.0;
  16.         float paint_fuzz=0.0;
  17.         float ink_thresh=0.10;
  18.         float ink_fuzz=0.0;
  19.                 float line_scale_master=1.0;
  20.  
  21.         string up_highlight="z";
  22.         float line_scale_highlight=16.0;
  23.         float contrast_highlight=1.0;
  24.         float brightness_highlight=0.5;
  25.         float randomness_highlight=0.0;
  26.         float noise_size_highlight=1.0;
  27.         color color_curve_highlight_color1=(0.0,0.0,0.0);
  28.         color color_curve_highlight_color2=(0.1,0.05,0.0);
  29.         color color_curve_highlight_color3=(0.8,0.6,0.4);
  30.         color color_curve_highlight_color4=(1.0,1.0,0.8);
  31.         color color_curve_highlight_color5=(1.0,1.0,1.0);
  32.         color color_curve_highlight_color6=(1.0,1.0,1.0);
  33.         float surface_opac_highlight=1.0;
  34.         float surface_trans_highlight=1.0;
  35.         float fuzz_highlight=0.3;
  36.                 float tfreq_highlight=50;
  37.                 color illumcolor_highlight=(0.0,0.0,0.0);
  38.                 float illumination_highlight=0.0;
  39.                 float Ka_highlight=0.10;
  40.                 float Kd_highlight=0.70;
  41.                 float Ks_highlight=0.20;
  42.                 float roughness_highlight=0.2;
  43.                 
  44.         string up_paint="y";
  45.         float line_scale_paint=16.0;
  46.         float contrast_paint=1.0;
  47.         float brightness_paint=0.3;
  48.         float randomness_paint=0.0;
  49.         float noise_size_paint=1.0;
  50.         color color_curve_paint_color1=(0.0,0.0,0.0);
  51.         color color_curve_paint_color2=(0.1,0.05,0.0);
  52.         color color_curve_paint_color3=(0.8,0.6,0.4);
  53.         color color_curve_paint_color4=(1.0,1.0,0.8);
  54.         color color_curve_paint_color5=(1.0,1.0,1.0);
  55.         color color_curve_paint_color6=(1.0,1.0,1.0);
  56.         float surface_opac_paint=1.0;
  57.         float surface_trans_paint=1.0;
  58.                 float fuzz_paint=0.3;
  59.                 float tfreq_paint=50;
  60.                 color illumcolor_paint=(0.0,0.0,0.0);
  61.                 float illumination_paint=0.0;
  62.                 float Ka_paint=0.10;
  63.                 float Kd_paint=0.70;
  64.                 float Ks_paint=0.20;
  65.                 float roughness_paint=0.2;
  66.                     
  67.         string up_ink="x";
  68.         float line_scale_ink=16.0;
  69.         float contrast_ink=1.0;
  70.         float brightness_ink=0.5;
  71.         float randomness_ink=0.0;
  72.         float noise_size_ink=1.0;
  73.         color color_curve_ink_color1=(0.0,0.0,0.0);
  74.         color color_curve_ink_color2=(0.1,0.05,0.0);
  75.         color color_curve_ink_color3=(0.8,0.6,0.4);
  76.         color color_curve_ink_color4=(1.0,1.0,0.8);
  77.         color color_curve_ink_color5=(1.0,1.0,1.0);
  78.         color color_curve_ink_color6=(1.0,1.0,1.0);
  79.         float surface_opac_ink=1.0;
  80.         float surface_trans_ink=1.0; 
  81.                 float fuzz_ink=0.3;
  82.                 float tfreq_ink=50;
  83.                 color illumcolor_ink=(0.0,0.0,0.0);
  84.                 float illumination_ink=0.0;
  85.                 float Ka_ink=0.10;
  86.                 float Kd_ink=0.70;
  87.                 float Ks_ink=0.20;
  88.                 float roughness_ink=0.2;
  89. )
  90. {
  91.     /** Surface main-code start **/
  92.  
  93.     /** hightlights **/
  94.     
  95.     point P_highlight = P;
  96.     normal N_highlight = N;
  97.     color Ci_highlight = Ci;
  98.     color Oi_highlight = Oi;
  99.  
  100.     color surface_color_highlight;
  101.     color layer_color_highlight;
  102.     color color_curve_highlight;
  103.     color layer_opac_highlight;
  104.     float stripemin_highlight;
  105.     float stripemax_highlight;
  106.     float tt_highlight;
  107.     float x_highlight;
  108.     float y_highlight;
  109.     float z_highlight;
  110.     float i_highlight;
  111.     float n_highlight;
  112.     float ns_highlight;
  113.     vector Nf_highlight;
  114.     vector V_highlight;
  115.     point Psh_highlight;
  116.     Psh_highlight=transform("shader",P_highlight);
  117.     Nf_highlight=faceforward(normalize(N_highlight),I);
  118.     V_highlight=-normalize(I);
  119.  
  120. /** setup tt_highlight with chosen axis **/
  121.     if (up_highlight == "z")
  122.     {z_highlight=zcomp(Psh_highlight);
  123.         z_highlight*=line_scale_highlight*line_scale_master;
  124.         tt_highlight=mod(z_highlight,1);
  125.     }
  126.     else if (up_highlight == "y")
  127.     {y_highlight=ycomp(Psh_highlight);
  128.         y_highlight*=line_scale_highlight*line_scale_master;
  129.         tt_highlight=mod(y_highlight,1);
  130.     }
  131.     else {x_highlight=xcomp(Psh_highlight);
  132.         x_highlight*=line_scale_highlight*line_scale_master;
  133.         tt_highlight=mod(x_highlight,1);
  134.     }
  135.     illumcolor_highlight=(Ka_highlight*ambient()+Kd_highlight*diffuse(Nf_highlight)+Ks_highlight*specular(Nf_highlight,V_highlight,roughness_highlight));
  136.     illumination_highlight=max(max(comp(illumcolor_highlight,0),comp(illumcolor_highlight,1)),comp(illumcolor_highlight,2));
  137.     n_highlight=0;
  138.     ns_highlight=noise_size_highlight;
  139.  
  140.     for (i_highlight=0; i_highlight<6.0 ;i_highlight+=1.0){
  141.         n_highlight+=(2*noise(Psh_highlight*ns_highlight)-1)/ns_highlight;
  142.         ns_highlight*=2.17;
  143.     }
  144.     illumination_highlight=(illumination_highlight*contrast_highlight)+(1.0-contrast_highlight)/2.0+(brightness_highlight-1.0)+n_highlight*randomness_highlight;
  145.     if (illumination_highlight<0.01)
  146.     {surface_color_highlight=color(0.0,0.0,0.0);
  147.         layer_color_highlight=0.0;
  148.     }
  149.     else if (illumination_highlight>0.99)
  150.     {surface_color_highlight=color(1.0,1.0,1.0);
  151.         layer_color_highlight=1.0;
  152.     }
  153.     else {surface_color_highlight=color(0.0,0.0,0.0);
  154.         layer_color_highlight=1.0;
  155.     }
  156.     stripemin_highlight=0.5-smoothstep(0.0,1.0,illumination_highlight)/2.0;
  157.     stripemax_highlight=0.5+smoothstep(0.0,1.0,illumination_highlight)/2.0;
  158.     float val_highlight=(smoothstep((stripemin_highlight)-(fuzz_highlight),(stripemin_highlight),(tt_highlight))-smoothstep((stripemax_highlight)-(fuzz_highlight),(stripemax_highlight),(tt_highlight)));
  159.     layer_opac_highlight=color(val_highlight);
  160.     surface_color_highlight=((surface_color_highlight)*(1-(layer_opac_highlight))+(layer_color_highlight)*(layer_opac_highlight));
  161.     float color_val_highlight=mix(0.0,1.0,val_highlight);
  162.     color_curve_highlight=spline(color_val_highlight,color_curve_highlight_color1,color_curve_highlight_color2,color_curve_highlight_color3,color_curve_highlight_color4,color_curve_highlight_color5,color_curve_highlight_color6);
  163.     float spacescale_highlight=length(vtransform("shader",normalize(N_highlight)));
  164.     vector Ndisp_highlight=normalize(N_highlight)*(0/max(spacescale_highlight,1e-6));
  165.     
  166.     P_highlight+=0*Ndisp_highlight;
  167.     N_highlight=normalize(calculatenormal(P_highlight+(1-0)*Ndisp_highlight));
  168.     Ci_highlight=surface_opac_highlight*color_curve_highlight;
  169.     Oi_highlight=surface_trans_highlight;
  170.     
  171.     /** paint **/
  172.     
  173.     point P_paint = P;
  174.     normal N_paint = N;
  175.     color Ci_paint = Ci;
  176.     color Oi_paint = Oi;
  177.  
  178.     color surface_color_paint;
  179.     color layer_color_paint;
  180.     color color_curve_paint;
  181.     color layer_opac_paint;
  182.     float stripemin_paint;
  183.     float stripemax_paint;
  184.     float tt_paint;
  185.     float x_paint;
  186.     float y_paint;
  187.     float z_paint;
  188.     float i_paint;
  189.     float n_paint;
  190.     float ns_paint;
  191.     vector Nf_paint;
  192.     vector V_paint;
  193.     point Psh_paint;
  194.     Psh_paint=transform("shader",P_paint);
  195.     Nf_paint=faceforward(normalize(N_paint),I);
  196.     V_paint=-normalize(I);
  197.     if (up_paint == "z")
  198.     {z_paint=zcomp(Psh_paint);
  199.         z_paint*=line_scale_paint*line_scale_master;
  200.         tt_paint=mod(z_paint,1);
  201.     }
  202.     else if (up_paint == "y")
  203.     {y_paint=ycomp(Psh_paint);
  204.         y_paint*=line_scale_paint*line_scale_master;
  205.         tt_paint=mod(y_paint,1);
  206.     }
  207.     else {x_paint=xcomp(Psh_paint);
  208.         x_paint*=line_scale_paint*line_scale_master;
  209.         tt_paint=mod(x_paint,1);
  210.     }illumcolor_paint=(Ka_paint*ambient()+Kd_paint*diffuse(Nf_paint)+Ks_paint*specular(Nf_paint,V_paint,roughness_paint));
  211.     illumination_paint=max(max(comp(illumcolor_paint,0),comp(illumcolor_paint,1)),comp(illumcolor_paint,2));
  212.     n_paint=0;
  213.     ns_paint=noise_size_paint;
  214.     for ( i_paint=0; i_paint<6.0 ; i_paint+=1.0 ){
  215.         n_paint+=(2*noise(Psh_paint*ns_paint)-1)/ns_paint;
  216.         ns_paint*=2.17;
  217.     }
  218.     illumination_paint=(illumination_paint*contrast_paint)+(1.0-contrast_paint)/2.0+(brightness_paint-1.0)+n_paint*randomness_paint;
  219.     if (illumination_paint<0.01)
  220.     {surface_color_paint=color(0.0,0.0,0.0);
  221.         layer_color_paint=0.0;
  222.     }
  223.     else if (illumination_paint>0.99)
  224.     {surface_color_paint=color(1.0,1.0,1.0);
  225.         layer_color_paint=1.0;
  226.     }
  227.     else {surface_color_paint=color(0.0,0.0,0.0);
  228.         layer_color_paint=1.0;
  229.     }stripemin_paint=0.5-smoothstep(0.0,1.0,illumination_paint)/2.0;
  230.     stripemax_paint=0.5+smoothstep(0.0,1.0,illumination_paint)/2.0;
  231.     float val1=(smoothstep((stripemin_paint)-(fuzz_paint),(stripemin_paint),(tt_paint))-smoothstep((stripemax_paint)-(fuzz_paint),(stripemax_paint),(tt_paint)));
  232.     layer_opac_paint=color(val1);
  233.     surface_color_paint=((surface_color_paint)*(1-(layer_opac_paint))+(layer_color_paint)*(layer_opac_paint));
  234.     float color_val1=mix(0.0,1.0,val1);
  235.     color_curve_paint=spline(color_val1,color_curve_paint_color1,color_curve_paint_color2,color_curve_paint_color3,color_curve_paint_color4,color_curve_paint_color5,color_curve_paint_color6);
  236.     float spacescale1=length(vtransform("shader",normalize(N_paint)));
  237.     vector Ndisp1=normalize(N_paint)*(0/max(spacescale1,1e-6));
  238.     P_paint+=0*Ndisp1;
  239.     N_paint=normalize(calculatenormal(P_paint+(1-0)*Ndisp1));
  240.     Ci_paint=surface_opac_paint*color_curve_paint;
  241.     Oi_paint=surface_trans_paint;
  242.     
  243.     /** ink **/
  244.     
  245.     point P_ink = P;
  246.     normal N_ink = N;
  247.     color Ci_ink = Ci;
  248.     color Oi_ink = Oi;
  249.  
  250.     color surface_color_ink;
  251.     color layer_color_ink;
  252.     color color_curve_ink;
  253.     color layer_opac_ink;
  254.     float stripemin_ink;
  255.     float stripemax_ink;
  256.     float tt_ink;
  257.     float x_ink;
  258.     float y_ink;
  259.     float z_ink;
  260.     float i_ink;
  261.     float n_ink;
  262.     float ns_ink;
  263.     vector Nf_ink;
  264.     vector V_ink;
  265.     point Psh_ink;
  266.     Psh_ink=transform("shader",P_ink);
  267.     Nf_ink=faceforward(normalize(N_ink),I);
  268.     V_ink=-normalize(I);
  269.     if (up_ink == "z")
  270.     {z_ink=zcomp(Psh_ink);
  271.         z_ink*=line_scale_ink*line_scale_master;
  272.         tt_ink=mod(z_ink,1);
  273.     }
  274.     else if (up_ink == "y")
  275.     {y_ink=ycomp(Psh_ink);
  276.         y_ink*=line_scale_ink*line_scale_master;
  277.         tt_ink=mod(y_ink,1);
  278.     }
  279.     else {x_ink=xcomp(Psh_ink);
  280.         x_ink*=line_scale_ink*line_scale_master;
  281.         tt_ink=mod(x_ink,1);
  282.     }illumcolor_ink=(Ka_ink*ambient()+Kd_ink*diffuse(Nf_ink)+Ks_ink*specular(Nf_ink,V_ink,roughness_ink));
  283.     illumination_ink=max(max(comp(illumcolor_ink,0),comp(illumcolor_ink,1)),comp(illumcolor_ink,2));
  284.     n_ink=0;
  285.     ns_ink=noise_size_ink;
  286.     for ( i_ink=0; i_ink<6.0; i_ink+=1.0 ){
  287.         n_ink+=(2*noise(Psh_ink*ns_ink)-1)/ns_ink;
  288.         ns_ink*=2.17;
  289.     }
  290.     illumination_ink=(illumination_ink*contrast_ink)+(1.0-contrast_ink)/2.0+(brightness_ink-1.0)+n_ink*randomness_ink;
  291.     if (illumination_ink<0.01)
  292.     {surface_color_ink=color(0.0,0.0,0.0);
  293.         layer_color_ink=0.0;
  294.     }
  295.     else if (illumination_ink>0.99)
  296.     {surface_color_ink=color(1.0,1.0,1.0);
  297.         layer_color_ink=1.0;
  298.     }
  299.     else {surface_color_ink=color(0.0,0.0,0.0);
  300.         layer_color_ink=1.0;
  301.     }stripemin_ink=0.5-smoothstep(0.0,1.0,illumination_ink)/2.0;
  302.     stripemax_ink=0.5+smoothstep(0.0,1.0,illumination_ink)/2.0;
  303.     float val2=(smoothstep((stripemin_ink)-(fuzz_ink),(stripemin_ink),(tt_ink))-smoothstep((stripemax_ink)-(fuzz_ink),(stripemax_ink),(tt_ink)));
  304.     layer_opac_ink=color(val2);
  305.     surface_color_ink=((surface_color_ink)*(1-(layer_opac_ink))+(layer_color_ink)*(layer_opac_ink));
  306.     float color_val2=mix(0.0,1.0,val2);
  307.     color_curve_ink=spline(color_val2,color_curve_ink_color1,color_curve_ink_color2,color_curve_ink_color3,color_curve_ink_color4,color_curve_ink_color5,color_curve_ink_color6);
  308.     float spacescale2=length(vtransform("shader",normalize(N_ink)));
  309.     vector Ndisp2=normalize(N_ink)*(0/max(spacescale2,1e-6));
  310.     P_ink+=0*Ndisp2;
  311.     N_ink=normalize(calculatenormal(P_ink+(1-0)*Ndisp2));
  312.     Ci_ink=surface_opac_ink*color_curve_ink;
  313.     Oi_ink=surface_trans_ink;
  314.  
  315.     /** blend 3 layers **/
  316.     
  317.     float
  318.     glinespec(vector N, V; float roughness)
  319.     {
  320.         float C = 0;
  321.         vector H;
  322.  
  323.         illuminance(P, N, PI/2) {
  324.             H = normalize(normalize(L)+V);
  325.             C += pow(N.H, 1/roughness);
  326.         }
  327.         return C;
  328.     }
  329.  
  330.     float cos_here;
  331.     float diff, spec;
  332.     normal Nf;
  333.     vector normI, half;
  334.  
  335.     color gline_color, layer_color;
  336.     color gline_opac, layer_opac;
  337.  
  338.     Nf = faceforward(normalize(N), I);
  339.     normI = normalize(I);
  340.  
  341.     /* background layer  */
  342.     gline_color = color (0, 0, 0);
  343.     gline_opac = Os;
  344.  
  345.     /* gline paint layer ) */
  346.  
  347.     /* only care about percentage illumination */
  348.     diff = 0;
  349.     illuminance(P, Nf, PI/2)
  350.     diff += normalize(L).Nf;
  351.  
  352.     /* antialias the transition */
  353.     diff = smoothstep(paint_trans - paint_fuzz/2, paint_trans + paint_fuzz/2, diff);
  354.  
  355.     /* specular -- transition also antialiased */
  356.     spec = glinespec(Nf, -normalize(I), roughness);
  357.     spec = smoothstep(paint_spec - paint_fuzz/2, paint_spec + paint_fuzz/2, spec);
  358.  
  359.     layer_color = Cs*(Kd*diff*Ci_paint + Ka*ambient()) + Ci_highlight*Ks*spec;
  360.     layer_opac = Oi_paint;
  361.  
  362.     gline_color = blend(gline_color, layer_color, layer_opac);
  363.     gline_opac = union(gline_opac, layer_opac);
  364.  
  365.     /* gline ink layer ) */
  366.  
  367.     cos_here = normalize(Nf).normI;
  368.  
  369.     /* antialias ink lines */
  370.     layer_opac = 1 - smoothstep(ink_thresh - ink_fuzz/2, ink_thresh + ink_fuzz/2, abs(cos_here));
  371.  
  372.     if (abs(cos_here) < ink_thresh)
  373.         layer_color = Ci_ink;
  374.     else
  375.         layer_color = 0;
  376.  
  377.     gline_color = blend(gline_color, layer_color, layer_opac);
  378.     gline_opac = union(gline_opac, layer_opac);
  379.  
  380.     color glineOi = gline_opac;
  381.     color glineCi = gline_opac * gline_color;
  382.  
  383.     /** Surface main-code end **/
  384.  
  385.     Ci = glineCi;
  386.     Oi = gline_opac;
  387. }
  388.