topical media & game development

talk show tell print

graphic-player-10-pixel-filter-shaders-Spherize.pbk / pbk



  <languageVersion: 1.0;> // The languageVersion is required in PixelBender files. This was added in the 2nd pre-release, and MUST be declared on the first line (comments excluded)
  kernel Spherize
  <
      namespace: "popforge::ImageProcessing";
      vendor: "joa ebert";
      version: 1;
      description: "applies spherical displacement map to an image";
  >
  {
  
      input image4 src;
      output pixel4 result;
      
      parameter float refractionIndex
      <
          minValue: 0.2;
          maxValue: 1.0;
          defaultValue: (1.0 / 1.8);
          description: "refraction index of the sphere";
      >;
          
      parameter float radius
      <
          minValue: 1.0;
          maxValue: 375.0;
          defaultValue: 200.0;
          description:"radius of the sphere";
      >;
      
      parameter float2 center
      <
          minValue: float2(0.0,0.0);
          maxValue: float2(500.0,500.0);
          defaultValue: float2(275.0,200.0);
          description: "center of the sphere";
      >;
      
      void evaluatePixel()
      {
          float2 coord = outCoord();
          float2 dist = coord - center;
          float radius2 = radius * radius;
          float r2 = dist.x * dist.x + dist.y * dist.y; //maybe there is a function for this? remember its not |dist|
          
          // check if we actually want to displace or not
          if ( r2 > 0.0 && r2 < radius2 )
          {
              // distance from radius
              float z2 = radius2 - r2;
              float z = sqrt(z2);
              
              // refraction
              float xa = asin( dist.x / sqrt( dist.x * dist.x + z2 ) );
              float xb = xa - xa * refractionIndex;
              float ya = asin( dist.y / sqrt( dist.y * dist.y + z2 ) );
                          float yb = ya - ya * refractionIndex;
              
              // displace by refraction
                          coord.x -= z * tan( xb );
              coord.y -= z * tan( yb );
          }
          
          // sample with interpolation
          result = sampleLinear(src,coord);
      }
  }
  
  


(C) Æliens 20/2/2008

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.