webgl readpixels总是返回0,0,0,0 [英] webgl readpixels is always returning 0,0,0,0

查看:588
本文介绍了webgl readpixels总是返回0,0,0,0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WebGl中进行选择。我有两个渲染的形状,每个都映射了不同的纹理。我试图在某些坐标上抓取像素。以下是示例。

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);

但是pixelValues总是包含[0,0,0,0]。我究竟做错了什么?我是否需要做一些与帧缓冲相关的事情?

But pixelValues always contain [0,0,0,0]. What am I doing wrong? Do I need to do something related to framebuffer?

推荐答案

根据 WebGL最新规范,你现在需要调用 getContext 设置 preserveDrawingBuffer flag,如:

According to WebGL latest specs, you now need to call getContext setting the preserveDrawingBuffer flag, like:

var ctx = canvas.getContext("experimental-webgl", {preserveDrawingBuffer: true});

这可以防止绘图缓冲区(颜色,深度,模板)在绘制到屏幕后被清除。请记住,这可能会导致性能下降。

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.

这篇关于webgl readpixels总是返回0,0,0,0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