vec4 frag () {
mat4 thresholdMatrix = mat4(1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0, 13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0, 4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0, 16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0);
vec4 col = texture(tDiffuse,v_uv);
vec2 pos = v_screenPos.xy;
int x = int(mod(pos.x, 4.));
int y = int(mod(pos.y, 4.));
// 这样访问会报错
thresholdMatrix[x][y];
// 这样访问不报错
thresholdMatrix[0][0];
const int a = 0;
const int b = 0;
// 这样访问不报错
thresholdMatrix[a][b];
CC_APPLY_FOG(col, v_position);
return CCFragOutput(col);
}
同样的在shadertoy里试了一下,不用Const申明int,可以正常访问矩阵下标
precision highp float;
precision highp int;
varying vec2 vTextureCoord;
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
mat4 thresholdMatrix = mat4(1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0, 13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0, 4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0, 16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0);
float targetAlpha = 0.1;
vec2 pos = fragCoord;
int x = int(mod(pos.x, 4.));
int y = int(mod(pos.y, 4.));
if(targetAlpha - thresholdMatrix[x][y] < 0.) {
discard;
}
fragColor = vec4(1.,1.,0.,1.);
}