webgl readpixels 始终返回 0,0,0,0
我正在尝试在 WebGl 中进行挑选。我渲染了两个形状,每个形状上映射了不同的纹理。我正在尝试抓取某些坐标上的像素。这是一个例子。
var pixelValues = new Uint8Array(4);
gl.readPixels(10, 35, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelValues);
console.log(pixelValues);
但像素值始终包含 [0,0,0,0]。我做错了什么?我需要做一些与帧缓冲区相关的事情吗?
I am trying to do picking in WebGl. I have two shapes rendered along with different texture mapped on each. I am trying to grab pixel on certain co-ordinates. Here is the example.
var pixelValues = new Uint8Array(4);
gl.readPixels(10, 35, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelValues);
console.log(pixelValues);
But pixelValues always contain [0,0,0,0]. What am I doing wrong? Do I need to do something related to framebuffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要
preserveDrawingBuffer: true
来调用readPixels
。您需要的是在退出当前事件之前调用readPixels
。规范规定,如果您调用任何影响画布的函数(gl.clear、gl.drawXXX),那么浏览器将在下一次复合操作后清除画布。复合操作何时发生取决于浏览器。可能是在处理多个鼠标事件或键盘事件或单击事件之后。顺序未定义。定义的是在当前事件退出之前它不会执行此操作,因此
地方起作用
在 as不起作用的
请注意,由于复合操作(浏览器实际上使用 HTML 的其余部分在页面上绘制画布)触发了清除,因此如果画布不在页面上,则不会复合,也不会被清除。
换句话说,上面不起作用的情况在这里起作用
现在,如果您想在其他事件中调用
readPixels
,例如当用户单击某个元素时,那么您至少有 2 个选项Set
preserveDrawingBuffer: true
< /p>在事件中再次渲染
You don't need
preserveDrawingBuffer: true
to callreadPixels
. What you need is to callreadPixels
before exiting the current event.The spec says if you call any function that affects the canvas (gl.clear, gl.drawXXX) then the browser will clear the canvas after the next composite operation. When that composite operation happens is up to the browser. It could be after it processes several mouse events or keyboard events or click events. The order is undefined. What is defined is that it won't do it until the current event exits so
works where as
does not work
Note that since it's the composite operation (the browser actually drawing the canvas on the page with the rest of the HTML) that triggers the clear, if the canvas is not on the page then it's not composited and won't be cleared.
In other words the case that didn't work above does work here
Now, if you want to call
readPixels
in some other event, like when the user clicks an element, then you have at least 2 optionsSet
preserveDrawingBuffer: true
Render again in your event
根据 WebGL 规范,您需要调用
getContext
设置preserveDrawingBuffer
标志,例如:如果您打算在退出渲染 GL 上下文的事件后读取像素。这可以防止绘图缓冲区(颜色、深度、模板)在绘制到屏幕后被清除。请记住,这可能会导致性能损失。
或者,您可以在呈现像素之前读取像素,这也应该可行。
According to WebGL specs, you need to call
getContext
setting thepreserveDrawingBuffer
flag, like:if you plan to read the pixels after exiting the event where the GL context is rendered. This prevents the drawing buffer (color, depth, stencil) from being cleared after they are draw to screen. Keep in mind that settings this may cause a performance penalty.
Alternatively, you can read the pixels before they are presented, which should also work.
我确信您认为
gl.readPixels
从左上角开始读取像素,但事实并非如此。gl.readPixels
从左下角开始读取它们。来自 WebGLRenderingContext.readPixels() 文档:
I'm sure you thought
gl.readPixels
starts reading pixels from the top left corner, but it doesn't.gl.readPixels
starts reading them from the lower left corner.From the WebGLRenderingContext.readPixels() documentation: