Import multiple object using a loop

Software: Away3D 4.x

Goupix, Newbie
Posted: 17 March 2012 04:00 PM   Total Posts: 9

Hello,

The code I am trying to achieve is to use the values ??of an affine chart display object from file. Obj.
My code looks like this one:

/Objet
var mapVillage:Array = new Array();
 
mapVillage[0] [00'bat01''bat03'000000];
 
mapVillage[1] [0000000000];
 
mapVillage[2] [0000'bat01'00'bat03'00];
 
mapVillage[3] [00'bat01''bat02''bat01'00000];
 
mapVillage[4] [0000'bat01'00000];
var 
test:Class3dObject = new Class3dObjectmapVillage );
 
data.scene.addChild(test); 

Class3dObject.as

package  
{
 import away3d
.containers.ObjectContainer3D;
 
 public class 
Class3dObject extends ObjectContainer3D
 {
  
public var  cptI:int = new int,
     
cptJ:int = new int,
     
content:ObjectContainer3D = new ObjectContainer3D;
  
  public function 
Class3dObject(map:Array) 
  
{
   
for ( cptI 0cptI 9cptI++){
    
for ( cptJ 0cptJ 9cptJ++){
     
if (map[cptI][cptJ] != 0{
      
      trace
('Class3dObject("' map[cptI][cptJ] '")');
      var 
param:String map[cptI][cptJ] ';' cptI ';' +cptJ;
      var 
test:Class3dObjectInit = new Class3dObjectInitparam );
      
      
content.addChild(test);
     
}
    }
   }
   addChild
(content);
  
}
 }

Class3dObjectInit.as

package  
{
 import away3d
.containers.ObjectContainer3D;
 
import away3d.entities.Mesh;
 
import away3d.loaders.Loader3D;
 
import away3d.materials.methods.BasicDiffuseMethod;
 
import away3d.materials.methods.BasicSpecularMethod;
 
import away3d.materials.methods.FresnelSpecularMethod;
 
import away3d.materials.methods.SubsurfaceScatteringDiffuseMethod;
 
import away3d.materials.TextureMaterial;
 
import away3d.library.*;
 
import away3d.library.assets.*;
 
import away3d.events.AssetEvent;
 
 
import flash.utils.getDefinitionByName;
 
 public class 
Class3dObjectInit extends ObjectContainer3D
 {
  
private var data:ClassData;
  
  
//scene objects 3ds
  
public var loader:Loader3D;
  
//material objects obj
  
public var subsurfaceMethod:SubsurfaceScatteringDiffuseMethod;
  public var 
fresnelMethod:FresnelSpecularMethod;
  public var 
diffuseMethod:BasicDiffuseMethod;
  public var 
specularMethod:BasicSpecularMethod;
  
  
//Objet
  //Batiments
   //bat01
   
public var bat01:Mesh;
    public var 
bat01x:int 5;
    public var 
bat01y:int 3;
    
[Embed(source="embed/bat01.obj"mimeType="application/octet-stream")]
    
public var Modelbat01:Class;
   
//bat02
   
public var bat02:Mesh;
    public var 
bat02x:int 5;
    public var 
bat02y:int 3;
    
[Embed(source="embed/bat02.obj"mimeType="application/octet-stream")]
    
public var Modelbat02:Class;
   
//bat03
   
public var bat03:Mesh;
    public var 
bat03x:int 7;
    public var 
bat03y:int 5;
    
[Embed(source="embed/bat03.obj"mimeType="application/octet-stream")]
    
public var Modelbat03:Class;
   
  public var 
obj:Array = new Array;

  public function 
Class3dObjectInit(objet:String
  
{
   data 
= new ClassData();
   
obj objet.split(';');
   
trace('nom : ' obj[0] ' - X : ' obj[1] ' - Y : ' obj[2]);
   
   
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
   
   switch( 
obj[0] {
    
case "bat01":
     
trace("asset bat01");
     
AssetLibrary.loadData( new Modelbat01() );
     break;
    case 
"bat02":
     
trace("asset bat02");
     
AssetLibrary.loadData( new Modelbat02() );
     break;
    case 
"bat03":
     
trace("asset bat03");
     
AssetLibrary.loadData( new Modelbat03() );
     break;
   
}
  }
   
private function onAssetComplete(event:AssetEvent):void
   {
    AssetLibrary
.removeEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
    if (
event.asset.assetType == AssetType.MESH{
     trace
('Insertion de : ' obj[0]);
     
     
this[obj[0]] event.asset as Mesh;
     
//this[obj[0]].name = obj[0] + 'x' + obj[1] + 'y' + obj[2];
     
this[obj[0]].geometry.scale(1);
     
this[obj[0]].= ( obj[1] + ( this[obj[0]+"x"/) ) * 1;
     
this[obj[0]].= ( obj[2] + ( this[obj[0] "y") ) * 1;
     
     
addChildthis[obj[0]] );
    
}
   }
 }

The problem lies in the fact that I only have one object that appears. for after my test, this is the first object in the list. It does not take into account the following!

Would you have a solution?

Sincerely yours,
Goupix

   

Avatar
Matse, Sr. Member
Posted: 17 March 2012 05:01 PM   Total Posts: 149   [ # 1 ]

Parsing a mesh is an asynchronous process, I didn’t check but I would guess that adding the same eventListener several times only results in 1 eventListener : you call Class3dObjectInit several times in a row, but onAssetComplete gets called later, after a few frames, when the meshes are parsed and loaded.
So when your first mesh gets loaded, it removes the eventListener and even though the other meshes get loaded, you never “see” them wink

It’s a bad idea to do it like that anyway : you should load all meshes first, and only then create your level. The reason is because you want to re-use geometry, not load a new one for exactly the same model. So you load “bat01.obj” once, it gets stored into the AssetLibrary or you store it your own way if you prefer, and everytime you need a “bat01” mesh, you clone the original one.

See this article by DerSchmale for details about why re-using geometry (and materials) is the way to go : http://www.derschmale.com/2011/07/25/building-efficient-content-in-away3d-4-0-sharing-materials-geometries/

   

Goupix, Newbie
Posted: 19 March 2012 10:47 AM   Total Posts: 9   [ # 2 ]

Thank you to you.

I finally found the solution.
It was enough to add a token when listening to the Asset.

this.posX px;
this.posY py;
ns px '_' py;
AssetLibrary.loadData( new Modelbat01(), nullns );

...

private function 
onAssetComplete(event:AssetEvent):void
{
 
if (event.asset.assetType == AssetType.MESH && event.asset.assetNamespace == ns {
  AssetLibrary
.removeEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
 ...
 
}
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X