More complex examples of flex Away3d (support for dragging and changing camera angles)

Source: Internet
Author: User
Tags addchild

1, the following is a more complex example that demonstrates the use of Away3d

(1) Add a floor to the center of the stage and place a square above the floor. There is a distance between the square and the floor.
(2) floors and squares use different textures of the skin.
(3) The default camera angle is tilted 15 degrees along the x axis.
(4) Hold down the left mouse button to drag the view can change the camera perspective, the mouse wheel can change the camera distance.
(5) The whole stage changes with the size of the window, always keep full-screen.
(6) Add anti-aliasing settings to make the edges of the object smoother.
2, the effect chart is as follows:


package{
Import Flash.display.Sprite;
Import flash.display.StageAlign;
Import Flash.display.StageScaleMode;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.geom.Vector3D;

Import Away3d.containers.View3D;
Import Away3d.controllers.HoverController;
Import Away3d.entities.Mesh;
Import away3d.materials.TextureMaterial;
Import Away3d.primitives.CubeGeometry;
Import Away3d.utils.Cast;

[SWF (Framerate=, backgroundcolor= "#FFFFFF")]
public class S3 extends Sprite {

private Var _view3d:view3d;
Private var cameracontroller:hovercontroller;//360 panoramic display camera controller

Object picture (used as a texture for squares)
[Embed (source= "Assets/snow_diffuse.png")]
private Var Blockclass:class;

Floor picture (used as a texture for the floor)
[Embed (source= "assets/road.jpg")]
private Var Floorclass:class;

Object Material
private Var blockmaterial:texturematerial;

Floor Material
private Var floormaterial:texturematerial;

private Var Lastpanangle:number;
private Var Lasttiltangle:number;
private Var Lastmousex:number;
private Var Lastmousey:number;
private Var Move:boolean;

Public Function S3 () {
Initengine ();
Initmaterials ();
Initobjects ();
Initlisteners ();
}

/**
* Initialize engine
*/
Private Function Initengine (): void
{
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;

Create a viewport
_view3d = new View3D ();
_view3d.antialias = 4; Set anti-aliasing level

Initializing the camera
Cameracontroller = new Hovercontroller (_view3d.camera);
/*cameracontroller.distance = 1000;
Cameracontroller.mintiltangle = 0;
Cameracontroller.maxtiltangle = 90;
Cameracontroller.panangle = 45;*/
Cameracontroller.tiltangle = 15;

AddChild (_VIEW3D);
}

/**
* Initialization of the material
*/
Private Function Initmaterials (): void
{
The texture of the floor
floormaterial = new Texturematerial (Cast.bitmaptexture (Floorclass));
The texture of the square
blockmaterial = new Texturematerial (Cast.bitmaptexture (Blockclass));
}

/**
* Initialize Object
*/
Private Function initobjects (): void
{
Create a square (floor) in a three-dimensional stage
var Cube1:mesh = new Mesh (new Cubegeometry (M, m), floormaterial);
_view3d.scene.addchild (CUBE1);

Create a square on a three-dimensional stage (a square on the floor)
var Cube2:mesh = new Mesh (new Cubegeometry, blockmaterial);
Cube2.position = new Vector3D (0,100,0);
_view3d.scene.addchild (CUBE2);
}

/**
* Initialization Monitoring
*/
Private Function Initlisteners (): void
{
AddEventListener (Event.enter_frame, _onenterframe);
Mouse Event Monitoring
Stage.addeventlistener (Mouseevent.mouse_down, OnMouseDown);
Stage.addeventlistener (mouseevent.mouse_up, onMouseUp);
Stage.addeventlistener (Mouseevent.mouse_wheel,onwheel);
Stage.addeventlistener (Event.resize, onResize);
OnResize ();
}

/**
* Render View
*/
Private Function _onenterframe (e:event): void
{

Moving perspective
if (move) {
Cameracontroller.panangle = 0.3 * (stage.mousex-lastmousex) + lastpanangle;
Cameracontroller.tiltangle = 0.3 * (Stage.mousey-lastmousey) + lasttiltangle;
}
Render View
_view3d.render ();
}

/**
* Use stage size always full screen
*/
Private Function onResize (event:event = null): void
{
_view3d.width = Stage.stagewidth;
_view3d.height = Stage.stageheight;
}

/**
* Mouse Wheel Event
*/
Private Function Onwheel (e:mouseevent): void
{
if (E.delta > 0) {
if (Cameracontroller.distance < 1000)
Cameracontroller.distance + 100;
}else{
if (Cameracontroller.distance > 600)
Cameracontroller.distance-= 100;
}
}

/**
* Mouse Down Event
*/
Private Function OnMouseDown (event:mouseevent): void
{
Lastpanangle = Cameracontroller.panangle;
Lasttiltangle = Cameracontroller.tiltangle;
Lastmousex = Stage.mousex;
Lastmousey = Stage.mousey;
move = true;
}

/**
* Mouse Bounce Event
*/
Private Function OnMouseUp (event:mouseevent): void
{
move = false;
}
}
}

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.