I’d like to say hello and welcome to those of you reading my feeds via MXNA. In this tutorial, we are going to use Flash Player 10’s 3D capabilities to render a cube. The drawTriangles method added to the Graphics class is used to render 3D objects in FP10. Lets get started.
First up,create a new document class. Here we’re going to name it Cube3D. Open the Cube3D.as file and type in the following code.
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*; public class Cube3D extends Sprite
{
private var matrix3D:Matrix3D;
private var vertices:Vector.<Number>;
private var projectedVertices:Vector.<Number>;
private var UVData:Vector.<Number>;
private var indices:Vector.<int>;
private var cube:Sprite;
public function Cube3D()
{
matrix3D = new Matrix3D();
vertices = Vector.<Number>([50,-50,50,
50,-50,-50,
-50,-50,-50,
-50,-50,50,
50,50,50,
50,50,-50,
-50,50,-50,
-50,50,50
]);
projectedVertices = new Vector.<Number>;
UVData = new Vector.<Number>;
indices = Vector.<int>([3,1,0, 3,2,1, 5,7,4, 5,6,7, 1,4,0, 1,5,4, 2,5,1, 2,6,5, 3,6,2, 3,7,6, 7,0,4, 7,3,0]);
cube = new Sprite();
addChild(cube);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
cube.x = cube.y = 200;
matrix3D.appendRotation(1,Vector3D.X_AXIS);
matrix3D.appendRotation(1,Vector3D.Y_AXIS);
matrix3D.appendRotation(1,Vector3D.Z_AXIS);
Utils3D.projectVectors(matrix3D,vertices,projectedVertices,UVData);
cube.graphics.clear();
cube.graphics.beginFill(0x0066FF);
cube.graphics.lineStyle(1,0x0000FF,1);
cube.graphics.drawTriangles(projectedVertices, indices);
cube.graphics.endFill();
}
}
}
Now lets take a look at the code:
Now we can use the Vector class to store data instead of Arrays. Vectors are similar to arrays but allow you to store only one type of data. The vertices vector stores the xyz co-ordinates of the 8 vertices of the cube. In order to draw a 3D object, we use the drawTriangles method. The syntax for this method is
drawTriangles(vertices:Vector.<Number>, indices:Vector.<int> = null, uvtData:Vector.<Number> = null, culling:String = “none”)
For the indices parameter, we have to split the cube into triangles. A good way for that is to take 1 side of a cube, and split it into 2 triangles. Read the drawTriangles section of this post by senocular for better understanding of splitting an object into triangles. A section from the post:
As you can see, a square can be split up into 2 triangles.For the square, the indices are 0,1,2 and 1,2,3. Similarly using the same technique for the cube’s 6 sides, we can calculate the indices.
Look at the following code:
matrix3D.appendRotation(1,Vector3D.X_AXIS);
matrix3D.appendRotation(1,Vector3D.Y_AXIS);
matrix3D.appendRotation(1,Vector3D.Z_AXIS);
This is equivalent to rotating through the X,Y&Z axes by 1 degree. Hence this code is executed in every frame. Now take a look at the next line :
Utils3D.projectVectors(matrix3D,vertices,projectedVertices,UVData);
The syntax for the projectVectors method is projectVectors(m:Matrix3D, verts:Vector.<Number>, projectedVerts:Vector.<Number>, uvts:Vector.<Number>)
This method, using a projection Matrix3D object, projects a Vector of three-dimensional space coordinates ( verts ) to a Vector of two-dimensional space coordinates ( projectedVerts
). This method gives us the 2D vertices (projectedVertices) to use in the drawTriangles method.
Here is the final cube :
And with a little experimenting, here is a pyramid :
Feel free to drop a comment if you want to kknow anything.
Happy experimenting