Flex:3D Introduction

PS:此篇为转文,原文链接:http://www.cnblogs.com/yjmyzz/archive/2010/05/08/1730697.html

三维坐标系:右手法则坐标系

三维透视基本规则:
物体坐标在Z轴上越大(越远),则看起来越小,如果距离足够远,则物体消失于屏幕上的某个特定点(“消失点”)

技术上的主要处理:动态调整物体的scaleX与scaleY(同时因物体大小改变后,相应的x,y坐标值通常也会改变,所以x,y坐标也要做相应调整以符合透视规则),基本公式如下:
scale = fl/(fl+z)

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;

    public class Base3D extends Sprite
    {
        public function Base3D()
        {
            var ball:Ball = new Ball();
            addChild(ball);

            //观察点 相对于 消失点的坐标
            var xPos:Number    = 0;
            var yPos:Number    = 0;
            var zPos:Number    = 0;
            var fl:Number    = 250;//焦距
            //消失点
            var vpX:Number    = stage.stageWidth/2;
            var vpY:Number    = stage.stageHeight/2;

            addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
            stage.addEventListener(MouseEvent.MOUSE_WHEEL,MouseWheelHandler);

            //鼠标滚轮事件(注:必须让stage获取焦点时-即用鼠标在动画上点击一下,该事件才会触发,另外还要注意:嵌入网页时,浏览器也会响应鼠标滚轮)
            function MouseWheelHandler(e:MouseEvent):void
            {
                zPos += (e.delta*5);
            }

            function EnterFrameHandler(event:Event):void
            {
                if (zPos > -fl)
                {
                    ball.visible    = true;
                    xPos    = mouseX-vpX;
                    yPos    = mouseY-vpY;
                    var scale:Number    = fl / (fl + zPos);
                    ball.scaleX    = ball.scaleY    = scale;
                    ball.x    = vpX+xPos*scale;
                    ball.y    = vpY+yPos*scale;
                }
                else    ball.visible    = false;

                //辅助线
                graphics.clear();
                graphics.lineStyle(1,    0XCCCCCC);
                graphics.moveTo(vpX,    vpY);
                graphics.lineTo(vpX,    ball.y);
                graphics.moveTo(vpX,    vpY);
                graphics.lineTo(ball.x,    vpY);

                graphics.lineStyle(1,0X000FF,    0.5);
                graphics.moveTo(vpX,    vpY);
                graphics.lineTo(ball.x,    ball.y);

                graphics.lineStyle(1,0XFF0000,    0.5);
                graphics.moveTo(ball.x,    ball.y);
                graphics.lineTo(mouseX,    mouseY);
            }

            function KeyDownHandler(e:KeyboardEvent):void
            {
                if (e.keyCode == Keyboard.UP)
                    zPos    += 50;
                else if (e.keyCode == Keyboard.DOWN)
                    zPos    -= 50;
            }
        }
    }
}

3D View

此例中,"鼠标位置"充当"观察点"(即"人眼"位置),键盘↑↓键可调整小球在Z轴上的位置。移动鼠标,通过辅助线观察变化。

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;

    public class Base3D extends Sprite
    {
        private var ball:Ball;

        //相当于消失点的坐标
        private var xpos:Number=0;
        private var ypos:Number=0;
        private var zpos:Number=0;

        //x,y,z三轴上的速度分量
        private var vx:Number=0;
        private var vy:Number=0;
        private var vz:Number=0;

        private var friction:Number    = 0.98;
        private var fl:Number=250;

        //消失点
        private var vpX:Number    = stage.stageWidth/2;
        private var vpY:Number    = stage.stageHeight/2;

        public function Base3D()
        {
            init();
        }

        private function init():void
        {
            stage.align        = StageAlign.TOP_LEFT;
            stage.scaleMode    = StageScaleMode.NO_SCALE;
            ball = new Ball(20);
            addChild(ball);
            addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
        }

        private function EnterFrameHandler(event:Event):void
        {
            vpX = stage.stageWidth/2;
            vpY = stage.stageHeight/2;

            xpos    += vx;
            ypos    += vy;
            zpos    += vz;
            vx        *= friction;
            vy        *= friction;
            vz        *= friction;

            if (zpos>-fl) {
                var scale:Number = fl / (fl + zpos);
                ball.scaleX=ball.scaleY=scale;
                ball.x=vpX+xpos*scale;
                ball.y=vpY+ypos*scale;
                ball.visible=true;
            } else {
                ball.visible=false;
            }

            //辅助线
            graphics.clear();
            graphics.lineStyle(1,0xefefef);
            graphics.moveTo(0,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2-8);
            graphics.moveTo(stage.stageWidth,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2+8);

            graphics.moveTo(stage.stageWidth/2,0);
            graphics.lineTo(stage.stageWidth/2,stage.stageHeight);
            graphics.lineTo(stage.stageWidth/2-8,stage.stageHeight-15);
            graphics.moveTo(stage.stageWidth/2,stage.stageHeight);
            graphics.lineTo(stage.stageWidth/2+8,stage.stageHeight-15);
            graphics.lineStyle(1,0xdadada);
            graphics.moveTo(vpX,vpY);
            graphics.lineTo(ball.x,ball.y);

        }
        private function KeyDownHandler(e:KeyboardEvent):void
        {
            switch (e.keyCode)
            {
                case Keyboard.UP :
                    vy    -= 10;
                    break;
                case Keyboard.DOWN :
                    vy    += 10;
                    break;
                case Keyboard.LEFT :
                    vx    -= 10;
                    break;
                case Keyboard.RIGHT :
                    vx    += 10;
                    break;
                case Keyboard.SHIFT :
                    vz    += 5;
                    break;
                case Keyboard.CONTROL :
                    vz    -= 5;
                    break;
                default :
                    break;
            }
        }
    }
}

3D Motion

此例中,↑↓←→控制x,y轴方向速度,shift/ctrl控制z轴速度

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;

    public class Base3D extends Sprite
    {
        private var ball:Ball;
        private var xpos:Number=0;
        private var ypos:Number=0;
        private var zpos:Number=0;
        private var vx:Number    = Math.random()*12-6;
        private var vy:Number    = Math.random()*12-6;
        private var vz:Number    = Math.random()*12-6;
        private var fl:Number    = 250;

        //消失点
        private var vpX:Number    = stage.stageWidth/2;
        private var vpY:Number    = stage.stageHeight/2;

        //相对于消失点的六个边界面(上,下,左,右,前,后)
        private var top:Number        = -100;
        private var bottom:Number    = 100;
        private var left:Number        = -100;
        private var right:Number    = 100;
        private var front:Number    = 100;
        private var back:Number        = -100;

        public function Base3D()
        {
            init();
        }

        private function init():void
        {
            stage.align     = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            ball    = new Ball(15);
            addChild(ball);
            addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
        }

        private function EnterFrameHandler(event:Event):void {
            vpX = stage.stageWidth/2;
            vpY = stage.stageHeight/2; 

            xpos    += vx;
            ypos    += vy;
            zpos    += vz;
            var radius:Number    = ball.getRadius();

            //左右边界
            if (xpos+radius    > right)
            {
                xpos    =     right-radius;
                vx        *=     -1;
            }
            else if (xpos - radius < left)
            {
                xpos    =     left+radius;
                vx        *=    -1;
            }

            //上下边界
            if (ypos+radius > bottom)
            {
                ypos    =     bottom-radius;
                vy        *=    -1;
            }
            else if (ypos - radius < top)
            {
                ypos    =     top+radius;
                vy        *=     -1;
            }

            //前后边界
            if (zpos+radius > front)
            {
                zpos    =     front-radius;
                vz        *=    -1;
            }
            else if (zpos - radius < back)
            {
                zpos    =    back+radius;
                vz        *=    -1;
            }

            //换算成平面二维坐标及缩放比率
            if (zpos > -fl)
            {
                var scale:Number = fl / (fl + zpos);
                ball.scaleX=ball.scaleY=scale;
                ball.x=vpX+xpos*scale;
                ball.y=vpY+ypos*scale;
                ball.visible    = true;
            }
            else
                ball.visible=false;

            //辅助线
            graphics.clear();
            graphics.lineStyle(1,0xccccff);
            graphics.moveTo(0,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2-8);
            graphics.moveTo(stage.stageWidth,stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2+8);

            graphics.moveTo(0,stage.stageHeight);
            graphics.lineTo(stage.stageWidth,0);
            graphics.lineTo(stage.stageWidth-15,2);
            graphics.moveTo(stage.stageWidth,0);
            graphics.lineTo(stage.stageWidth-6,13);

            graphics.moveTo(stage.stageWidth/2,0);
            graphics.lineTo(stage.stageWidth/2,stage.stageHeight);
            graphics.lineTo(stage.stageWidth/2-8,stage.stageHeight-15);
            graphics.moveTo(stage.stageWidth/2,stage.stageHeight);
            graphics.lineTo(stage.stageWidth/2+8,stage.stageHeight-15);
            graphics.lineStyle(1,0xffccff);
            graphics.moveTo(vpX,vpY);
            graphics.lineTo(ball.x,ball.y);
        }
    }
}

