由于官方已经提供了这个例子,我干脆弄了一个例子上来,其实也是按它的做法来完成的。
sandy 3.1版本新增了一个类库包sandy.core.scenegraph.mode7
这个类库包里面提供了两个类,一个是Mode7,一个是CameraMode7,这两个类非常有用。Mode7常用在地面,地步一些渲染上,能够提高一些质量,比plane3D要好一些。 而CameraMode7类和Camera3D其实差不多,仅仅是他的一个子类,但是区别在于,他约束Y轴的旋转。
<textarea cols="50" rows="15" name="code" class="c-sharp">package { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.*; import flash.net.*; import flash.text.*; import flash.ui.Keyboard; import mx.events.*; import mx.resources.*; import sandy.core.Scene3D; import sandy.core.data.*; import sandy.core.scenegraph.*; import sandy.core.scenegraph.mode7.*; import sandy.materials.*; import sandy.materials.attributes.*; import sandy.primitive.*; [SWF(width='700',height='500',backgroundColor='0xCCBBBB',frameRate='30')] public class Example4 extends Sprite { private var scenne:Scene3D; private var camera:Camera3D; private var ranY:Number=0.5; private var box:Box; private var box2:Box; private var box3:Box; private var plane:Plane3D; [Embed(source="map.jpg")] public var pic:Class; private var bitmap:Bitmap; private var _upPush:int; private var _downPush:int; private var _leftPush:int; private var _rightPush:int; private var _mode7:Mode7; public function Example4() { bitmap=new pic() as Bitmap; camera=new CameraMode7(700,500); camera.x = 0; camera.y = 100; camera.z = 0; camera.tilt =20; var root:Group=createScene(); scenne=new Scene3D("scene1",this,camera,root); addEventListener(Event.ENTER_FRAME,Run); stage.addEventListener (KeyboardEvent.KEY_DOWN, onKey); stage.addEventListener (KeyboardEvent.KEY_UP, onKey); } private function onKey (kEvt:KeyboardEvent):void { if (kEvt.type==KeyboardEvent.KEY_DOWN) { if (kEvt.keyCode==Keyboard.UP) { _upPush=1; } else if (kEvt.keyCode == Keyboard.DOWN) { _downPush=1; } else if (kEvt.keyCode == Keyboard.LEFT) { _leftPush=1; trace("left"); } else if (kEvt.keyCode == Keyboard.RIGHT) { _rightPush=1; } } else if (kEvt.type == KeyboardEvent.KEY_UP) { if (kEvt.keyCode==Keyboard.UP) { _upPush=0; } else if (kEvt.keyCode == Keyboard.DOWN) { _downPush=0; } else if (kEvt.keyCode == Keyboard.LEFT) { _leftPush=0; } else if (kEvt.keyCode == Keyboard.RIGHT) { _rightPush=0; } } } private function createScene():Group { var g:Group=new Group(); box=new Box("box",50,50,50); box.x=-20; box.y=0; box.z=-100; box.rotateX=50; box2=new Box("box2",50,50,50); box2.x=50; box2.y=0; box2.z=-100; box2.rotateX=50; box3=new Box("box3",50,50,50); box3.x=120; box3.y=0; box3.z=-100; box3.rotateX=50; _mode7 = new Mode7(); _mode7.setBitmap ( bitmap.bitmapData ); _mode7.setHorizon (true, 0x000000, 1); _mode7.setNearFar (true); g.addChild( _mode7); g.addChild(box); g.addChild(box2); g.addChild(box3); _mode7.repeatMap = false; var materialAttr:MaterialAttributes = new MaterialAttributes( new LineAttributes( 0.5, 0x2111BB, 0 ), new LightAttributes( true, 0.1) ); var material:Material = new ColorMaterial( 0x0000FF, 1, materialAttr ); var material2:Material = new ColorMaterial( 0x00FF00, 1, materialAttr ); var material3:Material = new ColorMaterial( 0xFF0000, 1, materialAttr ); material.lightingEnable = true; material2.lightingEnable = true; material3.lightingEnable = true; var app:Appearance = new Appearance( material ); var app2:Appearance = new Appearance( material2 ); var app3:Appearance = new Appearance( material3 ); box.appearance = app; box2.appearance = app2; box3.appearance = app3; box.container.buttonMode = true; box2.container.buttonMode = true; box3.container.buttonMode = true; box.container.addEventListener(MouseEvent.CLICK, clickHandler); box2.container.addEventListener(MouseEvent.CLICK, clickHandler2); box3.container.addEventListener(MouseEvent.CLICK, clickHandler3); return g; } private function Run(event:Event):void { scenne.render(); box.rotateY+=1; box2.rotateY+=1; box3.rotateY+=1; camera.rotateY += (_leftPush - _rightPush) * 2; var rotationRadian:Number=Math.PI*camera.rotateY/180; camera.x += Math.sin(- rotationRadian) * (_upPush - _downPush) * 8; camera.z += Math.cos(- rotationRadian) * (_upPush - _downPush) * 8; } private function clickHandler(event:MouseEvent):void { trace("第一个你点了"); navigateToURL(new URLRequest("http://www.baidu.com"),"_blank"); } private function clickHandler2(event:MouseEvent):void { trace("第二个你点了我 "); navigateToURL(new URLRequest("http://www.google.cn"),"_blank"); } private function clickHandler3(event:MouseEvent):void { trace("第三个你点了我 "); navigateToURL(new URLRequest("http://www.hao123.com"),"_blank"); } } }</textarea>
创建一个CameraMode7 摄影机。
<textarea cols="50" rows="15" name="code" class="c-sharp"> camera=new CameraMode7(700,500); camera.x = 0; camera.y = 100; camera.z = 0; camera.tilt =20;</textarea>
创建一个Mode7对象,指定他的位图数据,由于我们是在flex里面编写,所以使用内嵌资料。指定他的位图
[Embed(source="map.jpg")]
public var pic:Class;//内嵌位图
<textarea cols="50" rows="15" name="code" class="c-sharp"> _mode7 = new Mode7(); _mode7.setBitmap ( bitmap.bitmapData ); _mode7.setHorizon (true, 0x000000, 1); _mode7.setNearFar (true); </textarea>
其中这个程序里面使用了键盘交互,因此对其一些简单算法需要了解一下就可以。
演示地址:http://blog.5d.cn/user45/hero82748274/200903/515641.html
下载文件:http://blog.5d.cn/user45/hero82748274/upload/2009-03/Example4.rar