package{ import flash.display.Sprite; import flash.events.Event; import jp.progression.commands.lists.SerialList; import mat.progression.commands.DoAlphaIn; import mat.progression.commands.DoAlphaOut; import org.papervision3d.core.proto.MaterialObject3D; import org.papervision3d.events.FileLoadEvent; import org.papervision3d.events.InteractiveScene3DEvent; import org.papervision3d.materials.BitmapFileMaterial; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.parsers.DAE; import org.papervision3d.view.BasicView; public class LoadModel extends Sprite { private var view :BasicView; private var dae :DAE;//DAE物件 private var _bol :Boolean = false; public function LoadModel(){ init3DEngine(); init3DObject(); } private function init3DEngine():void{ view = new BasicView(0, 0, true, true); this.addChild(view); this.addEventListener(Event.ENTER_FRAME, _onEventRender3D); } private function init3DObject():void { //載入dae dae = new DAE(); dae.scale = 150; dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, _onLoadComplete); dae.load("dae/cow.dae"); view.scene.addChild(dae); } private function _onLoadComplete(e:FileLoadEvent):void { //dae載入完成 trace("_onLoadComplete()") //將dae內所有材質的interactive屬性設為true setInteractive(dae, true); dae.useOwnContainer = true; //指定偵聽 addEventListeners(dae, InteractiveScene3DEvent.OBJECT_PRESS, _onPress ); addEventListeners(dae, InteractiveScene3DEvent.OBJECT_OVER, _onOver ); addEventListeners(dae, InteractiveScene3DEvent.OBJECT_OUT, _onOut ); } private function _onPress(e:InteractiveScene3DEvent):void { _bol = !_bol; var _cmd:SerialList = new SerialList(null, new DoAlphaOut(dae), function():void { //更換材質 var _matSrc:String = _bol ? "dae/cow_yallow.png" : "dae/cow.png"; var mat:BitmapFileMaterial = new BitmapFileMaterial(_matSrc); mat.interactive = true; dae.replaceMaterialByName(mat, "material0"); //material0為dae內的材質id名稱 }, new DoAlphaIn(dae) ); _cmd.execute(); } private function _onOver(e:InteractiveScene3DEvent):void { view.buttonMode = true; } private function _onOut(e:InteractiveScene3DEvent):void { view.buttonMode = false; } private function setInteractive(pTarget3D:DisplayObject3D, pBol:Boolean):void { for each (var mat:MaterialObject3D in pTarget3D.materials.materialsByName) { mat.interactive = true; } } private function addEventListeners(pTarget3D:DisplayObject3D, pEventType:String, pListener:Function):void { pTarget3D.addEventListener(pEventType, pListener); for each (var child:DisplayObject3D in pTarget3D.children) { addEventListeners(child, pEventType, pListener); } } private function _onEventRender3D(e:Event):void{ dae.rotationY += 2; view.camera.x += (Math.sin(mouseX / stage.stageWidth * 3 * Math.PI) * 1000 - view.camera.x) * .1; view.camera.z += (Math.cos(mouseX / stage.stageWidth * 3 * Math.PI) * 1000 - view.camera.z) * .1; view.camera.y += (mouseY / stage.stageHeight * 2000 - 500 - view.camera.y) * .05; view.singleRender(); } }}