/** * Author: xleon, http://www.xinterface.net (malandro@gmail.com) */ package { import caurina.transitions.Equations; import caurina.transitions.Tweener; import com.gskinner.utils.Rnd; import flash.display.MovieClip; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import net.xinterface.items.ItemButton; import org.papervision3d.materials.WireframeMaterial; import org.papervision3d.materials.utils.MaterialsList; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.primitives.Cone; import org.papervision3d.objects.primitives.Cube; import org.papervision3d.objects.primitives.Cylinder; import org.papervision3d.objects.primitives.PaperPlane; import org.papervision3d.objects.primitives.Plane; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.objects.special.UCS; import org.papervision3d.view.BasicView; [SWF(frameRate="31", width="500", height="350", backgroundColor="0x000000")] public class Primitives extends Sprite { private var view:BasicView; private var objects:Array; private var selectedObj:* = null; // almacenará el objeto seleccionado public function Primitives() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; objects = new Array; Init3D(); } private function Init3D():void { view = new BasicView(500, 350, false, false); addChild(view); // Configuramos la cámara: view.camera.focus = 25; view.camera.zoom = 10; view.camera.z = -500; view.camera.y = 300; view.camera.x = 0; CreateObjects(); PlaceObjects(); CreateButtons(); addEventListener(Event.ENTER_FRAME, Loop3D); } private function CreateObjects():void { // material que usaremos en todas las primitivas var mat:WireframeMaterial = new WireframeMaterial(0x00ffff); mat.doubleSided = true; // ucs var ucs:UCS = new UCS(250); ucs.name = "ucs"; objects.push(ucs); // plano var plane:Plane = new Plane(mat, 500, 500, 8, 8); plane.name = "plano"; objects.push(plane); plane.rotationX = -90; // esfera var sp:Sphere = new Sphere(mat, 250); sp.name = "esfera"; objects.push(sp); // Cono var cone:Cone = new Cone(mat, 150, 500); cone.name = "cono"; objects.push(cone); // Cilindro var cil:Cylinder = new Cylinder(mat, 100, 500); cil.name = "cilindro"; objects.push(cil); // PaperPlane var pplane:PaperPlane = new PaperPlane(mat, 3); pplane.name = "avioncito"; objects.push(pplane); // Cubo /** * El cubo necesita una lista de materiales en lugar de uno solo como ocurría con el resto de primitivas * Cada material especificado en la lista (MaterialList) necesita un nombre que se corresponde con la cara del cubo. * Si ponemos el nombre "all" a uno de los materiales, este se repetirá por todas las caras del cubo * */ var materialList:MaterialsList = new MaterialsList; materialList.addMaterial(mat, "all"); var cube:Cube = new Cube(materialList, 500, 500, 500, 4, 4, 4); cube.name = "cube"; objects.push(cube); } /** Este metodo esparce los objectos en la escena 3D */ private function PlaceObjects(spacing:Number = 1000):void { objects.forEach( function(it:*, index:int, arr:Array):void { it.z = index * spacing; if(index > 0) { it.x = Rnd.float(-4000, 4000); it.y = Rnd.float(-2000, 2000); } view.scene.addChild(it); } ); } private function CreateButtons():void { objects.forEach( function(it:*, index:int, arr:Array):void { // Este "Asset" está en un swc linkado al proyecto var buttonasset:MovieClip = new Asset() as MovieClip; buttonasset.text_txt.text = it.name; // Creamos el boton con el asset definido anteriormente var button:ItemButton = new ItemButton(buttonasset); button.x = button.width * index + 10; button.y = 320; // Asignamos eventos button.addEventListener(ItemButton.RELEASE, function():void { GoToObject(it); }); addChild(button); if(index == 0) { button.Click(); } } ); } /** Esta función hace que la camara apunte directamente al * objeto, que se mueva hacia el, y que el objeto en cuestion y rote */ private function GoToObject(object:DisplayObject3D):void { view.camera.target = object; selectedObj = object; // usado en Loop3D Tweener.addTween(view.camera, {time:.8, transition:Equations.easeInOutCirc, x:object.x, y:object.y + 500, z:object.z - 500}); } private function Loop3D(e:Event):void { // hacemos rotar el objeto seleccionado if(selectedObj != null) { DisplayObject3D(selectedObj).yaw(2); DisplayObject3D(selectedObj).pitch(4); DisplayObject3D(selectedObj).roll(1); } view.singleRender(); } } }