Jump to content

Dynamic diffuseTexture path in babylon file


royibernthal
 Share

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.

Link to comment
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!

Link to comment
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) parsedTexture.name = Texture.urlParser(parsedTexture.name);

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? :)

Link to comment
Share on other sites

If we add it there, the user will not be able to set it because it will be executed during construction.

A new suggestion, combining all suggestions until now:

Two new observers:

onBeforeTextureInit and onAfterTextureInit, set at the engine (!) and executed at the beginning and the end of the texture constructor? So, "global" functions for constructing a texture object.

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...