我正在将 THREE.js 与 WebGL 着色器一起使用。我想在片段着色器中声明一个 float 组。 GLSL 常量 float 组定义如下:
#define KERNEL_LENGTH 9
const float kernel[KERNEL_LENGTH] = {
1.0/16.0, 2.0/16.0, 1.0/16.0,
2.0/16.0, 4.0/16.0, 2.0/16.0,
1.0/16.0, 2.0/16.0, 1.0/16.0
};
我也尝试过:
#define KERNEL_LENGTH 9
const float kernel[KERNEL_LENGTH] = float[KERNEL_LENGTH](
1.0/16.0, 2.0/16.0, 1.0/16.0,
2.0/16.0, 4.0/16.0, 2.0/16.0,
1.0/16.0, 2.0/16.0, 1.0/16.0
);
但是对于 WebGL,它们都不起作用。错误信息:
ERROR: 0:44: 'kernel' : arrays may not be declared constant since they cannot be initialized ERROR: 0:44: '=' : syntax error
那么我应该如何定义一个 const float 数组?
最佳答案
只是走一走:
float kernel[KERNEL_LENGTH];
kernel[0] = kernel[4] = kernel[20] = kernel[24] = 1.0/273.0;
kernel[1] = kernel[3] = kernel[5] = kernel[9] = kernel[15] = kernel[19]
= kernel[21] = kernel[23] = 4.0/273.0;
kernel[2] = kernel[10] = kernel[14] = kernel[22] = 7.0/273.0;
kernel[6] = kernel[8] = kernel[16] = kernel[18] = 16.0/273.0;
kernel[7] = kernel[11] = kernel[13] = kernel[17] = 26.0/273.0;
kernel[12] = 41.0/273.0;
关于arrays - WebGL 着色器中的 const float 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262729/