actionscript-3 - 将着色器过滤器应用于 BitmapData 对象会忽略传递的矩形 - 如何正确应用着色器过滤器?

标签 actionscript-3 shader

我有以下代码,仅用于测试,因为这是我想解决的一种错误:

        _shader = new Shader(new TheShader() as ByteArray);
        _shader.data.width.value = [64.0];
        _shader.data.height.value = [64.0];
        _shaderFilter = new ShaderFilter(_shader);
        _sequence = new Vector.<BitmapData>();
        var smallBD:BitmapData;
        var i:int;
        _delta = new Point();
        var megabase:BitmapData = new TheBitmap().bitmapData;
        var _rect:Rectangle = new Rectangle(0, 0, 64, 64);
        for (i = 0; i < 64; i++) {
            smallBD = new BitmapData(64, 64, true, 0x00808080);
            //_rect.x = i;
            _rect.y = i;
            smallBD.applyFilter(megabase, _rect, _delta, _shaderFilter);
            _sequence.push(smallBD);
        }

然后我循环遍历 _sequence 以查看更改矩形是否确实执行了某些操作。如果 _shaderFilter 实际上是一个着色器过滤器,它什么都不做。使用任何内置 Flash 过滤器进行测试均按预期工作,但使用 ShaderFilter 有时工作就像提供的矩形是简单的 sourceBitmapData.rect,无论源位图是什么,有时它的行为就像没有传递数据,边界为位于奇怪的位置 - 位图大小为 512x384,传递给着色器的区域边缘显然位于 (256,192) 或位图的中心。到目前为止,我只能实现一种解决方法,即首先 copyPixels() 所需的区域,然后 applyFilter() 到位。有人可以证明这是一个错误而不是我做错了什么吗?

PS:我正在使用 FlashDevelop,项目目标是 Flash Player 10.3,我不知道 FP11 是否解决了这个问题。

最佳答案

好吧,遗憾的是我不能告诉你如何解决这个问题,但我可以确认这不是你的错!

问题似乎是 Flash 忽略了 sourceRect完全在使用自定义着色器时。起初我认为它可能将值传递给着色器中未记录的参数,但后来我注意到输出位图的每个像素都会改变,即使 sourceRect较小或 destPoint非零。似乎也没有 inCoord()匹配函数 outCoord() ,所以看起来这不是开发人员预期的用途!

我可以提供一个建议;而不是将 ROI 复制到新的 BitmapData对象,添加 float2 offset参数到您的着色器,并按此值移动所有像素查找。它将节省一些处理。

这是我用来确认行为的简化测试用例:

ShaderTest.as:

package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shader;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.filters.ShaderFilter;

    final public class ShaderTest extends Sprite {
        [Embed(source="test.pbj",mimeType="application/octet-stream")]
        private static const sCopy : Class;

        final private function R( x, y, w, h ) : Rectangle {
            return new Rectangle( x, y, w, h );
        }
        final public function ShaderTest( ) {
            super( );
            var
            s  : Shader = new Shader( new sCopy( ) ),
            f  : ShaderFilter = new ShaderFilter( s ),
            d1 : BitmapData = new BitmapData( 256, 256, false, 0 ),
            d2 : BitmapData = new BitmapData( 128, 128, false ),
            b1 : Bitmap = new Bitmap( d1 ),
            b2 : Bitmap = new Bitmap( d2 ),
            w  : Rectangle = R( 16, 16, 64, 64 );
            b2.x = 274;
            addChild( b1 );
            addChild( b2 );

            for( var i : int = 0; i < 8; ++ i ) {
                for( var j : int = 0; j < 8; ++ j ) {
                    d1.fillRect( R( i * 32 + 1, j * 32 + 1, 30, 30 ), (((i + j) & 1) * 0x00FF00) | (i << 21) | (j << 5) );
                }
            }
            d2.applyFilter( d1, w, new Point( 10, 10 ), f );
            d1.fillRect( R( w.x, w.y, 1, w.height ), 0xFF0000 );
            d1.fillRect( R( w.x, w.y, w.width, 1 ), 0xFF0000 );
            d1.fillRect( R( w.x, w.y + w.height - 1, w.width, 1 ), 0xFF0000 );
            d1.fillRect( R( w.x + w.width - 1, w.y, 1, w.height ), 0xFF0000 );
        }
    }
}

测试.pbk:
<languageVersion:1.0;>

kernel bugtest <namespace:"Me";vendor:"Me";version:1;>{
    input image4 src;
    output pixel4 dst;
    void evaluatePixel(){
        dst = sampleNearest(src,outCoord());
    }
}

输出:

Screenshot of output

(右侧的小方块使用着色器从大方块复制。红色框显示 sourceRectdestPoint 是 (10,10)。尽管有这些设置,它实际上渲染了整个位图)

关于actionscript-3 - 将着色器过滤器应用于 BitmapData 对象会忽略传递的矩形 - 如何正确应用着色器过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182199/

相关文章:

python - 使用 GLSL 的 OpenGL 阴影贴图

objective-c - 从 FLA 输出 SVG

flash - 如何在 Actionscript 3 中按字母顺序比较两个字符串

actionscript-3 - AS3 : is-not keyword?

c++ - 无法将 id 分配给 OpenGL 中的属性

opengl - dFdxFine 和 dFdxCoarse 的区别

actionscript-3 - .name 属性的 AS3 引用影片剪辑

actionscript-3 - 将 Sprite 绘制到 bitmapData 上而不进行填充

c++ - 没有任何 vbo 输入的顶点着色器输出

c++ - 只有第一个计算着色器数组元素出现更新