In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ActionScript 3.0 Image Effects p.325

Get Adobe Flash player
by Biz_R 15 Jun 2009
    Embed
package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.filters.BlurFilter;
    import flash.filters.DisplacementMapFilter;
    import flash.filters.DisplacementMapFilterMode;
    import flash.events.Event;
    import flash.geom.Point;
    
    [SWF(width="465",height="465",backgroundColor="0x111911")]
    
    public class Main extends Sprite
    {
        private static const TOTAL_TREES:uint=10;
        private static const TREE_DISPERSAL:uint=400;
        private static const TREE_SIZE:uint=40;
        private static const PLANES_IN_TREE:uint=4;
        
        private var _forest:Sprite;
        
        public function Main()
        {
            createForest();
            rotationX=15;
            addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);
        }
        
        private function createForest():void
        {
            _forest=new Sprite();
            _forest.x=stage.stageWidth/2;
            _forest.y=stage.stageHeight/2;
            var treeImage:BitmapData=createTreeImage();
            for(var i:uint=0; i<TOTAL_TREES; i++)
            {
                _forest.addChild(createTree(treeImage));
            }
            addChild(_forest);
        }
        
        private function createTreeImage():BitmapData
        {
            var point:Point=new Point();
            var treeImage:BitmapData=new BitmapData(TREE_SIZE,TREE_SIZE,true,0x00000000);
            var leaves:BitmapData = treeImage.clone();
            var noise:BitmapData = treeImage.clone();
            leaves.fillRect(leaves.rect, 0xFF005500);
            noise.perlinNoise(TREE_SIZE/10, TREE_SIZE/8, 2, 0, false, true);
            leaves.copyChannel(noise, noise.rect, point, BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
            
            var sprite:Sprite=new Sprite();
            sprite.graphics.beginBitmapFill(leaves);
            sprite.graphics.lineTo(TREE_SIZE/2, 10);
            sprite.graphics.lineTo(TREE_SIZE-10, TREE_SIZE-10);
            sprite.graphics.lineTo(10, TREE_SIZE-10);
            sprite.graphics.lineTo(TREE_SIZE/2, 10);
            sprite.graphics.endFill();
            treeImage.draw(sprite);
            
            var displaceFilter:DisplacementMapFilter=new DisplacementMapFilter(noise, new Point(), BitmapDataChannel.RED, BitmapDataChannel.RED, 0, -TREE_SIZE/4);
            var blurFilter:BlurFilter = new BlurFilter(4,4);
            treeImage.applyFilter(treeImage, treeImage.rect, point, displaceFilter);
            treeImage.applyFilter(treeImage, treeImage.rect, point, blurFilter);
            
            noise.dispose();
            leaves.dispose();
            
            return treeImage;
        }
        
        private function createTree(treeImage:BitmapData):Sprite
        {
            var tree:Sprite=new Sprite();
            for(var i:uint=0; i<PLANES_IN_TREE; i++)
            {
                tree.addChild(createTreePlane(treeImage,i));
            }
            tree.scaleY=1+Math.random()*0.8;
            tree.x=Math.random()*TREE_DISPERSAL-TREE_DISPERSAL/2;
            tree.z=Math.random()*TREE_DISPERSAL-TREE_DISPERSAL/2;
            return tree;
        }
        
        private function createTreePlane(treeImage:BitmapData, index:uint):Sprite
        {
            var bitmap:Bitmap=new Bitmap(treeImage);
            bitmap.x=-TREE_SIZE/2;
            bitmap.y=-TREE_SIZE/2;
            var treePlane:Sprite=new Sprite();
            treePlane.rotationY=index*(180/PLANES_IN_TREE);
            treePlane.addChild(bitmap);
            return treePlane;
        }
        
        private function onSpriteEnterFrame(e:Event):void
        {
            _forest.rotationY += 1;
        }
    }
}