南京交互式多媒体/多点触摸/三维虚拟现实/unity3D/Flash/Flex/ActionScript3 QQ:781444988
效果预览:
很多时候我们需要控制加载模型的多个面,并让其响应不同的鼠标事件,比如单独给某一个面换贴图,或者点击不同的面显示不同的内容等等。要想获取模型的每个面其实很简单,在加载模型的时候采用AssetEvent.ASSET_COMPLETE侦听器即可精确的获得模型的每个面,在加载的过程中,模型的每个面自动转换成mesh对象,对每一个mesh对象启用鼠标交互mesh.mouseEnabled = true;和添加侦听器即可使模型的每个面响应鼠标事件。
本例完整源码如下:
package { import away3d.cameras.Camera3D; import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d.entities.Mesh; import away3d.events.LoaderEvent; import away3d.events.MouseEvent3D; import away3d.loaders.Loader3D; import away3d.materials.ColorMaterial; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.display.*; import away3d.events.AssetEvent; import away3d.library.assets.AssetType; import away3d.loaders.parsers.Parsers; import flash.geom.Vector3D; import flash.text.TextField; /** * ... * @author yl */ [SWF(width='800',height='600',frameRate="60", backgroundColor="0x000000")] public class MyAway3d_4 extends Sprite { [Embed(source = "/../embeds/myqiu.awd", mimeType="application/octet-stream")] private static var _dModel:Class; [Embed(source = "../embeds/myAway3d_1.jpg")] private static var _img1:Class; private var _view:View3D; private var _camera:Camera3D; private var _hovController:HoverController; private var _loader3D:Loader3D; private var _meshArray:Array = []; private var _index:int = 1; private var _lastTiltAngle:Number; //camera变化前的X轴角度 private var _lastPanAngle:Number; //camera变化前的Y轴角度 private var _lastMouseX:Number; //鼠标移动之前的X轴坐标 private var _lastMouseY:Number; //鼠标移动之前的Y轴坐标 private var _move:Boolean; private var _infoText:TextField; public function MyAway3d_4() { //侦听舞台初始化完毕 if (stage) { init(); }else { this.addEventListener(Event.ADDED_TO_STAGE, init); } } private function init(e:Event=null):void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; this.removeEventListener(Event.ADDED_TO_STAGE, init); _infoText = new TextField(); addChild(_infoText); _infoText.textColor = 0xFFFFFF; _infoText.selectable = false; _infoText.text = "选择的对象是:\nQQ:781444988"; //实例化视口 _view = new View3D(); addChild(_view); _view.x = stage.stageWidth / 2 - 150; _view.y = stage.stageHeight / 2 - 150; //设置抗锯齿参数 _view.antiAlias = 6; _view.width = 300; _view.height = 300; _camera = _view.camera; _hovController = new HoverController(_camera, null, 180, 0, 1200,-90,90); _hovController.yFactor = 1; Parsers.enableAllBundled(); _loader3D = new Loader3D(); _loader3D.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete); _loader3D.addEventListener(LoaderEvent.RESOURCE_COMPLETE, loadComplete); _loader3D.loadData(new _dModel()); //_view.scene.addChild(_loader3D); _hovController.lookAtObject = _loader3D; //_view.camera.lookAt(_loader3D.position); //_view.camera.z = -1500; stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler); stage.addEventListener(MouseEvent.MOUSE_UP, upHandler); this.addEventListener(Event.ENTER_FRAME, onRender); } private function upHandler(e:MouseEvent):void { _move = false; stage.removeEventListener(Event.MOUSE_LEAVE, leaveHandler); } private function downHandler(e:MouseEvent):void { _move = true; _lastTiltAngle = _hovController.tiltAngle; //记录camera的X轴旋转角度 _lastPanAngle = _hovController.panAngle; //记录camera的Y轴旋转角度 _lastMouseX = stage.mouseX; _lastMouseY = stage.mouseY; //添加鼠标离开stage区域的事件 stage.addEventListener(Event.MOUSE_LEAVE, leaveHandler); } private function leaveHandler(e:Event):void { _move = false; stage.removeEventListener(Event.MOUSE_LEAVE, leaveHandler); } private function loadComplete(e:LoaderEvent):void { trace("load complete", _meshArray.length); //_view.scene.addChild(_loader3D); /*(_meshArray[15] as Mesh).material = new ColorMaterial(0xFF0000); (_meshArray[15] as Mesh).addEventListener(MouseEvent3D.CLICK, meshClick); (_meshArray[16] as Mesh).material = new ColorMaterial(0x00FF00); (_meshArray[16] as Mesh).addEventListener(MouseEvent3D.CLICK, meshClick);*/ } private function meshClick(e:MouseEvent3D):void { trace(e.target.name); _infoText.text = "选择的对象是:"+e.target.name+"\nQQ:781444988"; } private function onAssetComplete(e:AssetEvent):void { if (e.asset.assetType == AssetType.MESH) { var mesh:Mesh = e.asset as Mesh; mesh.mouseEnabled = true; mesh.material = new ColorMaterial(0xFFFFFF * Math.random()); mesh.name = String(_index); mesh.addEventListener(MouseEvent3D.CLICK, meshClick); _index++; _view.scene.addChild(mesh); _meshArray.push(mesh); } } private function onRender(e:Event):void { if (_move) { _hovController.tiltAngle = _lastTiltAngle + 0.3 * (stage.mouseY - _lastMouseY); _hovController.panAngle = _lastPanAngle + 0.3 * (stage.mouseX - _lastMouseX); } _view.camera.lookAt(_loader3D.position); _view.render(); } } }
本例中的AWD模型下载:https://files.cnblogs.com/njflash/myqiu.rar