Daily Archive for May 4th, 2008

Controlling a Papervision Cube

Papervision 3D Cube Example

View Example

The goal for this example was to create a simple way to display a cube, then rotate it one side at a time. The image is just from Google Images which will be all 6 sides of the cube. This again like the exploding image example illustrates some control over your elements. You can easily modify this example to include mouse events that you can use trigger a view each side of the cube.
import caurina.transitions.Tweener;

import com.foomonger.utils.Later;

import flash.display.*;

import flash.events.Event;

import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cone;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;

public class PapervisionControlledCube extends Sprite
private var __camera:Camera3D;
private var __scene:Scene3D;
private var __viewport:Viewport3D;
private var __renderer:BasicRenderEngine;

private var __cube:Cube;
private var __matList:MaterialsList;

private var __width:Number;
private var __height:Number;

private var __background:Shape;

//Only needed if you are linking the image from FlexBuilder
//public var Brick:Class;

protected var __brick:DisplayObject;

protected var __brickClip:MovieClip;

protected var __rotation:Number;

public function PapervisionControlledCube():void


protected function init():void
__rotation = 0;


//Only needed for flexbuilder

protected function setDefaultWidthHeight():void
__width = 500;
__height = 250;

protected function initPapervision():void
__scene = new Scene3D();
__camera = new Camera3D();

__camera.zoom = 11;
__camera.z = -5000;
__camera.y = 200;

__viewport = new Viewport3D(__width,__height,false,true,true);
__renderer = new BasicRenderEngine();

protected function initMaterials():void
__matList = new MaterialsList();

var movieMat:MovieMaterial = new MovieMaterial(__brickClip);

__matList.addMaterial( movieMat, “front” );
__matList.addMaterial( movieMat, “back” );
__matList.addMaterial( movieMat, “left” );
__matList.addMaterial( movieMat, “right” );
__matList.addMaterial( movieMat, “top” );
__matList.addMaterial( movieMat, “bottom” );


protected function initObjects():void
__cube = new Cube(__matList,250,130,130);


protected function zoomIn(e:Event = null):void

//Rotate Cube

protected function rotateCube(e:Event = null):void

__rotation += 90;

protected function initEvents():void

protected function render(e:Event):void

protected function createChildren():void
__brick = new Brick();
__brickClip = new MovieClip();

//Only used in flexbuilder
private function setupStage():void
stage.stageWidth = __width;
stage.stageHeight = __height;
stage.align = “cc”;
stage.scaleMode = “noScale”;
stage.stageFocusRect = true;

Download Source

Other classes used:

1) Papervision 2.0

2) Foomonger’s Code (Call Later)

3) Tweener

Papervision 2.0 – Getting started from a simple starting example

At first when trying to create my first few Papervision projects it seemed a bit overwhelming. As you work with it more and more you notice that there are really only a few elements for creating a simple Papervision project. Then you can forget about the code and just think about what cool things you can do.

For any Papervision 2.0 project you need:
//As always your imports
import flash.display.*;
import flash.events.Event;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.BasicRenderEngine;

//1) A Scene
var scene:Scene3D = new Scene3D()

//2) A Camera
var camera = new Camera3D()
//Setting the camera zoom to 11 will make sure your objects are rendered at 100%
camera.zoom = 11;

//3) A view area “viewport” – think of this as your canvas or stage for your 3D objects
var viewport:Viewport3D = new Viewport3D(500,300,false,true,true)

//4) A render engine
var renderer:BasicRenderEngine = new BasicRenderEngine();

//5) add the viewport to the stage

//6) Something in your scene such as a Plane, Cube, Sphere, etc – in this case a plane with a black color material
var plane:Plane = new Plane(new ColorMaterial(0×000000),100,100)
//add your object to your scene

//7) Create a loop for rendering your animation
function render(e:Event):void
//Add some simple animation
plane.rotationY += 2; //Could also be plane.yaw(2);
//Render the scene

Don’t forget to download the latest Papervision code

The great thing is that if you build this once into a nice clean class, you can just keep re-using that class as your papervision starting place. Then you can do what you do best, create and have fun!

Start with this and add a cube or different materials and animations. Then toss in some Tweener code to get some cool movement effects. In a few days you’ll have some amazing things.

Follow papervision2 on Twitter

RSS Feed