Creating the initial application

At this point, your authoring tool is configured and a new project has been created and configured to use the Away3D library and a version of the Flex SDK that targets Flash Player 10. It's now time to create your first Away3D application.

Away3D includes a large number of features, but there are a few basic classes that need to be set up before these features can be used. These classes are:

  • Scene3D, which represents the 3D space that holds the 3D objects that are to be displayed on the screen. This is commonly referred to as the scene.
  • Camera3D, which provides an object through which the scene is viewed. This is commonly referred to as the camera.
  • View3D, which is a container Sprite added to the stage that displays the scene as it is viewed by the camera. This is commonly referred to as the view.

Tip

The term 3D object used throughout this book is a general term that refers to any 3D model or primitive shape that is placed in the scene. 3D objects are represented by the Object3D class, from the away3d.core.base package, or classes that extend the Object3D class.

The Away3DTemplate class will include the basic logic required to create and initialize the scene, camera, and view objects. Let's take a look at the code that makes up the Away3DTemplate class.

package 
{

The scene, camera, and view, which are represented by classes called Scene3D, Camera3D, and View3D, are imported to make them available within the class.

  import away3d.cameras.Camera3D;
  import away3d.containers.Scene3D;
  import away3d.containers.View3D;

We also import the Flash Sprite class, which will be extended by the Away3DTemplate class, and the Flash Event class, used by the Flash event system.

  import flash.display.Sprite;
  import flash.events.Event;

By extending the Sprite class, the Away3DTemplate class can be added to the Flash stage just like any other visual element.

  public class Away3DTemplate extends Sprite
  {

We define properties for the scene, camera, and view, using the classes that were imported above.

    protected var scene:Scene3D;
    protected var camera:Camera3D;
    protected var view:View3D;

The constructor goes on to call a number of functions, each of which is used to set up an aspect of the 3D application.

    public function Away3DTemplate():void
    {

First user interface elements are created by the initUI() function.

      initUI();

Those properties that make up the core elements of the Away3D engine (that is, the scene, camera, and view) are initialized by calling the initEngine() function.

      initEngine();

The scene is populated by the initScene() function.

      initScene();

Event listeners are configured by the initListeners() function.

      initListeners();
    }

The initEngine() function is where the core elements of the Away3D engine are created.

    protected function initEngine():void
    {

The only object we need to explicitly create is the View3D class.

      view = new View3D();

While we could also manually create the scene and camera, these objects are created by the View3D class by default. Here we simply get a reference to these two objects for convenience.

Tip

By default, the camera is positioned 1,000 units towards the negative end of the Z-axis (so its position is (0, 0, -1000)), looking back at the scene origin. See the section Positioning objects in a 3D scene for more information on positioning within a 3D scene.

      scene = view.scene;
      camera = view.camera;

Just like our own Away3DTemplate class, the View3D class extends the Flash Sprite class. In order for the View3D object to be visible on the screen we need to add it as a child.

      addChild(view);

Finally, the View3D object is repositioned so it is in the centre of the screen. Note that the X and Y coordinates assigned here position the View3D object within the Flash stage, and are not related to a position within the 3D scene.

      view.x = stage.stageWidth / 2;
      view.y = stage.stageHeight / 2;
    }

The initListeners() function is used to register event handlers.

    protected function initListeners():void
    {

We have registered the onEnterFrame() function to be called once per frame in response to the Event.ENTER_FRAME event.

      addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

In the onEnterFrame() function we render a single frame to the screen by calling the View3D render() function. Remember that the onEnterFrame() function is called continuously in response to the Event.ENTER_FRAME event. By continuously rendering frames in this manner, we can create the impression of movement or animation within a 3D scene, just like the frames of a film strip being projected onto a screen.

    protected function onEnterFrame(event:Event):void
    {
      view.render();
    }

The initScene() function is where we populate the scene, giving us something to look at when the application is run. Because the Away3DTemplate class is designed to be generic, this function is empty. It is expected that additional classes will extend the Away3DTemplate class and implement the initScene() function.

    protected function initScene():void {}

The initUI() function is where we create any user interface elements. Just like the initScene() function, the ini tUI() function is empty, and is expected to be implemented by classes that extend the Away3DTemplate class.

    protected function initUI():void {}
  }
}

Tip

It is common to create and initialize the three classes referenced by the initEngine() function regardless of the type of 3D application you are trying to create, be it a game, a 3D user interface, or a simple banner. Away3D does include a class called SimpleView, contained in the away3d.test package, that allows you to get up and running quickly by initializing these classes. The Away3DTemplate, while achieving much the same end result, has been designed to be easily used as the base for all the applications that will be created throughout the rest of the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.190