Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const float array in WebGL shader

I'm using THREE.js with WebGL shader. I want to declare an array of float numbers in fragment shader. The GLSL constant float array is defined like:

#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
};

I've also tried with:

#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
);

But with WebGL, neither of them works. Error information:

ERROR: 0:44: 'kernel' : arrays may not be declared constant since they cannot be initialized ERROR: 0:44: '=' : syntax error

So how should I define an array of const float?

like image 945
Ovilia Avatar asked Apr 20 '26 12:04

Ovilia


1 Answers

Just a walk-around:

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;
like image 187
Ovilia Avatar answered Apr 23 '26 06:04

Ovilia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!