Tag Archive for 'Papervision Cube'

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.
[as]
package
{
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
//[Embed(source="assets/brick.png")]
//public var Brick:Class;

protected var __brick:DisplayObject;

protected var __brickClip:MovieClip;

protected var __rotation:Number;

public function PapervisionControlledCube():void
{

init();

}
protected function init():void
{
__rotation = 0;

setDefaultWidthHeight();

//Only needed for flexbuilder
//setupStage();
//————————–

createChildren();
initPapervision();
initMaterials();
initObjects();
initEvents();
}
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();
addChild(__viewport);
}

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);
__scene.addChild(__cube);

Later.call(this,zoomIn,1000,true);

}
protected function zoomIn(e:Event = null):void
{
Tweener.addTween(__camera,{z:-1000,transition:”easeInOutQuint”,time:1,onComplete:rotateCube})
}

//———————————————————————–
//Rotate Cube
//———————————————————————–

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

__rotation += 90;
Tweener.addTween(__cube,{rotationY:__rotation,delay:.6,transition:”easeInOutQuint”,time:1,onComplete:rotateCube})
}

protected function initEvents():void
{
this.addEventListener(Event.ENTER_FRAME,render);
}

protected function render(e:Event):void
{
__renderer.renderScene(__scene,__camera,__viewport,true);
}

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

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

}
}
}
[/as]
Download Source

Other classes used:

1) Papervision 2.0

2) Foomonger’s Code (Call Later)

3) Tweener


Follow papervision2 on Twitter

RSS Feed