3D Motion Rebound

时间: 2024-08-13 04:33:44

Flex:3D Introduction的相关文章

【转】【Flex】FLEX 学习网站分享

[转:http://hi.baidu.com/tanghecaiyu/item/d662fbd7f5fbe02c38f6f764 ] FLEX 学习网站分享 http://blog.minidx.com/flex核心开发技术:http://blog.csdn.net/mervyn_lee/archive/2008/10/07/3027039.aspxfl部落:http://www.fltribe.com/捕鱼者说http://www.cnblogs.com/fishert/category/85

FLEX各种特效集合

http://www.noupe.com/adobe/flex-developers-toolbox-free-components-themes-and-tutorials.html经典中的经典 http://demo.quietlyscheming.com/fisheye/TileExplorer.html (2D)鱼眼效果 http://www.efflex.org/EfflexExplorer.html堪称经典 http://mofeichen.javaeye.com/blog/4661

Flex相关案例及资源搜集

Flex一些例子: http://blog.minidx.com/ 上千个Flex例子,对于学习者来说是一个庞大的资源宝库. http://fleksray.org/Flex_skin.html http://www.scalenine.com/samples/themeSwapper/themeSwap.html http://www.scalenine.com/ http://code.google.com/p/openflux/ http://blogs.ilog.com/elixir/2

[转]Flash、Flex、AS3.0框架及类库资源收集之十全大补

原文地址:http://www.d5power.com/portal.php?mod=view&aid=27 APIs.Libs.Components1.as3ebaylibhttp://code.google.com/p/as3ebaylib/2.as3youtubelibhttp://code.google.com/p/as3youtubelib/3.as3flickrlibhttp://code.google.com/p/as3flickrlib/4.Yahoo ASTRA Flash C

A.Kaw矩阵代数初步 学习笔记: 1. Introduction

“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. 第1章课程讲义下载(PDF) Summary Matrix A matrix is a rectangular array of elements. Matrix $A$ is denoted by $$A = \begin{bmatrix}a_{11} & \cdots & a_{1n}\\ \vdots&

OpenCASCADE Expression Interpreter by Flex &amp; Bison

OpenCASCADE Expression Interpreter by Flex & Bison [email protected] Abstract. OpenCASCADE provide data structure of any expression, relation or function used in mathematics. Flex and Bison are tools for building programs that handle structured input

透过HT for Web 3D看动画Easing函数本质

http://www.hightopo.com/guide/guide/plugin/form/examples/example_easing.html 50年前的这个月诞生了BASIC这门计算机语言,回想起自己喜欢上图形界面这行,还得归功于当年在win98下用QBASIC照葫芦画瓢敲了一段绘制奥运五环的代码,当带色彩的奥运五环呈现在自己面前时我已知道自己这辈子要走的路了.在这个忘本逐新的年代不见多少媒体提及这影响了几代人的BASIC语言的50年庆了. 如今消费者对用户体验的高要求,以远不能以静

Lou&#39;s Pseudo 3d Page

Lou's Pseudo 3d Page   (C) 2013 Louis Gorenfeld, updated May 3, 2013 NEW: Important details on the segmented road system and some additional links NEW: An (optional) explanation of finding field-of-view for the 3d projection formula NEW: An analysis

基于HTML5实现的Heatmap热图3D应用

Heatmap热图通过众多数据点信息,汇聚成直观可视化颜色效果,热图已广泛被应用于气象预报.医疗成像.机房温度监控等行业,甚至应用于竞技体育领域的数据分析. 已有众多文章分享了生成Heatmap热图原理,可参考<How to make heat maps>和<How to make heat maps in Flex>,本文将介绍基于HTML5技术的实现方式,主要基于Cavans和WebGL这两种HTML5的2D和3D技术的应用,先上最终例子实现的界面效果和操作视频: 实现Heat