Guide to Getting Started in Papervision 3D

Building your First Papervision Application

What goes into a Papervision Scene?

At first it can be a little bit overwhelming when looking at the sheer number of classes that come with Papervision. But fear not, these are of no concern to you (yet).

Think of a Papervision world as a movie set, there are actors, props, cameras, costumes, and a ton of other things. You as a director just have to look/ask around to see who and what you need for this particular scene. There are always the basic necessities though: a camera and a stage.

Fortunately, the developers of Papervision thought of that for you and set up some basic classes that you can use to get started with all the necessities in them already; the most common of them being the BasicView.

Setting up the Basics

Let’s start off by taking advantage of the BasicView class by extending it, because BasicView extends Sprite we can make it our document class in Flash.

We will create a main class for our application and call it HelloPapervision.as and enter the following:

package {
	import org.papervision3d.view.BasicView;
	public class HelloPapervision extends BasicView {
		public function HelloPapervision() {
			trace("hello papervision");
		}
	}
}

That is all well and good, but it didn’t do anything other than set up a scene and trace a couple things to the output window. So now we will work on adding some content to the scene.

Working with Primitives

The first thing that we need to do is figure out what we want to add, here I will show you how to add some basic shapes (primitives). You can take a look at the documentation for the included primitives here: http://www.papervision3d.org/docs/as3/org/papervision3d/objects/package-detail.html

Adding primitives is simple, it is just like creating an instance of anything else on the stage. You simply import the correct class, create an instance and add it to the scene.

package {
	import org.papervision3d.objects.primitives.PaperPlane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;

	public class HelloPrimitives extends BasicView {

		// create a sphere and a paper airplane
		// create these at class level in case they are to be used in other functions later
		var myFirstSphere:Sphere;
		var myFirstPlane:PaperPlane;

		public function HelloPrimitives() {

			myFirstSphere = new Sphere();
			myFirstPlane = new PaperPlane(null, 3);
			// move the plane in 3d space
			myFirstPlane.x = 150;
			myFirstPlane.y = 150;
			myFirstPlane.z = -50;
			// add the objects to the Papervision scene
			scene.addChild(myFirstSphere);
			scene.addChild(myFirstPlane);
			// this is an inherited method (onRenderTick) that starts an
			// enterframe event to render the scene
			startRendering();
		}
	}
}

Hopefully this little addition didn’t scare you off. All that I have done is created a Sphere and a Paper Plane, moved the plane and added the items to the scene. Simple, right? But what are shapes without materials? Boring !

Working with Materials

It is possible to get away without using materials on most objects, but it because a very confusing scene if everything is sitting there without proper textures. Papervision allows for numerous types of materials that you can apply to different parts (or all) of your objects. You can take a look at the documentation for the materials here: http://www.papervision3d.org/docs/as3/org/papervision3d/materials/package-detail.html

Creating a material is much like creating a primitive, just instead of adding the material to the scene, you apply it to the object.

package {
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieAssetMaterial;
	import org.papervision3d.objects.primitives.PaperPlane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;

	public class HelloMaterials extends BasicView {
		// create a sphere and a paper airplane
		var myFirstSphere:Sphere;
		var myFirstPlane:PaperPlane;

		public function HelloMaterials() {
			// create a movie material for the sphere from the library
			var sphereMaterial:MovieAssetMaterial = new MovieAssetMaterial("sphereMaterial");
			// create a color material for the plane
			var planeMaterial:ColorMaterial = new ColorMaterial(0x55FF11);
			// set the properties of the sphere instance
			// set material, radius and segements
			myFirstSphere = new Sphere(sphereMaterial, 100, 12, 12);
			// set the properties of the paper plane instance
			// set the material and the scale
			myFirstPlane = new PaperPlane(planeMaterial, 3);
			// move the plane in 3d space
			myFirstPlane.x = 150;
			myFirstPlane.y = 150;
			myFirstPlane.z = -50;
			// add the objects to the Papervision scene
			scene.addChild(myFirstSphere);
			scene.addChild(myFirstPlane);
			// this is an inherited method (onRenderTick) that starts an
			// enterframe event to render the scene
			startRendering();
		}
	}
}

All that we did here was we took what we did in the HelloPrimitives class and brought in a couple of materials. One thing to note is the use of the MovieAssetMaterial, for this I had to create a MovieClip in the Flash library and export it for actionscript (Linkage). Once defined, the materials can be used on any object, over and over again. You can even compound materials if you want to, but that is beyond the scope of this tutorial.

Playing in 3D Space

So now you know how to create objects and make them look half-way decent. But what about animation? I am sure that you don’t want to stop with static objects on your stage, what a boring movie that would be.

Well, little do you know, you have already started the animation when you wrote startRendering. That told Papervision to call its renderer and renders the scene every frame that passes.

package {
	import flash.events.Event;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieAssetMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.PaperPlane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;

	public class HelloAnimation extends BasicView {

		// move the definitions out here so the whole class can get them
		private var myFirstSphere:Sphere;
		private var myFirstPlane:PaperPlane;
		private var pivotContainer:DisplayObject3D;

		public function HelloAnimation() {
			// create a movie material for the sphere from the library
			var sphereMaterial:MovieAssetMaterial = new MovieAssetMaterial("sphereMaterial");
			// create a color material for the plane
			var planeMaterial:ColorMaterial = new ColorMaterial(0x55FF11);
			planeMaterial.doubleSided = true;
			// create a sphere and a paper airplane, increase the triangles on the sphere
			myFirstSphere = new Sphere(sphereMaterial, 100, 12, 12);
			myFirstPlane = new PaperPlane(planeMaterial, 3);
			// move the plane in 3d space
			myFirstPlane.x = 150;
			myFirstPlane.y = 150;
			myFirstPlane.z = -50;
			// set an initial rotation for the plane
			myFirstPlane.rotationY = -135;
			// add the sphere to the scene
			scene.addChild(myFirstSphere);
			// add a container to use for pivoting the plane rotation
			pivotContainer = new DisplayObject3D();
			// add the plane to the container
			pivotContainer.addChild(myFirstPlane);
			// add the container to the scene
			scene.addChild(pivotContainer);
			// this is an inherited method (onRenderTick) that starts an
			// enterframe event to render the scene
			startRendering();
		}

		override protected function onRenderTick(event:Event = null):void {
			// rotate the pivot container (will offset the plane rotation)
			pivotContainer.rotationY += 1;
			// rotate the sphere
			myFirstSphere.rotationY -= 1;
			// make sure that it is calling the renderer
			super.onRenderTick();
		}
	}
}

Now things are getting interesting ! Lets break down what happened here. In an attempt to make the paperplane orbit around the sphere, we had to create a container for it to act as a pivot point. To do this, we created a DisplayObject3D (do3d) object in the scene which is basically like a blank MovieClip. The reason we use the do3d is because it is empty but it can have children and maintains properties that we can then animate (just like in this example).

This is our first time using the onRenderTick method. Since the method is protected, we had to override it and we eventually call the super at the end. This method is what that startRendering call starts. It is just an onEnterFrame that tells the renderer to render the scene, overriding and using this method is helpful for many types of animation and interactivity in the Papervision world. We have added two lines of code here that simply rotate our object. Notice that the Plane isn’t listed, but it is animating. That is because it is a child of the pivotContainer and the pivotContainer is set to rotate.

Leave a Reply

Your email address will not be published. Required fields are marked *