Michael Jackson Pearl Jam Stevie Ray Vaughan Ozzy Osbourne Finntroll MP3 list A Fine Frenzy Capone Phil Collins Shakira Pink Floyd Keane

Getting started using BasicView.as

basicview-image

download-source

In a previous tutorial (Creating a Papervision Base Template) we created a base Papervision template that we could re-use. Now however Papervision includes a form of this base template called BasicView.as

With just a few lines of code you're up and running. Here is the very simple / basic use of BasicView.as:

Actionscript:
  1. package
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.materials.ColorMaterial;
  5.     import org.papervision3d.objects.primitives.Plane;
  6.     import org.papervision3d.view.BasicView;
  7.    
  8.     /**
  9.      * ...
  10.      * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
  11.      */
  12.    
  13.     public class Main extends BasicView
  14.     {
  15.         protected var plane:Plane;
  16.         protected var colorMaterial:ColorMaterial;
  17.        
  18.         public function Main():void
  19.         {
  20.             super();
  21.            
  22.             //Create one material and make it doublesided
  23.             colorMaterial = new ColorMaterial(0xFF0000, .5);
  24.             colorMaterial.doubleSided = true;
  25.            
  26.             //Create a plane using the colorMaterial
  27.             plane = new Plane(colorMaterial, 100, 100);
  28.             scene.addChild(plane);
  29.            
  30.             //Start the rendering
  31.             startRendering();
  32.         }
  33.        
  34.         override protected function onRenderTick(event:Event = null):void
  35.         {
  36.             super.onRenderTick(event);
  37.            
  38.             //Rotate the plane
  39.             plane.yaw(2);
  40.         }
  41.     }
  42. }

This very simple example you will just see a single light red plane rotating.

That is all you need to do to get started in Papervision. Test it out. Don't forget to add your 3D objects to the scene.

Actionscript:
  1. scene.addChild(plane);

Here is what the BasicView.as class looks like:

Actionscript:
  1. package org.papervision3d.view
  2. {
  3.     import org.papervision3d.cameras.SpringCamera3D;   
  4.     import org.papervision3d.cameras.Camera3D;
  5.     import org.papervision3d.cameras.CameraType;
  6.     import org.papervision3d.cameras.DebugCamera3D;
  7.     import org.papervision3d.core.view.IView;
  8.     import org.papervision3d.objects.DisplayObject3D;
  9.     import org.papervision3d.render.BasicRenderEngine;
  10.     import org.papervision3d.scenes.Scene3D;
  11.  
  12.     /**
  13.      * <p>
  14.      * BasicView provides a simple template for quickly setting up
  15.      * basic Papervision3D projects by creating a viewport, scene,
  16.      * camera, and renderer for you. Because BasicView is a subclass of
  17.      * Sprite, it can be added to any DisplayObject.
  18.      *
  19.      * </p>
  20.      *
  21.      * <p>
  22.      * <p>
  23.      * Example:
  24.      * </p>
  25.      * <pre><code>
  26.      * var width:Number = 640;
  27.      * var heigth:Number = 480;
  28.      * var scaleToStage:Boolean = true;
  29.      * var interactive:Boolean = true;
  30.      * var cameraType:String = Camera3D.TYPE;
  31.      *
  32.      * var myBasicView:BasicView = new BasicView(width, height, scaleToStage, interactive, cameraType);
  33.      * myDisplayObject.addChild(myBasicView);
  34.      * </code></pre>
  35.      * </p>
  36.      * @author Ralph Hauwert
  37.      */
  38.     public class BasicView extends AbstractView implements IView
  39.     {
  40.         /**
  41.          * @param viewportWidth  Width of the viewport
  42.          * @param viewportHeight    Height of the viewport
  43.          * @param scaleToStage    Whether you viewport should scale with the stage
  44.          * @param interactive      Whether your scene should be interactive
  45.          * @param cameraType        A String for the type of camera. @see org.papervision3d.cameras.CameraType
  46.          *
  47.          */ 
  48.         public function BasicView(viewportWidth:Number = 640, viewportHeight:Number = 480, scaleToStage:Boolean = true, interactive:Boolean = false, cameraType:String = "Target")
  49.         {
  50.             super();
  51.            
  52.             scene = new Scene3D();
  53.             viewport = new Viewport3D(viewportWidth, viewportHeight, scaleToStage, interactive);
  54.             addChild(viewport);
  55.             renderer = new BasicRenderEngine();
  56.            
  57.             switch(cameraType)
  58.             {
  59.                 case CameraType.DEBUG:
  60.                     _camera = new DebugCamera3D(viewport);
  61.                     break;
  62.                 case CameraType.TARGET:
  63.                     _camera = new Camera3D(60);
  64.                     _camera.target = DisplayObject3D.ZERO;
  65.                     break;
  66.                 case CameraType.SPRING:
  67.                     _camera = new SpringCamera3D();
  68.                     _camera.target = DisplayObject3D.ZERO;   
  69.                     break;     
  70.                 case CameraType.FREE:
  71.                 default:
  72.                     _camera = new Camera3D(60);
  73.                     break;
  74.             }
  75.            
  76.             cameraAsCamera3D.update(viewport.sizeRectangle);
  77.         }
  78.        
  79.         /**
  80.          * Exposes the camera as a <code>Camera3D</code>
  81.          */
  82.         public function get cameraAsCamera3D():Camera3D
  83.         {
  84.                 return _camera as Camera3D;
  85.         }
  86.        
  87.         /**
  88.          * Exposes the camera as a <code>DebugCamera3D</code>
  89.          */
  90.         public function get cameraAsDebugCamera3D():DebugCamera3D
  91.         {
  92.                 return _camera as DebugCamera3D;
  93.         }
  94.     }
  95. }

Since BasicView.as extends AbstractView.as you can see a few more of the methods available to you just by extending BasicView.as

Actionscript:
  1. package org.papervision3d.view
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.    
  6.     import org.papervision3d.core.proto.CameraObject3D;
  7.     import org.papervision3d.core.view.IView;
  8.     import org.papervision3d.render.BasicRenderEngine;
  9.     import org.papervision3d.scenes.Scene3D;
  10.    
  11.     /**
  12.      * @Author Ralph Hauwert
  13.      */
  14.     public class AbstractView extends Sprite implements IView
  15.     {
  16.         protected var _camera:CameraObject3D;
  17.         protected var _height:Number;
  18.         protected var _width:Number;
  19.        
  20.         public var scene:Scene3D;
  21.         public var viewport:Viewport3D;
  22.         public var renderer:BasicRenderEngine;
  23.        
  24.         public function AbstractView()
  25.         {
  26.             super();
  27.         }
  28.        
  29.         public function startRendering():void
  30.         {
  31.             addEventListener(Event.ENTER_FRAME, onRenderTick);
  32.             viewport.containerSprite.cacheAsBitmap = false;
  33.         }
  34.        
  35.         public function stopRendering(reRender:Boolean = false, cacheAsBitmap:Boolean = false):void
  36.         {
  37.             removeEventListener(Event.ENTER_FRAME, onRenderTick);
  38.             if(reRender){
  39.                 onRenderTick()
  40.             }
  41.             if(cacheAsBitmap){
  42.                 viewport.containerSprite.cacheAsBitmap = true;
  43.             }else{
  44.                 viewport.containerSprite.cacheAsBitmap = false;
  45.             }
  46.         }
  47.        
  48.         public function singleRender():void
  49.         {
  50.             onRenderTick();
  51.         }
  52.        
  53.         protected function onRenderTick(event:Event = null):void
  54.         {
  55.             renderer.renderScene(scene, _camera, viewport);
  56.         }
  57.        
  58.         public function get camera():CameraObject3D
  59.         {
  60.             return _camera;
  61.         }
  62.        
  63.         public function set viewportWidth(width:Number):void
  64.         {
  65.             _width = width;
  66.             viewport.width = width;
  67.         }
  68.        
  69.         public function get viewportWidth():Number
  70.         {
  71.             return _width;
  72.         }
  73.        
  74.         public function set viewportHeight(height:Number):void
  75.         {
  76.             _height = height;
  77.             viewport.height = height;
  78.         }
  79.        
  80.         public function get viewportHeight():Number
  81.         {
  82.             return _height;
  83.         }
  84.        
  85.     }
  86. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

9 Responses to “Getting started using BasicView.as”


  • really good tutorial ! ! !

  • Thanks for the great tutorials. :)

  • it works! it lives! thank you. I was able to bring in CINEMA4D this way. Version 10.

  • I change .dae

    but

    INFO: Viewport autoScaleToStage : Papervision has changed the Stage scale mode.
    INFO: BitmapFileMaterial: Loading bitmap from daeModel/guy.jpg
    ERROR: BitmapFileMaterial: Unable to load file daeModel/guy.jpg
    ERROR: COLLADA file load error

    Can you help me?

  • This is a terrible first tutorial. It explains nothing about what Papervision does, how to set it up, and how to make an actual running application.

    This:
    scene.addChild(plane);

    tells me nothing. What the heck is scene?

  • @disappointed –

    The title is “Getting Started using BasicView.as” BasicView.as is a class used by papervision; if you’re reading this tutorial you should have some knowledge of papervision itself. This is what I would consider the “infant stage tutorial” why don’t you come back (or not come back because I don’t care) when you’ve read over the documentation and understand the “baby stage” of papervision.

    And next time don’t be a wuss. Leave your email address; try and be part of the community instead of a heckler. If you simply asked – “what is a scene” your question would have been answered.

    Charlie

    Here are the docs. Happy reading..
    http://papervision3d.googlecode.com/svn/trunk/as3/trunk/docs/index.html

  • hey y’all i was trying to follow charlie’s advice – get started on the baby stage learning process, however the official docs over at papervision3d seem to be just a collection of function and class definitions. I need the very basics! I know the (rudimentary) principles of AS.

    Any nods in the right direction!?

    v best
    Maxx

  • hi,
    tuts are helpfull,
    i wonder that using basicview class is simple or can we make other advanced staff…
    for exammple it uses spring cam, can i change it and how..

    thanks for furter advance detail..

  • I’m with maxxer. This Tutorial looks good but it’s one step ahead of me and I really need an idiots guide to Papervision basics. Any links anyone?

Leave a Reply


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed