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

Dream Machine

Dream Machine

http://www.acceler8or.com/2012/09/brion-gysins-dream-machine-build-your-own-portal-to-inner-visions/
http://en.wikipedia.org/wiki/Dreamachine

@author Devon O.
Get Adobe Flash player
by devon_o 29 Nov 2013
/**
 * Copyright devon_o ( http://wonderfl.net/user/devon_o )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3upl
 */

package
{
    /**
     * Dream Machine
     * 
     * http://www.acceler8or.com/2012/09/brion-gysins-dream-machine-build-your-own-portal-to-inner-visions/
     * http://en.wikipedia.org/wiki/Dreamachine
         * 
     * @author Devon O.
     */
    
    import com.adobe.utils.AGALMiniAssembler;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.display3D.Context3D;
    import flash.display3D.Context3DProgramType;
    import flash.display3D.Context3DTextureFormat;
    import flash.display3D.Context3DVertexBufferFormat;
    import flash.display3D.IndexBuffer3D;
    import flash.display3D.Program3D;
    import flash.display3D.textures.Texture;
    import flash.display3D.VertexBuffer3D;
    import flash.events.Event;
    import flash.geom.Matrix3D;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    import flash.utils.getTimer;

    [SWF(width="465", height="465", frameRate="60", backgroundColor="#000000")]
    public class DreamMachine extends Sprite
    {
        public static const NUM_SAMPLES:int = 30;
        
        private var lightPos:Vector.<Number> = Vector.<Number>( [ .5, .5, 1, 1 ]);
        private var values1:Vector.<Number> = Vector.<Number>( [ 1, 1, 1, 1 ]);
        private var values2:Vector.<Number> = Vector.<Number>( [ 1, 1, 1, 1 ]);    
        
        private var context3d:Context3D;
        private var vertBuffer:VertexBuffer3D;
        private var indexBuffer:IndexBuffer3D; 
        private var program:Program3D;
        private var matrix:Matrix3D = new Matrix3D();
        private var textureData:BitmapData;
        private var texture:Texture;
        
        public function DreamMachine()
        {    
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);    
        }
        
        private function init(event:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            initStage();
            loadImage();
            
            addEventListener(Event.ENTER_FRAME, onTick);
        }
        
        
        private function loadImage():void {
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
            l.load(new URLRequest("http://assets.wonderfl.net/images/related_images/3/37/3715/37155de690e641dff16e2eaca4125e9d6ad7bca6"), new LoaderContext(true));
        }
        
        private function onImageLoad(event:Event = null):void
        {
            event.currentTarget.removeEventListener(Event.COMPLETE, onImageLoad);
            var l:Loader = (event.currentTarget as LoaderInfo).loader;
            this.textureData = (l.content as Bitmap).bitmapData;    
            stage.stage3Ds[0].addEventListener( Event.CONTEXT3D_CREATE, initStage3d );
            stage.stage3Ds[0].requestContext3D();
        }
        
        private function initStage():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
        }
        
        private function initStage3d(event:Event):void
        {
            this.context3d = stage.stage3Ds[0].context3D;            
            this.context3d.configureBackBuffer(stage.stageWidth, stage.stageHeight, 1, true);
            
            var vertices:Vector.<Number> = Vector.<Number>([
            //    x           y       z            u  v
                -1.0,       -1.0,     0,           0, 0, 
                -1.0,        1.0,     0,           0, 1,
                 1.0,        1.0,     0,           1, 1,
                 1.0,       -1.0,     0,           1, 0  ]);
                 
            
            this.vertBuffer = this.context3d.createVertexBuffer(4, 5);
            this.vertBuffer.uploadFromVector(vertices, 0, 4);

            this.indexBuffer = this.context3d.createIndexBuffer(6);            
            this.indexBuffer.uploadFromVector (Vector.<uint>([0, 1, 2, 2, 3, 0]), 0, 6);
            
            this.texture = this.context3d.createTexture(this.textureData.width, this.textureData.height, Context3DTextureFormat.BGRA, true);
            this.texture.uploadFromBitmapData(this.textureData);
            
            this.context3d.setVertexBufferAt(0, this.vertBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
            this.context3d.setVertexBufferAt(1, this.vertBuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
            
            generateProgram();
        }
        
        
        private function generateProgram():void
        {
            var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
            vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
                "m44 op, va0, vc0 \n" +
                "mov v0, va1"
            );
            
            var frag:String = "";

            frag += "mov ft6.xy, v0.xy \n";
            frag += "add ft6.x, ft6.x, fc2.w \n";
            frag += "sub ft0.xy, ft6.xy, fc0.xy \n";
            frag += "mul ft0.xy, ft0.xy, fc1.ww \n";
            frag += "tex ft1,  ft6.xy, fs0 <2d, wrap, linear, mipnone> \n";  
            frag += "mov ft2.x, fc0.w \n";
            frag += "mov ft4.xy, ft6.xy \n";
            
            for (var i:int = 0; i < NUM_SAMPLES; i++)
            {
                frag += "sub ft4.xy, ft4.xy, ft0.xy \n"; 
                frag += "tex ft3,  ft4.xy, fs0 <2d, wrap, linear, mipnone> \n";
                frag += "mul ft2.y, ft2.x, fc2.x \n";
                frag += "mul ft3.xyz, ft3.xyz, ft2.yyy \n";
                frag += "add ft1.xyz, ft1.xyz, ft3.xyz \n";
                frag += "mul ft2.x, ft2.x, fc2.y \n";
            }
            
            frag += "mul ft1.xyz, ft1.xyz, fc2.zzz \n";
            frag += "mov oc, ft1";

            var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
            fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT, frag);
            
            this.program = this.context3d.createProgram();
            this.program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
        }
        
        private function onTick(event:Event):void
        {
            if ( !this.context3d ) 
                return;
            
            this.context3d.clear ( 0, 0, 0, 1 );
            this.context3d.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, this.matrix, true);
            this.context3d.setTextureAt(0, this.texture);
            this.context3d.setProgram(this.program);
            
            // 78 rpm - the good ol days
            var t:Number = getTimer() / (1/1.3 * 1000);
            
            this.lightPos[0] = .5 + t;
            this.lightPos[1] = .5;
            
            this.values1[0] = NUM_SAMPLES;
            this.values1[1] = 3.5;
            this.values1[2] = NUM_SAMPLES * values1[1];
            this.values1[3] = 1 / values1[2];
            
            this.values2[0] = .1;
            this.values2[1] = .90;
            this.values2[2] = 2.5;
            this.values2[3] = t;
            
            this.context3d.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, this.lightPos, 1 );    
            this.context3d.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 1, this.values1,  1 );
            this.context3d.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 2, this.values2,  1 );
            
            this.context3d.drawTriangles(this.indexBuffer);
            this.context3d.present();
            
        }
    }
}