/* * Copyright (c) 2007 michiyasu wada * http://www.seyself.com/ * * Distributed under The MIT License. * [http://www.opensource.org/licenses/mit-license.php] */ package { import flash.display.Sprite; import flash.events.Event; // Import Papervision3D import org.papervision3d.scenes.Scene3D; import org.papervision3d.cameras.Camera3D; import org.papervision3d.cameras.FreeCamera3D; import org.papervision3d.objects.*; import org.papervision3d.materials.MaterialsList; import org.papervision3d.materials.MovieAssetMaterial; import org.papervision3d.materials.ColorMaterial; public class Cubic extends Sprite { // ___________________________________________________________________ Static static public var SCREEN_WIDTH :int = 550; static public var SCREEN_HEIGHT :int = 400; // ___________________________________________________________________ 3D vars private var container :Sprite; private var scene :Scene3D; private var camera :FreeCamera3D; private var cube :Cube; private var ground :DisplayObject3D; // ___________________________________________________________________ Maze private var maze :Maze; private var preX :Number; private var preY :Number; // ___________________________________________________________________ main public function Cubic() { init3D(); maze = new Maze(10,10); maze.create(); var i:int, j:int; var x:int = maze.map.length; var y:int = maze.map[0].length; for(j=2;j<=y-3;j++){ for(i=2;i<=x-3;i++){ if ( maze.map[i][j] ) { //output += 'X'; else output += ' '; var n = (i + x * j); var front:Boolean = true; var back:Boolean = false; var top:Boolean = !!maze.map[i][j+1] && j!=y-3; var bottom:Boolean = !!maze.map[i][j-1] && j!=2; var left:Boolean = !!maze.map[i-1][j] && i!=2; var right:Boolean = !!maze.map[i+1][j] && i!=x-3; createCube( "c"+n, i-x/2+0.5 , j-y/2+0.5, 0 , [front, back, top, bottom, left, right] ); } } } pvCreateSphere("ball" ); this.addEventListener( Event.ENTER_FRAME, loop ); } // ___________________________________________________________________ Init3D private function init3D():void { // Create container sprite and center it in the stage container = new Sprite(); addChild( container ); container.x = SCREEN_WIDTH /2; container.y = SCREEN_HEIGHT /2; // Create scene scene = new Scene3D( container ); // Create camera camera = new FreeCamera3D(); camera.z += -2000; camera.zoom = 12; //trace("camera.zoom = "+camera.zoom); ground = new DisplayObject3D(); scene.addChild( ground ); } // ___________________________________________________________________ Create Cube private function createCube( name:String, _x:Number=0, _y:Number=0, _z:Number=0, excludes:Array=null ) { // Attributes var size :Number = 100; var quality :Number = 1; // Materials var materials:MaterialsList = getMaterial( true ); // Cube face settings // You can add or sustract faces to your selection. For examples: Cube.FRONT+Cube.BACK or Cube.ALL-Cube.Top. // On single sided materials, all faces will be visible from the inside. var insideFaces :int = Cube.ALL; // Front and back cube faces will not be created. var excludeFaces :int = 0; if( excludes[0] ) excludeFaces += Cube.FRONT; if( excludes[1] ) excludeFaces += Cube.BACK; if( excludes[2] ) excludeFaces += Cube.TOP; if( excludes[3] ) excludeFaces += Cube.BOTTOM; if( excludes[4] ) excludeFaces += Cube.LEFT; if( excludes[5] ) excludeFaces += Cube.RIGHT; // Create the cube. var _cube:Cube = new Cube( materials, size, size, size, quality, quality, quality, 0, excludeFaces, { x:_x*size, y:_y*size, z:_z*size } ); //_cube.rotationX = Math.floor(Math.random()*4)*90; //_cube.rotationY = Math.floor(Math.random()*4)*90; //_cube.rotationZ = Math.floor(Math.random()*4)*90; ground.addChild( _cube, name ); } private function pvCreateSphere( name:String, radius:Number=50, _x:Number=0, _y:Number=0, _z:Number=0 ) { var material:ColorMaterial = new ColorMaterial( 0xFF00FF, 1 ); var segmentsW:int = 8; var segmentsH:int = 6; var initObject:Object = { x:_x, y:_y, z:_z }; var sphere:Sphere = new Sphere( material, radius, segmentsW, segmentsH, initObject ); ground.addChild( sphere, name ); } private function getMaterial( flag:Boolean ):MaterialsList { var materials:MaterialsList; if ( flag ) { var matBox:Array = ["Front", "Back", "Left", "Right", "Top", "Bottom"]; matBox = materialRotationX( matBox , Math.random()*4>>0 ); matBox = materialRotationY( matBox , Math.random()*4>>0 ); matBox = materialRotationZ( matBox , Math.random()*4>>0 ); materials = new MaterialsList( { //all: front: new MovieAssetMaterial( matBox[0], true ), back: new MovieAssetMaterial( matBox[1], true ), left: new MovieAssetMaterial( matBox[2], true ), right: new MovieAssetMaterial( matBox[3], true ), top: new MovieAssetMaterial( matBox[4], true ), bottom: new MovieAssetMaterial( matBox[5], true ) } ); } else { materials = new MaterialsList( { //all: front: new MovieAssetMaterial( "Rect", true ), back: new MovieAssetMaterial( "Rect", true ), right: new MovieAssetMaterial( "Rect", true ), left: new MovieAssetMaterial( "Rect", true ), top: new MovieAssetMaterial( "Rect", true ), bottom: new MovieAssetMaterial( "Rect", true ) } ); } return materials; } private function materialRotationX( mat:Array , len:int ):Array { var res:Array = mat.concat(); for (var i:int = 0; i < len; i++ ) { var f:String = res[0]; var k:String = res[1]; var t:String = res[4]; var b:String = res[5]; res = [ b, t, res[2], res[3], f, k ]; } return res; } private function materialRotationY( mat:Array , len:int ):Array { var res:Array = mat.concat(); for (var i:int = 0; i < len; i++ ) { var f:String = res[0]; var k:String = res[1]; var l:String = res[2]; var r:String = res[3]; res = [ l, r, k, f, res[4], res[5] ]; } return res; } private function materialRotationZ( mat:Array , len:int ):Array { var res:Array = mat.concat(); for (var i:int = 0; i < len; i++ ) { var l:String = res[2]; var r:String = res[3]; var t:String = res[4]; var b:String = res[5]; res = [ res[0], res[1], b, t, l, r ]; } return res; } // ___________________________________________________________________ Loop private function loop(event:Event):void { update3D(); } private function update3D():void { //camera.tilt( container.mouseX / 2000 ); ground.rotationX = ( container.mouseY * 1.0 ); ground.rotationY = ( -container.mouseX * 1.0 ); if( preX != ground.rotationX || preY != ground.rotationY ){ // Render scene.renderCamera( this.camera ); } preX = ground.rotationX; preY = ground.rotationY; } } }