Webgl readPixels()无效操作:格式/类型组合无效

时间:2016-04-25 10:20:43

标签: javascript textures webgl shader

我试图读取我拥有的纹理的RGBA值,但它一直返回以下错误,每个值都显示为0:

WebGL: INVALID_OPERATION: readPixels: invalid format/type combination

在初始化时我的代码是,像素是纹理值数组:

   texture = gl.createTexture()
   gl.activeTexture(gl.TEXTURE0)
   gl.bindTexture(gl.TEXTURE_2D, texture)
   gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1)
   gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, n, n, 0,
     gl.RGBA, gl.FLOAT, new Float32Array(pixels))

   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)

   FBO = gl.createFramebuffer()

   gl.bindFramebuffer(gl.FRAMEBUFFER, FBO)
   gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
       gl.TEXTURE_2D, texture, 0)

然后,在点击事件中,我运行以下内容:

gl.bindFramebuffer(gl.FRAMEBUFFER, FBO);

var data = new Uint8Array(resolution * resolution * 4);
gl.readPixels(0, 0, resolution, resolution, gl.RGBA, gl.UNSIGNED_BYTE, data);

console.log(data)

数据返回大量零。 什么是格式和类型的正确组合或我在哪里出错?

1 个答案:

答案 0 :(得分:1)

使用gl.getContext("experimental-webgl")

我设法使用以下内容读取浮点纹理的像素:

  gl.bindFramebuffer(gl.FRAMEBUFFER, _this.myFrameBufferObject);
  var data = new Float32Array(resolution * resolution * 4);
  gl.readPixels(0,0,resolution,resolution,gl.RGBA,gl.FLOAT,data);

归功于以下评论this answer