Webgl readPixels() 无效操作:无效的format/type组合

Webgl readPixels() invalid operation: invalid format/type combination

我正在尝试读取我拥有的纹理的 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)

数据returns 一大堆零。 格式和类型的正确组合是什么,或者我哪里出错了?

使用后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