/** * ... * @author ever5u */ public class fuxi extends Sprite { private var scene:Scene3D; private var camera:Camera3D; private var tg:TransformGroup;
public function fuxi() { //先創(chuàng)建攝像機 camera = new Camera3D( 300, 300 ); camera.z = -400; camera.lookAt(0,0,0); //創(chuàng)建 Group var root:Group = createScene(); //創(chuàng)建場景 scene = new Scene3D( "scene", canvas, camera, root ); scene.rectClipping = true; addEventListener( Event.ENTER_FRAME, enterFrameHandler ); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedHandler); } private function createScene():Group { var g:Group = new Group(); tg = new TransformGroup('myGroup'); //創(chuàng)建幾個平面體,并定義坐標使其組合成為一個形狀 var right:Shape3D = new Plane3D('right', 150, 100, 10, 10, Plane3D.YZ_ALIGNED,'quad'); right.moveLateraly(100); right.moveForward(50) var left:Shape3D = new Plane3D('left', 150, 100, 10, 10, Plane3D.YZ_ALIGNED,'quad'); left.moveLateraly(-100); left.moveForward(50) var back:Shape3D = new Plane3D('back', 100, 250, 10, 10, Plane3D.XY_ALIGNED,'quad'); back.moveForward(100); var bottom:Shape3D = new Plane3D('bottom', 150, 250, 10, 10, Plane3D.ZX_ALIGNED,'quad'); bottom.moveForward(50); bottom.moveUpwards( -50); //著色設(shè)置 var materialAttr:MaterialAttributes = new MaterialAttributes( new LineAttributes( 0.5, 0x2111BB, 0.4 ), new LightAttributes( true, 0.1) ); var material01:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr ); material01.lightingEnable = false; var material02:Material = new ColorMaterial( 0xFEA792, 1, materialAttr ); material02.lightingEnable = false; var app01:Appearance = new Appearance( material01 ); var app02:Appearance = new Appearance( material02 ); //應(yīng)用著色 right.enableBackFaceCulling = false; right.appearance = app01; left.enableBackFaceCulling = false; left.appearance = app01; back.enableBackFaceCulling = false; back.appearance = app02; bottom.enableBackFaceCulling = true; bottom.appearance = app02; //是否獨立渲染 right.useSingleContainer = false; left.useSingleContainer = false; back.useSingleContainer = false; bottom.useSingleContainer = false; //組合到組 tg.addChild(right); tg.addChild(left); tg.addChild(back); tg.addChild(bottom); g.addChild(tg); return g; } private function enterFrameHandler( event : Event ) : void { scene.render(); } private function keyPressedHandler(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP: tg.tilt +=2; break; case Keyboard.DOWN: tg.tilt -=2; break; case Keyboard.RIGHT: tg.pan +=2; break; case Keyboard.LEFT: tg.pan -=2; break; } } } }