Description of the problem
Since the texture object store the texture information in the .image
property, we have to use callback function even the image data is already in the memory like the following:
var imageDataInBase64 = '.........';
var image = new Image();
image.src = `data:image/png;base64' + imageDataInBase64;
var texture = new THREE.Texture();
texture.image = image;
image.onload = function() {
texture.needsUpdate = true;
};
It will be very convenient if we can construct a texture object directly and synchronously without any callbacks when the image data is already in the memory. (IMHO, It’s also good to de-couple the logic of loading data and using data from the software engineering perspective.)
By the way, there are many cases when a texture image data is already there in the memory, such as:
- The image data is generated by code in the memory;
- The image data is loaded from a local database/disk when in Electron/NW.js applications.
Three.js version
- Dev
- [X ] r87
- …
Browser
- All of them
- Chrome
- Firefox
- Internet Explorer
OS
- All of them
- Windows
- macOS
- Linux
- Android
- iOS
I don’t think that is guaranteed. You have to be careful not to set the
needsUpdate
flag too early, in the event thatrender()
is called before the image loads.