简体   繁体   English

读取WebGLTexture中的像素(将WebGL渲染到纹理)

[英]Read pixels in WebGLTexture (Rendering WebGL to texture)

I'm generating a texture on the GPU and rendering it to my own framebuffer object. 我正在GPU上生成纹理并将其渲染到我自己的帧缓冲对象。 It works fine and the texture is rendered to a WebGLTexture that I can pass to other shaders. 它工作正常,纹理呈现为WebGLTexture,我可以传递给其他着色器。 However I want to access the WebGLTexture pixels in javascript. 但是我想在javascript中访问WebGLTexture像素。 Is there a way to accomplish that? 有办法实现吗?

At the moment I'm using gl.ReadPixels to read the pixels after I've drawn the texture to my framebuffer. 目前我正在使用gl.ReadPixels在我将纹理绘制到我的帧缓冲区后读取像素。 That works fine but wouldn't it be better if I could directly access the pixels in the WebGLTextureObject? 这工作正常,但如果我可以直接访问WebGLTextureObject中的像素会不会更好?

What I'm trying to accomplish is this: I have GLSL perlin noise shader that can render high def heightmap and normal map on the GPU. 我想要实现的是:我有GLSL perlin噪声着色器,可以在GPU上渲染高清高度图和法线贴图。 I want to pass the heightmap to the CPU so that I can generate the vertices for the mesh. 我想将高度图传递给CPU,以便我可以为网格生成顶点。 I could of course just position the vertices in the vertex shader but I need it in the CPU for collision detection. 我当然可以将顶点定位在顶点着色器中,但我需要在CPU中进行碰撞检测。

I hope my question is clear. 我希望我的问题很清楚。 Any feedback is welcome! 欢迎任何反馈!

Thanks! 谢谢!

You can read the pixels out of most textures by attaching them to a framebuffer. 您可以通过将像素附加到帧缓冲区来读取大多数纹理中的像素。

var fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
  var pixels = new Uint8Array(width * height * 4);
  gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM