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:

package 
{
	import flash.events.Event;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	/**
	 * ...
	 * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
	 */
 
	public class Main extends BasicView 
	{
		protected var plane:Plane;
		protected var colorMaterial:ColorMaterial;
 
		public function Main():void 
		{
			super();
 
			//Create one material and make it doublesided
			colorMaterial = new ColorMaterial(0xFF0000, .5);
			colorMaterial.doubleSided = true;
 
			//Create a plane using the colorMaterial
			plane = new Plane(colorMaterial, 100, 100);
			scene.addChild(plane);
 
			//Start the rendering
			startRendering();
		}
 
		override protected function onRenderTick(event:Event = null):void 
		{
			super.onRenderTick(event);
 
			//Rotate the plane
			plane.yaw(2);
		}
	}
}

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.

scene.addChild(plane);

Here is what the BasicView.as class looks like:

package org.papervision3d.view
{
	import org.papervision3d.cameras.SpringCamera3D;	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.cameras.DebugCamera3D;
	import org.papervision3d.core.view.IView;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
 
	/**
	 * <p>
	 * BasicView provides a simple template for quickly setting up
	 * basic Papervision3D projects by creating a viewport, scene,
	 * camera, and renderer for you. Because BasicView is a subclass of
	 * Sprite, it can be added to any DisplayObject.
	 * 
	 * </p>
	 * 
	 * <p>
	 * <p>
	 * Example:
	 * </p>
	 * <code>
	 * var width:Number = 640;
	 * var heigth:Number = 480;
	 * var scaleToStage:Boolean = true;
	 * var interactive:Boolean = true;
	 * var cameraType:String = Camera3D.TYPE;
	 * 
	 * var myBasicView:BasicView = new BasicView(width, height, scaleToStage, interactive, cameraType);
	 * myDisplayObject.addChild(myBasicView);
	 * </code>
	 * </p>
	 * @author Ralph Hauwert
	 */
	public class BasicView extends AbstractView implements IView
	{
		/**
		 * @param viewportWidth		Width of the viewport 
		 * @param viewportHeight	Height of the viewport
		 * @param scaleToStage		Whether you viewport should scale with the stage
		 * @param interactive		Whether your scene should be interactive
		 * @param cameraType		A String for the type of camera. @see org.papervision3d.cameras.CameraType
		 * 
		 */	
		public function BasicView(viewportWidth:Number = 640, viewportHeight:Number = 480, scaleToStage:Boolean = true, interactive:Boolean = false, cameraType:String = "Target")
		{
			super();
 
			scene = new Scene3D();
			viewport = new Viewport3D(viewportWidth, viewportHeight, scaleToStage, interactive);
			addChild(viewport);
			renderer = new BasicRenderEngine();
 
			switch(cameraType)
			{
				case CameraType.DEBUG:
					_camera = new DebugCamera3D(viewport);
					break;
				case CameraType.TARGET:
					_camera = new Camera3D(60);
					_camera.target = DisplayObject3D.ZERO;
					break;
				case CameraType.SPRING:
					_camera = new SpringCamera3D();
					_camera.target = DisplayObject3D.ZERO;		
					break;			
				case CameraType.FREE:
				default:
					_camera = new Camera3D(60);
					break;
			}
 
			cameraAsCamera3D.update(viewport.sizeRectangle);
		}
 
        /**
         * Exposes the camera as a <code>Camera3D</code>
         */
        public function get cameraAsCamera3D():Camera3D
        {
                return _camera as Camera3D;
        }
 
        /**
         * Exposes the camera as a <code>DebugCamera3D</code>
         */
        public function get cameraAsDebugCamera3D():DebugCamera3D 
        {
                return _camera as DebugCamera3D;
        }
	}
}

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

