Loading OBJ model

Software: Away3D 4.x

bellab, Newbie
Posted: 15 May 2013 09:53 AM   Total Posts: 4

I tried to load two simple meshs in OBJ format (cube and sphere created and exported with prefab3d, no materials).

At the ressource complete loader event, I assign color and material manually and explicitly.

My problem is that only the cube is rendered, but the sphere is missing. If I simply change the sequence of the cube and the sphere in OBJ file, everything works fine, both are rendered. Removing both “o tags” naming the meshes from the OBJ file also fixes the bug.

Seems that the sequence of the occuring meshes int the OBJ file matters.
Any hints?

 

   

Avatar
Fabrice Closier, Administrator
Posted: 15 May 2013 10:52 AM   Total Posts: 1265   [ # 1 ]

Order is not relevant. So either there is a bug in your code or one in OBJParser.
Please post the relevant part of your code + a test obj so we can take a look.

 

   

bellab, Newbie
Posted: 15 May 2013 11:24 AM   Total Posts: 4   [ # 2 ]

Thank you very much for your answer to my request,
Attached see the obj-files:
cusp.obj IS NOT working
spcu.obj IS working

cusp.obj has first the cube and second the sphere
spcu.obj has first the sphere and second the cube

See also my relevant code, I am preparsing the obj file and then loading with Loader3D.
(Away3d FP11 4.0.0 beta)

[code

  private function initLoader():void
  {
  // load Model(s)
  Parsers.enableAllBundled();
  loader = new Loader3D();
 
  //loader.addEventListener(AssetEvent.ASSET_COMPLETE, onObjAssetComplete);
 
  // Get OBJ file from maxloader
  var text:String = preloader.getContent(“model”);
  trace(text);
 
  // we dont care about materials (mtl files) and textures //
  var assetLoaderToken:AssetLoaderToken = loader.loadData(text);
  loader.addEventListener(away3d.events.LoaderEvent.RESOURCE_COMPLETE, resourceCompleteHandler);
 
  }
 
  private function resourceCompleteHandler(event:Event):void
  {
  loader.removeEventListener(away3d.events.LoaderEvent.RESOURCE_COMPLETE, resourceCompleteHandler);
  trace(“loader has currently “+loader.numChildren+” children”);
  var asset:IAsset;
  var it : AssetLibraryIterator = AssetLibrary.createIterator();
  var mesh:Mesh;
  while (asset = it.next()) {
  if(asset.assetType == AssetType.MESH)
  {
    mesh = (asset as Mesh);
    //mesh.geometry.scale(0.1);
    for each (var m:SubMesh in mesh.subMeshes){
    //add material color
    if(textureMaterial!= null)
    m.material = textureMaterial;
    else
    {
    var cm:ColorMaterial = new ColorMaterial(0xFFFFFF);
    cm.lightPicker = this.lightPicker;
    m.material = cm;
    }
   
    }
    if(mesh!=null)
    view.scene.addChild(mesh);
  }
  }

  }
code]

Thank in advance!

 

File Attachments
objfiles.zip  (File Size: 54KB - Downloads: 166)
   

Avatar
Fabrice Closier, Administrator
Posted: 15 May 2013 11:49 AM   Total Posts: 1265   [ # 3 ]

Something goes wrong with the downloads…
anyway, first make sure that the second (or somewhere in first obj lines) that there is NO useMtl +url mtl file. If yes, reexport with no dependency or remove all the lines before v tags where mtl material references are set.

then add this listener to your loader.
loader.addEventListener(AssetEvent.MESH_COMPLETE, onMeshReady);

and use this method.
private function onMeshReady(event:AssetEvent):void{
var mesh:Mesh = Mesh(event.asset);
var cm:ColorMaterial = new ColorMaterial(0xFFFFFF);
mesh.material = cm;
MaterialBase(mesh.material).lightPicker = this.lightPicker;
}

and change your complete method by
private function resourceCompleteHandler(event:Event):void
{
view.scene.addChild(ObjectContainer3D(event.currentTarget));
}

 

   

bellab, Newbie
Posted: 15 May 2013 01:02 PM   Total Posts: 4   [ # 4 ]

Ok sorry for the obj-files. I changed it and uploaded the two obj files as zip format. They should be safe now.

I followed your advises as best as I can.
Due to your excellent Prefab3D tool (nice work) I had removed all dependecies to mtl, so that should not be a problem.

I changed my code to your advise, see below but still no luck.
Attribute of numChildren is always 1 (instead of expected number of 2)

private function initLoader():void
  { 
   
// load Model(s) 
   
Loader3D.enableParser(OBJParser);
   
loader = new Loader3D();

   
   
// Get OBJ file from maxloader
   
var text:String preloader.getContent("model");
   
//trace(text);
   
   
trace("sphere"+text.search("sphere"));
   
trace("cube"+text.search("cube"));
   var 
assetLoaderToken:AssetLoaderToken loader.loadData(text);
   
loader.addEventListener(AssetEvent.MESH_COMPLETEonMeshReady);
   
loader.addEventListener(away3d.events.LoaderEvent.RESOURCE_COMPLETEresourceCompleteHandler);
   
   
  
}
  
  
private function onMeshReady(event:AssetEvent):void{
   
var mesh:Mesh Mesh(event.asset);
   var 
cm:ColorMaterial = new ColorMaterial(0xFFFFFF);
   
mesh.material cm;
   
MaterialBase(mesh.material).lightPicker this.lightPicker;
  
}
  
  
private function resourceCompleteHandler(event:Event):void
  { 
   view
.scene.addChild(ObjectContainer3D(event.currentTarget));
  

 

   

Avatar
Fabrice Closier, Administrator
Posted: 15 May 2013 01:24 PM   Total Posts: 1265   [ # 5 ]

set the trace onMeshReady. trace(“mesh name: “+mesh.name);

Also, what is the engine version that you use? Try use dev branch (4.1.1 beta)
Several fixes were made in past on ObjParser and I recall there was an issue on “forgotten” mesh. If you wish to stay on 4.0 (if you are) You can try to replace the parser of 4.0 with the one in Hotfix branch.

Another option in case obj is still failing, is to export as AS3. (no loaders required) where only thing required in your code would be to do
import MyExport;

var myExport:MyExport = new MyExport();
view.scene.addChild(myExport);
Looping over myExport.meshes array would allow you to alter the materials as you wish too.
Or export as awd or any of the by Away supported formats.

I’ve just loaded both your obj, exported as obj and reloaded the obj back in Prefab. all is displayed as expected. As it holds the hotfix branch fixes, there is a chance that by swapping the parser your problem would be gone. Worth a try I think…

 

   

bellab, Newbie
Posted: 15 May 2013 08:31 PM   Total Posts: 4   [ # 6 ]

You were right! I was using 4.0.0 beta. Now after changing to 4.1.1 beta loading my obj files works fine. Everything behaves as expected. Nice!

Thank you very much for your advices and help.
Brilliant.

 

 

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X