是否有可能获得顶点的实际值? [英] Is it possible to get the actual value of a vertex?

查看:39
本文介绍了是否有可能获得顶点的实际值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从顶点着色器中恢复一些顶点数据,但我没有在互联网上找到任何相关信息.

I was trying to recover some vertex data from vertex shader, but I haven't found any relevant information about this on the internet.

我正在使用顶点着色器通过 GPU 计算我的顶点位置,但我需要在 Javascript 中获取应用程序逻辑的结果.有没有可能的方法来做到这一点,而无需在 Javascript 中计算它?

I'm using the vertex shader to calculate my vertex positions using the GPU, but I need to get the results for the logic of my application in Javascript. Is there a possible way to do this without calculating it in Javascript too?

推荐答案

在 WebGL2 中,您可以使用转换反馈(如 Pauli 建议),并且您可以使用 getBufferSubData 读回数据,但理想情况下,如果您只是要在另一个绘制调用中使用数据,您不应该读回它,因为回读很慢.

In WebGL2 you can use transform feedback (as Pauli suggests) and you can read back the data with getBufferSubData although ideally, if you're just going to use the data in another draw call you should not read it back as readbacks are slow.

变换反馈只是意味着您的顶点着色器可以将其输出写入缓冲区.

Transform feedback simply means your vertex shader can write its output to a buffer.

在 WebGL1 中,您可以通过将顶点渲染为附加到帧缓冲区的浮点纹理来实现.您将在每个顶点中包含一个顶点 id 属性.您可以使用该属性来设置 gl_Position.您可以使用 gl.POINT 进行绘图.它将允许您有效地渲染输出纹理中的每个单独像素,让您获得变换反馈.不同之处在于您的结果最终会出现在纹理而不是缓冲区中.您可以在此处查看相关示例

In WebGL1 you could do it by rendering your vertices to a floating point texture attached to a framebuffer. You'd include a vertex id attribute with each vertex. You'd use that attribute to set gl_Position. You'd draw with gl.POINT. It would allow you to render to each individual pixel in the output texture effectively letting you get transform feedback. The difference being your result would end up in a texture instead of a buffer. You can kind of see a related example of that here

如果您不需要 JavaScript 中的值,那么您可以使用刚刚写入的纹理作为未来绘制调用的输入.如果您确实需要 JavaScript 中的值 您必须先转换将浮点值转换为可读格式(使用着色器),然后使用 gl.readPixel

If you don't need the values back in JavaScript then you can just use the texture you just wrote to as input to future draw calls. If you do need the values back in JavaScript you'll have to first convert the values from floating point into a readable format (using a shader) and then read the values out using gl.readPixel

这篇关于是否有可能获得顶点的实际值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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