17

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?

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

There is a workaround: /* given variable i is the index */ for (int x = 0; x < MAX_VALUE_FOR_INDEX; x++) { if (x == i) { /* use variable x as index here */ }}
-1

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

1 Comment

This fails at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.