package { import flash.display.Sprite; import flash.display.MovieClip; import flash.events.*; import flash.utils.*; import sandy.core.World3D; import sandy.core.data.*; import sandy.core.scenegraph.*; import sandy.materials.*; import sandy.math.*; import sandy.primitive.*; import sandy.parser.*; import sandy.view.CullingState; import sandy.view.ViewPort; [SWF(width="550", height="400", backgroundColor="#000000", frameRate=30)] public class SandyTutorial2 extends Sprite { /* A lot of this, such as the code below, is covered in the last tutorial. Please see SandyTutorial1 for more info. This tutorial is for camera movement and Collada file import.*/ private var world : World3D; private var rootGroup:Group = new Group("rootGroup"); private var tg: TransformGroup = new TransformGroup("MainTransform"); private var starShip:Shape3D; public function SandyTutorial2() { world = World3D.getInstance(); world.container = this; world.root = rootGroup; world.camera = new Camera3D( 400, 550 ); world.camera.z = 10000; world.camera.x = 10000; world.camera.lookAt(0,0,0); world.root.addChild( world.camera ); loadCollada(); this.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function loadCollada():void{ /* Creates a variable of type IParser. The function which creates it takes the string path to the ship you want to load. In this case it's a folder above the source folder in a folder called "assets". The next is an enumerated string that you can get from the Parser class called "MAX_3DS". This string tells the system what kind of file it is to parse. Lastly is the scale factor of the model. This determines how big the model will be in relation to its default size. Scale's default value is 1. */ var parser:IParser = Parser.create("../assets/starfighterplay.3ds", Parser.MAX_3DS, 100); /* The line below will add an event listener to be triggered when the parser initializes. This event will then call the "createScene" function and ask it to create the model and add it to the right place. */ parser.addEventListener( ParserEvent.onInitEVENT, createScene ); parser.parse(); } private function createScene(e:ParserEvent):void{ starShip = e.group.getChildList()[0] ; trace(e.group.getChildList()[0]); var appear:Appearance = new Appearance( new ColorMaterial( 0xFF, 100, new LineAttributes(1,0xFFF000) ) ); starShip.appearance = appear; starShip.setPosition(0,0,0); tg.addChild(starShip); world.root.addChild(tg); } private function onEnterFrame(e:Event):void{ /*x0, y0, and z0 all specify the center point of the rotation sphere.*/ var x0:int = 0; var y0:int = 0; var z0:int = 0; var radius:int = 10000; var theta:Number = 2 * stage.mouseX * Math.PI/180; var sigma:Number = 2 * stage.mouseY * Math.PI/180; world.camera.x = x0 + radius * Math.cos(theta) * Math.sin(sigma); world.camera.y = y0 + radius * Math.sin(theta) * Math.sin(sigma); world.camera.z = z0 + radius * Math.cos(sigma); if(starShip) world.camera.lookAt(starShip.x, starShip.y, starShip.z); world.render(); } } }