Skip to main content
Inspiring
February 25, 2011
Question

using shader question

  • February 25, 2011
  • 2 replies
  • 667 views

I have a shader,it's role is change the color of picture,like follows:
<languageVersion : 1.0;>
kernel NewFilter
<   namespace : "Your Namespace";
    vendor : "Your Vendor";
    version : 1;
    description : "your description";
>
{
    input image4 src;
    output pixel4 dst;
    void
    evaluatePixel()
    {
        pixel4 px = sampleNearest(src,outCoord());
        px.rb=px.br;
        dst=px;
    }
}

Above code can run well under Pixel Bender,and can change the color of picture,then I export it as test.pbj.Then I call it under ActionScript3,the code is follows:
package{
import flash.display.Bitmap;
import flash.display.Shader;
import flash.display.Sprite;
import flash.filters.ShaderFilter;
import flash.utils.ByteArray;

[SWF(width=700,height=500,backgroundColor=0x000000)]

public class playTest extends Sprite{

[Embed(source='test.pbj',mimeType='application/octet-stream')]
private static const DesaturateKernel:Class;

[Embed(source='test.jpg')]
private static const BitmapClass:Class;

public function playTest(){
var bitmap:Bitmap=new BitmapClass() as Bitmap;
var byteArray:ByteArray=new DesaturateKernel() as ByteArray;
var shader:Shader=new Shader(byteArray);
var filter:ShaderFilter=new ShaderFilter(shader);
bitmap.filters=[filter];
addChild(bitmap);
}
}
}

When I run above code,I find the color of picture don't change! Why? Where is wrong?
Thanks

This topic has been closed for replies.

2 replies

Kenneth Kawamoto
Community Expert
Community Expert
February 25, 2011

Try this:

{
    input image4 src;
    output pixel4 dst;

    void
    evaluatePixel()
    {
        dst = sampleNearest(src,outCoord());
        pixel4 px4 = dst;
        px4.rb = dst.br;
        dst = px4;
    }
}

Inspiring
February 28, 2011

kennethkawamoto2 's answer is correct, but I don't understand why code1 is wrong and code2 is right? Thanks

code1:

   pixel4 px = sampleNearest(src,outCoord());
   px.rb=px.br;
   dst=px;

code2:

  dst = sampleNearest(src,outCoord());
  pixel4 px4 = dst;
  px4.rb = dst.br;
  dst = px4;

Kenneth Kawamoto
Community Expert
Community Expert
February 28, 2011

My theory initially was if you swizzle the Vector itself like that by the time you do vec4[2] = vec4[0] the value of vec4[0] is vec4[2]. So I used "temp" vector to get it working, as you can see. But this doesn't explain why you get your filter working in Pixel Bender. I think you'd better ask this in PB forum - Adobe team will probably answer for you (then let me know the answer )

Inspiring
February 25, 2011

Why do you try to cast to ByteArray? Try without casting:

var shader:Shader = new Shader(new DesaturateKernel() );