20Mar

[AS 3] Papervision Cube

2 comments so far

So I found a tutorial over at Curious Minds Media, but since it was constructed for Flex, I opted to create a Document Class file for this. The comments within the posting has helped in creating this little demo. If you move your mouse around the flash movie below it will rotate the cube around.

Here is the Document Class PapervisionCube.as:

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.objects.Collada;
	import org.papervision3d.core.proto.DisplayObjectContainer3D;
	import org.papervision3d.objects.Cube;
	import org.papervision3d.materials.ColorMaterial;

	public class PapervisionCube extends Sprite
	{
		private var image1:BitmapData = new CubeTexture(0,0);
		private var container:Sprite;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var rootNode:DisplayObject3D;

		public function PapervisionCube()
		{
			init3D();
			addEventListener(Event.ENTER_FRAME, Timeline);
		}

		private function init3D():void {
			container = new Sprite();
			addChild( container );
			container.x = stage.stageWidth * .5;
			container.y = stage.stageHeight * .5;
			scene = new Scene3D( container );
			camera = new Camera3D();
			camera.zoom = 10;

			rootNode = scene.addChild( new DisplayObject3D("rootNode") );

			rootNode.addChild( new Cube( new BitmapMaterial( image1 ), 200, 200, 200, 1, 1, 1 ), "myCube01" );
		}

		private function Timeline( event:Event ):void {
			var screen:DisplayObject3D = this.scene.getChildByName("rootNode");
			var rotationY:Number = -(this.mouseX / this.stage.stageWidth * 275);
			var rotationX:Number = -(this.mouseY / this.stage.stageHeight * 275);
			screen.rotationY += (rotationY - screen.rotationY) / 12;
			screen.rotationX += (rotationX - screen.rotationX) / 12;

			this.scene.renderCamera(this.camera);
		}
	}
}

Share/Save/Bookmark

Thursday, March 20th, 2008 at 9:14 am and is filed under Actionscript 3, Papervision. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “[AS 3] Papervision Cube”

  1. Posted by gahlord 21st March, 2008 at 10:53 am

    That rules Morgan.

    Can you slap different pics on each face of the cube?

    g

  2. Posted by admin 24th March, 2008 at 8:23 am

    Thanks G-

    Check this out:

    http://manewc.com/2008/03/24/as-3-papervision-cube-with-different-material-sides/

Leave a reply