arrays - WebGL 着色器中的 const float 数组

标签 arrays constants three.js webgl shader

我正在将 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/

相关文章:

javascript - [].slice 与 Array.prototype.slice

python - 如何有效地执行这个 numpy 数组操作?

java - Spring Controller 路径不被视为常量

compiler-errors - 三.WebGL程序错误X6077

javascript - Three.js 中的超链接 CSS3D 对象

c++ - 替换char数组中的字符c++

java - 从数组中取出字符并将它们随机放置以创建字符串

java - 有没有办法在java中将变量转换为常量?

C++:在函数内部声明数组时,表达式必须具有常量值

javascript - 使用三个js和NSF OpenTopography数据的点云: How do you set up the camera and a controls library to navigate the data?