获取应用于Sprite中的Flash Sprite的滤镜的界限

csga3l58  于 2022-09-21  发布在  Apache
关注(0)|答案(4)|浏览(153)

我有一个带有Sprite元件的Flash库,这些元件是由设计时应用的滤镜的其他精灵组成的。我将这些符号嵌入到Flex应用程序中,如下所示:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed(source="Resources.swf", symbol="SquareContainer")]
            private var squareContainer_class:Class;

            private function log(msg:String):void {
                    output.text = output.text + "n" + msg;
            }
        ]]>
    </mx:Script>

    <mx:VBox horizontalAlign="center" width="100%" height="100%" >
        <mx:Image id="squareContainer" source="{squareContainer_class}"/>
        <mx:Button click="log(squareContainer.width + ', ' + squareContainer.height);"/>
        <mx:TextArea id="output" width="100%" height="100%" />
    </mx:VBox>

</mx:Application>

在本例中,SquareContainer元件的宽度为100px,高度为100px;但是,它包含一个带有发光和模糊滤镜的子子精灵,这会使精灵看起来明显大于100x100。因为我不能确定容器的组成,所以我不能使用BitmapData.GenerateFilterRect()来获取应用于嵌套精灵的过滤器。

我怎样才能得到精灵加上滤镜的大小?

lrpiutwd

lrpiutwd1#

哦,甜蜜的成功!(谢谢你的提示)我的一个朋友用一个很好的递归函数帮助解决了这个问题,该函数可以处理嵌套的精灵上可能存在的过滤器:

private function getDisplayObjectRectangle(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var final_rectangle:Rectangle = processDisplayObjectContainer(container, processFilters);

    // translate to local
    var local_point:Point = container.globalToLocal(new Point(final_rectangle.x, final_rectangle.y));
    final_rectangle = new Rectangle(local_point.x, local_point.y, final_rectangle.width, final_rectangle.height);       

    return final_rectangle;
}

private function processDisplayObjectContainer(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var result_rectangle:Rectangle = null;

    // Process if container exists
    if (container != null) {
        var index:int = 0;
        var displayObject:DisplayObject;

        // Process each child DisplayObject
        for(var childIndex:int = 0; childIndex < container.numChildren; childIndex++){
            displayObject = container.getChildAt(childIndex);

            //If we are recursing all children, we also get the rectangle of children within these children.
            if (displayObject is DisplayObjectContainer) {

                // Let's drill into the structure till we find the deepest DisplayObject
                var displayObject_rectangle:Rectangle = processDisplayObjectContainer(displayObject as DisplayObjectContainer, processFilters);

                // Now, stepping out, uniting the result creates a rectangle that surrounds siblings
                if (result_rectangle == null) { 
                    result_rectangle = displayObject_rectangle.clone(); 
                } else {
                    result_rectangle = result_rectangle.union(displayObject_rectangle);
                }                       
            }                       
        }

        // Get bounds of current container, at this point we're stepping out of the nested DisplayObjects
        var container_rectangle:Rectangle = container.getBounds(container.stage);

        if (result_rectangle == null) { 
            result_rectangle = container_rectangle.clone(); 
        } else {
            result_rectangle = result_rectangle.union(container_rectangle);
        }

        // Include all filters if requested and they exist
        if ((processFilters == true) && (container.filters.length > 0)) {
            var filterGenerater_rectangle:Rectangle = new Rectangle(0,0,result_rectangle.width, result_rectangle.height);
            var bmd:BitmapData = new BitmapData(result_rectangle.width, result_rectangle.height, true, 0x00000000);

            var filter_minimumX:Number = 0;
            var filter_minimumY:Number = 0;

            var filtersLength:int = container.filters.length;
            for (var filtersIndex:int = 0; filtersIndex < filtersLength; filtersIndex++) {                      
                var filter:BitmapFilter = container.filters[filtersIndex];

                var filter_rectangle:Rectangle = bmd.generateFilterRect(filterGenerater_rectangle, filter);

                filter_minimumX = filter_minimumX + filter_rectangle.x;
                filter_minimumY = filter_minimumY + filter_rectangle.y;

                filterGenerater_rectangle = filter_rectangle.clone();
                filterGenerater_rectangle.x = 0;
                filterGenerater_rectangle.y = 0;

                bmd = new BitmapData(filterGenerater_rectangle.width, filterGenerater_rectangle.height, true, 0x00000000);                      
            }

            // Reposition filter_rectangle back to global coordinates
            filter_rectangle.x = result_rectangle.x + filter_minimumX;
            filter_rectangle.y = result_rectangle.y + filter_minimumY;

            result_rectangle = filter_rectangle.clone();
        }               
    } else {
        throw new Error("No displayobject was passed as an argument");
    }

    return result_rectangle;
}
oxosxuxt

oxosxuxt2#

这里有一个略有不同的方法:只需将整个对象绘制到BitmapData中,然后计算位图非透明区域的边界。这种方法可能性能更好,尤其是在复杂对象上。

