4

不幸的是,过滤器在 GPU 模式下不起作用(投影、发光)。我正在寻找机会在这种模式下将这些效果用于文本。我会欢迎任何建议。

4

3 回答 3

6

正如 Astraport 所提到的,每次使用bitmapData.draw().

如果您使用textField.getBounds来确定所需的 bitmapData 的大小,则生成的边界矩形将不包括由于过滤器而产生的额外大小(例如,DropShadowFilter 根据“距离”和“模糊”将文本框的一侧伸出某些像素')。为确保在绘制位图时包含过滤器,您还需要使用bitmapData.generateFilterRect()来获得正确大小的矩形。

代码片段(未经测试,但总体思路):

// Remember the transform matrix of the text field
var offset : Matrix = myTextField.transform.matrix.clone();
// Get the bounds of just the textfield (does not include filters)
var tfBounds : Rectangle = myTextField.getBounds( myTextField.parent );     
// Create a bitmapData that is used just to calculate the size of the filters
var tempBD : BitmpaData = new BitmapData( Math.ceil(tfBounds.width), Math.ceil(tfBounds.height) );
// Make a copy of the textField bounds. We'll adjust this with the filters
var finalBounds : rectangle = tfBounds.clone();
// Step through each filter in the textField and adjust our bounds to include them all
var filterBounds : rectangle;
for each (var filter : BitmapFilter in myTextField.filters) {
    filterBounds = tempBD.generateFilterRect( tfBounds, filter );
    finalBounds.left = Math.min( finalBounds.left, filterBounds.left );
    finalBounds.right = Math.max( finalBounds.right, filterBounds.right );
    finalBounds.top = Math.min( finalBounds.top, filterBounds.top );
    finalBounds.bottom = Math.max( finalBounds.bottom, filterBounds.bottom );
}

// Now draw the textfield to a new bitmpaData
var textFieldBD : BitmpaData = new BitmapData( Math.ceil(finalBounds.width), math.ceil(finalBounds.height) );
offset.tx = -finalBounds.x;
offset.ty = -finalBounds.y;
textFieldBD.draw( myTextField.parent, offset, myTextField.transform.colorTransform );

// Create a bitmap and add the bitmap data. Note: normally you would create a
// bitmap once and just update the bitmpaData
var bitmap : Bitmap = new Bitmap();
myTextField.parent.addChild( bitmap );

// Position the bitmap in same place as textField
bitmap.bitmapData = textFieldBD;
bitmap.x = myTextField.x - finalBounds.x;
bitmap.y = myTextField.y - finalBounds.y;
myTextField.visible = false;
于 2013-05-29T09:58:40.903 回答
2

以下是如何将 ANY 转换DisplayObjectBitmap- 用于在 AIR GPU mobile 中“恢复”滤镜效果rendermode。这是Pixelthis的解决方案,已修复、优化和测试:

    // => 'bitmap' must belong to the same parent as 'obj'. 'obj' should be invisible.
    static public function Update(obj:DisplayObject, bitmap:Bitmap):void {
        //trace("CacheToBmp",obj.name);

        // Remember the transform matrix of the text field
        var offset:Matrix = obj.transform.matrix.clone();
        // Get the bounds of just the textfield (does not include filters)
        var bounds:Rectangle = obj.getBounds(obj);
        // Create a bitmapData that is used just to calculate the size of the filters
        var tempBD:BitmapData = new BitmapData( Math.ceil(bounds.width), Math.ceil(bounds.height), false );
        bounds.width = obj.width;
        bounds.height = obj.height;
        // Make a copy of the textField bounds. We'll adjust this with the filters
        var finalBounds:Rectangle = new Rectangle(0,0,bounds.width,bounds.height);

        // Step through each filter in the textField and adjust our bounds to include them all
        var filterBounds:Rectangle;
        for each (var filter:BitmapFilter in obj.filters) {
            filterBounds = tempBD.generateFilterRect( tempBD.rect, filter );
            finalBounds = finalBounds.union(filterBounds);
        }
        finalBounds.offset(bounds.x,bounds.y);
        finalBounds.x = Math.floor(finalBounds.x);
        finalBounds.y = Math.floor(finalBounds.y);
        finalBounds.width = Math.ceil(finalBounds.width);
        finalBounds.height = Math.ceil(finalBounds.height);

        // Now draw the textfield to a new bitmpaData
        var data:BitmapData = new BitmapData( finalBounds.width, finalBounds.height, false, 0 );
        offset.tx = -finalBounds.x;
        offset.ty = -finalBounds.y;
        data.drawWithQuality( obj, offset, obj.transform.colorTransform, obj.blendMode, null, true, StageQuality.HIGH );
        bitmap.bitmapData = data;

        // Position the bitmap in same place as 'obj'
        bitmap.x = obj.transform.matrix.tx + finalBounds.x;
        bitmap.y = obj.transform.matrix.ty + finalBounds.y;
    }
于 2013-12-12T15:21:41.847 回答
0

基本思想是正常应用过滤器,然后将显示对象绘制到位图数据并将位图添加到舞台。有关示例,请参见http://forums.adobe.com/message/3934192 。

如果您应用它的文本是静态的,它应该很容易做到,但是如果您想应用这个动态文本(例如,一个会经常变化的分数计数器,或者用户可编辑的文本)我想它可能开始变得烦人,但我不知道任何其他解决方案。

于 2012-09-13T13:55:35.130 回答