royibernthal Posted September 13, 2017 Share Posted September 13, 2017 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. Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 13, 2017 Share Posted September 13, 2017 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! Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 14, 2017 Author Share Posted September 14, 2017 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? Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 15, 2017 Share Posted September 15, 2017 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? Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 16, 2017 Author Share Posted September 16, 2017 Sure that would work too. Would the observables be static? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 18, 2017 Share Posted September 18, 2017 I would say it could be hosted by the scene as the scene manages the list of textures Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 18, 2017 Author Share Posted September 18, 2017 Where in the scene would I need to add the logic? (before the texture is loaded) Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 21, 2017 Author Share Posted September 21, 2017 Deltakosh Can you be more specific? I'd like to try to make a PR. Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 22, 2017 Share Posted September 22, 2017 Hi @royibernthal, we will discuss it and let you know. Would be great to get the PR from you Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 22, 2017 Share Posted September 22, 2017 I gave it some thoughts. I think the right place could be there: https://github.com/BabylonJS/Babylon.js/blob/master/src/babylon.engine.ts#L2754 We could add an engine.onBeforeTextureCreation(url) Thoughts? Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 22, 2017 Share Posted September 22, 2017 Would that also change the texture insurance itself, or does it stay imutable? If it's just for url manipulations, it's the perfect place. But if you check texture.url it'll still be the original url and not the changed one. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 22, 2017 Share Posted September 22, 2017 right..then we probably need to move it here: https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/Textures/babylon.texture.ts#L109 Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 23, 2017 Author Share Posted September 23, 2017 Is that the final conclusion? Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 24, 2017 Share Posted September 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 25, 2017 Share Posted September 25, 2017 Sorry I was unclear but I'm with Raanan: th engine or the scene have to hold the observables but we need to call them in the texture constructor RaananW 1 Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 25, 2017 Author Share Posted September 25, 2017 How would the texture constructor access the observables? Which would make the most sense? The scene or the engine? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 25, 2017 Share Posted September 25, 2017 it will use the scene (parameter) to get either access to the scene or to the engine (scene.getEngine()) I'm more to move it to the engine Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 26, 2017 Share Posted September 26, 2017 I am totally neutral about it Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 26, 2017 Author Share Posted September 26, 2017 Sent a PR, let me know if this is what you had in mind. https://github.com/BabylonJS/Babylon.js/pull/2869 I'm having troubles compiling bjs in vs code so I wasn't able to test it. Is it supposed to work as is or am I supposed to change something for it to compile properly? RaananW 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted September 26, 2017 Share Posted September 26, 2017 Merged, built and deployed to preview Quote Link to comment Share on other sites More sharing options...
royibernthal Posted September 26, 2017 Author Share Posted September 26, 2017 Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.