-->

Previous articles summary :

  • First steps in Papervision3D : Part 1
    Creation of a new Papervision3D project within eclipse or Flex Builder 3 and a simple example of a 3D scene illustrating some basic Papervision3D classes.

Following from my last post, I’d like to show how Papervision3D v2.0 provides us with a class to encapsulate the viewport, camera, scene and renderer. This class is called the BasicView and is basically a wrapper class for all these objects. Rather than inheriting from a Sprite, this example is derived from the BasicView and as a result inherits automatically the four key objects named above.

1. Example using BasicView
The following code produces the same example that we saw in the Part 1 but using this new class. If you’re using Eclipse or Flex Builder 3, you can add this example to the project you create from the last post then simply right-click on the new file and select Set as Default Application.

package {
 
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
 
  import org.papervision3d.core.geom.Lines3D;
  import org.papervision3d.core.geom.renderables.Line3D;
  import org.papervision3d.core.geom.renderables.Vertex3D;
  import org.papervision3d.core.proto.MaterialObject3D;
  import org.papervision3d.materials.WireframeMaterial;
  import org.papervision3d.materials.special.LineMaterial;
  import org.papervision3d.objects.primitives.Sphere;
  import org.papervision3d.view.BasicView;

  public class Example002 extends BasicView {
 
    private var sphere:Sphere;
   
    public function Example002() {
      super(0, 0, true, false);
     
      // set up the stage
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;

      // Initialise Papervision3D
      init3D();
     
      // Create the 3D objects
      createScene();
     
      // Start rendering the scene
      startRendering();
    }
   
    private function init3D():void {

      // position the camera
      camera.x = -100;
      camera.y = -100;
      camera.z = -500;
    }

    private function createScene():void {

      // First object : a sphere
     
      // Create a new material for the sphere : simple white wireframe
      var sphereMaterial:MaterialObject3D = new WireframeMaterial(0xFFFFFF);

      // Create a new sphere object using wireframe material, radius 50 with
      //   10 horizontal and vertical segments
      var sphere:Sphere = new Sphere(sphereMaterial, 50, 10, 10);

      // Position the sphere (default = [0, 0, 0])
      sphere.x = -100;

      // Second object : x-, y- and z-axis
     
      // Create a default line material and a Lines3D object (container for Line3D objects)
      var defaultMaterial:LineMaterial = new LineMaterial(0xFFFFFF);
      var axes:Lines3D = new Lines3D(defaultMaterial);

      // Create a different colour line material for each axis
      var xAxisMaterial:LineMaterial = new LineMaterial(0xFF0000);
      var yAxisMaterial:LineMaterial = new LineMaterial(0x00FF00);
      var zAxisMaterial:LineMaterial = new LineMaterial(0x0000FF);

      // Create a origin vertex
      var origin:Vertex3D = new Vertex3D(0, 0, 0);

      // Create a new line (length 100) for each axis using the different materials and a width of 2.
      var xAxis:Line3D = new Line3D(axes, xAxisMaterial, 2, origin, new Vertex3D(100, 0, 0));
      var yAxis:Line3D = new Line3D(axes, yAxisMaterial, 2, origin, new Vertex3D(0, 100, 0));
      var zAxis:Line3D = new Line3D(axes, zAxisMaterial, 2, origin, new Vertex3D(0, 0, 100));
     
      // Add lines to the Lines3D container
      axes.addLine(xAxis);
      axes.addLine(yAxis);
      axes.addLine(zAxis);

      // Add the sphere and the lines to the scene
      scene.addChild(sphere);
      scene.addChild(axes);
    }
    

  }
}

We see below the Flash animation produced - as with the Part 1 a wireframe sphere on the left and the axis in the centre (click on image to launch Flash animation).


2. Initialising and rendering the scene
Compared to the example shown in Part 1, the initialisation of the Papervision3D is simpler : for this example we specify in the constructor the values we want for the viewport :

    public function Example002() {
      super(0, 0, true, false);

If you look at the source of BasicView you’ll see that these values are passed directly to a new Viewport3D instance. The camera by default is focused on the origin but can be changed also by specifying a different type of camera by adding another parameter in the call to the super constructor being either CameraType.DEBUG, CameraType.TARGET (default) or CameraType.FREE.

Next, we set up the stage as in Part 1 and then perform the remaining initialisation of the 3D : all we need to do is specify the position for the camera, which rather handily, is accesible through the public function camera(). For this example we use exactly the same position as in Part 1.

    private function init3D():void {

      // position the camera
      camera.x = -100;
      camera.y = -100;
      camera.z = -500;
    }

The objects in the scene are identical to the previous example so I won’t go into detail here, however I should say that the scene is accessible through the public variable scene. The only remaining difference is the last line of the constructor when we start the rendering :

      // Start rendering the scene
      startRendering();

If you look at the the contents of startRendering() in the BasicView class, you’ll see that it simply adds a Flash frame enter listener calling a function called onRenderTick(event:Event=null). This method can be overloaded in our own inherited classes to perform any necessary changes to the scene.

And that’s all there is to it! The objective of this post was really just to show an alternative method of quickly setting up a 3D scene in Papervision3D. Its worthwhile examining the contents of the BasicView class and you’ll see that it performs exactly the same steps as shown in the previous example.

The next post should be along shortly and I’ll illustrate how some simple animation can be performed in the onRenderTick function.

Next article:

Leave a Reply

-->