[ActionScript 3.0] Perspective projection

Source: Internet
Author: User
Tags addchild

The following example shows how to use perspective projection to create 3D space. The example shows how to projectionCenter modify the vanishing point and change the perspective projection of space through properties. After this modification, the recalculation is forced focalLength and fieldOfView (and its associated 3D space warp).

This example:

    1. Create a sprite named center as a circle that contains a crosshair

    2. Property that assigns the coordinates of the sprite to the properties of the center root transform property perspectiveProjection projectionCenter

    3. Add an event listener for mouse events to invoke the modified projectionCenter handler so that center the object's location can be traced

    4. Create a four-fold style box that will form the walls of the perspective space in these four boxes

When you test this example, projectiondragger.swf drag the circle to a different location. The vanishing point moves with the circle until the circle is released. When moving the center of the projection away from the center of the stage, observe the stretching and distortions that occur in the box that surrounds the space.

1  Package2 {3     ImportFlash.display.Sprite;4     ImportFlash.display.Shape;5     ImportFlash.geom.Point;6     Importflash.events.*;7      Public classProjectiondraggerextendsSprite8     {9         Private varCenter:sprite;Ten         Private varBoxpanel:shape; One         Private varIndrag:Boolean=false; A  -          Public functionProjectiondragger ():void -         { the createboxes (); - Createcenter (); -         } -          Public functionCreatecenter ():void +         { -             varCenterradius:int = 20; +  ACenter =NewSprite; at  -             //Circle -Center.graphics.lineStyle (1,0x000099); -Center.graphics.beginFill (0xcccccc,0.5); -Center.graphics.drawCircle (0,0, Centerradius); - Center.graphics.endFill (); in             //Cross hairs; -Center.graphics.moveTo (0, Centerradius); toCenter.graphics.lineTo (0,-Centerradius); +Center.graphics.moveTo (centerradius,0); -Center.graphics.lineTo (-centerradius,0); thecenter.x = 175; *CENTER.Y = 175; $Center.z = 0;Panax Notoginseng              This. AddChild (center); -  the Center.addeventlistener (mouseevent.mouse_down,startdragprojectioncenter); + Center.addeventlistener (mouseevent.mouse_up,stopdragprojectioncenter); A Center.addeventlistener (mouseevent.mouse_move,dodragprojectioncenter); theRoot.transform.perspectiveProjection.projectionCenter =NewPoint (CENTER.X,CENTER.Y); +         } -          Public functionCreateboxes ():void $         { $             //Createboxpanel (); -             varBoxwidth:int = 50; -             varBoxheight:int = 50; the             varNumlayers:int = 12; -             varDepthperlayer:int = 50;Wuyi  the             //var boxvec:vector.<shape> = new vector.<shape> (numlayers); -              for(varI:int = 0; i < numlayers; i++) { Wu                  This. AddChild (Createbox (150,50, (numlayers-i) * DEPTHPERLAYER,BOXWIDTH,BOXHEIGHT,0XCCCCFF)); -                  This. AddChild (Createbox (50,150, (numlayers-i) * DEPTHPERLAYER,BOXWIDTH,BOXHEIGHT,0XFFCCCC)); About                  This. AddChild (Createbox (250,150, (numlayers-i) * DEPTHPERLAYER,BOXWIDTH,BOXHEIGHT,0XCCFFCC)); $                  This. AddChild (Createbox (150,250, (numlayers-i) * DEPTHPERLAYER,BOXWIDTH,BOXHEIGHT,0XDDDDDD)); -             } -         } -  A          Public functionCreatebox (xpos:int=0,ypos:int=0,zpos:int=100,w:int=50,h:int=50,color:int=0xdddddd): Shape +         { the             varBox:shape =NewShape; -Box.graphics.lineStyle (2,0x666666); $Box.graphics.beginFill (color,1.0); theBox.graphics.drawRect (0,0, w,h); the Box.graphics.endFill (); thebox.x =XPos; theBOX.Y =YPos; -Box.z =Zpos; in             returnbox; the         } the          Public functionStartdragprojectioncenter (e:event) About         { the Center.startdrag (); theIndrag =true; the         } +  -          Public functionDodragprojectioncenter (e:event) the         {Bayi             if(Indrag) { theRoot.transform.perspectiveProjection.projectionCenter =NewPoint (CENTER.X,CENTER.Y); the             } -         } -  the          Public functionStopdragprojectioncenter (e:event) the         { the Center.stopdrag (); theRoot.transform.perspectiveProjection.projectionCenter =NewPoint (CENTER.X,CENTER.Y); -Indrag =false; the         } the     } the}

Effect

[ActionScript 3.0] Perspective projection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.