// Cube - FIVe3D Primitive Cube Renderer V2 (Modified for FIVe3D FP10) // // released under MIT License // http://www.opensource.org/licenses/mit-license.php // //@author Devin Reimer //@version 2.0.0 //@website http://blog.almostlogical.com //Important Note - This class (Cube) was inspired by @author Zack Jordan - { P I X E L W E L D E R S . C O M } // while this class was written from scratch some similar exists between this class (Cube) and Zack Jordan VectorCube class //Copyright (c) 2009 Devin Reimer /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package almostlogical.five3d.primitives { import net.badimon.five3D.display.Sprite3D; public class Cube extends Sprite3D { private static const DEFAULT_COLOR:uint = 0x0066FF; public static const FRONT:int = 1; public static const BACK:int = 2; public static const RIGHT:int = 3; public static const LEFT:int = 4; public static const TOP:int = 5; public static const BOTTOM:int = 6; private var _width:Number; private var _height:Number; private var _depth:Number; private var _faces:Array; //colorsArray [front,back,right,left,top,bottom] public function Cube(width:Number,depth:Number,height:Number,colorsArray:Array=null,borderColor:uint=0x000000) { _width = width; _height = height; _depth = depth; if (colorsArray == null) { colorsArray = [ DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR ]; } createCube(colorsArray, borderColor); } private function createCube(colorsArray:Array,borderColor:uint):void { faces = new Array(); var frontFace:Sprite3D = createFace(width, height, colorsArray[0],borderColor); var backFace:Sprite3D = createFace(width, height, colorsArray[1],borderColor); var rightFace:Sprite3D = createFace(depth, height, colorsArray[2],borderColor); var leftFace:Sprite3D = createFace(depth, height, colorsArray[3],borderColor); var topFace:Sprite3D = createFace(width, depth, colorsArray[4],borderColor); var bottomFace:Sprite3D = createFace(width, depth, colorsArray[5],borderColor); frontFace.z -= depth / 2; backFace.z += depth / 2; backFace.rotationY += 180; leftFace.x += width / 2; leftFace.rotationY -= 90; rightFace.x -= width / 2; rightFace.rotationY += 90; topFace.y -= height / 2; topFace.rotationX -= 90; bottomFace.y += height / 2; bottomFace.rotationX += 90; addChild(bottomFace); addChild(backFace); addChild(rightFace); addChild(leftFace); addChild(frontFace); addChild(topFace); faces[FRONT] = frontFace; faces[BACK] = backFace; faces[RIGHT] = rightFace; faces[LEFT] = leftFace; faces[TOP] = topFace; faces[BOTTOM] = bottomFace; } protected function createFace(width:Number, height:Number, color:uint,borderColor:uint):Sprite3D { var face:Sprite3D = new Sprite3D(); if (borderColor >>> 24 != 0xFF) { face.graphics3D.lineStyle(1,borderColor); }//get alpha is completely invisible don't bother drawing border face.graphics3D.beginFill(color); face.graphics3D.drawRect(-width/2,-height/2, width, height); face.graphics3D.endFill(); face.singleSided = true; return face; } public override function get width():Number { return _width; } public override function get height():Number { return _height; } public function get depth():Number { return _depth; } public function get faces():Array { return _faces; } public function set faces(val:Array):void { _faces = val; } //type is one of the constants defined above. ex: FRONT public function getFaceByType(type:int):Sprite3D { return faces[type]; } } }