Variable array index not possible in webgl shaders?

此生再无相见时 提交于 2019-11-28 02:46:55

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/6247572/variable-array-index-not-possible-in-webgl-shaders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!