如果你必须使用`new Image`来渲染WebGL中的图像

问题描述 投票:0回答:1

我在WebGL绘图图片中看到的示例都使用DOM Image对象:

var image = new Image();
image.src = "resources/f-texture.png";
image.addEventListener('load', function() {
  // Now that the image has loaded make copy it to the texture.
  gl.bindTexture(gl.TEXTURE_2D, texture);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, image);
  gl.generateMipmap(gl.TEXTURE_2D);
});

想知道是否有任何方法可以在ArrayBuffer或其他东西中使用像素,而不是使用Image对象,然后将其绘制为图像。如果是这样,通常想知道代码看起来想要实现这一目标。这将是伟大的,因为那时我也可以将像素数据用于其他事情,因此没有重复下载图像像素数据。

image browser webgl pixel
1个回答
1
投票

你可能应该read some tutorials on WebGL

是的,您可以从ArrayBuffer将数据加载到纹理

gl.bindTexure(gl.TEXTURE_2D, tex);
const data = new Uint32Array([
   255, 0, 0, 255, // red
   0, 255, 0, 255, // green
   0, 0, 255, 255, // blue
   255, 255, 0, 255, // yellow
]);
const level = 0;
const internalFormat = gl.RGBA;
const width = 2;
const height = 2;
const border = 0;
const format = gl.RGBA;
const type = gl.UNSIGNED_BYTE
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border,
              format, type, data);
gl.generateMipmap(gl.TEXTURE_2D);
© www.soinside.com 2019 - 2024. All rights reserved.