I want to load textures without callbacks like .onload()
. I am trying to use texture from blob: https://plnkr.co/edit/n4CbWMEOAyJMQFYy
const response = await fetch("./assets/texture.png");
const imageBlob = await response.blob();
const imageBitmap = await createImageBitmap(imageBlob);
console.log(imageBitmap);
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, imageBitmap);
Expected result is:
But I have this:
I can see in the Network
tab that the image has been uploaded:
This line of code console.log(imageBitmap);
says that the image exists:
texImage2D()
documentation says that I can use ImageBitmap as source.
Updated
I tried to use the loadImage
function from this answer: https://stackoverflow.com/a/52060802/4159530 Playground: https://plnkr.co/edit/n4CbWMEOAyJMQFYy
function loadImage(url) {
return new Promise(resolve => {
const image = new Image();
image.addEventListener('load', () => {
resolve(image);
});
image.src = url;
});
}
/* ... */
async function init() {
/* ... */
const image = await loadImage("./assets/texture.png");
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
/* ... */
}
But that didn't solve the problem.