Asset loading management - naming has to happen once asset is loaded ?

Software: Away3D 4.x

Avatar
Matse, Sr. Member
Posted: 21 September 2011 11:13 AM   Total Posts: 149

I’d like to be able to load multiple assets and handle them in a generic way, but I can’t seem to find a way to name an asset before it’s fully loaded, and can’t access it’s url or something that would allow me to distinguish it from other assets being loaded… the only ways I can think of seem to be :

- addEventListener on the AssetLoaderToken returned by AssetLibrary, and assign it to a specific function, which is not what I’m after.

- use namespace as a name, which is not very logical but should work.

Am I missing something ? I saw this thread http://away3d.com/forum/viewthread/437/

but AssetLibrary.namingStrategyPreference doesn’t seem to exist anymore.

I can’t think of a way to name assets once they’re loaded, unless I load them one after the other and then know exactly which asset has just finished loading… am I missing something ?

Hope I’m understandable here smile

BTW congrats to the away3D team for the awesome work, I’m very pleased with the framework so far although it’s a bit hard to get into it at first, keep it up !

   

Avatar
Matse, Sr. Member
Posted: 22 September 2011 07:18 PM   Total Posts: 149   [ # 1 ]

In case anyone is interested, here’s the method I came up with. Obviously it’s not perfect nor generic, but that’s for a protoype so I’ll probably come back to this later.

I’d love to hear from other people how they’re doing it, or if I’m “wrong”, or if there’s a better way, or an easier way to do the same thing… etc smile

package  
{
 import away3d
.events.AssetEvent;
 
import away3d.events.LoaderEvent;
 
import away3d.library.AssetLibrary;
 
import away3d.library.assets.AssetType;
 
import away3d.loaders.parsers.MD5AnimParser;
 
import away3d.loaders.parsers.MD5MeshParser;
 
 
import flash.events.Event;
 
import flash.events.EventDispatcher;
 
import flash.net.URLRequest;
 
 
/**
  * ...
  * @author Matse
  */
 
public class ResourceLoader extends EventDispatcher 
 {
  
protected var _basePath:String "assets/";
  protected var 
_resourceList:Vector.<Vector.<String>>;
  protected var 
_resourceCount:int;
  protected var 
_loadCount:int;
  
  public function 
ResourceLoader() 
  
{
   _resourceList 
Vector.<Vector.<String>>([
   
// PLAYER resources
   
Vector.<String>(["player::mesh""swar/swar.md5mesh"]),
   
Vector.<String>(["player::diffuse0""swar/swar_diffuse.png"]),
   
Vector.<String>(["player::normal0""swar/swar_normal.png"]),
   
Vector.<String>(["player::diffuse1""swar/weapon_diffuse.png"]),
   
Vector.<String>(["player::normal1""swar/weapon_normal.png"]),
   
Vector.<String>(["player::diffuse2""swar/hair_diffuse.png"]),
   
Vector.<String>(["player::normal2""swar/hair_normal.png"]),
   
Vector.<String>(["player::idle""swar/idle.md5anim"]),
   
//Vector.<String>(["", ""]),
   
   // MONSTER 0 resources
   //Vector.<String>(["", ""]),
   
]);
   
   
AssetLibrary.enableParser(MD5AnimParser);
   
AssetLibrary.enableParser(MD5MeshParser);
   
   
_resourceCount _resourceList.length;
   
_loadCount 0;
  
}
  
  
public function startLoading():void
  {
   AssetLibrary
.addEventListener(AssetEvent.ASSET_COMPLETEassetCompletefalse0true);
   
AssetLibrary.addEventListener(LoaderEvent.RESOURCE_COMPLETEresourceCompletefalse0true);
   
AssetLibrary.addEventListener(LoaderEvent.LOAD_ERRORloadErrorfalse0true);
   
   var 
req:URLRequest;
   
   for (var 
i:int 0_resourceCounti++) {
    req 
= new URLRequest(_basePath _resourceList[i][1]);
    
AssetLibrary.load(reqnullnull_resourceList[i][0]);
   
}
  }
  
  
protected function assetComplete($evt:AssetEvent):void
  {
   
var ns:String $evt.asset.assetNamespace;
   
   
AssetLibrary.removeAsset($evt.assetfalse);
   
   var array:Array = 
ns.split("::");
   if (
$evt.asset.assetType == AssetType.SKELETON{
    $evt
.asset.resetAssetPath("skeleton"array[0]);
   
else {
    $evt
.asset.resetAssetPath(array[1]array[0]);
   
}
   AssetLibrary
.addAsset($evt.asset);
  
}
  
  
protected function resourceComplete($evt:LoaderEvent):void
  {
   _loadCount 
++;
   if (
_loadCount == _resourceCount{
    allDone
();
   
}
  }
  
  
protected function loadError($evt:LoaderEvent):void
  {
   
  }
  
  
protected function allDone():void
  {
   AssetLibrary
.removeEventListener(AssetEvent.ASSET_COMPLETEassetComplete);
   
AssetLibrary.removeEventListener(LoaderEvent.RESOURCE_COMPLETEresourceComplete);
   
AssetLibrary.removeEventListener(LoaderEvent.LOAD_ERRORloadError);
   
   
// clear assets from loading namespaces
   
for (var i:int 0_resourceCounti++) {
    AssetLibrary
.removeNamespaceAssets(_resourceList[i][0]false);
   
}
   
   dispatchEvent
(new Event(Event.COMPLETE));
  
}
  
  
//________________________________________________________________________________
  // GETTERS & SETTERS
  
public function get loadProgress():int
  {
   
return _loadCount;
  
}
  
  
public function get loadTotal():int
  {
   
return _resourceCount;
  
}
 }

edit : the “preview post” feature doesn’t seem to be working raspberry

Just wanted to add : using this “method” I can easily retrieve my assets “later” in other scripts, for example :

playerMesh AssetLibrary.getAsset("mesh""player"); 
   

Richard Olsson, Administrator
Posted: 24 September 2011 01:37 PM   Total Posts: 1192   [ # 2 ]

Unfortunately there is no way to build a feature like the one you’re requesting, for the general case. Because a single file can (and usually will) contain many resources, it is impossible to apply a name before the loading starts. Because of this, you are expected to either set the names in the files (i.e. establish a naming convention with your artist) or set them as they come in using some application specific logic. That application specific logic is what you have done above to solve your problem, and is not something that we can generalize.

   

Avatar
Matse, Sr. Member
Posted: 24 September 2011 04:02 PM   Total Posts: 149   [ # 3 ]

Thanks for answering, sorry if it felt like a feature request : it wasn’t, I was just looking for directions about how to handle asset loading as I was feeling like I was missing something.

Your explanation makes perfect sense to me, I think I got a bit misleaded by the fact I started right away with md5 models from my artist and if I remember correctly the asset name was always “null”. I’ll check that again to see if it’s a md5 thing (maybe the exporter’s fault, several possibilities here) or my artist going lazy (although I doubt it since we’ve been working together for quite a long time using shockwave3d and always had naming conventions).

I’m currently working on a game prototype, I’ll most certainly come back to this subject when I’ll start loading game levels (and so multiple models in a single file). For now the above script works just fine for my needs smile

   

Avatar
Matse, Sr. Member
Posted: 27 September 2011 11:19 AM   Total Posts: 149   [ # 4 ]

Coming back on this now that I have a level to load and test smile

(I’m compiling using the very latest .zip download from github)

Still using the above script, with more files now, I’m logging every asset on AssetEvent.ASSET_COMPLETE this way :

var ns:String $evt.asset.assetNamespace;
   var 
type:String $evt.asset.assetType;
   var 
name:String $evt.asset.name;
   var 
msg:String "asset loaded TYPE :: " $evt.asset.assetType
        
" NAME :: " name " NAMESPACE ::" ns

As I stated, md5mesh names are always null, but this may be 3dsMax Exporter’s fault, I don’t know yet and it’s not a problem for me right now so I’ll look into that later.
md5anim are all named “md5sequence” but again not a problem to me for now since I have one md5anim per animation sequence anyway.

Bitmaps were named “bitmap” but are now named with their loading path, which is cool.

What I have a problem with is OBJ Assets : I have an OBJ file for the level geometry, consisting of several models, I looked into the file itself and models are named “tree”, “wall” etc
But when I load them here’s what I get :

asset loaded TYPE :: mesh NAME :: obj0 NAMESPACE ::level::ground
asset loaded TYPE :: mesh NAME :: obj1 NAMESPACE ::level::ground
asset loaded TYPE :: mesh NAME :: obj2 NAMESPACE ::level::ground

As you can see “wall”, “tree” etc models were renamed to “objX” during loading/parsing.

Please note this is *before* I do any renaming : right after catching the ASSET_COMPLETE event

Should I file a bug or something for this ? Or is it the expected behavior in the current state of things ?

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X