Cocos 3.7.1 effect里用int类型变量当mat的下标编译报错

  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.);
}

核心问题就是,怎么在cocos的effect里,把float类型的变量取模之后取整,并当做mat的下标来访问,获取到mat的某行某列元素值(float)

引擎支持的最低版本GLSL100中,只有矩阵支持动态变量下标,而 vector 或数组都只支持常量下标,所以你只能用vec4 matLine = mat[x]先接一行,然后用 ?= 或 if 来通过 y 选择返回行的 xyzw
想要支持这个特性,至少需要110及以上的版本,可以把你的代码用 #if __VERSION__ > 100 宏来包裹,但是在很低端的平台(如一些小游戏平台),这些代码就不能被执行到,需要给一个 fallback 的算法

了解了,感谢回答~