2012年7月23日 星期一

[教學] Away3D 4.0 Gold 基礎模型載入

為大家介紹Away3D 4.0 Gold 載入.3ds 檔, 然後手動貼上紋理(Texture) 於模型:

請按 [觀看] 範例

當主程式進入ADDED_TO_STAGE 狀態, 我們會設定以下步驟:
  • 設定View3D 與Camera3D:
    private function setUpView():void
    {
      // <----- define variable
      _view = new View3D();
      
      // <----- set View3d
      _view.width = width;
      _view.height = height;
      _view.backgroundColor = 0xffffff;
      _view.antiAlias = 16; //抗鋸齒有效設值為 0,2,4和16
      
      // <----- set camera
      _view.camera.position = new Vector3D( 0, 1400, -1000);
      _view.camera.lookAt(new Vector3D(0, 400, 0));
      _view.camera.lens.near = 0.2;
      (_view.camera.lens as PerspectiveLens).fieldOfView = 60;
      
      // <----- add object
      addChild(_view);
      addChild(new AwayStats(_view));
    }
    

  • 設定光源:
    private function setUpLights():void
    {
      var light1:DirectionalLight = new DirectionalLight(0,-2,1);
      var light2:DirectionalLight = new DirectionalLight(0,2,-2);
      _lightPicker = new StaticLightPicker([light1, light2]);
      _view.scene.addChild(light1);
      _view.scene.addChild(light2);
    }
    

  • 載入.3ds 檔, 並關閉自動貼Texture 功能 new AssetLoaderContext(false):
    private function initMesh():void
    {
      Loader3D.enableParser(Max3DSParser);
      _loader = new Loader3D();
      _loader.addEventListener(AssetEvent.MESH_COMPLETE, meshCompleteHandler);
      _loader.loadData(MyMesh, new AssetLoaderContext(false), null, new Max3DSParser());
      _loader.scale(1);
      _view.scene.addChild(_loader);
    }
    

  • 最後, Mesh 完成載入時, 手動貼上Texture 於每一個Mesh 上:
    private function meshCompleteHandler(event:AssetEvent):void{
      // <----- define materials
      var m1:TextureMaterial = new TextureMaterial(Cast.bitmapTexture(Skin01Texture));
      var m2:TextureMaterial = new TextureMaterial(Cast.bitmapTexture(Skin02Texture));
      
      // <----- set materials
      m1.specular = 0.4;
      m1.lightPicker = _lightPicker;
      m2.specular = 0.9;
      m2.lightPicker = _lightPicker;
      
      // <----- apply materials to mesh
      for (var i:int = 0; i < _loader.numChildren; i++){
        var mesh:Mesh = Mesh(_loader.getChildAt(i));
        if(mesh.name.indexOf("polySurfac")>-1)
          mesh.material = m1;
        else
          mesh.material = m2;
      }
    }
    

播放時, Stage3D 會因應系統自動選擇DirectX, OpenGL 和Software 作為硬體加速模式. 我們製作和顯示模型時, 應考慮多數用戶的硬體效能, 選擇合適設定, 為用戶達至最佳顯示效果.
大家可以 [下載] 範例試試看.

沒有留言: