¡Es absolutamente posible crear una textura con una matriz de píxeles! Utilizo lo siguiente en mi código todo el tiempo para crear una textura de color sólido de un solo píxel.
function createSolidTexture(gl, r, g, b, a) {
var data = new Uint8Array([r, g, b, a]);
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
return texture;
}
EDIT: extrapolar esto un poco más, la mayor parte de lo que necesita saber está en la llamada gl.texImage2d
. Para crear una textura a partir de datos RGB (A) sin formato, necesita una matriz de valores de bytes sin signo, debe especificar a WebGL qué representan los datos (RGB o RGBA) y necesita conocer las dimensiones de la textura. Una función más generalizada se vería así:
function textureFromPixelArray(gl, dataArray, type, width, height) {
var dataTypedArray = new Uint8Array(dataArray); // Don't need to do this if the data is already in a typed array
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, type, width, height, 0, type, gl.UNSIGNED_BYTE, dataTypedArray);
// Other texture setup here, like filter modes and mipmap generation
return texture;
}
// RGB Texture:
// For a 16x16 texture the array must have at least 768 values in it (16x16x3)
var rgbTex = textureFromPixelArray(gl, [r,g,b,r,g,b...], gl.RGB, 16, 16);
// RGBA Texture:
// For a 16x16 texture the array must have at least 1024 values in it (16x16x4)
var rgbaTex = textureFromPixelArray(gl, [r,g,b,a,r,g,b,a...], gl.RGBA, 16, 16);
Lo siento, quizás no estaba demasiado claro lo que quise decir. Actualicé la Q. – alex
Si sus datos están en un ArrayBuffer, podría usar una función de WebGL (que no recuerdo ahora) – Chiguireitor
@Chiguireitor ¿podría darme un ejemplo sobre cómo crear dicha matrizBuffer? – alex