/** * ... * @author ever5u */ public class fuxi extends Sprite { private var scene:Scene3D; private var camera:Camera3D; private var lightX = 0; private var lightY = 0; private var lightZ = 10; public function fuxi() { //創(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 ); scene.light.setDirection(lightX, lightY, lightZ); //創(chuàng)建實時偵聽 addEventListener( Event.ENTER_FRAME, enterFrameHandler ); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); } //這次用個球型 var mySphere:Sphere = new Sphere( "theSphere", 80, 32, 24); public function createScene() { var g:Group = new Group(); //復習一下為物體著色吧 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 );
mySphere.appearance = app;
g.addChild( mySphere ); return g; } public function enterFrameHandler(_evt:Event) { mySphere.pan += 1; scene.render(); } private function keyPressed(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.PAGE_DOWN: scene.light.setPower(scene.light.getPower() - 5); break; case Keyboard.PAGE_UP: scene.light.setPower(scene.light.getPower() + 5); break; case Keyboard.UP: lightY+=10; scene.light.setDirection(lightX, lightY, lightZ); break; case Keyboard.DOWN: lightY-=10; scene.light.setDirection(lightX, lightY, lightZ); break; case Keyboard.RIGHT: lightX+=10; scene.light.setDirection(lightX, lightY, lightZ); break; case Keyboard.LEFT: lightX-=10; scene.light.setDirection(lightX, lightY, lightZ); break; } } }