Go to Chapter 2 3D line and filling (1) (as3.0)

Source: Internet
Author: User

Chapter 2 describes 3D, but only places an object in a 3D space and sets the size and position. Object reality
On or 2D. This is like in an old 3D game, we can move around an object or character, and these objects will
Come and face us. These objects or characters don't really turn around-they just look like this, because they
They are all 2D objects, which is the only angle of view we can see.
   In this chapter, we will create a 3D model in flash. Specifically, 3D points and lines are created and used,
Fill and 3D images. After learning this chapter, you can create various shapes in any 3D space and execute
Move and rotate rows.
Create points and lines
   It makes no sense to create a point in 3D without creating a line. In theory, a point
There is no dimension and it is invisible. At the beginning, we will also use the ball3d class to give a very small radius, as long
It's enough to see where it is. In this way, you only need to create several lines to connect these balls. It is quite simple. Me
We have done this before, but this time we need to add pivoting on the ball and put them into 3D space.
   You can set the instance of a vertex to black and 10 pixels wide. Create some vertex instances and add them
Rotate rows (as described at the end of the previous chapter) and draw some lines between points. Code and rotatexy. As in the previous chapter
Files are almost the same. The main difference lies in the last line of the onenterframe, from the first point to draw the line cyclically to the remaining
All vertices. At the same time, I also deleted the sortz method because there are no identical methods here. The following is lines3d1.
.

The running result is 16-1.


        Figure 16-1 3D point and line

    Actually, there is not much to be changed. If you want to convert to a three-dimensional 3D model, You have to discard all these black
Point. The first attempt is very simple. You only need to set the radius of each ball3d instance to 0 at creation, as shown below:
   VaR ball: ball3d = new ball3d (0 );

The running result is 16-2.

Figure 16-2 3D lines, points invisible
    Looking back at the previous code, At this time, we found that many places are redundant. For example, scalex and scaley use
And visible are useless for an invisible point. No visible or scalable objects. According to this
Clue: Let's take a deeper look at this general class inherited from the sprite class. Now it is actually only five variables.
Container: xpos, ypos, zpos, X, Y. Too much. A video has many features-sending events, drawing,
Include other sprite films and so on-and occupy a large amount of resources. Using a Sprite is like opening
Tank picked up a piece of bread in the city.
   If you only need to store variables, let's create a class that only stores these variables as attributes. Things
In fact, it is more meaningful to put perspective and coordinate rotation in this class than simply storing only a few attributes.Ladies and Gentlemen,
Ladies and gentlemen, now I will send this point3d class to you:

Package {
 Public class point3d {
 Public var FL: Number = 250;
 Private var vpx: Number = 0;
 Private var vpy: Number = 0;
 Private var CX: Number = 0;
 Private var Cy: Number = 0;
 Private var CZ: Number = 0;
 Public var X: Number = 0;
 Public var y: Number = 0;
 Public var Z: Number = 0;
Public Function point3d (X: Number = 0, Y: Number = 0, Z: Number = 0 ){
 This. x = X;
 This. Y = y;
 This. z = z;
}
Public Function setvanishingpoint (vpx: Number, vpy: Number): void {
 This. vpx = vpx;
 This. vpy = vpy;
}
Public Function setcenter (CX: Number, Cy: Number, CZ: Number = 0): void {
 This. Cx = Cx;
 This. Cy = Cy;
 This. cz = cz;
}
Public Function get screenx (): number {
 VaR scale: Number = fL/(FL + Z + cz );
 Return vpx + cx + x * scale;
}
Public Function get screeny (): number {
 VaR scale: Number = fL/(FL + Z + cz );
 Return vpy + Cy + y * scale;
}
Public Function rotatex (anglex: Number): void {
 VaR cosx: Number = math. Cos (anglex );
 VaR SiNx: Number = math. Sin (anglex );
 VaR Y1: Number = y * cosx-z * SiNx;
 VaR Z1: Number = z * cosx + y * SiNx;
 Y = Y1;
 Z = Z1;
   }
   Public Function rotatey (angley: Number): void {
     VaR cosy: Number = math. Cos (angley );
     VaR Siny: Number = math. Sin (angley );
     VaR X1: Number = x * cosy-z * Siny;
     VaR Z1: Number = z * cosy + x * Siny;
     X = x1;
     Z = Z1;
   }
   Public Function rotatez (anglez: Number): void {
     VaR cosz: Number = math. Cos (anglez );
     VaR sinz: Number = math. Sin (anglez );
     VaR X1: Number = x * cosz-y * sinz;
     VaR Y1: Number = y * cosz + x * sinz;
     X = x1;
     Y = Y1;
   }
 }
}
      Now, when using this class to create 3D points, you can input the X, Y, Z coordinates, or use the public x, y, z
