sky

by x10der
How to interact with this?
What inspired you?
Core logic explanation?
or requests for viewers?
If this field is left blank, description will be auto extracted from code comments.
♥1 | Line 87 | Modified 2011-03-03 00:01:36 | MIT License
play

ActionScript3 source code

/**
 * Copyright x10der ( http://wonderfl.net/user/x10der )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1HSi
 */

package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    import flash.geom.Point;

    [SWF(width="1000", height="500", frameRate="31", backgroundColor="#000000")]
    public class StarField extends Sprite {
            
        private const NUM_STARS:int = 500;
                
        public function StarField() {
            this.init();
        }
        
        private var _starFieldCanvas:BitmapData;
        private var _starFogCanvas:BitmapData;
        private var _starFieldBlur:BitmapData;
        
        private var _alphaFilter:ColorMatrixFilter;
        private const _blurFilter:BlurFilter = new BlurFilter(4, 4, 2);
        
        private var _starsArray:Array;
        private var _amplitudesMap:Array;
        
        private function init():void {
            
            var matrix:Array = new Array();
            matrix = matrix.concat([1, 0, 0, 0, 0]);
            matrix = matrix.concat([0, 1, 0, 0, 0]);
            matrix = matrix.concat([0, 0, 1, 0, 0]);
            matrix = matrix.concat([0, 0, 0, .2, 0]);
            this._alphaFilter = new ColorMatrixFilter(matrix);
            
            this._amplitudesMap = new Array();
            
            this._starFieldBlur = new BitmapData(super.stage.stageWidth, super.stage.stageHeight, true, 0);
            super.addChild(new Bitmap(this._starFieldBlur));
            
            this._starFieldCanvas = new BitmapData(super.stage.stageWidth, super.stage.stageHeight, true, 0);
            super.addChild(new Bitmap(this._starFieldCanvas));
            
            this._starFogCanvas = new BitmapData(super.stage.stageWidth, super.stage.stageHeight, true, 0);
            var tempDo:DisplayObject = super.addChild(new Bitmap(this._starFogCanvas));
            tempDo.blendMode = BlendMode.LAYER;
                        
            for (var k:int = 0; k<15; ++k) {
                this._amplitudesMap[k] = new Point(Math.random()*this._starFogCanvas.width, Math.random()*this._starFogCanvas.height);
            }
            
            this._starsArray = new Array();
            for (var i:int = 0; i<NUM_STARS; i++) {
                this._starsArray.push(new Star(Math.random()*super.stage.stageWidth, Math.random()*super.stage.stageHeight, Math.round(Math.random()*3), Math.round(Math.random()*2)));  
            }
            
            super.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame);
        }
        
        private function handler_enterFrame(event:Event):void {
            this._starFogCanvas.lock();
            for (var k:int = 0; k<15; ++k) {
                this._amplitudesMap[k].x = this._amplitudesMap[k].x-2;
            }
            this._starFogCanvas.perlinNoise(this._starFogCanvas.width*.5,this._starFogCanvas.height*.5,3,2,false,false,0,true,this._amplitudesMap);
            this._starFogCanvas.applyFilter(this._starFogCanvas, this._starFogCanvas.rect, new Point(), this._alphaFilter);
            this._starFogCanvas.unlock();
            this._starFieldCanvas.lock();
            
            this._starFieldCanvas.applyFilter(this._starFieldCanvas, this._starFieldCanvas.rect, new Point(), this._alphaFilter);
            
            for each (var temp:Star in this._starsArray) {
                var color:uint = 0xFFFFFFFF;
                var a:uint = color >>> 24;
                var r:uint = color >>> 16 & 0xFF;
                var g:uint = color >>>  8 & 0xFF;
                var b:uint = color & 0xFF;
                a = Math.round(a/temp.d);
                var j:int;                
                for(j = 0; j<temp.b; j++) {
                    this._starFieldBlur.setPixel32(temp.x+Math.random()*temp.b-(temp.b>>1), temp.y+Math.random()*temp.b-(temp.b>>1), a << 24 | r << 16 | g << 8 | b);
                }                
                this._starFieldCanvas.setPixel32(temp.x, temp.y, a << 24 | r << 16 | g << 8 | b);
            }
            this._starFieldBlur.applyFilter(this._starFieldBlur, this._starFieldBlur.rect, new Point(), this._blurFilter);
            this._starFieldCanvas.unlock();
        }
    }
}

internal class Star {
    public var x:Number;
    public var y:Number;
    public var d:Number;
    public var b:Number;
    public function Star(x:Number, y:Number, distance:Number, brightness:Number):void {
        this.x = x;
        this.y = y;
        this.d = distance;
        this.b = brightness;
    }
}