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

stage3d vertex problem

v0 = (-1, -1, 0)
v1 = (-1. 1. 0)
v2 = (1, 1, 0)
v3 = (1, -1, 0)
I rotated these vertices at 30 degrees arount X axis. so x components remain same. y and z components are in [-1, 1]. after that I added 1 to z components and multiplied z components by 0.5. then all xyz components are in range [-1, 1], [-1, 1], [0, 1], respectively. but half of image is clipped. what's the matter?
Get Adobe Flash player
by codeonwort 14 Feb 2012

    Talk

    spanvega at 14 Feb 2012 19:24
    give a try using transposedMatrix to true like context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, transform, true)
    codeonwort at 14 Feb 2012 20:16
    It works! But I thought context.setProgramConstantsFromMatrix() uploads the matrix row by row. vc0 = row[0] of transform vc1 = row[1] of transform vc2 = row[2] of transform vc3 = row[3] of transform Then isn't this correct? (v is vertex position) v.x = dot product of vc0 and v v.y = dot product of vc1 and v v.z = dot product of vc2 and v v.w = dot product of vc3 and v
    codeonwort at 15 Feb 2012 14:01
    I figured all out. What I described above is correct. AGAL's m44 operation is performed as follow: m44 dst, v, M (v is 4x1 vector, M is 4x4 matrix) dst <- M * v But flash.geom.Matrix3D performs this: (this time v is 1x4 vector, not 4x1) dst <- v * M flash.geom.Matrix3D class' appendXXX/prependXXX methods are implemented to support latter case. For example Matrix3D setup translation matrix as follow(a, b, c is amount of movement in x, y, z axis): 1 0 0 0 0 1 0 0 0 0 1 0 a b c 1 But to perform translation in stage3d, the matrix should be(just transpose of the matrix above): 1 0 0 a 0 1 0 b 0 0 1 c 0 0 0 1 That's the reason why forth parameter of context.setProgramConstantsFromMatrix() should be true when I use flash.geom.Matrix3D to make a transform matrix in stage3d.
    spanvega at 15 Feb 2012 17:38
    alternatively you can let transposedMatrix to false if you perform Matrix3D.transpose before sending it to context.setProgramConstantsFromMatrix () rawData before Matrix3D.transpose (); 1, 0, 0, 0, 0, -1, 6.1, 0, 0, -1.2, -0.5, 0, 0, 0, 0.5, 1 after 1, 0, 0, 0, 0, -1, -1.2, 0, 0, 6.1, -0.5, 0.5, 0, 0, 0, 1 it acts the way you mentionned above otherwise, why not using the &~1 trick to gain some more fps context.configureBackBuffer(stage.stageWidth&~1, stage.stageHeight&~1, 4, false)
    codeonwort at 15 Feb 2012 18:50
    I saw that &~1 somewhere but I cannot sure what effect it has exactly.
    yonatan at 17 Feb 2012 11:53
    The &~1 thing speeds up software rendering (maybe hardware too, I didn't try). Courtesy of 9re: http://wonderfl.net/c/juU4

    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/svGu
 */

package {
    
    import flash.display.Sprite
    import flash.events.Event
    
    public class Main extends Sprite {
        
        public function Main() {
            stage.scaleMode = "noScale"
            stage.align = "LT"
            
            stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initStage3D)
            stage.stage3Ds[0].requestContext3D()
        }
        
        private function initStage3D(e:Event):void {
            new Study01(this, e.target.context3D)
        }
        
    }
    
}
//package  {
    import com.adobe.utils.AGALMiniAssembler;
    import com.adobe.utils.PerspectiveMatrix3D;
    
    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 perspectiveTransform:PerspectiveMatrix3D
        
        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()
        }
        
        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()
            perspectiveTransform = new PerspectiveMatrix3D
            perspectiveTransform.perspectiveFieldOfViewLH(45, stage.stageWidth / stage.stageHeight, 0, 1000)
        }
        
        protected function readyResource():void {
            //
        }
        
        // 셰이더 코드에 \n + 붙이기 귀찮아서
        protected function unify(...commands):String {
            var str:String = ""
            for each(var cmd:String in commands) {
                str += cmd + "\n"
            }
            return str
        }
        
    }

//}

    import flash.display.Sprite;
    import flash.events.Event
    import flash.display3D.Context3D;
    import flash.geom.Matrix3D
    import flash.geom.Matrix
    import flash.display.BitmapData
    import flash.display3D.textures.Texture
    import flash.display3D.IndexBuffer3D
    import flash.display3D.VertexBuffer3D
    import flash.display3D.Context3DProgramType
    import flash.geom.Vector3D
    
    import com.adobe.utils.AGALMiniAssembler
    /**
     * 아 몰라
     * @author codeonwort
     */
    internal class Study01 extends Study_Base 
    {
        
        public function Study01(document:Sprite, context:Context3D) 
        {
            super(document, context)
        }
        
        protected override function readyResource():void {
            // 정점 버퍼
            var verts:Vector.<Number> = new Vector.<Number>
            var t:Number = 1
            verts.push( -t, -t, 0, 1,    0, 1, // x,y,z,w, u,v
                        -t, t, 0, 1,    0, 0,
                        t, t, 0, 1,        1, 0,
                        t , -t, 0, 1,    1, 1)
            vertBuf = context.createVertexBuffer(4, 6) // 6 성분을 한 묶음으로 4개
            vertBuf.uploadFromVector(verts, 0, 4)
            context.setVertexBufferAt(0, vertBuf, 0, 'float4')
            context.setVertexBufferAt(1, vertBuf, 4, 'float2')
            
            // 인덱스 버퍼
            var indices:Vector.<uint> = new Vector.<uint>
            indices.push(0, 1, 2,
                        2, 3, 0)
            idxBuf = context.createIndexBuffer(6)
            idxBuf.uploadFromVector(indices, 0, 6)
            
            // 텍스쳐
            var bd:BitmapData = new BitmapData(512, 512, true)
            bd.perlinNoise(256, 256, 8, 152323, false, true)
            var tex:Texture = context.createTexture(bd.width, bd.height, 'bgra', true)
            tex.uploadFromBitmapData(bd)
            context.setTextureAt(0, tex)
            
            // 정점 셰이더
            var vertShaderSrc:String =
<agal><![CDATA[
m44 op, va0, vc0
mov v0, va1]]></agal>
            vertShaderAsm.assemble(Context3DProgramType.VERTEX, vertShaderSrc)
            
            var frag_src:String = "tex oc, v0, fs0 <2d,clamp,linear>"
            fragShaderAsm.assemble(Context3DProgramType.FRAGMENT, frag_src)
            program.upload(vertShaderAsm.agalcode, fragShaderAsm.agalcode)
            context.setProgram(program)
            
            render()
            stage.addEventListener('mouseMove', render)
        }
        
        private var transform:Matrix3D = new Matrix3D
        private function render(e:Object = null):void {
            context.setCulling('none')
            
            transform.identity()
            transform.appendRotation(stage.mouseX/2, Vector3D.X_AXIS)
            transform.appendTranslation(0, 0, 1)
            transform.appendScale(1, 1, .5)
            /*
            var t:Number = 1;
            (function(px:Number, py:Number):Function {
            var v:Vector3D = transform.transformVector(new Vector3D( px, py, 0, 1))
            v.scaleBy(v.w)
            trace(v, v.w)
            return arguments.callee
            })(-t,-t)(-t,t)(t,-t)(t,t)
            trace("========================")
            */
            context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, transform, true)
            
            context.clear(0, 0, 0)
            context.drawTriangles(idxBuf, 0, 2)
            context.present()
        }
        
    }