BasicView – Making Life In Papervision a Little Easier


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:

  1. Renderer - A BasicRenderEngine is required so you can render out your content
  2. Scene - A scene holds all your data - so you have to have it if you want to display anything
  3. Viewport - Without a viewport there is nowhere to draw your content.
  4. 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:

Actionscript:
  1. public var scene:Scene3D;
  2. public var camera:Camera3D;
  3. public var viewport:Viewport3D;
  4. public var renderer:BasicRenderEngine;
  5.  
  6. public function init():void{
  7.  
  8. scene = new Scene3D();
  9.  
  10. camera = new Camera3D();
  11. camera.focus = 100;
  12. camera.zoom = 12;
  13. camera.z = -1000;
  14.  
  15. renderer = new BasicRenderEngine();
  16.  
  17. viewport = new Viewport3D(stage.stageWidth, stage.stageHeight,false, false, false, false);
  18. addChild(viewport);
  19.  
  20. addEventListener(Event.ENTER_FRAME, loop);
  21. }

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:

Actionscript:
  1. private var view:BasicView;
  2.  
  3. private function init():void {
  4.  
  5. view = new BasicView(stage.stageWidth, stage.stageHeight, false, false, Camera3D.TYPE);
  6. addChild(view);
  7.  
  8. addEventListener(Event.ENTER_FRAME, loop);
  9. }

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:

Actionscript:
  1. view.camera.x -= 100;
  2. 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!

Actionscript:
  1. function loop(e:Event):void{
  2.  
  3. view.singleRender();
  4.  
  5. }

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!

Get The Sample Source


12 Comments, Comment or Ping

  1. Am

    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

  2. hey andy,

    nice explanation :-) See you soon,

    Seb

    January 26th, 2008

  3. aYo

    Lord Andy :D
    Slick

    January 26th, 2008

  4. @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

  5. Thanks Andy – this will be fun to experiment with!

    Nik

    January 27th, 2008

  6. Am

    Thanks!!

    January 28th, 2008

  7. this is rad andy! thanx for the tutorial. its ultra simple. thanx again!

    January 28th, 2008

  8. Daniel

    Thanks a lot!! This is a huuuuge time-saver! :)

    February 21st, 2008

  9. Ricardo

    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

  10. I love you ADNY THX~~~

    June 16th, 2009

  11. Noahdecoco

    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”