question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Texture as a Promise

See original GitHub issue
Description of the problem

Loading textures with TextureLoader.load is great for as long as someone stays strictly within ThreeJS env (since needsUpdate does all the work.)

When trying to go “outside” of three, for example “change this icon, when texture is loaded” it becomes a bit … dated. Loader allows only for onLoad callbacks, which are ok only for as long as you care about a single texture. When you want to “change this icon, when these 3 textures are loaded”, there are two options:

  • chain onLoad callbacks (bad)
  • wrap each load in a Promise manually, and .all(...) them (better)

Would be very nice to have this Promise functionality out of the box (this also makes async / await, which improves readability significantly.)

I can see 1 or 2 nice improvements that could help there:

  • (1) Add .promise(url) method to TextureLoader, that instead of Texture returns a Promise (not ideal, but already better, if you care more about loading.)
const textureLoader = new Three.TextureLoader();

Promise.all([
  textureLoader.promise(url1), // new Promise instead of new Texture
  textureLoader.promise(url2),
  textureLoader.promise(url3)
])
.then((textures) => {
   // ...
});

Obvious con here is that .promise() can’t nicely return both texture and a promise… unless!

  • (2) Add PromisedTexture wrapper extending Texture, that would add Promise proto functionality (similarly as CanvasTexture does with canvas.) Promised texture would resolve/reject after initially loading, allowing for handling the async process manually outside of ThreeJS.
const textureLoader = new Three.TextureLoader();

const texture1 = textureLoader.promise(url1); // new PromiseTexture
const texture2 = textureLoader.promise(url2); 
const texture3 = textureLoader.promise(url3); 

Promise.all([ texture1, texture2, texture3 ])
.then((textures) => {
   // ...
});
Three.js version

Latest

Browser

N/A

OS

N/A

Hardware Requirements (graphics card, VR Device, …)

N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
mrdoobcommented, Apr 12, 2020

How about Loader.prototype.loadAsync?

4reactions
mrdoobcommented, Apr 7, 2020

If we did this we would have to change all the loaders in the lib. I think it may be easier to make an abstraction in your project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

THREE.js Add textures with Promises - Stack Overflow
I'm trying to use promises to load textures in THREE.JS but i don't know how to do it. I've seen how they work,...
Read more >
The Promise and Perils of Near-regular Texture
1 Motivation. Near-regular textures are common in our daily life. They can be observed in man-made products, by hand or by machine, ranging...
Read more >
"new BABYLON.Texture()" as promise - Questions
When I execute a var texture = BABYLON.Texture(...); I would like to receive a promise (to work with await/async). Is there a workaround...
Read more >
Promise loading with Three.js - ITNEXT
This function returns a Promise that resolves with the loaded geometry when the file is loaded. Next we can make a similar wrapper...
Read more >
The Promise and Perils of Near-Regular Texture | SpringerLink
Motivated by the low structural fidelity for near-regular textures in current texture synthesis algorithms, we propose and implement an alternative texture.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found