作为背景——GLSL 看起来很像 C,但编译起来有点不同。事情非常展开,条件可以并行执行并在最后切换,诸如此类。取决于硬件...
您可以使用循环索引或常量来索引数组。循环中的分配是可以的,但 tileID 的访问不是。
WebGL 着色器语言来自 GLES,记录在案
http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
附录第 5 节讨论:
Indexing of Arrays, Vectors and Matrices
Definition:
constant-index-expressions are a superset of constant-expressions. Constant-index-expressions can include loop indices as defined in Appendix A section 4.
The following are constant-index-expressions:
• Constant expressions
• Loop indices as defined in section 4
• Expressions composed of both of the above
When used as an index, a constant-index-expression must have integral type.
希望有帮助!
哦,至于修复它,在上面的确切示例中......看起来你可以从 tileID 计算而不是预先计算和索引。
或者,预先计算您喜欢的任何数组,并将其作为纹理传递。当然,纹理可以按您喜欢的方式编入索引。
这是我使用的一个 javascript 辅助方法,用于将浮点数传递给着色器:
function glSetupStuff() { ...
...
if(!gl.getExtension("OES_texture_float")) // <<-- enables RGBA float values, handy!
alert("cant pass in floats, use 8-bit values instead.");
... }
/*
* Pass in an array of rgba floats,
* for example: var data = new Float32Array([0.1,0.2,0.3,1, .5,.5,1.0,1]);
*/
function textureFromFloats(gl,width,height,float32Array)
{
var oldActive = gl.getParameter(gl.ACTIVE_TEXTURE);
gl.activeTexture(gl.TEXTURE15); // working register 31, thanks.
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
width, height, 0,
gl.RGBA, gl.FLOAT, float32Array);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.activeTexture(oldActive);
return texture;
}
请注意 gl.NEAREST 的使用,因此它不会“模糊”您的值!然后你可以在 gl.drawXxx 调用之前设置它,比如
textureUnit = 3; // from 0 to 15 is ok
gl.activeTexture(gl.TEXTURE0 + textureUnit);
gl.bindTexture(gl.TEXTURE_2D, texture);
var z = gl.getUniformLocation(prog, "uSampler");
gl.uniform1i(z, textureUnit);
在着色器中(我相信片段或顶点;一些早期的 webgl 不支持顶点纹理......)
uniform sampler2D uSampler;
...
vec4 value = texture2D(uSampler, vec2(xValueBetween0And1,yValueBetween0And1));
因此,您必须在 0 到 1 的范围内对数组作为纹理的大小进行适当的索引。尝试从每个值/像素的中间进行采样。就像,如果数组是 2 个值宽,索引 0.25 和 0.75。
这就是它的要点!