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

AGAL box blur

this works at most boxBlurCount = 3 (line 223).
if boxBlurCount > 3, shader just dies. no error message appears and I don't know why it dies.
Get Adobe Flash player
by codeonwort 27 Oct 2011
  • Forked from codeonwort's AGAL gaussian blur
  • Diff: 93
  • Related works: 2
  • Talk

    9balls at 28 Oct 2011 11:02
    I think you have too much opcodes in fragmentShader(line 243) if boxBlurCount > 3 . boxBlurCount = 1................35 opcodes boxBlurCount = 2................92 boxBlurCount = 3................177 boxBlurCount = 4................290 AGAL limits: 200 opcodes per program. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display3D/Context3D.html
    codeonwort at 28 Oct 2011 11:14
    aha it were not 2000 opcodes :(

    Tags

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

// forked from codeonwort's AGAL 9-tab gaussian blur
// forked from codeonwort's AGAL gaussian blur
// forked from codeonwort's AGAL zoom blur
// forked from codeonwort's context3D creation problem
package {
    
    import flash.display.Sprite;
    import flash.events.Event
    import flash.display.Stage3D
    import flash.display3D.Context3D
    import flash.display3D.Context3DRenderMode
    
    public class FlashTest extends Sprite {

        public function FlashTest() {
            // write as3 code here..
            //Wonderfl.disable_capture()
            stage ? init() : addEventListener("addedToStage", init)
        }
        
        private function init(e:Event=null):void {
            removeEventListener("addedToStage", arguments.callee)
            
            var s3d:Stage3D = stage.stage3Ds[0]
            s3d.addEventListener(Event.CONTEXT3D_CREATE, initStage3D)
            s3d.requestContext3D(Context3DRenderMode.AUTO)
        }
        
        private function initStage3D(e:Event):void {
            var context:Context3D = e.target.context3D as Context3D
            new Study05_BoxBlur(this, context)
        }

    }
    
}
import flash.system.LoaderContext;
import flash.display.LoaderInfo;
import flash.display.DisplayObject;
import flash.text.TextField;
import flash.net.URLRequest;
import flash.display.Loader;

//package {
    import com.adobe.utils.AGALMiniAssembler;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display3D.Context3D;
    import flash.display3D.Context3DTriangleFace
    import flash.display3D.IndexBuffer3D;
    import flash.display3D.Program3D;
    import flash.display3D.VertexBuffer3D;
    import flash.events.Event
    import flash.geom.Matrix3D;
    /**
     * ...
     * @author codeonwort
     */
    internal class Study_Base 
    {
        
        protected var document:Sprite, stage:Stage, context:Context3D
        //protected var antiAlias:int = 4, enableDepthAndStencil:Boolean = false
        
        protected var worldTransform:Matrix3D
        
        protected var vertBuf:VertexBuffer3D
        protected var idxBuf:IndexBuffer3D
        
        protected var vertShaderAsm:AGALMiniAssembler
        protected var fragShaderAsm:AGALMiniAssembler
        protected var program:Program3D
        
        public function Study_Base(document:Sprite, context:Context3D) 
        {
            this.document = document
            stage = document.stage
            this.context = context
            
            stage.addEventListener("resize", resize)
            
            setupContext()
            readyResource()
            //render()
        }
        
        protected function resize(e:Event = null):void {
            setupContext()
        }
        
        protected function setupContext():void {
            context.configureBackBuffer(stage.stageWidth, stage.stageHeight, 4, false)
            context.setCulling(Context3DTriangleFace.BACK)
            program = context.createProgram()
            vertShaderAsm = new AGALMiniAssembler
            fragShaderAsm = new AGALMiniAssembler
            worldTransform = new Matrix3D
            worldTransform.identity()
        }
        
        protected function readyResource():void {
            //
        }
        
        protected function render():void {
            //
        }
        
        // 셰이더 코드에 \n + 붙이기 귀찮아서
        protected function unify(...commands):String {
            var str:String = ""
            for each(var cmd:String in commands) {
                str += cmd + "\n"
            }
            return str.substr(0, str.length - 1)
        }
        
    }

//}

//package {
    import flash.events.Event;
    import flash.utils.ByteArray;
    import flash.geom.Matrix;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite
    import flash.display3D.Context3D
    import flash.display3D.VertexBuffer3D;
    import flash.display3D.textures.Texture;
    
    import com.adobe.utils.AGALMiniAssembler
    import com.bit101.components.Slider
    /**
     * ...
     * @author codeonwort
     */
    internal class Study05_BoxBlur extends Study_Base
    {
        public function Study05_BoxBlur(document:Sprite, context:Context3D) 
        {
            super(document, context)
            var L:Loader = new Loader
            L.load(new URLRequest("http://assets.wonderfl.net/images/related_images/d/d9/d9b9/d9b9e27367920f3ef42cc14e1155ec0f18908b3b"), new LoaderContext(true))
            L.contentLoaderInfo.addEventListener("complete", load_complete)
            
            stage.addEventListener("keyDown", kd)
        }
        
        /////////////////////////////////////////////////////////////////////////////////////////////
        // for capture the screen
        private var bmp:Bitmap
        private function kd(e:Object):void {
            if(bmp){
                bmp.bitmapData.dispose()
                document.removeChild(bmp)
                bmp = null
            }else{
                var bd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xff0000)
                bmp = new Bitmap(bd)
                render()
                document.addChild(bmp)
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////
        
        private var fragShaderAgalcodes:Vector.<ByteArray>
        
        protected override function resize(e:Event = null):void {
            super.resize(e)
        }
        
        protected function load_complete(e:Event):void {
            // vertex stream
            var verts:Vector.<Number> = new Vector.<Number>
            verts.push( -1, -1, 0,
                        -1, 1, 0,
                        1, 1, 0,
                        1, -1, 0)
            vertBuf = context.createVertexBuffer(4, 3)
            vertBuf.uploadFromVector(verts, 0, 4)
            context.setVertexBufferAt(0, vertBuf, 0, "float3")
            
            // index stream
            var indices:Vector.<uint> = new Vector.<uint>
            indices.push(0, 1, 2,
                        2, 3, 0)
            idxBuf = context.createIndexBuffer(6)
            idxBuf.uploadFromVector(indices, 0, 6)
            
            // uv
            var uv:Vector.<Number> = new Vector.<Number>
            uv.push(0, 1,
                    0, 0,
                    1, 0,
                    1, 1)
            var uvBuf:VertexBuffer3D = context.createVertexBuffer(4, 2)
            uvBuf.uploadFromVector(uv, 0, 4)
            context.setVertexBufferAt(1, uvBuf, 0, "float2")
            
            // texture
            var info:LoaderInfo = e.target as LoaderInfo
            var img:Loader = info.loader
            var bd:BitmapData = new BitmapData(1024, 1024, false)
            var mat:Matrix = new Matrix
            mat.scale(bd.width / img.width, bd.height / img.height)
            try {
                bd.draw(img.content, mat)
            } catch(err:Error) {
                bd.perlinNoise(512, 512, 8, 12323, false, true)
            }
            var tex:Texture = context.createTexture(1024, 1024, "bgra", true)
            tex.uploadFromBitmapData(bd)
            context.setTextureAt(0, tex)
            
            // vertex shader 정점 셰이더
            var vertShaderSrc:String = unify("m44 op, va0, vc0", "mov v0, va1")
            vertShaderAsm.assemble("vertex", vertShaderSrc)
            context.setProgramConstantsFromMatrix("vertex", 0, worldTransform)
            
            function merge(opcode:String, coord:String):String {
                return unify(opcode + " ft2." + coord + ", ft2." + coord + ", fc0." + coord,
                             "tex ft3, ft2.xy, fs0 <2d,clamp,linear>",
                             "add ft1.xyz, ft1.xyz, ft3.xyz")
            }
            function boxblur(count:uint):String {
                var s:String = ""
                var i:int, j:int
                for(i = -count ; i <= count ; i++){
                    s = unify(s, "sub ft2.xy, ft0.xy, fc0.ww")
                    for(j = -count ; j < i ; j++) s = unify(s, "add ft2.y, ft2.y, fc0.y")
                    for(j = -count ; j <= count ; j++){
                        
                        s = unify(s, merge("add", "x"))
                    }
                }
                s = unify(s, "div ft1.xyz, ft1.xyz, fc0.zzz")
                return s
            }
            
            var boxBlurCount:uint = 3
            var fragShaderSrc:String = unify("mov ft0, v0", "tex ft1, ft0, fs0 <2d,clamp,linear>", boxblur(boxBlurCount), "mov oc, ft1")
            
            fragShaderAsm.assemble("fragment", fragShaderSrc)
            
            // render
            var const_register:Vector.<Number> = new Vector.<Number>
            const_register.push(1 / 465, 1 / 465, Math.pow(boxBlurCount * 2 + 1, 2), 1 / 465 * boxBlurCount)
            context.setProgramConstantsFromVector("fragment", 0, const_register)
            program.upload(vertShaderAsm.agalcode, fragShaderAsm.agalcode)
            context.setProgram(program)
            render()
        }
        
        protected override function render():void {
            context.clear(0, 0, 0)
            context.drawTriangles(idxBuf, 0, 2)
            if(bmp) context.drawToBitmapData(bmp.bitmapData)
            context.present()
        }
        
    }

//}
/*
// calculate combination
function C(n:uint, k:uint):uint {
    return factorial(n) / factorial(k) / factorial(n-k)
}
function factorial(n:uint):uint {
    if(n == 0) return 1
    var cnt:uint = n
    while(cnt --> 1) n *= cnt
    return n
}
*/