Sign in to follow this  

Dynamic diffuseTexture path in babylon file

Recommended Posts

I'm loading a mesh from a .babylon file using AssetsManager/addMeshTask(). The diffuseTexture path is defined inside the file.

I'd like to change the diffuseTexture's path dynamically before the texture is automatically loaded by bjs.

Ideally there could be some callback after the .babylon is loaded and before bjs starts loading assets that are referred to in that file.

My goal is to load regular textures on desktop and optimized textures on mobile.

Share this post

Link to post
Share on other sites

Hi @royibernthal,

That's a great question :) 

First - the framework has no internal solution for this. It will have to be externally developed. We can gladly discuss adding those events, but it will have to be planned correctly.

I can offer a few ways to solve this:

1) Reimplement the loader (my favorite option, BTW) - you can register a new .babylon loader, with your functionality integrated in it. Meaning - in the load texture function you test if the device is mobile (using your favorite way for that) and load the correct file / load the file from the correct base path.

2) Use delayLoading (scene.useDelayedTextureLoading = true), and set a new URL right after loading the .babylon . Due to the way JS executes the delay functions you will be able to update the URL of the texture, if needed, before the data is downloaded. 

3) Use onLoadedObservable in the Texture class - which triggers after the texture loads. Downside here - the (wrong) file was already downloaded. In that case I would first download the low-res files and only then will download the high-res ones to save bandwidth for mobile devices.

I have implemented option 3 in a few projects already, for the sake of loading speed and better quality on desktops. It is the simplest one to implement. And it is working.

I guess there are many other ways to achieve that (you can also do it on the server level - deliver a different file when requesting an image from mobile/desktop), but they will all probably use the same mechanism.


I hope it helps!

Share this post

Link to post
Share on other sites

Hi @RaananW, thanks for the detailed response.


I like the style of your first suggestion, but it'd be great to have a less cumbersome solution.


I'd like to add something to bjs source, maybe a static callback in the first line of Texture/parse():

if (Texture.urlParser != null) = Texture.urlParser(;

I'm not sure if it's most fitting place to do this, but it does the job.

Otherwise, something of the sort somewhere between the plugin's loadMesh() to Texture/parse(). What do you think? Raanan? Deltakosh? :)

Share this post

Link to post
Share on other sites

That's a bit too specific IMO :)

Having said that - we COULD add observables for before and after texture load, with a reference to the texture object. This will allow you to change the URL after it was initialized in the constructor. Would that work?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.