package org.papervision3d.view
{
	import flash.display.Sprite;
	import flash.events.Event;
 
	import org.papervision3d.core.proto.CameraObject3D;
	import org.papervision3d.core.view.IView;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
 
	/**
	 * @Author Ralph Hauwert
	 */
	public class AbstractView extends Sprite implements IView
	{
		protected var _camera:CameraObject3D;
		protected var _height:Number;
		protected var _width:Number;
 
		public var scene:Scene3D;
		public var viewport:Viewport3D;
		public var renderer:BasicRenderEngine;
 
		public function AbstractView()
		{
			super();
		}
 
		public function startRendering():void
		{
			addEventListener(Event.ENTER_FRAME, onRenderTick);
			viewport.containerSprite.cacheAsBitmap = false;
		}
 
		public function stopRendering(reRender:Boolean = false, cacheAsBitmap:Boolean = false):void
		{
			removeEventListener(Event.ENTER_FRAME, onRenderTick);
			if(reRender){
				onRenderTick();	
			}
			if(cacheAsBitmap){
				viewport.containerSprite.cacheAsBitmap = true;
			}else{
				viewport.containerSprite.cacheAsBitmap = false;
			}
		}
 
		public function singleRender():void
		{
			onRenderTick();
		}
 
		protected function onRenderTick(event:Event = null):void
		{
			renderer.renderScene(scene, _camera, viewport);
		}
 
		public function get camera():CameraObject3D
		{
			return _camera;
		}
 
		public function set viewportWidth(width:Number):void
		{
			_width = width;
			viewport.width = width;
		}
 
		public function get viewportWidth():Number
		{
			return _width;
		}
 
		public function set viewportHeight(height:Number):void
		{
			_height = height;
			viewport.height = height;
		}
 
		public function get viewportHeight():Number
		{
			return _height;
		}
 
	}
}

download-source

This entry was posted in Actionscript 3 Papervision, Actionscript 3.0, Papervision, Papervision 3D, Source Code, Tutorials and tagged , , , . Bookmark the permalink.

19 Responses to Getting started using BasicView.as

  1. Pingback: 4. Basic Template Usage at Papervision 3D Tutorials

  2. Pingback: 3. Creating a Papervision Base Template at Papervision 3D Tutorials

  3. Pingback: Basic Texturing (v2) at Papervision 3D Tutorials

  4. Pingback: Fixing Z-Sorting issues with the QuadrantRenderEngine at Papervision 3D Tutorials

  5. dim says:

    really good tutorial ! ! !

  6. Thanks for the great tutorials. :)

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

  8. realfriend says:

    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?

  9. disappointed says:

    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?

  10. Charlie says:

    @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

  11. maxxer says:

    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

  12. viaria says:

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

  13. Diceman says:

    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?

  14. Pingback: 火柴工作坊 » PV3D和Away3D入门好教程

  15. Kate says:

    Charlie:

    There is no better way to discurage someone to use any language than send him to the documentation. It shows nothing. It tells nothing. It is just a list of function. Understanding a language doesn’t mean knowing functions….

    For the basics visit:
    http://www.emanueleferonato.com/2009/05/08/papervision3d-for-the-absolute-beginners/

    The reason people dont use PV3D is that there is only a few tutorials for begginers, and when U ask for the answers people act like Charlie: “Im sooooo great, and you are soooo noob go learn some”… People are asking for help so don’t reply if you are not willing to help them….

  16. Charlie says:

    @Kate – If you heckle me instead of asking questions or starting a dialog I’m going to give it back to you. Mr Disappointed left a rude comment just saying this was a terrible tutorial and sarcastically asking what scene was.

    You now have just left a rude comment attacking me personally.

    If anyone has a question I ALWAYS try to help or allow others to help. But by Mr or Mrs Disappointed just saying “This is terrible” and being rude is not going to put me in a mood of being delighted to help you.

    If I didn’t care and I thought I was sooooo great I wouldn’t have taken the time to put up this website. Do you see any ads on this site? Do I try to make money off of you? No!? Do I want people to learn? Yes.

    If Mr / Mrs Disappointed had just requested a tutorial that was even simpler than this one I would have personally written one for him OR explained in full any questions he / she may have.

    Charlie

  17. Jackass says:

    I R Retarded. I dont get it. I wanna bitch about it because I dont get it. Charlie you suck at life, you should get a new one. I R 2 stupid to write tutorial for how you do that since I waste time trolling interwebs looking for something to complain about instead. Sincerely,

    ~Internet Jackass

  18. Charlie says:

    Twitter says I should love you both. So @Kate and @Disappointed I love you both and how can I be of service to you? What tutorials can I write for you today to better explain how to use Papervision?

  19. Charlie says:

    @Jacka$# lol – funny – I’ll get you back on twitter.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>