FIVe3D Custom Primitive: VectorCube Class
Thursday, July 17th, 2008After 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.