I need to import mesh synchronoulsy to do something with the mesh after.
I already try ImportMesh without expected effect.
To example i have this following code
main.js :
let obj = new Obj_Asset(objName, filePath, fileNameWithExtension);
obj.loadObj(babylonPositionXYZ);
// do something with obj.mesh, for example the folowing console.log
console.log(obj.mesh); // currently return undefined
I donât want to put make loadObj async and put an await on ImportMesh, it force me to put an await on obj.loadObj on main an a asyn on his parent function, so it force me to put an await to⌠bla⌠bla⌠blaâŚ
Formally, I believe the answer to the question âIs it possible to load a mesh synchronously?â is, âNo, not really.â If Iâm understanding this correctly, this is because of the way JavaScript works: synchronous in this sense means blocking, and because loading a mesh can involve operations that leave JavaScript and then have to come back, blocking becomes problematic. I know adding asynchrony will have cascading effects, but I really think itâs much better to embrace asynchrony early on and design around it. Because itâs a reality of JavaScript and Web code, this situation will keep popping up, and without a solid asynchrony pattern itâs going to be a fight every time.
my question is not really answered, i know that is it possible to make loadObj() async and use await but it force me to use async and await on my main.js , I already do it but I donât find it clean.
my question is more like âThere is a way to import mesh synchronously without using async/await ?â (i edit my post title)
asynchronous is natural process for reading/loading file in any programming language, because it takes time, possibly something going wrong, etc. so, it canât be avoided.
but, you can preload all/some the assets before showing anything.
I agree. This question doesnât make sense. You cannot
using js, without committing that the mesh information is actually loaded. No matter the method you choose for that. BJS is sure smart and versatile but is not an oracle.
As per @sebavan post, I use the importAsync + then method quite often with obj objects, combined with onload for external scripts. Itâs fair enough since it lets you do the transform on your imported mesh or scripts only once ready yet, meanwhile, potentially continues to process the rest of the code. I believe this is the closest you can have to a seamless âsynchronousâ processing of the scene and imported assets. Though, I believe I can understand what you mean by ânot cleanâ. It can be a bit tricky to track the parts relying on whatever âthenâ or âonloadâ when you imbricate a number of these. All I can say (and what I do) is comment the start and end of (then or onload). A bit tedious but in the end not dramatic I believe.
It would be nice for a synchronous method to exist which operates on a base 64 data uri, which is perhaps what OP was asking for. Iâm likewise running into a situation right now where itâd be helpful!
I guess so. Well, if you have enough insight on this and are willing to synthetize, you could still try your luck making a feature request. May be youâll get lucky. I am constantly amazed to see new complex or specific methods being implemented on simple demand. Who knows, may be you can trigger something and someone will absolutely want to do thisâŚ
Loading an image from base64 will also require to wait for the onLoad event. The only way would be to also store those in a binary form that you upload as a texture. It would basically require a different format being potentially way bigger or some tools to parse/decode pngs inline.