I successfully created a WebGL texture from an image and drew it into a canvas element.
function initTexture(src) {
texture = gl.createTexture();
texture.image = new Image();
texture.image.onload = function() {
handleLoadedTexture(texture)
}
texture.image.src = src;
}
I also tried to create a texture from one of these datatypes, but without success.
- [object ImageData]
- [object CanvasPixelArray]
- [object CanvasRenderingContext2D]
Is it possible to create a texture just with an image's pixel array? Or in other words: Is it possible to create a JS Image object out of a pixel array?
Edit:
The pixel array looks like this [r,g,b,a,r,g,b,a,r,g,b,a,...]
and each value is in a range of {0..255}.
I want to create a texture with the pixels in the given array.
It's absolutely possible to create a texture with a pixel array! I use the following in my code all the time to create a single pixel, solid color texture.
EDIT: To extrapolate this a little further, most of what you need to know is in the
gl.texImage2d
call. In order to create a texture from raw RGB(A) data you need an array of unsigned byte values, you need to specify to WebGL what the data represents (RGB or RGBA), and you need to know the dimensions of the texture. A more generalized function would look like this: