topical media & game development

talk show tell print

graphic-flex-image-effects-05-source-squareGradient.pbk / pbk



  <languageVersion : 1.0;>
  
  kernel SquareGradient
  <
      namespace:      "com.27Bobs";
      vendor:         "Todd Yard";
      version:        1;
      description:    "Creates a square gradient.";
  >
  {
      parameter int rotation
      <
          minValue:       int(0);
          maxValue:       int(360);
          defaultValue:   int(0);
          description:    "The rotation of the gradient.";
      >;
  
      parameter float2 center
      <
          minValue:       float2(0.0,0.0);
          maxValue:       float2(1024.0,1024.0);
          defaultValue:   float2(256.0,256.0);
          description:    "The center of the gradient.";
      >;
  
      input image4 gradient;
      output pixel4 result;
  
      void
      evaluatePixel()
      {
      
          float2 coord = outCoord();
          float2 relativePos = coord - center;
          // find angle of current pixel coordinate relative to the center of the gradient
          float theta = atan(relativePos.y, relativePos.x);
          // adjust angle based on rotation parameter
          theta = degrees(theta) - float(rotation);
          // keep angle between 0 and 360
          if (theta < -360.0) {
              theta += 720.0;
          } else if (theta < 0.0) {
              theta += 360.0;
          } else if (theta > 360.0) {
              theta -= 360.0;
          }
          theta = clamp(theta, 0.0, 360.0);
          result = sampleNearest(gradient, float2(floor(theta), 1));
      }
      
  }
  


(C) Æliens 18/6/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.