At the beginning lets create the simplest possible example. We start with creating static scene contains single box observed from some distance. Listing 1.1 contains full code.
Listing 1.1 First3DScene
package {
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.primitives.Box;
import alternativa.engine3d.core.Object3DContainer;
import alternativa.engine3d.core.View;
import alternativa.engine3d.core.Camera3D;
import flash.display.Sprite;
[SWF(backgroundColor=0xEEEADB,frameRate="0")]
public class First3DScene extends Sprite {
public function First3DScene() {
var camera : Camera3D = new Camera3D();
camera.view = new View(640, 480);
addChild(camera.view);
camera.y = -500;
camera.z = 400;
camera.rotationX = -130 * Math.PI / 180;
var container : Object3DContainer = new Object3DContainer();
container.addChild(camera);
var box : Box = new Box();
box.scaleX = 2;
var material : FillMaterial = new FillMaterial(0xdea200, 1, 1, 0x0);
box.setMaterialToAllFaces(material);
box.rotationZ = Math.PI / 6; // eq. 30 * Math.PI / 180
container.addChild(box);
camera.render();
}
}
}
1. Add the camera.
The Camera3D object is created as the first object. Camera is used to do a projection of 3D scene into the 2D picture which is actually displayed to the user (view). View creates a viewport where camera draws a 2D picture. By setting width and height of the view we decide a size of the window we look at our 3D world.
By default camera is located at (0,0,0) position. To get isometric-like view we need to move it a bit and set some rotation to face the camera towards the scene objects position.
camera.y = -500;
camera.z = 400;
camera.rotationX = -130 * Math.PI / 180;
Note that Alternativa3d defines angles in radians. Thus if we want rotate object with 45 degree we need to do extra calculation.Of course we can use directly appropriate values precomputed ourself ;).
45deg = Math.PI / 4 = ~0.78
90deg = Math.PI / 2 = ~1.57
180deg = Math.PI = ~3.14
2. Add objects to the scene
In the above example there is only one object displayed in 3D scene - the box. Default constructor for Box class assumes a cube with side size equals 100. Note there is no easy ways to change width, height and length of the box except using scaleX/scaleY/scaleZ properties. Another important issue is a material. In the very first approach material is a way the object's faces (sides) will be cover. In this example we choose covering faces with plain dark yellow colour and black line colour. After adding box to the scene root container and invoking rendering on camera object we should receive the following picture:

No comments:
Post a Comment