Away3D基础教程(二):加载外部模型

预览地址:http://leoas.host-home-idc.k5.fhfinance.com/tutorials/2/glass.html

模型随鼠标转动,中键滚轮缩放。

模型和完整源码下载:http://files.cnblogs.com/njflash/src2.rar

预览图片:

Away3D基础教程(二):加载外部模型

  away3d支持的模型格式较为丰富。MD2,MD5,3DS,OBJ等我们常用的格式都支持。另外还有away3d的专有格式awd。awd可以通过away3d 的模型编辑器preFab来导出生成。通常支持骨骼动画的模型有MD2 MD5 3DS AWD 等。这里我们将加载一个AWD模型。

1:首先创建3D视口,设置视口的相关参数

_view = new View3D();

//背景色

_view.backgroundColor = 0x666666;

//抗锯齿参数

_view.antiAlias = 4;

this.addChild(_view);

2:初始化渲染侦听器和鼠标滚轮侦听

this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);

3:用Loader3D类来加载和解析模型

//Parsers.enableAllBundled()这样away3d就能解析所有它能解析的格式了

Parsers.enableAllBundled();

            

_loader = new Loader3D();

_loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE,onResourceComplete);

//_loader.addEventListener(AssetEvent.ASSET_COMPLETE,assetComplete);

_loader.addEventListener(LoaderEvent.LOAD_ERROR, onLoadError);

//AntModel为绑定嵌入的模型资源

_loader.loadData(new AntModel());

4:加载完成后的事件处理函数

_loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE,onResourceComplete);

_loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);

_view.scene.addChild(_loader);

_view.camera.z=1000;

_view.camera.lookAt(_loader.position);

5:完整源码

package

{

    import away3d.containers.View3D;

    import away3d.entities.Mesh;

    import away3d.events.AssetEvent;

    import away3d.events.LoaderEvent;

    import away3d.loaders.Loader3D;

    import away3d.loaders.misc.*;

    import away3d.loaders.parsers.Parsers;

    import away3d.library.assets.AssetType;

    

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.net.URLRequest;

    import flash.events.MouseEvent;

    

    [SWF(width="550", height="400", frameRate="60", backgroundColor="0x666666")]

    public class GettingStartedWithAway3D extends Sprite

    {

        [Embed(source="vase.awd",mimeType="application/octet-stream")]

        public static var AntModel:Class;

        

        private var _view : View3D;

        private var _loader : Loader3D;

        

        public function GettingStartedWithAway3D()

        {

            _view = new View3D();

            //背景色

            _view.backgroundColor = 0x666666;

            //抗锯齿参数

            _view.antiAlias = 4;

            this.addChild(_view);

            

            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

            stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);

            

            //Parsers.enableAllBundled()这样away3d就能解析所有它能解析的格式了

            Parsers.enableAllBundled();

            

            _loader = new Loader3D();

            _loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);

            /*下面这个事件精确到侦听模型中每一个Mesh是否加载完成,可以更简单的提取MESH,

            可以打开这里的注释和下面的事件处理函数看看不同的效果*/

            //_loader.addEventListener(AssetEvent.ASSET_COMPLETE, assetComplete);

            _loader.addEventListener(LoaderEvent.LOAD_ERROR, onLoadError);

            //AntModel为绑定嵌入的模型资源

            _loader.loadData(new AntModel());

        }

        

        /*private function assetComplete(e:AssetEvent):void {

            //trace(e.asset.assetType == AssetType.MESH);

            if (e.asset.assetType == AssetType.MESH) {

                var mesh3d:Mesh = e.asset as Mesh;

                _view.scene.addChild(mesh3d);    

            }

        }*/

        

        

        private function onResourceComplete(ev : LoaderEvent) : void

        {

            _loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);

            _loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);

            _view.scene.addChild(_loader);

            _view.camera.z=1000;

            _view.camera.lookAt(_loader.position);

        }

        

        

        private function onLoadError(ev : LoaderEvent) : void

        {

            trace('Could not find', ev.url);

            _loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);

            _loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);

            _loader = null;

        }

        

        private function onWheel(e:MouseEvent):void{

            trace(e.delta);

            if(e.delta>0){

                _view.camera.moveForward(30);

            }else{

                _view.camera.moveBackward(30);

            }

        }

        

        private function onEnterFrame(ev : Event) : void

        {

            _loader.rotationY = stage.mouseX - stage.stageWidth/2;

            _view.camera.y = 3 * (stage.mouseY - stage.stageHeight/2);

            _view.camera.lookAt(_loader.position);

            

            _view.render();

        }

    }

}

 

 

你可能感兴趣的:(基础)