WebGL着色器中无法使用可变数组索引? [英] Variable array index not possible in webgl shaders?
问题描述
正如标题所述,我不能在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?
推荐答案
http ://www.khronos.org/registry/webgl/specs/latest/#DYNAMIC_INDEXING_OF_ARRAYS "WebGL只允许使用常量表达式,循环索引或它们的组合进行动态索引.唯一的例外是顶点着色器中的统一访问,可以使用任何表达式对其进行索引."
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-else可以正常工作. AFAIK统一值将以任何方式加载到寄存器中,因此对它们进行十二个数学运算周期不会使着色器变慢.
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.
这篇关于WebGL着色器中无法使用可变数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!