Away3D - 给加载进来的3D模型设置皮肤(3ds格式为例)
在前文中:Away3D - 加载外部的3D模型(3ds格式为例)。演示了如何在 Away3D 工程项目中加载 3d 模型。对于 3ds 格式的模型来说,通常其内部就已经定义好对应的贴图名字了。但也有时候模型中并没有将所有对象的皮肤图片都设置好,这是我们就需要在模型加载完毕后动态地添加皮肤纹理了。
(2)由于模型不是我做的,所以我并不知道花盆对应的名字。只能遍历 mesh,然后一个一个试了。(最后发现花盆由上下两部分组成,mesh.name 为"3dstylish0"和"3dstylish3")
(3)这里给花盆简单地添加个纯色资源(白色),并添加个灯光。
3,最终效果图
相关资源包下载:assets3.zip
1,缺少部分皮肤的模型演示
下面是一个盆栽的模型,我们将其加载到舞台上会发现,虽然叶子提供了贴图,下方的花盆却没有。最终效果就是花盆白花花一片,表面纹理细节都看不到。
2,给花盆添加纹理皮肤
(1)通过 LoaderEvent.RESOURCE_COMPLETE 的响应方法,我们可以在模型加载完毕后设置其内部各个对象的皮肤。(当然也可以修改原来默认的皮肤)(2)由于模型不是我做的,所以我并不知道花盆对应的名字。只能遍历 mesh,然后一个一个试了。(最后发现花盆由上下两部分组成,mesh.name 为"3dstylish0"和"3dstylish3")
(3)这里给花盆简单地添加个纯色资源(白色),并添加个灯光。
package{ import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Vector3D; import flash.net.URLRequest; import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d.entities.Mesh; import away3d.events.LoaderEvent; import away3d.lights.PointLight; import away3d.loaders.Loader3D; import away3d.loaders.parsers.Max3DSParser; import away3d.materials.ColorMaterial; import away3d.materials.TextureMaterial; import away3d.materials.lightpickers.StaticLightPicker; [SWF(frameRate="60", backgroundColor="#FFFFFF")] public class ModelLoader extends Sprite { private var _view3D:View3D; private var cameraController:HoverController;//360全景展示相机控制器 //用来加载3D模型 private var loader:Loader3D; //地板材质 private var floorMaterial:TextureMaterial; private var _lastX:Number = 0; private var _lastY:Number = 0; //灯光对象 private var light1:PointLight; //点光源 private var light2:PointLight; //点光源 private var light3:PointLight; //点光源 private var lightPicker:StaticLightPicker; //灯光容器 public function ModelLoader() { initEngine(); initLights(); initMaterials(); initObjects(); initListeners(); } /** * 初始化引擎 */ private function initEngine():void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // 创建一个视口 _view3D = new View3D(); _view3D.antiAlias = 4; //设置抗锯齿级别 //初始化摄像头 cameraController = new HoverController(_view3D.camera); cameraController.tiltAngle = 15; addChild(_view3D); } /** * 初始化灯光 */ private function initLights():void { //点光源 light1 = new PointLight(); light1.position = new Vector3D(400, 800, 800); light1.color = 0xffffEB light1.ambient = 0.3; light2 = new PointLight(); light2.position = new Vector3D(-400, 800, 800); light2.color = 0xffffEB light2.ambient = 0.1; light3 = new PointLight(); light3.position = new Vector3D(0, 400, -800); light3.color = 0xffffEB light3.ambient = 0.05; //添加到灯光容器中 lightPicker = new StaticLightPicker([light1,light2,light3]); } /** * 初始化材质 */ private function initMaterials():void { } /** * 初始化物体 */ private function initObjects():void { loadModel("3dstylish_dvs001.3DS"); } /** * 加载模型 */ public function loadModel(product:String):void { Loader3D.enableParser(Max3DSParser); loader = new Loader3D(); loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onModelLoaded); loader.load(new URLRequest("assets3/" + product)); loader.scale(3); _view3D.scene.addChild(loader); } /** * 模型加载完毕 */ private function onModelLoaded(e: LoaderEvent) : void { var i:int; var mesh:Mesh; var numChil:uint = loader.numChildren; for (i = 0; i < numChil; ++i) { mesh = Mesh(loader.getChildAt(i)); trace(mesh.name); //设置画盆的皮肤 if(mesh.name == "3dstylish3" || mesh.name == "3dstylish0"){ //给花盆设置皮肤和灯光 var mat:ColorMaterial; mat = new ColorMaterial(0xCCCCCC, 1); mat.lightPicker = lightPicker; mesh.material = mat; } } } /** * 初始化监听 */ private function initListeners():void { addEventListener(Event.ENTER_FRAME, _onEnterFrame); //鼠标事件监听 stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel); stage.addEventListener(Event.RESIZE, onResize); onResize(); } /** * 渲染视图 */ private function _onEnterFrame(e:Event):void { //渲染视图 _view3D.render(); } /** * 使用舞台大小一直全屏 */ private function onResize(event:Event = null):void { _view3D.width = stage.stageWidth; _view3D.height = stage.stageHeight; } /** * 鼠标滚轮事件 */ private function onWheel(e:MouseEvent):void { if(e.delta > 0){ if(cameraController.distance < 1000) cameraController.distance += 100; }else{ if(cameraController.distance > 600) cameraController.distance -= 100; } } /** * 鼠标按下事件 */ private function onMouseDown(event:MouseEvent):void { _view3D.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); _lastX = _view3D.mouseX; _lastY = _view3D.mouseY; } /** * 鼠标弹起事件 */ private function onMouseUp(event:MouseEvent):void { _view3D.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } /** * 鼠标移动事件 */ private function mouseMoveHandler(event:MouseEvent):void { //移动摄像机 var dx:Number = _view3D.mouseX - _lastX; var dy:Number = _view3D.mouseY - _lastY; cameraController.panAngle += dx; cameraController.tiltAngle += dy; _lastX = _view3D.mouseX; _lastY = _view3D.mouseY; } } }
3,最终效果图
相关资源包下载:assets3.zip