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

  1.  
  2. /**************************************************************************/
  3. /**************************************************************************/
  4. /*** DO NOT EDIT THE .SL FILE FOR THIS SHADER *****************************/
  5. /*** INSTEAD EDIT THE .SLM4 FILE WHICH GENERATES THE .SL FILE *************/
  6. /**************************************************************************/
  7. /**************************************************************************/
  8.  
  9. /*
  10. * To do: accomodate negative widths. twosided/onesided cards.
  11. */
  12.  
  13. /*
  14. ** $Id: k3d_softboxes.sl,v 1.1 2006/02/25 20:11:54 tshead Exp $
  15. **
  16. ** Derived from softbox3 v1.2 - Author Bjorke for all
  17. **
  18. ** Full RCS log in .slm4 file
  19. **
  20. **
  21. */
  22.  
  23. #define BOOL float
  24. #define ENUM float
  25.  
  26. #define SHAD_BOX_FILT 0
  27. #define SHAD_GAUSSIAN_FILT 1
  28.  
  29. #define TEX_GAUSSIAN_FILT 0
  30. #define TEX_BOX_FILT 1
  31. #define TEX_RADIAL_FILT 2
  32. #define TEX_DISK_FILT 3
  33.  
  34. #define HALFSIZE
  35.  
  36. #define OBEY_THIS "Reflectivity"
  37.  
  38. #define CLASSIC 0
  39. #define SPHERICAL 1
  40.  
  41. #define CUBEFACE 0
  42. #define LATLONG 1
  43.  
  44.  
  45.  
  46. /**************************************************************************/
  47. /* Superellipse soft clipping - straight out of "uberlight"        ***/
  48. /* Input:                                ***/
  49. /*   - point Q on the x-y plane                        ***/
  50. /*   - the equations of two superellipses (with major/minor axes given    ***/
  51. /*      by  a,b and A,B for the inner and outer ellipses, respectively)    ***/
  52. /* Returns:                                ***/
  53. /*   - 0 if Q was inside the inner ellipse                ***/
  54. /*   - 1 if Q was outside the outer ellipse                ***/
  55. /*   - smoothly varying from 0 to 1 in between                ***/
  56. /**************************************************************************/
  57.  
  58. /* this is the identical function used by sqLight etc */
  59.  
  60. float clipSuperellipse (
  61.     point Q;          /* Test point on the x-y plane */
  62.     uniform float a, b;       /* Inner superellipse */
  63.     uniform float A, B;       /* Outer superellipse */
  64.     uniform float roundness;  /* Same roundness for both ellipses */
  65. ) {
  66.     varying float result;
  67.     varying float x = abs(xcomp(Q)), y = abs(ycomp(Q));
  68.     if (roundness < 1.0e-6) {
  69.     /* Simpler case of a square */
  70.     result = 1 - (1-smoothstep(a,A,x)) * (1-smoothstep(b,B,y));
  71.     } else {
  72.     /* more-difficult rounded corner case */
  73.     varying float re = 2/roundness;        /* roundness exponent */
  74.     varying float q = a * b * pow (pow(b*x, re) + pow(a*y, re), -1/re);
  75.     varying float r = A * B * pow (pow(B*x, re) + pow(A*y, re), -1/re);
  76.     result = smoothstep (q, r, 1);
  77.     }
  78.     return result;
  79. }
  80.  
  81. /*****************************************************************/
  82. /*** Given info on a softbox, ************************************/
  83. /*****************************************************************/
  84.  
  85. void softbox_contrib2(
  86.     varying point    surfPt;
  87.     varying vector    reflVect;
  88.     uniform string    boxCoords;
  89.     uniform string    boxTexture;
  90.     uniform float    boxTexStr,
  91.             boxTexBlur,
  92.             boxFilter,
  93.             boxWidth,
  94.             boxHeight,
  95.             boxWEdge,
  96.             boxHEdge,
  97.             boxRoundness;
  98.     uniform color    boxColor,
  99.                 boxOpac; 
  100.     uniform float    decayExp;
  101.     output float theDist;
  102.     output color theColor;
  103.     output color theOpac;
  104. ) {
  105.     uniform string filtTypes[4] = {"gaussian","box","radial-bspline","disk"};
  106.     uniform string theFilterName = filtTypes[clamp(boxFilter,0,3)];
  107.     varying float contrib;
  108.     varying color ct = 1;
  109.     varying float ot = 1; 
  110.     /* Get the surface position */
  111.     varying point Pb1 = transform (boxCoords, surfPt);
  112.     varying vector Vlight = vtransform (boxCoords, reflVect);
  113.     varying float zv = zcomp(Vlight);
  114.     varying point Pplane = Pb1 - Vlight*(zcomp(Pb1)/zcomp(Vlight));
  115. #ifdef HALFSIZE
  116.     uniform float bw2 = boxWidth/2;
  117.     uniform float bh2 = boxHeight/2;
  118. #else
  119. #define bw2 boxWidth
  120. #define bh2 boxHeight
  121. #endif
  122.     uniform float we = max(boxWEdge,-bw2);
  123.     uniform float he = max(boxHEdge,-bh2);
  124.     uniform float bW = bw2+we;
  125.     uniform float bH = bh2+he;
  126.     uniform float iW = min(bW,bw2);
  127.     uniform float iH = min(bH,bh2);
  128.     uniform float oW = max(bW,bw2);
  129.     uniform float oH = max(bH,bh2);
  130.     if (sign(zcomp(Pb1)) == sign(zcomp(Vlight))) {
  131.     contrib = 0;
  132.     } else if (abs(zv) < 0.0001) {
  133.     contrib = 0;
  134.     } else {
  135.     contrib = 1 - clipSuperellipse (Pplane, iW, iH, oW,oH, boxRoundness);
  136.     if (boxTexture != "") {
  137.         uniform float nChans;
  138.         textureinfo(boxTexture,"channels",nChans);
  139.         varying float theS = (oW+xcomp(Pplane))/(oW*2);
  140.         varying float theT = (oH-ycomp(Pplane))/(oH*2);
  141.         theS = min(2,max(-1,theS));
  142.         theT = min(2,max(-1,theT));
  143.         if (nChans>1) {
  144.         ct = texture(boxTexture,theS,theT,
  145.                 "filter",    theFilterName,
  146.                 "blur",    boxTexBlur);
  147.         if (boxTexStr != 1) {
  148.             ct = (ct*boxTexStr)+(1-boxTexStr);
  149.         }
  150.         }
  151.         if ((nChans==1)||(nChans>3)) {
  152.         uniform float alphaChan;
  153.         if (nChans==1) {
  154.             alphaChan = 0;
  155.         } else {
  156.             alphaChan = 3;
  157.         }
  158.         ot = float texture(boxTexture[alphaChan],theS,theT,
  159.                 "filter",    theFilterName,
  160.                 "blur",    boxTexBlur);
  161.         }
  162.     }
  163.     }
  164. #pragma nolint
  165.     varying point ppC = transform(boxCoords,"world",Pplane);
  166.     varying point spw = transform("world",surfPt);
  167.     varying float pDist = length(ppC - spw); /* in "world" coords */
  168.     theDist = pDist;
  169.     theOpac = contrib * boxOpac * ot;
  170.     theColor = contrib * ct * boxColor / pow(pDist,decayExp);    /* premultiplied!!!!! */
  171. }
  172.  
  173. /**************************************************/
  174. /*** SHADOW ***************************************/
  175. /**************************************************/
  176.  
  177. float sbShadow(
  178.     uniform string    theName;
  179.     varying point    thePoint;
  180.     uniform ENUM    theFilt;
  181.     uniform float    theBlur,
  182.             theSamples,
  183.             theBias;
  184. ) {
  185.     uniform string filtTypes[2] = {"box", "gaussian"};
  186.     uniform string theFilterName = filtTypes[clamp(theFilt,0,1)];
  187.     varying float inShadow = shadow (theName,
  188.                 thePoint,
  189.                 "filter",    theFilterName,
  190.                 "blur",    theBlur,
  191.                 "samples",    theSamples,
  192.                 "bias",    theBias);
  193.     return(inShadow);
  194. }
  195.  
  196. /*****************************************************************/
  197. /*** MAIN SHADER *************************************************/
  198. /*****************************************************************/
  199.  
  200. light k3d_softboxes(
  201.     string    Comment = "";
  202. #ifdef BMRT
  203.     string    Tcl = "";
  204.     string    NodeName = "";
  205. #else /* !BMRT -- slc compiler doesn't like these definitions */
  206.     string    Tcl = "[addL]";    
  207.     string    NodeName = "$OBJNAME";
  208. #endif /* BMRT */
  209.     float    intensity    = 1;
  210.     color    lightcolor    = color (1,1,1);
  211.     float    decayRate    = 0;
  212.     float    meterDistance = 1;
  213.     string    meterSpace = "";
  214.     float    edgeRolloff = 0,
  215.             edgeAngle = 90,
  216.         edgeExp = 1;
  217.     string    boxCoord1    = "";
  218.     color    boxColor1    = color (1,1,1),    /* multiplied by lightcolor&intensity */
  219.         boxOpacity1    = color (1,1,1);
  220.     float    boxWidth1    = 1,
  221.         boxWEdge1    = -0.1,
  222.         boxHeight1    = 1,
  223.         boxHEdge1    = -0.1,
  224.         boxRoundness1    = 0;
  225.     string    boxTex1    = "";
  226.     float    boxFilter1    = TEX_GAUSSIAN_FILT,
  227.         boxTexStr1    = 1,
  228.         boxTexBlur1    = 0;
  229.     string    boxCoord2    = "";
  230.     color    boxColor2    = color (1,1,1),    /* multiplied by lightcolor&intensity */
  231.         boxOpacity2    = color (1,1,1);
  232.     float    boxWidth2    = 1,
  233.         boxWEdge2    = -0.1,
  234.         boxHeight2    = 1,
  235.         boxHEdge2    = -0.1,
  236.         boxRoundness2    = 0;
  237.     string    boxTex2    = "";
  238.     float    boxFilter2    = TEX_GAUSSIAN_FILT,
  239.         boxTexStr2    = 1,
  240.         boxTexBlur2    = 0;
  241.     string    boxCoord3    = "";
  242.     color    boxColor3    = color (1,1,1),    /* multiplied by lightcolor&intensity */
  243.         boxOpacity3    = color (1,1,1);
  244.     float    boxWidth3    = 1,
  245.         boxWEdge3    = -0.1,
  246.         boxHeight3    = 1,
  247.         boxHEdge3    = -0.1,
  248.         boxRoundness3    = 0;
  249.     string    boxTex3    = "";
  250.     float    boxFilter3    = TEX_GAUSSIAN_FILT,
  251.         boxTexStr3    = 1,
  252.         boxTexBlur3    = 0;
  253.     string    boxCoord4    = "";
  254.     color    boxColor4    = color (1,1,1),    /* multiplied by lightcolor&intensity */
  255.         boxOpacity4    = color (1,1,1);
  256.     float    boxWidth4    = 1,
  257.         boxWEdge4    = -0.1,
  258.         boxHeight4    = 1,
  259.         boxHEdge4    = -0.1,
  260.         boxRoundness4    = 0;
  261.     string    boxTex4    = "";
  262.     float    boxFilter4    = TEX_GAUSSIAN_FILT,
  263.         boxTexStr4    = 1,
  264.         boxTexBlur4    = 0;
  265.     uniform string    envTexName = "";
  266.     uniform float    EnvType = CLASSIC;
  267.     uniform float    MapType = CUBEFACE;
  268.     uniform float envTexIntensity    = 1;
  269.     uniform float envTexBlur    = 0;
  270.     uniform float envTexStr    = 1;
  271.     uniform float envTexFilter    = 0;
  272.     uniform string envReflSpace    = "";
  273.     color shadowcolor = 0;
  274.     float shadowintensity = 1;
  275.     string shadowname = "";
  276.     ENUM    shadowfilt = SHAD_BOX_FILT;
  277.     float   shadowblur = 0.01,
  278.         shadowbias = 0,
  279.         shadowsamples = 16;
  280.     string shadownameb = "";
  281.     ENUM    shadowfiltb = SHAD_BOX_FILT;
  282.     float   shadowblurb = 0.01,
  283.         shadowbiasb = 0,
  284.         shadowsamplesb = 16;
  285.     string shadownamec = "";
  286.     ENUM    shadowfiltc = SHAD_BOX_FILT;
  287.     float   shadowblurc = 0.01,
  288.         shadowbiasc = 0,
  289.         shadowsamplesc = 16;
  290.     string shadownamed = "";
  291.     ENUM    shadowfiltd = SHAD_BOX_FILT;
  292.     float   shadowblurd = 0.01,
  293.         shadowbiasd = 0,
  294.         shadowsamplesd = 16;
  295.     BOOL    NonDiffuse    = 1;
  296.     BOOL    NonSpecular    = 0;
  297.     BOOL    ObeyMateria    = 0;
  298.     BOOL    UseMateriaBlur    = 0;
  299.     output varying float __nondiffuse = 1;
  300.     output varying float __nonspecular = 0;
  301.     string    __category = "reflection";
  302.     output varying float __inShadow = 0;       
  303. ) {
  304.     uniform string rcsInfo = "$Id: k3d_softboxes.sl,v 1.1 2006/02/25 20:11:54 tshead Exp $";
  305.     uniform string filtTypes[4] = {"gaussian","box","radial-bspline","disk"};
  306.     uniform string theFilterName = filtTypes[clamp(envTexFilter,0,3)];
  307.     normal Nf = faceforward(normalize(N),I);
  308.     vector rv = reflect(I,Nf);
  309.     uniform float edgeLimVal = cos(radians(90-clamp(edgeAngle,0,90)));
  310.     uniform string theEnvSpace;
  311.     if (envReflSpace == "") {
  312.     theEnvSpace = "shader";
  313.     } else {
  314.     theEnvSpace = envReflSpace;
  315.     }
  316.     uniform float adjMeterDistance;
  317.     if (meterSpace == "") {
  318.     adjMeterDistance = meterDistance;
  319.     } else {
  320.     uniform point metP = transform(meterSpace,"shader",point (0,0,0));
  321.     adjMeterDistance = length(metP) + meterDistance;
  322.     }
  323.     uniform float adjIntensity = pow(adjMeterDistance,decayRate)*intensity;
  324.     varying float fullShad = 0;
  325.     uniform float materiaRefl = 1;
  326.     uniform float materiaBlur = 0;
  327.  
  328.     Cl = 0;
  329.     __nondiffuse = NonDiffuse;
  330.     __nonspecular = NonSpecular;
  331.     if (ObeyMateria > 0) {
  332.     if (surface("Reflectivity",materiaRefl) == 0) {
  333.         if (surface("abReflectivity",materiaRefl) == 0) {
  334.         if (surface("reflectivity",materiaRefl) == 0) {
  335.             if (surface("Kr",materiaRefl) == 0) {
  336.             materiaRefl = 0;
  337.             }
  338.         }
  339.         }
  340.     }
  341.     }
  342.     if (UseMateriaBlur > 0) {
  343.     if (surface("ReflectionMapBlur",materiaBlur) == 0) {
  344.         if (surface("abReflectionMapBlur",materiaBlur) == 0) {
  345.         if (surface("reflectionMapBlur",materiaBlur) == 0) {
  346.             materiaBlur = 0;
  347.         }
  348.         }
  349.     }
  350.     }
  351.     solar() {
  352.     if (materiaRefl != 0) {
  353.         uniform float boxCt=0;
  354.         varying float thisDist;
  355.         varying color thisOpac;
  356.         varying color thisColor;
  357.         varying float sortedDist[4];
  358.         varying color sortedOpac[4];
  359.         varying color sortedColor[4];
  360.         if (boxCoord1 != "") {
  361.         softbox_contrib2(Ps,rv,
  362. boxCoord1,boxTex1,boxTexStr1,
  363. (boxTexBlur1+materiaBlur),boxFilter1,
  364.             boxWidth1,boxHeight1,boxWEdge1,boxHEdge1,
  365.             boxRoundness1,boxColor1,boxOpacity1,decayRate,
  366.             thisDist,thisColor,thisOpac);
  367.         sortedDist[boxCt]  = thisDist;
  368.         sortedColor[boxCt] = thisColor;
  369.         sortedOpac[boxCt]  = thisOpac;
  370.         boxCt += 1;
  371.     }
  372.         if (boxCoord2 != "") {
  373.         softbox_contrib2(Ps,rv,
  374. boxCoord2,boxTex2,boxTexStr2,
  375. (boxTexBlur2+materiaBlur),boxFilter2,
  376.             boxWidth2,boxHeight2,boxWEdge2,boxHEdge2,
  377.             boxRoundness2,boxColor2,boxOpacity2,decayRate,
  378.             thisDist,thisColor,thisOpac);
  379.         sortedDist[boxCt]  = thisDist;
  380.         sortedColor[boxCt] = thisColor;
  381.         sortedOpac[boxCt]  = thisOpac;
  382.         boxCt += 1;
  383.     }
  384.         if (boxCoord3 != "") {
  385.         softbox_contrib2(Ps,rv,
  386. boxCoord3,boxTex3,boxTexStr3,
  387. (boxTexBlur3+materiaBlur),boxFilter3,
  388.             boxWidth3,boxHeight3,boxWEdge3,boxHEdge3,
  389.             boxRoundness3,boxColor3,boxOpacity3,decayRate,
  390.             thisDist,thisColor,thisOpac);
  391.         sortedDist[boxCt]  = thisDist;
  392.         sortedColor[boxCt] = thisColor;
  393.         sortedOpac[boxCt]  = thisOpac;
  394.         boxCt += 1;
  395.     }
  396.         if (boxCoord4 != "") {
  397.         softbox_contrib2(Ps,rv,
  398. boxCoord4,boxTex4,boxTexStr4,
  399. (boxTexBlur4+materiaBlur),boxFilter4,
  400.             boxWidth4,boxHeight4,boxWEdge4,boxHEdge4,
  401.             boxRoundness4,boxColor4,boxOpacity4,decayRate,
  402.             thisDist,thisColor,thisOpac);
  403.         sortedDist[boxCt]  = thisDist;
  404.         sortedColor[boxCt] = thisColor;
  405.         sortedOpac[boxCt]  = thisOpac;
  406.         boxCt += 1;
  407.     }
  408.         if (envTexName != "") {
  409. #pragma nolint
  410.         varying vector Rs = normalize (vtransform (theEnvSpace, normalize(-L)));
  411.         if (EnvType == SPHERICAL) {
  412. #pragma nolint
  413.             varying point PShd = transform (theEnvSpace, Ps);
  414.             varying float pl = vector(PShd).vector(PShd);
  415.             varying float pdotv = -vector(PShd).Rs;
  416.             Rs = vector( PShd + (pdotv + sqrt (abs (1 - pl + ((pdotv)*(pdotv)))))*Rs );
  417.         }
  418.         if( MapType == LATLONG ) {    /* latlong */
  419.             Rs = vector (-zcomp (Rs), xcomp (Rs), ycomp (Rs));
  420.         }
  421.         Cl = color environment (envTexName, Rs,
  422.                         "filter", theFilterName,
  423.                         "blur", (envTexBlur+materiaBlur));
  424.         if (envTexStr != 1) {
  425.             Cl = (Cl*envTexStr)+(1-envTexStr);
  426.         }
  427.         Cl *= envTexIntensity;
  428.         }
  429.         if (boxCt > 0) {
  430.         uniform float i, j, k;
  431.         for(k=0; k<(boxCt-1); k+=1) {
  432.             j = 0;
  433.             for(i=1; i<boxCt; i+=1) {
  434.             if (sortedDist[i]>sortedDist[j]) { /* farthest first */
  435.                 thisDist = sortedDist[j];
  436.                 thisOpac = sortedOpac[j];
  437.                 thisColor = sortedColor[j];
  438.                 sortedDist[j] = sortedDist[i];
  439.                 sortedOpac[j] = sortedOpac[i];
  440.                 sortedColor[j] = sortedColor[i];
  441.                 sortedDist[i] = thisDist;
  442.                 sortedOpac[i] = thisOpac;
  443.                 sortedColor[i] = thisColor;
  444.             }
  445.             j = j+1;
  446.             }
  447.         }
  448.         color ttc;
  449.         for(k=0; k<boxCt; k+=1) {
  450.             ttc = Cl;
  451.             Cl = sortedColor[k]+(Cl*(1-sortedOpac[k]));
  452.         }
  453.         }
  454.         /* Apply shadow mapped shadows */
  455.         varying vector Ln = normalize(L);
  456.         varying vector Nn = normalize(N);
  457.         varying vector In = normalize(I);
  458.         float theDot = Ln.Nn;
  459.         if (edgeRolloff > 0) {
  460.         float q;
  461.         q = In.Nn/edgeLimVal;
  462.         q = 1 - clamp(edgeRolloff*pow(clamp(abs(q),0,1),1/max(edgeExp,0.001)),0,1);
  463.         Cl = mix(color(0,0,0),Cl,q);
  464.         }
  465.         varying float shadowed;
  466.         if (shadowname != "") {
  467.         shadowed = sbShadow(shadowname,Ps,shadowfilt,
  468.             shadowblur,shadowsamples,shadowbias);
  469.         fullShad = max(fullShad,shadowed);
  470.     }
  471.         if (shadownameb != "") {
  472.         shadowed = sbShadow(shadownameb,Ps,shadowfiltb,
  473.             shadowblurb,shadowsamplesb,shadowbiasb);
  474.         fullShad = max(fullShad,shadowed);
  475.     }
  476.         if (shadownamec != "") {
  477.         shadowed = sbShadow(shadownamec,Ps,shadowfiltc,
  478.             shadowblurc,shadowsamplesc,shadowbiasc);
  479.         fullShad = max(fullShad,shadowed);
  480.     }
  481.         if (shadownamed != "") {
  482.         shadowed = sbShadow(shadownamed,Ps,shadowfiltd,
  483.             shadowblurd,shadowsamplesd,shadowbiasd);
  484.         fullShad = max(fullShad,shadowed);
  485.     }
  486.         __inShadow = fullShad; 
  487.     }
  488.     }
  489.     if (materiaRefl > 0) {
  490.     Cl *= (lightcolor * adjIntensity * materiaRefl);
  491.     Cl = mix(Cl, (shadowcolor*shadowintensity*adjIntensity), fullShad);
  492.     } else {
  493.     Cl = 0;
  494.     }
  495. }
  496.  
  497. /*************************************************/
  498. /***************************************** eof ***/
  499. /*************************************************/
  500.