24Mar

[AS 3] Papervision Cube with Different Material Sides

6 comments so far

Download Files (.zip)

So there was a request for an update to the Papervision cube that I did last week to allow for various materials on the 6 sides of the cube. I have noticed that there is a MaterialsList Class that allows you to addMaterials to your objects. I also found this a piece of code here:

http://kelvinluck.com/assets/papervision3d/cube_tweaks/

and also his updated Cube.as class file to be found here:

http://kelvinluck.com/assets/papervision3d/cube_tweaks/Cube.as

Here is my cube that pulls one image from the library with the class name of CubeTexture, loads an external image, and randomly chooses a color for the remaining sides of the cube.

Here is my document class:

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.BitmapFileMaterial;
	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 PapervisionCubeSides extends Sprite
	{
		// image 1 variable represents an image in the library with the class name of CubeTexture
		private var image1:BitmapData = new CubeTexture(0,0);
		private var container:Sprite;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var rootNode:DisplayObject3D;
		private var ml:MaterialsList = new MaterialsList();
		private var customcube:Cube;

		public function PapervisionCubeSides()
		{
			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") );

			var ml:MaterialsList = new MaterialsList();
			ml.addMaterial(new BitmapFileMaterial("http://manewc.com/projects/flash/PapervisionCubeSides/ski.jpg"), 'face1');
			ml.addMaterial(new BitmapMaterial( image1 ), 'face2');
			ml.addMaterial(new BitmapFileMaterial("http://manewc.com/projects/flash/PapervisionCubeSides/ski.jpg"), 'face3');
			ml.addMaterial(new BitmapMaterial( image1 ), 'face4');
			ml.addMaterial(new ColorMaterial(Math.random()*0xffffff), 'face5');
			ml.addMaterial(new ColorMaterial(Math.random()*0xffffff), 'face6');

			customcube = new Cube( ml, 200, 200, 200, 1, 1, 1 );
			rootNode.addChild( customcube, "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

Monday, March 24th, 2008 at 8:20 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.

6 Responses to “[AS 3] Papervision Cube with Different Material Sides”

  1. Posted by Sam 9th July, 2008 at 1:56 pm

    Hi, Thx for the example.

    Is this using Great White (PV3D 2.0)??? As I am getting errors when compiling:
    1046: Type was not found or was not a compile-time constant: MaterialsList.
    1180: Call to a possibly undefined method MaterialsList.
    1046: Type was not found or was not a compile-time constant: Cube.
    1137: Incorrect number of arguments. Expected no more than 0.
    1172: Definition org.papervision3d.materials:MaterialsList could not be found.
    1172: Definition org.papervision3d.objects:Collada could not be found.
    1172: Definition org.papervision3d.objects:Cube could not be found.

    Also is it possible to add an XML file approach to this for getting the images? So, the image names and urls that could be clicked by clicking the image are in an XML file?

    Any help around this would be highly appreciated.

  2. Posted by admin 14th July, 2008 at 8:48 am

    Hi Sam,

    In regards to the errors you should verify that you are using the updated cube.as file:

    http://kelvinluck.com/assets/papervision3d/cube_tweaks/Cube.as

    Yes it is PV3D v2.0 .. if you need my source files, please let me know.

    I don’t see why the images couldn’t be pulled in with an xml file - here is one method I used to pull in images with an xml file:

    http://manewc.com/2008/06/25/image-gallery-part-iv-xml-file-loading/

    -Morgan

  3. Posted by Michael 26th August, 2008 at 5:31 pm

    Hi!
    Great tutorial. But i’m getting some errors :(
    Can you send to my e-mail the source files ?

    Thank you,
    Regards
    Michael

  4. Posted by admin 27th August, 2008 at 4:49 am

    Hi Michael, I posted a link to download the files at the top of this post. I have a feeling people are experiencing errors because there is a reference to a CubeTexture class, which is a library item. Once you download the files and open the .fla you will see what I mean.

    Thanks,
    Morgan

  5. Posted by Michael 6th September, 2008 at 10:44 am

    Hi!
    Tks for the e-mail and the file.
    Yes the problem was CubeTexture class.
    Thanks,
    Michael

  6. Posted by Nishanthe 10th November, 2008 at 8:53 am

    It seems that MaterialList is Created twice with the same instance name

    “var ml:MaterialsList = new MaterialsList(); ”

    (line 27 and 47 )

Leave a reply