/** * ... * @author ever5u */ public class fuxi extends Sprite { private var scene:Scene3D; private var camera:Camera3D; public function fuxi() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; //創(chuàng)建一個攝像機 camera = new Camera3D(300, 300); camera.x = 100; camera.y = 100; camera.z = -300; camera.lookAt(0,0,0); //創(chuàng)建一個 Group var root:Group = createScene(); //創(chuàng)建場景 scene = new Scene3D( "scene", this, camera, root ); //創(chuàng)建實時偵聽 addEventListener( Event.ENTER_FRAME, enterFrameHandler ); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); } var box = new Box("box", 100, 100, 100); var myCone = new Cone("myCone", 50, 100); var myHedra = new Hedra( "myHedra", 80, 60, 60); var tg:TransformGroup; var Plane:Plane3D; public function createScene() { var g:Group = new Group();
//在場景畫一個坐標定位點 Plane = new Plane3D("Texture", 300, 300); var myXLine:Line3D = new Line3D( "x-coord", new Point3D( -20, 0, 0), new Point3D( 20, 0, 0 )); var myYLine:Line3D = new Line3D( "y-coord", new Point3D(0, -20, 0), new Point3D( 0, 20, 0 )); var myZLine:Line3D = new Line3D( "z-coord", new Point3D(0, 0, -20), new Point3D( 0, 0, 20 )); //創(chuàng)建一個新組 tg = new TransformGroup('myGroup'); //定義圓錐體和多面體的坐標 myCone.x = -160; myCone.z = 150; myHedra.x = 90; //復(fù)習一下為物體著色吧 var materialAttr:MaterialAttributes = new MaterialAttributes( new LineAttributes( 0.5, 0x2111BB, 0.4 ), new LightAttributes( true, 0.1) ); var material:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr ); material.lightingEnable = true; var app:Appearance = new Appearance( material ); myCone.appearance = app; myHedra.appearance = app; //將兩個物體放進新的組里 tg.addChild(myCone); tg.addChild(myHedra); g.addChild(tg); g.addChild(myXLine); g.addChild(myYLine); g.addChild(myZLine); //g.addChild( myCone ); //g.addChild( myHedra ); return g; } public function enterFrameHandler(_evt:Event) { //box.rotateX = mouseX; //box.rotateY = mouseY; myHedra.pan += 4; myCone.pan -= 4; scene.render(); } private function keyPressed(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP: tg.y +=2; break; case Keyboard.DOWN: tg.y -=2; break; case Keyboard.RIGHT: tg.roll +=2; break; case Keyboard.LEFT: tg.roll -=2; break; } } }