2011-06-06 109 views

回答

11

http://www.khronos.org/registry/webgl/specs/latest/#DYNAMIC_INDEXING_OF_ARRAYS 「WebGL只允許使用常量表達式,循環索引或組合的動態索引,唯一的例外是頂點着色器中的統一訪問,它可以使用任何表達式進行索引。

你試過了嗎?如果它不起作用,有幾個選項。

如果您有少量值,if-else可以正常工作。 AFAIK統一值將被加載到寄存器無論如何,因此做十幾個數學循環他們不會讓你的着色速度慢得多。

對於大量的值,紋理是你最好的選擇。

+10

有任何編譯錯誤是一種變通方法:'/ *賦予變量i索引* /對(INT X = 0; x 2013-02-19 09:20:02

-1

我沒有測試過,但我沒有得到從以下

//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]; 
+0

運行時失敗。 – jes5199 2017-02-10 05:56:20