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

webcam reflection

I've rigged up my raytracer to keep track of when rays escape to infinity.
This movie uses that information to render the webcam in the reflections.
Get Adobe Flash player
by wh0 09 Oct 2011
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hocx
 */

package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.media.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    public class FlashTest extends Sprite {
        
        private static function ba(l:int, d:Array):ByteArray {
            var r:ByteArray = new ByteArray();
            for each (var w:int in d) r.writeInt(w);
            r.length = l;
            return r;
        }
        
        private function proxy(url:String):String {
            return 'http://www.gmodules.com/ig/proxy?url=' + encodeURIComponent(url) + '&max_age=1';
        }
        
        public function FlashTest() {
            Wonderfl.disable_capture();
            if (stage) {
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;
                Security.allowDomain('www.gmodules.com');
                var l:Loader = new Loader();
                l.load(new URLRequest(proxy(loaderInfo.url)));
                addChild(l);
                return;
            } else {
                Security.allowDomain('swf.wonderfl.net');
                addEventListener(Event.ADDED_TO_STAGE, init);
            }
        }
        
        private var lc:Loader;
        private var lr:Loader;
        private var ls:Loader;
        private var n:int = 3;
        private var v:Video;
        private var bd:BitmapData;
        private var sh:Shader;
        
        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            x = (stage.stageWidth - 640) >> 1;
            y = (stage.stageHeight - 480) >> 1;
            
            lc = new Loader();
            lc.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            lc.load(new URLRequest(proxy('http://inst.eecs.berkeley.edu/~cs184-bv/color.png')));
            
            lr = new Loader();
            lr.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            lr.load(new URLRequest(proxy('http://inst.eecs.berkeley.edu/~cs184-bv/reflection.png')));
            
            ls = new Loader();
            ls.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            ls.load(new URLRequest(proxy('http://inst.eecs.berkeley.edu/~cs184-bv/scale.png')));
            
            var c:Camera = Camera.getCamera();
            c.setMode(256, 256, 30);
            
            v = new Video(256, 256);
            v.attachCamera(c);
            sh = new Shader(ba(142, [-1593769472,810831,1970553711,1869767680,-1560279949,1919090851,17064815,1819243008,-1560148878,1701211237,1668573551,1845535491,74670945,1818558625,33816832,258241396,10551556,33558369,1717987694,1694511105,15794176,268567297,12648960,268435713,12648960,-1342164991,15794432,268447747,15794176,268632833,14811904,402665475,15794176,268501249,14811904,402653184]));
            bd = new BitmapData(256, 256, false, 0x000000);
            sh.data.src.input = bd;
            sh.data.affine.value = [-255, -255, 255, 255];
        }
        
        private function complete(e:Event):void {
            if (!--n) start();
        }
        
        private function start():void {
            sh.data.color.input = (lc.content as Bitmap).bitmapData;
            sh.data.reflection.input = (lr.content as Bitmap).bitmapData;
            sh.data.scale.input = (ls.content as Bitmap).bitmapData;
            addEventListener(Event.ENTER_FRAME, frame);
        }
        
        private function frame(e:Event):void {
            bd.draw(v);
            graphics.clear();
            graphics.beginShaderFill(sh);
            graphics.drawRect(0, 0, 640, 480);
            graphics.endFill();
        }
        
    }
}

/*
parameter "_OutCoord", float2, f0.rg, in
texture "src", t0
texture "color", t1
texture "reflection", t2
texture "scale", t3
parameter "dst", float4, f1, out

parameter "affine", float4, f2.rgba, in

; load reflection
texn f1, f0.rg, t2
; adjust reflection
mul f1.rg, f2.rg
add f1.rg, f2.ba
; sample environment
texn f1, f1.rg, t0
; sample scale
texn f3, f0.rg, t3
; scale reflection
mul f1.rgb, f3.rgb
; sample color
texn f3, f0.rg, t1
; add to reflection
add f1.rgb, f3.rgb
*/