Properties. Class contains a recognized FL property and the property used for storage and release loss points. You can set it using the setvanishingpoint () method. Three rotation methods can be used to rotate around the center of a certain axis.
You can also use the setcenter method to change the center point. You will see these applications immediately. Finally, it can also
All pivoting. When we have already rotated or set the point position, we only need to use screenx and screeny.
Obtain the position of the pivot point on the screen.
    This class will make the program in the next two chapters much simpler.
    Using this new class to re-compile the program for the above line rotation will be incredibly simple, as shown in lines3d2.:

Figure 16-3 square coordinates in 3D space
    Note that all Z values are the same. This is a square on a plane. Place a square on the top of the plane
The simple method is to make all the points on a certain axis have the same position (here I chose the Z axis) and define the square
The other two axes (X and Y ). The following code replaces the point randomly created by a loop:
Points [0] = new point3d (-100,-100,100 );
Points [1] = new point3d (100,-100,100 );
Points [1] = new point3d (100,100,100 );
Points [3] = new point3d (-100,100,100 );
We also need to manually set the disappear point for each vertex. In the loop, it is much simpler:
For (var I: uint = 0; I <numpoints; I ++ ){
 Points [I]. setvanishingpoint (vpx, vpy );
}
Because there are four points, we have to change numpoints to 4. The remaining code can still work normally,
We need to do more: draw a line connecting the last vertex to the first vertex to close the graph. Below is
For all the code, see square3d. As (running result 16-4 ):


Figure 16-4 3D rotated Square
    It's amazing-a rotating square! Now we should be able to create any plane chart. I usually
All of these points are marked on the lattice paper (16-5) in advance to help plot.


Figure 16-5 use square paper to mark all vertices
    This draft can be used to help create the required points:
Points [0] = new point3d (-150,-250,100 );
Points [1] = new point3d (150,-250,100 );
Points [2] = new point3d (150,-150,100 );
Points [3] = new point3d (-50,-150,100 );
Points [4] = new point3d (-50,-50,100 );
Points [5] = new point3d (50,-50,100 );
Points [6] = new point3d (50, 50,100 );
Points [7] = new point3d (-50, 50,100 );
Points [8] = new point3d (-50,150,100 );
Points [9] = new point3d (150,150,100 );
Points [10] = new point3d (150,250,100 );
Points [11] = new point3d (-1, 150,250,100 );
This completes the effect of rotating the letter E in spinninge. As, the running result is 16-6. Don't forget
Change numpoints to 12. Later, we will use points. length to dynamically change this value.

Figure 16-6 3D rotating Letter e

 

     After the execution, we noticed that when the letter E is rotated to us, some points will become very close and
3D examples will be inverted. You may first consider increasing the Z value of all vertices, but this actually changes the effect.
Worse. For example, set Z to 500. During rotation, Z will go from 500 to-500, and even make some points
Moving back to the observation point makes things worse. However, we can use the setcenter method
To the Z axis, as follows:
For (var I: uint = 0; I <numpoints; I ++ ){
 Points [I]. setvanishingpoint (vpx, vpy );
 Points [I]. setcenter (0, 0,200 );
}
Open the point3d class and we will see that the scale calculation method is:
    VaR scale: Number = fL/(FL + Z + cz );
In this way, the entire system is moved by 200 pixels, including the rotating system. The original Z value remains unchanged, only because the calculation
When the perspective value is higher, all objects are displayed before observation. Use other values to replace them.
Animation effect. Later, we will make this image motion on other axes.

(If You Want To reprint it, please specify the source at http://blog.sina.com.cn/jooi. Thank you)

Related Article

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.