Skip to content


[AS 3] Papervision Cube

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);
		}
	}
}

Posted in Actionscript 3, Papervision.


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. gahlord says

    That rules Morgan.

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

    g



Some HTML is OK

or, reply to this post via trackback.