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

3d stuff

man, it's not obvious what the range of outputs for the vertex shader should be
Get Adobe Flash player
by wh0 02 Aug 2012
    Embed
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/yDfF
 */

// forked from wh0's AGAL feedback
package {
    import flash.display.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.geom.*;
    
    import com.adobe.utils.AGALMiniAssembler;
    public class FlashTest extends Sprite {

        private var stage3D:Stage3D;
        private var context3D:Context3D;
        private var indexBuffer3D:IndexBuffer3D;
        private var matrix3D:Matrix3D;

        public function FlashTest() {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function (e:UncaughtErrorEvent):void { Wonderfl.log(e.error); } );
            if (stage.stage3Ds.length == 0) throw 'no stage3D';
            
            // stage.frameRate = 60;
            
            stage3D = stage.stage3Ds[0];
            stage3D.addEventListener(Event.CONTEXT3D_CREATE, context3DCreate);
            stage3D.requestContext3D();
        }
        
        private function context3DCreate(e:Event):void {
            context3D = stage3D.context3D;
            context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0, false);
            prepareTestProgram();
            // addEventListener(Event.ENTER_FRAME, draw);
            draw(null);
        }
        
        private function prepareTestProgram():void {
            var vsa:AGALMiniAssembler = new AGALMiniAssembler();
            vsa.assemble(Context3DProgramType.VERTEX, <![CDATA[
m44 op, va0, vc0
mov v0, va0
            ]]>);
            var fsa:AGALMiniAssembler = new AGALMiniAssembler();
            fsa.assemble(Context3DProgramType.FRAGMENT, <![CDATA[
mov ft0, v0.x
neg ft0.z, v0.x
frc ft0.y, v0.z
mov oc, ft0
            ]]>);
            var p:Program3D = context3D.createProgram();
            p.upload(vsa.agalcode, fsa.agalcode);
            context3D.setProgram(p);
            
            var va:VertexBuffer3D = context3D.createVertexBuffer(4, 3);
            va.uploadFromVector(new <Number> [
            //   x   y   z
                -1, -1,  0,
                 1, -1,  0,
                -1, -1,  8,
                 1, -1,  8
            ], 0, 4);
            context3D.setVertexBufferAt(0, va, 0, Context3DVertexBufferFormat.FLOAT_3);
            
            matrix3D = new Matrix3D(new <Number> [
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 1, 1
            ]);
            matrix3D.transpose();
            
            indexBuffer3D = context3D.createIndexBuffer(6);
            indexBuffer3D.uploadFromVector(new <uint> [0, 1, 2, 1, 2, 3], 0, 6);
        }
        
        private function draw(e:Event):void {
            context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, matrix3D, true);
            context3D.clear();
            context3D.drawTriangles(indexBuffer3D);
            context3D.present();
        }

    }
}