3 Replies Latest reply on Mar 31, 2011 8:22 PM by fhfghgfh434

    Custom shader help

    fhfghgfh434 Level 1

      Hello  I've been trying to build a pixel bender filter for Flash player, but cant figure out the last part. The code supposed to display a black (or custom color) line, where a white area moves trough the line (like a pulsating line animation). My problem is, that i cant figure out, how to change the central part's color to black.

       

      heres my code until now:

      The .as file:

       

      package{

      import flash.display.Shader;

      import flash.display.Sprite;

      import flash.events.Event;

      import flash.filters.ShaderFilter;

      import flash.utils.ByteArray;

       

      public class test extends Sprite{

      [Embed(source = 'PBfilter2.pbj', mimeType = "application/octet-stream")]

      public var RB2:Class;

      private var shader2:Shader;

       

       

      private var bg:Sprite = new Sprite();

      public function test(){

      shader2 = new Shader(new RB2() as ByteArray);

      addChild(bg);

      bg.x = bg.y = 200

       

      addEventListener(Event.ENTER_FRAME,onFrame);

      graphics.clear();

      graphics.beginFill(0xff0000)

      graphics.drawRoundRect(1,1,400,400,20,20)

      }

      var cnt:Number = 0

      private function onFrame(e:Event):void{

       

      var lw:Number = mouseX-200;

      var lh:Number = mouseY-200;

      var high:Number = Math.abs(lh);

      if ( Math.abs(lw)>high ) high = Math.abs(lw);

      var len:Number = Math.sqrt(lw*lw+lh*lh)+100

      if (cnt> len) cnt =0

       

      shader2.data.center.value = [cnt-50];

      shader2.data.rotation.value = [Math.atan2( lh,lw )];

       

      shader2.data.attenuationDelta.value = [40];

      bg.graphics.clear();

      bg.graphics.lineStyle(2);

      bg.graphics.moveTo(0,0);

      bg.graphics.curveTo(mouseX-200,mouseY-200,mouseX-200,mouseY-200);

      bg.filters = [new ShaderFilter(shader2)]

      cnt = cnt+9;

       

      }

      }

      }

       

      the shader:

       

      <languageVersion : 1.0;>

       

      kernel SimplePointLight

      <   namespace : "noo";

          vendor : "me";

          version : 0;

          description : "aaaaaa";

      >

      {

          parameter float center<

              minValue: float(0.0);

              maxValue: float(4096.0);

              defaultValue: float(0.0);

          >;

       

          parameter float attenuationDelta<

                  minValue: float(0.0);

                  maxValue: float(500.0);

                  defaultValue: float(15.0);

          >;

       

          parameter float attenuationSpeed<

                  minValue: float(0.0);

                  maxValue: float(2.0);

                  defaultValue: float(1.0);

          >;

       

          parameter float attenuationDecay<

                  minValue: float(0.0);

                  maxValue: float(2.0);

                  defaultValue: float(1.0);

          >;

       

          parameter float rotation<

                  minValue: float(0.001);

                  maxValue: float(6.282);

                  defaultValue: float(0.01);

          >;

          #if !AIF_FLASH_TARGET

           parameter float2 size<

              minValue:float2(0,0);

              maxValue:float2(1024,1024);

              defaultValue:float2(1024, 512);

          >;

          region generated(){

              return region(float4(0, 0, size.x, size.y));

          }

          #endif

          parameter float bgred<

                  minValue: float(0.0);

                  maxValue: float(1);

                  defaultValue: float(0);

          >;

          parameter float bggreen<

                  minValue: float(0.0);

                  maxValue: float(1);

                  defaultValue: float(0);

          >;

          parameter float bgblue<

                  minValue: float(0.0);

                  maxValue: float(1);

                  defaultValue: float(0);

          >;

          output pixel4 dst;

          input image4 src;

       

          void evaluatePixel() {

              float2 out_pixel_coord = outCoord();

              float dist = cos(rotation)*( out_pixel_coord.x-tan(rotation)*(center/sin(rotation)-out_pixel_coord.y) );

              //??rosszfloat dist = sin(rotation)*( out_pixel_coord.x-center.x*(tan(rotation)-out_pixel_coord.y)/tan(rotation) );

              float att = pow(attenuationDelta/pow(abs(dist), attenuationDecay), attenuationSpeed);

             //  dst =  pixel4(bgred,bggreen,bgblue,0)+attenuation*pixel4(1,1,1,1);

              //dst =  attenuation*sampleNearest(src,outCoord());

       

       

              float4 colorAccumulator=float4(0.0, 0.0, 0.0, 0.0);

              float2 singlePixel=pixelSize(src);

              colorAccumulator +=sampleNearest(src, outCoord());

       

              colorAccumulator +=sampleNearest(src,outCoord()-(float2(0,1) * singlePixel ));

              colorAccumulator +=sampleNearest(src,outCoord()+(float2(0,1) * singlePixel ));

              colorAccumulator +=sampleNearest(src,outCoord()-(float2(0,1) * singlePixel*2.0 ));

              colorAccumulator +=sampleNearest(src,outCoord()+(float2(0,1) * singlePixel*2.0  ));

       

              colorAccumulator +=sampleNearest(src,outCoord()-(float2(1,0) * singlePixel ));

              colorAccumulator +=sampleNearest(src,outCoord()+(float2(1,0) * singlePixel ));

              colorAccumulator +=sampleNearest(src,outCoord()-(float2(1,0) * singlePixel*2.0 ));

              colorAccumulator +=sampleNearest(src,outCoord()+(float2(1,0) * singlePixel*2.0  ));

              //the part affected by att*colorAccumulator/5.0 should be white..

              dst=pixel4(0,0,0,0)+att*colorAccumulator/5.0+sampleNearest(src,outCoord());

          }

       

      }

       

      Thanks for help

        • 1. Re: Custom shader help
          unique_screenname_here Level 3

          Is the Pixel Bender filter doing what you want when run from the Pixel Bender Toolkit?

          • 2. Re: Custom shader help
            fhfghgfh434 Level 1

            > Is the Pixel Bender filter doing what you want when run from the Pixel Bender Toolkit?

             

            No, i dont really rely on the output, since i use it for a special case. I am using it to shade a line drawn in actionscript.

            I noticed, that the image i see in pixel bender differs a lot from what i see in flash player (for example alphas behave different)

            What i want to do is basically a combination of filters:

            i want a filter, that affects only a part of a line, the part it affects wil be thicker (this works OK), and has a different color (this does not work)  - here is one of my other unsuccesful approaches (substitute this in the place of dst=pixel4(0,0,0,0)+att*colorAccumulator/5.0+sampleNearest(src,outCoord());):

             

            float4 localGlow = att*colorAccumulator/5.0;
            float4 lineColor = sampleNearest(src,outCoord());
            dst=pixel4(1,1,1,localGlow.w)+pixel4(0,0,0,lineColor.w);

             

            if i change dst to:

            dst=pixel4(1,1,1,localGlow.w);

            The a part of the line gets colored white, while other parts stay opaque - this is OK.

             

            if i change dst to:

            dst=pixel4(0,0,0,lineColor.w);

            Then the whole line gets colored to black - this is what i want

             

            BUT if i add these two together the whole line will be white:

            dst=pixel4(1,1,1,localGlow.w)+pixel4(0,0,0,lineColor.w);

            why dont i see black color, at parts, where localGlow.w is almost 0?  (or is it higher than 1 everywhere? then why has my line transparent parts if i only use this part of the sum?)

             

            Thanks for help

            • 3. Re: Custom shader help
              fhfghgfh434 Level 1

              I could solve my problem, the changed line is:

               

               

               

               

               

                 float4 localGlow = att*colorAccumulator/5.0;

               

                  float4 lineColor = sampleNearest(src,outCoord());

               

                   dst=pixel4(localGlow.w-0.1,localGlow.w-0.1,localGlow.w,localGlow.w)+pixel4(0,0,0,lineColo r.w-0.3);

               

              but i ran into a new problem, ill post it as a separate thread