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

forked from: what the hell is this?

Get Adobe Flash player
by yonatan 17 Nov 2011
/**
 * Copyright yonatan ( http://wonderfl.net/user/yonatan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/80D1
 */

// forked from codeonwort's what the hell is this?
// forked from codeonwort's grid making test
// forked from codeonwort's stage3D base
package {
    
    import flash.display.Sprite
    import flash.events.Event
    
    import flash.display3D.Context3D
    
    public class Stage3DBase extends Sprite {
        
        private var context:Context3D
        
        public function Stage3DBase() {
            // write as3 code here..
            stage ? init() : addEventListener("addedToStage", init)
        }
        
        private function init(e:Event = null):void {
            // never mind
            if(e) removeEventListener(e.type, arguments.callee)

            Wonderfl.disable_capture();
            // request context 3d
            stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, contextGained)
            stage.stage3Ds[0].requestContext3D("auto")
        }
        
        // when I request context3D by Stage3D::requestContext3D
        // or when device loss occurs this listener is called.
        // make this listener to handle both situations.
        private function contextGained(e:Event):void {
            context = e.target.context3D
            
            var study:StudyBase = new StudyExample
            study.init(this, context)
        }
        
    }
    
}

import flash.display.Stage
import flash.display.Sprite
import flash.display3D.Context3D
import flash.display3D.Program3D
import flash.display3D.IndexBuffer3D
import flash.display3D.VertexBuffer3D
import com.adobe.utils.AGALMiniAssembler
class StudyBase {
    
    protected var document:Sprite
    protected var stage:Stage
    protected var context:Context3D
    protected var asm:AGALMiniAssembler
    
    protected var hardwareAccelerated:Boolean
    
    protected var vertBuf:VertexBuffer3D
    protected var idxBuf:IndexBuffer3D
    protected var program:Program3D
    
    public function StudyBase() {
        asm = new AGALMiniAssembler
    }
    
    public function init(doc:Sprite, cont:Context3D):void {
        if(context) context.dispose()
        context = cont
        document = doc
        stage = document.stage
        
        hardwareAccelerated = context.driverInfo.toLowerCase().indexOf("software") == -1
        
        program = context.createProgram()
    }
    
    protected function makeVertBuf(verts:Vector.<Number>, data32PerVert:uint):void {
        vertBuf = context.createVertexBuffer(verts.length / data32PerVert, data32PerVert)
        vertBuf.uploadFromVector(verts, 0, verts.length / data32PerVert)
    }
    protected function makeIdxBuf(indices:Vector.<uint>):void {
        idxBuf = context.createIndexBuffer(indices.length)
        idxBuf.uploadFromVector(indices, 0, indices.length)
    }
    
}

import com.adobe.utils.*;
import flash.display.*;
import flash.geom.Vector3D
import flash.geom.Matrix3D;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.utils.ByteArray
class StudyExample extends StudyBase {
    
    private var indices:Vector.<uint> = new Vector.<uint>
    
    public override function init(doc:Sprite, cont:Context3D):void {
        super.init(doc, cont)
        context.configureBackBuffer(stage.stageWidth&~1, stage.stageHeight&~1, 4, false) // &~1 hack from http://wonderfl.net/c/juU4
        context.setCulling("none")
        
        var verts:Vector.<Number> = new Vector.<Number>
        
        const num_cols:uint = 100, num_rows:uint = 100
        const left:Number = -1, top:Number = -1
        const width:Number = 2, height:Number = 2
        
        var x:Number, y:Number, z:Number
        var rand_color:uint
        var r:Number, g:Number, b:Number
        var ia:uint, ib:uint, ic:uint, id:uint
        for(var i:int = 0 ; i <= num_cols ; i++){
            for(var j:int = 0 ; j <= num_rows ; j++){
                // vertex attributes
                rand_color = Math.random() * 0xffffff
                r = (rand_color & 0xff) / 255
                g = (rand_color >> 8 & 0xff) / 255
                b = (rand_color >> 16 & 0xff) / 255
                x = left + width * i/num_cols
                y = top + height * j/num_rows
                z = (r + g + b) / 30
                verts.push(x, y, z, r/2, g, b)
                
                // indices
                if( i != num_cols && j != num_rows){
                    ia = i * (num_rows + 1) + j
                    ib = ia + 1
                    ic = (i + 1) * (num_rows + 1) + j
                    id = ic + 1
                    indices.push(ia,ib,ic, ib,id,ic)
                }
            }
        }
        
        makeVertBuf(verts, 3 + 3)
        makeIdxBuf(indices)
        
        context.setVertexBufferAt(0, vertBuf, 0, "float3")
        context.setVertexBufferAt(1, vertBuf, 3, "float3")
        
        var vertShader:ByteArray = asm.assemble("vertex",
                                                "mov v0, va1\n" +
                                                "m44 op, va0, vc0");
        var fragShader:ByteArray = asm.assemble("fragment", "mov oc, v0")
        program.upload(vertShader, fragShader)
        context.setProgram(program)
        
        model.identity()
        model.appendScale(100, 100, 100)
        model.appendRotation(-50, Vector3D.X_AXIS)
      
        view.identity();
        view.appendTranslation(0, 0, -100.0);

        projection.identity();
        projection.perspectiveFieldOfViewRH(45.0,1,10.0,1000.0);

        stage.addEventListener("mouseMove", render)
        render()
    }
    
    private var _s:BitmapData; // for wonderfl's screen capture
    private var projection:PerspectiveMatrix3D = new PerspectiveMatrix3D(); // (extends Matrix3D, provides perspectiveFieldOfViewRH method)
    private var model:Matrix3D = new Matrix3D();
    private var view:Matrix3D = new Matrix3D();
    private var modelViewProjection:Matrix3D = new Matrix3D();

    private var prevX:Number = 0
    private function render(e:MouseEvent=null):void {
        if(e){
            var degrees:Number = (prevX - e.stageX) / 2
            prevX = e.stageX
            model.prependRotation(degrees, Vector3D.Z_AXIS)
        }

        modelViewProjection.copyFrom(model);
        modelViewProjection.append(view);
        modelViewProjection.append(projection);

        context.setProgramConstantsFromMatrix("vertex", 0, modelViewProjection, true); // true to transpose the matrix
        context.clear(0.5, 0.5, 0.5)
        context.drawTriangles(idxBuf, 0, indices.length / 3)
        // if(!_s){_s=new BitmapData(stage.stageWidth&~1,stage.stageHeight&~1,false,0);document.addChildAt(new Bitmap(_s),0);context.drawToBitmapData(_s)};
        context.present()
    }
    
}