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

グロさを追求しきれなかったPerlinNoise その2(動かしてみた)

それぞれのオクターブ動かしてみたらどうなるだろうと思ったのですが、別段キモくはならなかったです・・・。
Get Adobe Flash player
by Hiiragi 05 Apr 2011
    Embed
package  
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.ConvolutionFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    
    /**
     * グロさを追求しきれなかったPerlinNoise その2
     * 
     * @author Hiiragi
     * 
     * 一応警告出してますが、大したことはないです。
     * 
     * それぞれのオクターブ動かしてみたらどうなるだろうと思ったのですが、別段キモくはならなかったです・・・。
     * 
     */
    [SWF(width = "465", height = "465", frameRate = "30", background = "0x0")]
    public class Organ extends Sprite
    {
        private var _perlinNoiseBmpd:BitmapData;
        private var _cf:ConvolutionFilter;
        
        private var _warningText:TextField;
        
        private var _perlinNoiseNumOctaves:uint;
        private var _perlinNoiseOffsets:Array;
        private var _perlinNoiseOffsetsVel:Array;
        
        private var _randomSeed:int;
        
        public function Organ() 
        {
            _perlinNoiseNumOctaves = 8;
            _perlinNoiseOffsets = [];
            _perlinNoiseOffsetsVel = [];
            
            for (var i:int = 0; i < _perlinNoiseNumOctaves; i++)
            {
                _perlinNoiseOffsets[i] = new Point();
                _perlinNoiseOffsetsVel[i] = new Velocity(4, 1);
            }
            
            if (stage) init();
            else this.addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            _perlinNoiseBmpd = new BitmapData(this.stage.stageWidth, this.stage.stageHeight, false);
            this.addChild(new Bitmap(_perlinNoiseBmpd));
            
            var mtxArray:Array = [0, 0, 0, 0, 0,
                                  0, -1.7, 0, 0, 0,
                                  0, 0, 1, 0, 0,
                                  0, 0, 0, 1.8, 0,
                                  0, 0, 0, 0, 0];
                                  
            _cf = new ConvolutionFilter(5, 5, mtxArray)
            
            _warningText = new TextField();
            _warningText.autoSize = TextFieldAutoSize.LEFT;
            _warningText.text = "一応、閲覧注意(Clickで描画)";
            _warningText.setTextFormat(new TextFormat(null, 25));
            _warningText.x = this.stage.stageWidth / 2 - _warningText.width / 2;
            _warningText.y = this.stage.stageHeight / 2 - _warningText.height / 2;
            
            this.addChild(_warningText);
            
            this.stage.addEventListener(MouseEvent.CLICK, onClickHandler);
        }
        
        private function onEnterFrameHandler(e:Event):void 
        {
            var len:int = _perlinNoiseNumOctaves;
            var pt:Point;
            var vel:Velocity;
            
            for (var i:int = 0; i < len; i++)
            {
                pt = _perlinNoiseOffsets[i];
                vel = _perlinNoiseOffsetsVel[i];
                
                pt.x += vel.vx;
                pt.y += vel.vy;                
            }
            
            
            _perlinNoiseBmpd.lock();
            
            _perlinNoiseBmpd.perlinNoise(_perlinNoiseBmpd.width * 5, _perlinNoiseBmpd.height * 5, 6, _randomSeed, true, false, 7, true, _perlinNoiseOffsets);
            
            _perlinNoiseBmpd.colorTransform(_perlinNoiseBmpd.rect, new ColorTransform(5, 2, 2));
            _perlinNoiseBmpd.applyFilter(_perlinNoiseBmpd, _perlinNoiseBmpd.rect, new Point(), _cf);

            _perlinNoiseBmpd.unlock();
            
        }
        
        private function onClickHandler(e:MouseEvent):void 
        {
            _randomSeed = Math.random() * int.MAX_VALUE;
            
            for (var i:int = 0; i < _perlinNoiseNumOctaves; i++) 
            {
                _perlinNoiseOffsetsVel[i].randomize();
            }
            
            if (_warningText.visible) _warningText.visible = false;
            
            if (!this.hasEventListener(Event.ENTER_FRAME))
            {
                this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
            }
            
        }
        
    }

}


class Velocity extends Object
{
    public var vx:Number;
    public var vy:Number;
    
    public var factor:Number;
    public var minNum:Number;
    
    public function Velocity(factor:Number = 10, minNum:Number = 5)
    {
        this.factor = factor;
        this.minNum = minNum;
        
        randomize();
    }
    
    public function randomize():void
    {
        vx = factor * Math.random() + minNum * getDirection();
        vy = factor * Math.random() + minNum * getDirection();
    }
    
    private function getDirection():Number
    {
        return (Math.random() > .5 ) ? 1 : -1;
    }
}