away3d 4.0加载obj文件
本例使用了7个建筑模型,其中包括哈尔滨的经典建筑:防洪纪念塔和索菲亚教堂。当然建筑的安排并没有按照实际的位置来做,而是将防洪纪念塔放置到最右侧,索菲亚教堂在最左侧。试用了LoaderMax来加载资源:
[code lang="as3"]
var queue:LoaderMax = new LoaderMax({name:"main",autoDispose:false,onProgress:onProgressHandler,onComplete:onCompleteHandler,onError:onErrorHandler});
dataLoader = new DataLoader("arch1/arch1.obj",{name:"arch1",format:"text"})
queue.append(dataLoader);
imageLoader = new ImageLoader("arch1/arch1-map.jpg",{name:"arch1-map"})
queue.append(imageLoader);
imageLoader = new ImageLoader("arch1/arch1-specular.jpg",{name:"arch1-specular"})
queue.append(imageLoader);
imageLoader = new ImageLoader("arch1/arch1-normal.jpg",{name:"arch1-normal"})
queue.append(imageLoader);
[/code]
使用自定义的loadModel函数来加载模型:
[code lang="as3"]
private function loadModel(name:String):void{
_material = new TextureMaterial(new BitmapTexture(ContentDisplay(LoaderMax.getContent(name+"-map")).rawContent.bitmapData));
_material.normalMap = new BitmapTexture(ContentDisplay(LoaderMax.getContent(name+"-normal")).rawContent.bitmapData);
_material.specularMap = new BitmapTexture(ContentDisplay(LoaderMax.getContent(name+"-specular")).rawContent.bitmapData);
_material.lightPicker = _lightPicker;
_material.gloss = 10;
_material.specular = 1;
_material.ambientColor = 0x303040;
_material.ambient = 1;
subsurfaceMethod = new SubsurfaceScatteringDiffuseMethod(2048, 2);
subsurfaceMethod.scatterColor = 0xff7733;
subsurfaceMethod.scattering = 0.05;
subsurfaceMethod.translucency = 4;
_material.diffuseMethod = subsurfaceMethod;
fresnelMethod = new FresnelSpecularMethod(true);
_material.specularMethod = fresnelMethod;
_material.diffuseMethod = new BasicDiffuseMethod();
_material.specularMethod = new BasicSpecularMethod();
Parsers.enableAllBundled();
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE,onAssetComplete);
AssetLibrary.loadData(LoaderMax.getContent(name));
}
[/code]
正面效果:
<img class="aligncenter size-medium wp-image-810" title="away3d_house2" src="http://www.everyinch.net/wp-content/uploads/2012/05/away3d_house2-300x176.jpg" alt="" width="300" height="176" />
背面的效果:
<img class="aligncenter size-medium wp-image-811" title="away3d_house1" src="http://www.everyinch.net/wp-content/uploads/2012/05/away3d_house1-300x180.jpg" alt="" width="300" height="180" />
从索菲亚教堂看过去:
<img class="aligncenter size-medium wp-image-809" title="away3d_house3" src="http://www.everyinch.net/wp-content/uploads/2012/05/away3d_house3-300x177.jpg" alt="" width="300" height="177" />