变量数组索引在 webgl 着色器中不可能?

发布于 2024-11-14 03:03:42 字数 125 浏览 4 评论 0原文

正如标题所说,我无法在 webgl 顶点着色器中执行 vector_array[foo] (假设 foo 是在范围内且为整数),对吗?

纹理是最好的选择,还是有解决方法或更好的方法来模仿查找表?

As the title says, I can't do vector_array[foo] (assuming foo is in-range and integer) in webgl vertex shaders, correct?

Are textures the best alternative, or is there a workaround or some better way to mimick a lookup table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-11-21 03:03:42

http://www.khronos.org/registry/webgl/specs/latest/ #DYNAMIC_INDEXING_OF_ARRAYS
“WebGL 只允许使用常量表达式、循环索引或组合进行动态索引。唯一的例外是顶点着色器中的统一访问,可以使用任何表达式进行索引。”

你尝试过吗?如果它不起作用,有几个选择。

如果值的数量很少,则 if-else 可以正常工作。 AFAIK 无论如何,统一值都会被加载到寄存器中,因此对它们进行十几个数学循环并不会让您的着色器变慢。

对于大量值,纹理是最好的选择。

http://www.khronos.org/registry/webgl/specs/latest/#DYNAMIC_INDEXING_OF_ARRAYS
"WebGL only allows dynamic indexing with constant expressions, loop indices or a combination. The only exception is for uniform access in vertex shaders, which can be indexed using any expression."

Did you try it? If it didn't work, there are a couple options.

If you have a small number of values, if-else could work ok. AFAIK the uniform values are going to be loaded into registers anyhow, so doing a dozen cycles of math on them isn't going to make your shader much slower.

For a large number of values, textures are your best bet.

水溶 2024-11-21 03:03:42

我还没有测试过,但我没有从以下代码中得到任何编译错误

//index as a float
attribute lowp float vColorIndex;
//the array
uniform vec4 Colors[16];

//type cast the float in an int
int index = int(vColorIndex);
//use index
vec4 col = Colors[index];

I haven't tested it, but I don't get any compilation error from the following

//index as a float
attribute lowp float vColorIndex;
//the array
uniform vec4 Colors[16];

//type cast the float in an int
int index = int(vColorIndex);
//use index
vec4 col = Colors[index];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文