【问题标题】:coloring with colormatrix in AS3 - please help!在 AS3 中使用 colormatrix 着色 - 请帮助!
【发布时间】:2011-07-07 15:09:01
【问题描述】:

我在尝试为太空游戏中飞来飞去的物体着色时遇到了一个大问题。

当我射击并击中它们时 - 受影响的敌人会眨眼。图形是预渲染的(即有一个旋转数组和函数,其中存储/计算对象的度数及其适当的旋转以获得更好的性能)。

所以 - 我的想法是通过额外着色的功能来增强旋转功能;但彩色和旋转对象应与正常旋转对象分开存储。为此,我制作了一个嵌套数组: 第一行有一个对象的 360 度旋转图形,第二行有一个旋转和彩色对象的 360 度图形。

问题:着色有效,但未旋转(始终为 0 度)。请帮帮我——我花了好几个小时才弄清楚为什么它不起作用,所以我放弃了。如果有人能找到问题,那就太酷了!非常感谢!

public function createRotationWithColorBlitArrayFromBD(sourceBitmapData:BitmapData, inc:int, offset:int = 0):Array
{
    trace("sourceBitmapData.width=" + sourceBitmapData.width);
    trace("sourceBitmapData.height=" + sourceBitmapData.height);
    tileList = [];
    tileListSec = [];
    levelArray = [];
    var rotation:int = offset; 

    while (rotation < (360 + offset))
    {
        var angleInRadians:Number = Math.PI * 2 * (rotation / 360);
        var rotationMatrix:Matrix = new Matrix();

        rotationMatrix.translate(-sourceBitmapData.width * .5, -sourceBitmapData.height * .5);
        rotationMatrix.rotate(angleInRadians);
        rotationMatrix.translate(sourceBitmapData.width * .5, sourceBitmapData.height * .5);

        var matrixImage:BitmapData = new BitmapData(sourceBitmapData.width, sourceBitmapData.height, true, 0x00000000);

        matrixImage.draw(sourceBitmapData, rotationMatrix);
        tileList.push(matrixImage.clone());

        var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter (
                                [1, 0, 0, 0, 0,
                                 0, 0, 0, 0, 0,
                                 0, 0, 0, 0, 0,
                                 0, 0, 0, 1, 0]);

        matrixImage.applyFilter(sourceBitmapData, sourceBitmapData.rect, point0, colorMatrix);

        tileListSec.push(matrixImage.clone());

        rotation += inc;

        matrixImage.dispose();
        matrixImage = null;
        rotationMatrix = null;
    }

    levelArray = [tileList, tileListSec];
    return(levelArray);
}

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    当您想将过滤器应用于源图像的旋转版本时,您似乎正在将过滤器应用于源图像。

    如果您仔细查看 BitmapData 的文档,应用过滤器函数会执行以下操作:

    获取源图像和过滤器对象并生成过滤后的图像。

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#applyFilter

    您的应用过滤器调用正在覆盖您在 draw() 函数中执行的矩阵转换,并使用源图像的新的、未旋转的副本。 applyFilter() 就像 draw 一样,除了它应用过滤器而不是转换矩阵。在这种情况下,您将转换矩阵应用于matrixImage.draw()(旋转),然后用matrixImage.applyFilter()(颜色)覆盖该数据。

    解决方案 一个快速的解决方案是进行以下更改:

    matrixImage.applyFilter(matrixImage, matrixImage.rect, point0, colorMatrix);
    

    这有 matrixImage 在它成为旋转图像之后将颜色过滤器应用于自身。

    顺便说一句:在这种情况下,我认为您不需要 matrixImage 对象的两个 clone()。仅由 applyFilter() 产生的最终对象就足够了,因为它是源和过滤器合二为一。但是您可能需要它们来处理您尚未发布的其他游戏逻辑。

    注意: 此解决方案的唯一问题是以下性能问题:(来自 BitmapData.applyFilter() 的 adobe 文档)

    如果 BitmapData 对象和指定为 sourceBitmapData 参数的对象是同一个对象,则应用程序使用该对象的临时副本来执行过滤器。为了获得最佳性能,请避免这种情况。

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#applyFilter

    实例化这些临时副本非常耗费资源,因此只需实例化一个缓冲区对象并重新使用它会快得多。如果您发现性能问题,最好创建第三个 BitmapData 对象作为源和过滤器之间的缓冲区。在这种情况下,您将拥有 3 个 bitmapData 对象,而不仅仅是 sourceBitmapDatamatrixImage

    【讨论】:

    • 非常感谢!你在这个答案上帮了我很多,让我的生活更轻松!非常感谢!:)
    • 嗨!很高兴为您提供帮助 :) 请将我的回答标记为“已接受”,以提高您在网站上的用户评分。
    • 感谢您的提示。我做到了。:)
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多