Jump to content

How best to manage textures/assets?


inteja
 Share

Recommended Posts

I read in another thread that BabylonJS intelligently handles multiple uses of the one texture for optimal performance i.e. internally if the texture path is the same then no extra vram is used for subsequent new textures with the same path.

I suspect I've been overcomplicating things up until now but here's some related questions.

Question 1

If I load my textures with AssetsManager like so:

var textureTask = assetsManager.addTextureTask("my-texture", "./path/to/my-texture.jpg");

Are the following methods of applying the textures equivalent behind the scenes (i.e. neither uses more VRAM)?

// Method 1
textureTask.onSuccess = function(task) {
  material.diffuseTexture = task.texture;
}

// Method 2 - elsewhere in application after assetsManager.onFinish() has been called.
material.diffuseTexture = new BABYLON.Texture("./path/to/my-texture.jpg", scene);

 

Question 2

What I've been doing up until now is assigning loaded assets to an assets array which I then pass around to various objects to use what they need like so:

textureTask.onSuccess = function(task) {

  assets[task.name] = task.texture;
}

// Stuff ...

var myCustomObject = new CustomObject(assets);

// In CustomObject ...

material.diffuseTexture = assets["my-texture"].clone();

The reason for the .clone() is when I need different uv scale and offset per instance.

If my method 1 and 2 are functionally equivalent and don't result in any additional vram usage or performance hit then I'm wasting my time passing around an array of loaded assets when I could simply instantiate a new texture with the same path that I know has already been loaded by AssetsManager.

Can anyone shed some light on this? How do other people manage this efficiently?

Link to comment
Share on other sites

Another related question ?

If I load a .babylon of .gltf model using the AssetsManager, does the AssetsManager consider the asset fully loaded when that file is loaded or when that file and all textures etc referenced by that file are loaded?

Link to comment
Share on other sites

@inteja

Your approach might work, however I've had issues with your specific structure before. I generally use loader.onFinish (loader is variable for array) to write the function as in the previous post:

 

I use this call to set material and other attributes if needed (as in creating a GUI). However, I wrap my scene creation in a separate function and call the scene function from loader.onFinish = function() to make everything much clearer to debug. I hope this makes sense. 

DB

Link to comment
Share on other sites

Thanks @dbawel 

Yes my experimentation and questions above were prompted by that post - it inspired me to try out a few different approaches including what you've suggested. My current project has a pretty deep structure and it's not that convenient to apply all assets in the one loader.onFinish() call at the same time. My application is more dynamic than that, which is why I was curious about whether my approach of passing an array of assets around makes any sense given it seems like I could just do new BABYLON.Texture(path, scene) at any point in time after all assets have been loaded and in theory it shouldn't impact memory usage or performance ... maybe.

Link to comment
Share on other sites

@inteja

You might want to consider you're above approach with one additional function. I don't know if this makes sense for your needs, but I often manage asset memory in my apps by using the asset manager in the way you've clearly grasp; but call a separate function to clear out all memory in the asset array by utilizing 'dispose' as this clears the assets from memory. Perhaps this is something you've considered? 

DB

Link to comment
Share on other sites

1. Yes they are equivalent, the engine is keeping a cache for you

2. True. There is no need for that. The Engine caches an array of InternalTexture so all the VRAM is safe :)

3. The meshAssetTask consider the task completed when the file is loaded and not when the textures are loaded

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...