// you’re reading...

3D

FIVe3D Custom Primitive: VectorCube Class

After a long Papervision-induced hiatus, I’ve come back to FIVe3D- mainly because I still think it’s a great library. Since it’s still relatively new, however, there’s not a lot of example code out there. Thus, here’s another chunk from your friends here at Pixelwelders. This class creates a cube out of Sprite3D planes.

Fig. 1: 27 Cubes

Fig.2: The VectorCube Class

/**
 * A vector Cube class for Mathieu Badimon's excellent FIVe3D library
 * @author Zack Jordan
 * { P I X E L W E L D E R S . C O M }
 */
package com.pixelwelders.five3d
{
	import five3D.display.Sprite3D;
 
	public class VectorCube extends Sprite3D
	{
 
		public var faces		: Array;
 
		public function VectorCube( size:Number, colors:Array = null )
		{
			super();
 
			if ( !colors ) colors = [ 0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000 ];
			createCube( size, colors );
		}
 
		/**
		 * Creates a new cube at the size specified
		 * 
		 * @param		size			Size of the cube to be created
		 * @return					The new cube
		 */
		private function createCube( size:Number, colors:Array ): void
		{
			faces = new Array();
 
			var xP:Array = new Array(	0, size / 2,	0, -size / 2, 0, 0 );
			var yP:Array = new Array(	0, 0,	0, 0,	-size / 2, size / 2 );
			var zP:Array = new Array(	-size / 2, 0, size / 2, 0, 0, 0	);
			var xR:Array = new Array(	0, 0,	0, 0, -90, 90 );
			var yR:Array = new Array(	0, -90, 180, 90,	0, 0 );
			var zR:Array = new Array(	0, 0,	0, 0, 0, 0 );
 
			for ( var i:int = 0; i < 6; i++ )
			{
				var face:Sprite3D = new Sprite3D();
				face.graphics3D.lineStyle( 1, 0x000000, 1 );
				face.graphics3D.beginFill( colors[ i ] );
				face.graphics3D.drawRect( -size / 2, -size / 2, size, size );
				face.graphics3D.endFill();
				face.singleSided = true;
 
				face.x = xP[ i ];
				face.y = yP[ i ];
				face.z = zP[ i ];
				face.rotationX = xR[ i ];
				face.rotationY = yR[ i ];
				face.rotationZ = zR[ i ];
 
				faces[ i ] = face;
				addChild( face );
			}
 
		}
 
	}
}

So what is this good for? Really, it’s just an easy way to create six faces and arrange them into a cube. You don’t have to worry about depth sorting or any of that stuff because each of the faces is one-sided. However, if you are using more than one of these in your presentation, make sure you specify that container.childrenSorted = true, where container is the Sprite3D containing your Cubes. Otherwise you’ll get some weird results.

Another note: the faces Array is public so you can continue to draw on each face of the cube, and even add children. So really, this is just a starting point; I’ll let you find something useful to do with it.

Discussion

5 comments for “FIVe3D Custom Primitive: VectorCube Class”

  1. You got the z-sorting right! Great! I didn’t know about that ‘childrenSorted’ parameter! Thanks

    Posted by Bartek Drozdz | July 18, 2008, 4:36 am
  2. You’re welcome! I had to do some hunting through the source to find it. Mathieu mentioned that it was added in v2.1, so I knew it had to be there- I just couldn’t find any examples using it.

    Posted by Zack Jordan | July 18, 2008, 9:01 am
  3. Zack: I made a similar class which makes all kinds of prisms, cylinders, pyramids, cones, and tubes. I hope you received my e-mail.

    Everyone else: you can check out an example of it at http://joaqo182.googlepages.com/VectorColorPrism.htm

    Posted by Joaqo | August 2, 2008, 12:55 pm
  4. Hey, those work really well. I may have to do a site post about using them.

    Posted by Zack Jordan | August 4, 2008, 6:20 am
  5. […] Direct link: click here […]

    Posted by Mathieu Badimon | FIVe3D | Blog » Blog Archive » FIVe3D Custom Primitive: VectorCube Class | August 30, 2008, 12:35 pm

Post a comment

Twitterpated