PV3D- barely white void-esque cubes update
July 21st, 2008
I re-worked my white void-esque cubes example a little after catching up on my Papervision3D email list reading. Turns out other people had a different approach that looks a whole lot better which wasn’t exactly a surprise. (Thanks to this thread for the better approach!) I thought I’d post the update since the first version’s source was pretty popular.
Check out the new version. (It works better in it’s own tab/window.)
I switched things around so now the camera is moving, not the container, and smoothed out the motion with Tweener.
The main new lines of code:
[as]
override protected function onRenderTick(event:Event = null):void
{
var rotX:Number = (stage.mouseX-(stage.stageWidth/2))*(0.5) – 90;
var rotY:Number = (stage.mouseY -(stage.stageHeight/2))*(0.5) +90;
Tweener.addTween(camera,{x:rotX, y:rotY,time:1.5, transition:”easOut”});
camera.target = bigBox;
super.onRenderTick(event);
}
[/as]
I also made some changes so that this will show up properly with the latest GreatWhite build of Papervision3D 2.0. Make sure you update your svn source as the cameras were changed a little. The current source will throw errors on older builds of GreatWhite. (If you don’t feel like updating just change camera.target to camera.lookAt() and adjust the zoom).
Flex Builder source is here (PV3D classes not included, but the Tweener classes are)
I’m a total Flex Builder noob, so if there is a better way to package up Flex Builder projects to share them, let me know in a comment below.
Full code of the CubeCamera class:
[as]
package {
import flash.events.Event;
import caurina.transitions.Tweener;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
public class CubeCamera extends BasicView
{
private var bigBox:Cube;
private var cube1:Cube;
private var cube2:Cube;
private var line1:Plane;
private var line2:Plane;
public function CubeCamera() {
super(1,1,true,true);
initScene();
startRendering();
}
private function initScene():void {
//Set the background
opaqueBackground = 0×000000;
//use this line for visible wireframe on the outter cube
//var wireFrameMaterial:WireframeMaterial = new WireframeMaterial(0×999999, 1, 2);
//use this line for an invisible outter cube
var wireFrameMaterial:WireframeMaterial = new WireframeMaterial(0×999999, 0, 0);
var wml:MaterialsList = new MaterialsList({all:wireFrameMaterial});
//create the main 3D container
bigBox = new Cube(wml, 400,200,400, 3,3,3);
//cube 1
var cubeMaterial:ColorMaterial = new ColorMaterial(0xcecece,1,true);
cubeMaterial.doubleSided = true;
cubeMaterial.interactive = true;
var ml:MaterialsList = new MaterialsList({all:cubeMaterial});
cube1 = new Cube(ml,100,50,100,1,1,1);
bigBox.addChild(cube1);
cube1.x = -100;
cube1.y = 0;
cube1.z = 0;
//cube2
cube2 = new Cube(ml,100,50,100,1,1,1);
bigBox.addChild(cube2);
cube2.x = cube1.x + 200;
cube2.y = 0;
cube2.z = 0;
//add the container object to the stage
scene.addChild(bigBox);
bigBox.visible = true;
//set up the camera
camera.zoom = 100;
//add a plane to show where bigBox zero point is
var lineMaterial:ColorMaterial = new ColorMaterial(0×00ff00,1,false);
line1 = new Plane(lineMaterial, 1,500,1,1);
line2 = new Plane(lineMaterial, 500, 1, 1,1);
bigBox.addChild(line1);
bigBox.addChild(line2);
line1.x = line2.x = 0;
line1.y = line2.y = 0;
}
override protected function onRenderTick(event:Event = null):void
{
var rotX:Number = (stage.mouseX-(stage.stageWidth/2))*(0.5) – 90;
var rotY:Number = (stage.mouseY -(stage.stageHeight/2))*(0.5) +90;
Tweener.addTween(camera,{x:rotX, y:rotY,time:1.5, transition:”easOut”});
camera.target = bigBox;
super.onRenderTick(event);
}
}
}
[/as]




