纳金网
标题:
Flare3D 2.0模型队列加载器Flare3DQueueLoader
[打印本页]
作者:
.
时间:
2013-3-15 13:51
标题:
Flare3D 2.0模型队列加载器Flare3DQueueLoader
有些时候我们在加载3D模型的时候需要把多个模型依次加载,特别是移动平台,依次加载多个模型可以大幅度提高加载时的性能,但是Flare3D API目前还没提供依次加载多个模型的方式,所以我在这里写了个Flare3D的队列加载器Flare3DQueueLoader与朋友们分享。 直接打包成了SWC库,可以直接下载: 使用示例:package{ import flare.basic.Scene3D; import flare.basic.Viewer3D; import flare.core.Camera3D; import flare.core.Pivot3D; import flare.loaders.Flare3DLoader1; import flare.materials.filters.EnvironmentFilter; import flare.materials.filters.PlanarFilter; import flare.materials.filters.SpecularFilter; import flare.system.Input3D; import flare.utils.Pivot3DUtils; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; import net.fl3d.flare.loader.Flare3DQueueLoader; import net.fl3d.racing.model.Car; public class Flash3DCar extends Sprite { private var _scene:Scene3D; private var _camera:Camera3D; private var _loaderManager:Flare3DQueueLoader; private var _timer:Timer; private var _car
ivot3D; public function Flash3DCar() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; initScene3D(); initCamera3D(); load3DModels(); } private function initScene3D():void { _scene = new Viewer3D(this); _scene.antialias = 2; _scene.showLogo = false; _scene.showMenu = false; _scene.registerClass(SpecularFilter); _scene.registerClass(EnvironmentFilter); _scene.registerClass(PlanarFilter); _scene.registerClass(Flare3DLoader1); } private function initCamera3D():void { _camera = new Camera3D("myCamera"); _scene.camera = this._camera; _camera.setPosition(20, 20, -20); _camera.lookAt(0, 0, 0); } private function load3DModels():void { /** * 生成队列加载器实例 * 参数scene:flare.basic.Scene3D, 可将加载后的模型直接加入到场景(可选) * 如果scene为null,那么加载所有模型都不能自动加入到场景,需要手动加入(_scene.addChild(..)) */ _loaderManager = new Flare3DQueueLoader(_scene); //打印加载详细过程的追踪信息 //_loaderManager.traceDetail = ***e; /** * 将需要加载的模型放入队列 * request:可处理如下类型: * flash.net.URLStream, * flash.utils.ByteArray, * Class, * String * key:加载模型的标识,方便在下文中提取模型实例 * 如果Flare3DQueueLoader实例中scene不为null, 则当key为null时自动添加到scene * * addToScene: 是否加入到scene,当Flare3DQueueLoader实例中scene为null时,此值永远为false */ //加入汽车模型,此模型不自动加入到场景,而且下文中可以通过“car”获取此模型 _loaderManager.push("res/jinngame_car.f3d", "car"); //加入坐标轴模型,此模型自动加入到场景 _loaderManager.push("res/axis.f3d", "axis", ***e); //加入地图1、2、3模型,此模型自动加入到场景,地图3可以在下文中提取,地图1、2则不可以 _loaderManager.pushToScene("res/map/jinngame_track0.f3d"); _loaderManager.push("res/map/jinngame_track1.f3d", null, ***e); _loaderManager.push("res/map/jinngame_track2.f3d", "map3", ***e); //加入地图碰撞体模型,次模型自动加入到场景,且可以在下文中提取 _loaderManager.push("res/jinngame_trackpzt.f3d", "mapPzt", ***e); _loaderManager.push("res/jinngame_carpzt.f3d", "carPzt"); _loaderManager.push("res/jinngame_weiqi.f3d", "n2"); //添加事件*** _loaderManager.addEventListener(Flare3DQueueLoader.PROGRESS_EVENT, progress); _loaderManager.addEventListener(Flare3DQueueLoader.COMPLETE_ONE_EVENT, completeOne) _loaderManager.addEventListener(Flare3DQueueLoader.COMPLETE_EVENT, complete); //开始加载 _loaderManager.load(); } private function progress(e:Event):void { trace("加载中:" + _loaderManager.totalLoadProgress * 100 + "%..."); /* trace("正在加载的模型索引:" + _loaderManager.currentLoadIndex); //索引从0开始 trace("正在加载的模型的进度:" + _loaderManager.currentLoadProgress);//从0 ~ 1 trace("整体加载进度:" + _loaderManager.totalLoadProgress);//从0 ~ 1 */ //_loaderManager.totalLoadCount;需要加载的模型的个数 } private function completeOne(e:Event):void { trace("已加载完成" + (_loaderManager.currentLoadIndex + 1) + "/" + _loaderManager.totalLoadCount + "个"); // 1 / 8 ~ 8 / 8 } private function complete(e:Event):void { trace("all complete..."); //获取已加载的模型 _car = _loaderManager.getPivot3D("car"); _scene.addChild(_car); _car.setPosition(-200, 0, -230); _car.rotateY(90); _loaderManager.getPivot3D("mapPzt").visible = false; _timer = new Timer(20, 0); _timer.addEventListener(TimerEvent.TIMER, game); _timer.start(); } private function game(e:TimerEvent):void { if(Input3D.keyDown(Input3D.UP) || Input3D.keyDown(Input3D.W)) { trace("前进"); _car.translateZ(1); } if(Input3D.keyDown(Input3D.DOWN) || Input3D.keyDown(Input3D.S)) { trace("后退"); _car.translateZ(-1); } Pivot3DUtils.setPositionWithReference(this._camera, 0, 16, -30, this._car, 0.178); Pivot3DUtils.lookAtWithReference(this._camera, 0, 8, 0, this._car); } }}同时公布源码,希望朋友们多提建议以便改进。
【Flare3DQueueLoader.as】package net.fl3d.flare.loader { import flare.basic.Scene3D; import flare.core.Pivot3D; import flare.loaders.Flare3DLoader; import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.Dictionary; /** * 此类用于模型队列依次加载 * 作者:V.Zhang * http://www.fl3d.net * 最后修改时间:2012年11月28日 09:41:43 */ public class Flare3DQueueLoader extends EventDispatcher { public static const PROGRESS_EVENT:String = Scene3D.PROGRESS_EVENT; public static const COMPLETE_EVENT:String = Scene3D.COMPLETE_EVENT; public static const COMPLETE_ONE_EVENT:String = "complete_one"; private var _scene:Scene3D;//3D场景 private var _loader:Flare3DLoader; private var _items:Vector.<QueueLoaderItem>;//需要加载的模型队列 private var _pivots
ictionary;//保存有key的Pivot3D实例的容器 private var _currentLoadIndex:uint;//当前加载索引 private var _totalLoadCount:uint;//需要加载的模型总数 private var _currentLoadProgress:Number;//当前单个模型的加载进度 private var _totalLoadProgress:Number;//总体加载进度 private var _allLoaded:Boolean;//是否全部加载完毕 private var _traceDetail:Boolean;//是否打印追踪信息 public function Flare3DQueueLoader(scene:Scene3D = null) { _scene = scene; _items = new Vector.<QueueLoaderItem>; _pivots = new Dictionary(); _currentLoadIndex = 0; _totalLoadCount = 0; _currentLoadProgress = 0; _totalLoadProgress = 0; _allLoaded = false; _traceDetail = false; } /** * 将需要加载的模型放入队列 * request:可处理如下类型: * flash.net.URLStream, * flash.utils.ByteArray, * Class, * String * key:加载模型的标识, 方便在下文中提取模型实例 * 如果Flare3DQueueLoader实例中scene不为null, 则当key为null时自动添加到scene * * addToScene: 是否加入到scene,当Flare3DQueueLoader实例中scene为null时,此值永远为false */ public function push(request:*, key:String = null, addToScene:Boolean = false):void { if(key == null && _scene == null) { throw new Error("当场景为null时,key不能为null!"); } if(_scene == null) { addToScene = false; } var item
ueueLoaderItem = new QueueLoaderItem(request, key, addToScene); _items.push(item); } public function pushToScene(request:*):void { if(_scene == null) { throw new Error("当前scene为null,不能加入模型到scene!"); } push(request, null, ***e); } public function load():void { if(_items.length == 0) { if(_traceDetail) { trace("当前加载队列为空!"); } return; } if(_currentLoadIndex == 0) { _totalLoadCount = _items.length; if(_traceDetail) { trace("============= 开始加载 ============="); trace("============= 加载队列:"); for(var i:uint = 0; i < _items.length; i++) { trace("[" + i + "]: " + _items
.key + " => " + _items
.request); } trace("=================================="); } } if(_currentLoadIndex < _totalLoadCount) { if(_traceDetail) { trace("开始加载: " + _items[_currentLoadIndex].key + " => " + _items[_currentLoadIndex].request); } _loader = new Flare3DLoader(_items[_currentLoadIndex].request); _loader.addEventListener(Scene3D.PROGRESS_EVENT, pivotProgress); _loader.addEventListener(Scene3D.COMPLETE_EVENT, pivotComplete); _currentLoadProgress = 0; _loader.load(); } else { if(_traceDetail) { trace("所有模型均加载完毕!"); } _loader = null; _allLoaded = ***e; this.dispatchEvent(new Event(COMPLETE_EVENT, false, false)); } } private function pivotProgress(e:Event):void { _currentLoadProgress = _loader.bytesLoaded / _loader.bytesTotal; _totalLoadProgress = (_currentLoadIndex + _currentLoadProgress) / _totalLoadCount; this.dispatchEvent(new Event(PROGRESS_EVENT, false, false)); if(_traceDetail) { trace("当前进度:" + _currentLoadProgress * 100 + "%, " + "当前次序:" + (_currentLoadIndex + 1) + "/" + _totalLoadCount + ", " + "总体进度:" + _totalLoadProgress * 100 + "%"); } } private function pivotComplete(e:Event):void { if(_traceDetail) { trace("加载完毕: " + _items[_currentLoadIndex].key + " => " + _items[_currentLoadIndex].request); } var currentKey:String = _items[_currentLoadIndex].key; if(currentKey != null) { _pivots[currentKey] = _loader; } else { _scene.addChild(_loader); } if(_items[_currentLoadIndex].addToScene) { _scene.addChild(_loader); } _loader.removeEventListener(Scene3D.PROGRESS_EVENT, pivotProgress); _loader.removeEventListener(Scene3D.COMPLETE_EVENT, pivotComplete); this.dispatchEvent(new Event(COMPLETE_ONE_EVENT, false, false)); _currentLoadIndex++; load(); } public function getPivot3D(key:String)
ivot3D { return _pivots[key]; } public function get currentLoadProgress():Number { return _currentLoadProgress; } public function get totalLoadProgress():Number { return _totalLoadProgress; } public function get currentLoadIndex():Number { return _currentLoadIndex; } public function get totalLoadCount():Number { return _totalLoadCount; } public function get allLoaded():Boolean { return _allLoaded; } public function set traceDetail(val:Boolean):void { _traceDetail = val; } }}
【QueueLoaderItem.as】package net.fl3d.flare.loader{ public class QueueLoaderItem { private var _request:*; private var _key:String; private var _addToScene:Boolean; public function QueueLoaderItem(request:*, key:String = null, addToScene:Boolean = false) { _request = request; _key = key; _addToScene = addToScene; } public function get key():String { return _key; } public function get addToScene():Boolean { return _addToScene; } public function get request():* { return _request; } }}
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5