Skip to content


[AS 3] Papervision Cube with Different Material Sides

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

Posted in Actionscript 3, Papervision.


10 Responses

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

  1. Sam says

    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. admin says

    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. Michael says

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

    Thank you,
    Regards
    Michael

  4. admin says

    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. Michael says

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

  6. Nishanthe says

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

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

    (line 27 and 47 )

  7. Esmin says

    there should be:
    import org.papervision3d.materials.utils.MaterialsList;

    instead of:
    import org.papervision3d.materials.MaterialsList;

  8. Michal says

    hey, what plugin are you using to display code?
    nice work, btw.

  9. admin says

    Michal, Here is the syntax highlighter source:

    http://code.google.com/p/syntaxhighlighter/

    -Morgan

  10. Arun says

    hi Michael,

    this is the way in which i did my cube:

    the heirarchy of loading one upon the other is:item->movieclip->moviematerial->plane->scene;

    i get a black plane like thing rotating, but the “item”, which is an mxml component, in this case a canvas, is not to be seen.



Some HTML is OK

or, reply to this post via trackback.