Like an object in a two-dimensional flash (such as a video clip instance), it can also be used to locate, rotate, scale, and combine a three-dimensional object. This tutorial will also show you how to use your usual gradient package in 3D space to gradient your 3D objects.
Required knowledge
This tutorial is based on another away3d tutorial. If you are a cainiao, we recommend that you first take the basic part of this tutorial. Each example has a complete source file. Click the link to download the source code and study how the as code completes the task. Note that all these examples use a file: Cover.. This file prevents multiple Flash files from simultaneously opening and dragging your computer down. If you have examples of how to use those classes, please refer to this tutorial
We start with the simplest away3d class:
1. Package {import away3d. containers. view3d; import away3d. primitives. cube;
2. Import flash. display. Sprite;
3. [SWF (width = "500", Height = "400", framerate = "60", backgroundcolor = "# ffffff")]
4. Public class basic07_cubeextends sprite {public function basic07_cube (){
5. // create a viewport var view: view3d = new view3d ({X: 250, Y: 200 });
6. addchild (View); // create a sphere and put it on the 3D stage
7. var Cube: Cube = new cube (); view. Scene. addchild (cube); // render the view
8. View. Render ();}}}
As: basic07_cube.as
Download this file or create a file as a starting point for this tutorial. Read this tutorial and learn and do it.
Use your hand!
Since everything around you is 3D, you will take it for granted that moving objects in 3D space is a piece of cake. It is not easy to do this on the computer, but we can use our hands here. To help you figure out how to move and rotate a 3D object, extend your hand as shown in.
Imagine the thumb as the Y axis, the index finger as the Z axis, and the middle finger as the X axis, which makes everything easier to understand. (Simplify the complexity and contribute. Da Bao sees you every day !)
Note: away3d and all other 3D engines do not use inverted coordinate sequences like in flash. Away3d uses the cart coordinate sequence commonly used in mathematics and geometry. In this coordinate sequence, X is right, right, left, and Y is top, right, and bottom. Try to forget the concept of coordinates that were originally messed up by flash. Let's look at the concept of "dial the chaos!
Movement in 3D space
All 3D objects in away3d can be moved by setting the x/y attribute, as in 2D. Of course, you can also set the Z value in 3D.
1. cube. x = 100; cube. Y =-100; cube. z = 100;
The value of x/y/z can be positive or negative, and is relative to the center of the 3D world and the viewport. When you create a visual interface, you can determine the center of the 3D world. Usually, this center is the center of the first view.
You can use the moveTo () method and position attribute to easily set the x/y/z value.
1. cube. Position = new number3d (100,-100,300); cube. moveTo (100,-100,300 );
These two lines of code produce the same effect. The only difference between them is to provide a new number3d when using. position, and you only need to provide a simple value for moveTo.
If you want a 3D object to be directed to another 3D object or to a specific point in the scene, you need to use the lookat method:
1. cube. lookat (New number3d (0, 0, 0 ));
2. cube. lookat (sphere. position );
One line of code can be used to direct a 3D object to any point in the scene, while two lines of code can be used to direct a 3D object to another 3D object.
Away3d also provides some special methods for moving 3D objects:
1. cube. moveforward (x); cube. movebackward (x); cube. moveup (x); cube. movedown (X );
2. cube. moveleft (x); cube. moveright (X );
The functions of these six methods are the same as those described in their names. X units are moved. These methods move a 3D object along its own axis instead of its parent axis. Let's take a look at this example:
Movie: Click here to watch as: basic07_moveforward.as
If you are playing an Air Combat Game and want to make it easier for the spacecraft to move freely using the above six methods, setting the x/y/z value to generate a mobile game is simply self-abuse.
Rotate
You can use the rotation attribute to rotate a 3D object around its center point along any axis:
1. cube. rotationx = 45; cube. rotationy =-10; cube. Fig = 200;
Do you still remember to use this little trick? Placing your hand in front of your eyes will make it easier for you to understand how 3D objects rotate along the axis.
You can also set the rotation value more easily:
1. cube. rotateto (45, 45, 0)
Zoom
You can scale a 3D object along three axes.
1. cube. scalex = 2; cube. scaley =. 5; cube. scalez = 1;
If the scaling value is set to 1, the original size will be twice the original size when it is set to 2, and if it is set to 0.5, it will be half the original size. The scale () method is used to scale a 3D object proportionally:
1. cube. Scale (2 );
In this way, the cube is zoomed twice as large as the original one.
3D object combination
When we want to control multiple 3D objects at the same time, I think you will first combine these 3D objects in one piece. In away3d, "objectcontainer3d" is used to combine 3D objects. Adding 3D objects to "objectcontainer3d" is the same as adding them to scenes.
Create an objectcontainer3d object and add it to the scene. In the following example, we add a record (regularpolygon), cube, and ball. In this combination, each frame must be rotated in a half degree, and the cube turns in the opposite direction, so it seems to you that the cube always faces you.
Movie: Click here to watch as: basic07_group.as
Note that containers such as objectcontainer3d are invisible and actually exist. We can move them like normal 3D objects or perform other operations.
Gradient of 3D objects
Gradient 3D objects, the same as the gradient in flash. Install your usual gradient class and specify the attributes for the gradient. Everything is so easy.
The gradient attached to flash is not recommended because of its slow speed. I recommend the following gradient schemes:
· Tweener
· Tweenlite
· Tweenfilterlite
· Tweenmax
· Zigoengine (fuse)
· Twease
· Hydrotween
· Gtween
· Tweego
· Go3d
· Goasap
Verify the speed of these gradient engines by http://blog.greensock.com/tweening-speed-test. Various Engine Features and syntaxes are different. Before you start a project, determine the type you want most.
As I said at the beginning, the gradient in 3D is the same as that in 2D. You can select an attribute for gradient and set its initial value and value. In the following example, we use a turtle element that comes with away3d. Clicking a turtle will generate a random position and rotation gradient.
Movie: Click here to watch as: basic07_tweening.as
This example uses tweenlite (http://www.tweenlite.com/), so if you want to test it on your local machine, download and install tweenlite is required.
Advanced mobile
Occasionally, you will try to make some changes when loading a model from another 3D application that is not what you want. So do this: reset the rotation value. It is then applied to 3D objects.
1. cube. rotationy = 45; cube. applyrotations ();
Sometimes you want to change the center of an object so that the object can be rotated around another point. In the two-dimensional flash world, you can change the registration point. In 3D, this registration point is said to be an "axis point". You need to do this to change the axis point of a three-dimensional object:
1. cube. movepivot (120 );
The following example shows how these methods change the rotation of a cube.
Movie: Click here to watch as: basic07_apply.as
The movepivot method does not affect the geometry inside the model. If you 'd like to apply a transformation that also changes the mesh of the model, you need to use the applyposition method.
1. cube. applyposition (120 );
Applyposition is extremly handy for modeling or correction purposes or for instance when an object needs to be duplicated, and then showed with different offsets using movepivot. this enables you want to make to separate models with different offsets.
Roll, pitch, and Yaw
Roll, pitch, and yaw are also rotating 3D objects. They are the same as the six methods of cube. moveforward in front of them, all of which are for their own coordinates. The rotationx/y/z rotation is relative to the world coordinate.
1. cube. Roll (15); cube. Pitch (5); cube. Yaw (5 );
Copy the above Code and test it in any example of creating a 3D object named cube to see how they work.