变量数组索引在 webgl 着色器中不可能?
正如标题所说,我无法在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
我还没有测试过,但我没有从以下代码中得到任何编译错误
I haven't tested it, but I don't get any compilation error from the following