픽셀 배열로 텍스처를 생성하는 것은 절대 가능합니다! 내 코드에서 항상 단일 픽셀, 솔리드 컬러 텍스처를 만들기 위해 다음을 사용합니다.
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;
}
편집
:이 조금 더, 당신이 알아야 할의 대부분이
gl.texImage2d
호출에 추정합니다. 원시 RGB (A) 데이터에서 텍스처를 생성하려면 부호없는 바이트 값의 배열이 필요합니다. WebGL에 데이터가 나타내는 내용 (RGB 또는 RGBA)을 지정해야하며 텍스처의 크기를 알아야합니다. 좀 더 일반화 된 함수는 다음과 같습니다.
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);
죄송합니다. Q : – alex
데이터가 ArrayBuffer에 있으면 WebGL에서 하나의 함수를 사용할 수 있습니다 (지금 당장 기억하지 못합니다). – Chiguireitor
@Chiguireitor는 이러한 arrayBuffer를 만드는 방법에 대한 예제를 제공 할 수 있습니까? – alex