Papervision can be daunting, that's for sure. You have to set up a slew of objects to even get started. It can be a real hassle and is part of the reason I have 30 different experiments that are completely unrelated with the same class name. Just to consider what all is needed just to get started:
- Renderer - A BasicRenderEngine is required so you can render out your content
- Scene - A scene holds all your data - so you have to have it if you want to display anything
- Viewport - Without a viewport there is nowhere to draw your content.
- Camera - Without an eye you can't see anything!
So, to get all of this setup, you would normally have an initialization function that looks something like:
-
public var scene:Scene3D;
-
public var camera:Camera3D;
-
public var viewport:Viewport3D;
-
public var renderer:BasicRenderEngine;
-
-
public function init():void{
-
-
scene = new Scene3D();
-
-
camera = new Camera3D();
-
camera.focus = 100;
-
camera.zoom = 12;
-
camera.z = -1000;
-
-
renderer = new BasicRenderEngine();
-
-
viewport = new Viewport3D(stage.stageWidth, stage.stageHeight,false, false, false, false);
-
addChild(viewport);
-
-
addEventListener(Event.ENTER_FRAME, loop);
-
}
But, it doesn't have to be that way! BasicView is an awesome class which takes care of all your initialization for you! To get the exact same initialization as above, all you need to do now is:
-
private var view:BasicView;
-
-
private function init():void {
-
-
view = new BasicView(stage.stageWidth, stage.stageHeight, false, false, Camera3D.TYPE);
-
addChild(view);
-
-
addEventListener(Event.ENTER_FRAME, loop);
-
}
BasicView extends a Sprite, so you add it to your stage. The constructor takes the following parameters:
- viewportWidth:Number - how wide to make your viewport in pixels
- viewportHeight:Number - how tall to make your viewport in pixels
- scaleToStage:Boolean - scale viewport with stage resize or not
- interactive:Boolean - detect interactions or not
- cameraType:String - type of camera to use. Defined by the static TYPE variable of the camera classes (i.e. Camera3D.TYPE, FreeCamera3D.TYPE, FrustrumCamera3D.TYPE)
Once you have created a BasicView, you have access to all the same objects as we initialized in the first example. You can access those objects via some BasicView properties:
- BasicView.renderer - the BasicRenderEngine
- BasicView.camera - the camera
- BasicView.viewport - the viewport
- BasicView.scene - the scene
You can then work with those objects the exact same way you would have before:
-
view.camera.x -= 100;
-
view.scene.addChild(new Sphere(material));
In addition, you no longer have to worry about passing multiple parameters to render your scene. BasicView handles the rendering for you when you call BasicView.singleRender(). It will render its scene, into its viewport, using its camera!
-
function loop(e:Event):void{
-
-
view.singleRender();
-
-
}
And that's it! All you have to worry about now is adding your content and making it cool! I never used BasicView before as i thought i was losing control somehow. Turns out it was just making my life harder for no particular reason. If you need to worry about handling multiple cameras, scenes, etc. this isn't the class for you - but for straightforward projects or experimentation, make use of it!
12 Comments, Comment or Ping
I apologize in advance for stupid questions, but i just can’t understand the units that are used in pv3d, the sizes, the zooms, the z’s etc. would you (or any one commenting here) be kind enough to give some explanation about it??
i’m dying to actually do something useful with pv3d but i just can’t figure how to control it so it would feel the screen or just a pre-determined part of it..
January 26th, 2008
hey andy,
nice explanation
See you soon,
Seb
January 26th, 2008
Lord Andy
Slick
January 26th, 2008
@aYo – congrats!!! you were the 200th comment on my blog!
@Am – you can easily specify the size of the container (viewport) used to display your content (using the width/height params in the BasicView constructor for example). If you turn clipping/culling on for the viewport you won’t get any content outside the viewport. If you are looking for some other tutorials that might be useful, John Lindquist has been doing some great tutorials/source on getting started with pv3d. Check out his blog at http://pv3d.org/
hth!
January 26th, 2008
Thanks Andy – this will be fun to experiment with!
Nik
January 27th, 2008
Thanks!!
January 28th, 2008
this is rad andy! thanx for the tutorial. its ultra simple. thanx again!
January 28th, 2008
Thanks a lot!! This is a huuuuge time-saver!
February 21st, 2008
I the sample code when you say “You need to cast the camera as your type if you want to use type specific functions” where and how can I do that
Thanks
rS
July 23rd, 2008
I love you ADNY THX~~~
June 16th, 2009
Hey Andy!
This is great! Thanks a lot for sharing this. It almost works for me, but I’m having an issue with the camera… I get an error :
1120: Access of undefined property Camera3D.
Is this because I need to import the camera? From what I understood in your blog, importing the BasicView auto imports the basic necessities like camera, viewport, renderer and scene. Would love some help from anyone.
Thanks again!
Noah
July 22nd, 2009
Reply to “BasicView – Making Life In Papervision a Little Easier”