actionscript-3pixel-bender

In AS3, how to update a portion of a bitmap with a Pixelbender instead of the whole bitmap?


In pure AS3, I have a pixelbender and a large bitmap. The pixelbender is configurable with a distance parameter to affect only a small area of the bitmap. The problem is that the pixelbender is executing over the whole bitmap. What would be the best way to update only the effected region of the bitmap?

Given this config:

shader.data.image.input = referenceBitmap.bitmapData; // 300x200
shader.data.position = [150,100];
shader.data.distance = [20];

The following does not work:

new ShaderJob(shader, 
              bitmap.bitmapData.getPixels(
                  new Rectangle(particle.x -10, 
                            particle.y -10,
                            20,
                            20))).start();

I could make a temporary array to hold the computed values and then copy them back into the bitmapData array. Although I would like for the shader to update the bitmapData pixels directly and only on the affected area.

The following works, but the shader runs on the whole 300x200 bitmap:

new ShaderJob(shader, bitmap.bitmapData).start();

Any suggestions?

EDIT: a filter will not work as there are 3 input images


Solution

  • BitmapData allows you to use a function called applyfilter.

    Instead of trying to use a bitmapdata with a shaderJob you could alternately use the shader as a shaderFilter and apply the shaderfilter on the bitmapdata.

    shader = new Shader(fr.data);
    shaderFilter = new ShaderFilter();
    shaderFilter.shader = shader;
    

    Once you have your shaderFilter you could use applyFilter on your bitmapData. Something like :

    referenceBitmap.bitmapData.applyFilter(bitmap.bitmapData,new Rectangle(particle.x -10, 
                            particle.y -10,
                            20,
                            20),new Point(0,0),shaderFilter);
    

    Hope this helps.