package lup.utils
{
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class BoundsHelper
    {
        private var _hBmd:BitmapData;
        private var _hBmdRect:Rectangle;
        private var _hMtr:Matrix;
        private var _hPoint:Point;

        private var _xMin:Number;
        private var _xMax:Number;
        private var _yMin:Number;
        private var _yMax:Number;

        /**
         * Specify maxFilteredObjWidth and maxFilteredObjHeight to match the maximum possible size
         * of a filtered object. Performance of the helper is inversely proportional to the product
         * of these values.
         *
         * @param maxFilteredObjWidth Maximum width of a filtered object.
         * @param maxFilteredObjHeight Maximum height of a filtered object.
         */
        public function BoundsHelper(maxFilteredObjWidth:Number = 500, maxFilteredObjHeight:Number = 500) {
            _hMtr = new Matrix();
            _hPoint = new Point();
            _hBmd = new BitmapData(maxFilteredObjWidth, maxFilteredObjHeight, true, 0);
            _hBmdRect = new Rectangle(0, 0, maxFilteredObjWidth, maxFilteredObjHeight);
        }

        /**
         * Calculates the boundary rectangle of an object relative to the given coordinate space.
         *
         * @param obj The object which bounds are to be determined.
         *
         * @param space The coordinate space relative to which the bounds should be represented.
         *        If you pass null or the object itself, then the bounds will be represented
         *        relative to the (untransformed) object.
         *
         * @param dst Destination rectangle to store the result in. If you pass null,
         *        new rectangle will be created and returned. Otherwise, the passed
         *        rectangle will be updated and returned.
         */
        public function getRealBounds(obj:DisplayObject, space:DisplayObject = null, dst:Rectangle = null):Rectangle {
            var tx:Number = (_hBmdRect.width  - obj.width ) / 2,
                ty:Number = (_hBmdRect.height - obj.height) / 2;

            // get transformation matrix that translates the object to the center of the bitmap
            _hMtr.identity();
            _hMtr.translate(tx, ty);

            // clear the bitmap so it will contain only pixels with zero alpha channel
            _hBmd.fillRect(_hBmdRect, 0);
            // draw the object; it will be drawn untransformed, except for translation
            // caused by _hMtr matrix
            _hBmd.draw(obj, _hMtr);

            // get the area which encloses all pixels with nonzero alpha channel (i.e. our object)
            var bnd:Rectangle = dst ? dst : new Rectangle(),
                selfBnd:Rectangle = _hBmd.getColorBoundsRect(0xFF000000, 0x00000000, false);

            // transform the area to eliminate the effect of _hMtr transformation; now we've obtained
            // the bounds of the object in its own coord. system (self bounds)
            selfBnd.offset(-tx, -ty);

            if (space && space !== obj) { // the dst coord space is different from the object's one
                // so we need to obtain transformation matrix from the object's coord space to the dst one
                var mObjToSpace:Matrix;

                if (space === obj.parent) {
                    // optimization
                    mObjToSpace = obj.transform.matrix;
                } else if (space == obj.stage) {
                    // optimization
                    mObjToSpace = obj.transform.concatenatedMatrix;
                } else {
                    // general case
                    var mStageToSpace:Matrix = space.transform.concatenatedMatrix; // space -> stage
                        mStageToSpace.invert();                                    // stage -> space
                    mObjToSpace = obj.transform.concatenatedMatrix;                // obj -> stage
                    mObjToSpace.concat(mStageToSpace);                             // obj -> space
                }

                // now we transform all four vertices of the boundary rectangle to the target space
                // and determine the bounds of this transformed shape

                _xMin =  Number.MAX_VALUE;
                _xMax = -Number.MAX_VALUE;
                _yMin =  Number.MAX_VALUE;
                _yMax = -Number.MAX_VALUE;

                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.x, selfBnd.y)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.right, selfBnd.y)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.x, selfBnd.bottom)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.right, selfBnd.bottom)));

                bnd.x = _xMin;
                bnd.y = _yMin;
                bnd.width = _xMax - _xMin;
                bnd.height = _yMax - _yMin;
            } else {
                // the dst coord space is the object's one, so we simply return the self bounds
                bnd.x = selfBnd.x;
                bnd.y = selfBnd.y;
                bnd.width = selfBnd.width;
                bnd.height = selfBnd.height;
            }

            return bnd;
        }

        private function getPoint(x:Number, y:Number):Point {
            _hPoint.x = x;
            _hPoint.y = y;
            return _hPoint;
        }

        private function expandBounds(p:Point):void {
            if (p.x < _xMin) {
                _xMin = p.x;
            }
            if (p.x > _xMax) {
                _xMax = p.x;
            }
            if (p.y < _yMin) {
                _yMin = p.y;
            }
            if (p.y > _yMax) {
                _yMax = p.y;
            }
        }
    }
}
2w3rbyxf

2w3rbyxf3#

我不确定使用getBound或getRect的普通方法是否可能做到这一点,因为这些方法只返回正常的100x100正方形。

我有几个建议给你。

首先,您可以动态应用筛选器。这样你就有了过滤器的数字,你可以编程计算出实际的大小。

你可以在FLA的电影剪辑中添加第二层,它的大小与原始正方形相同,外加所有滤镜。然后将该正方形的Alpha设置为零

第三,你可以在FLA中只有一个PNG,它包含了正方形和,比如说,它里面的光芒。

就我个人而言,我会选择中间选项,因为如果过滤器或原始正方形发生变化,它需要的维护量最少。

uqdfh47h

uqdfh47h4#

好吧,有好消息也有坏消息。坏消息是,真的没有一种有效的方法来“以正确的方式”做到这一点。好消息是,有一种足够的方法来近似它。

一般规则是宽度的大小大约是(filter.blurX1.5)+sprite.width,其中“Filter”是sprite.Filters数组中的滤镜。同样的道理也适用于肤色和身高。另一个一般规则是最小值x变成sprite.x-(filter.blurX1.5)/2;

这些数字都不是Adobe官方的,但工作在合理的误差范围内,可以允许您在此基础上创建一个BitmapData。

相关问题