WebGL: passing uniform arrays to shader
Those answers all use the function uniform3fv
. v
as in vector
.
So you should be using uniform1fv
, not uniform1f
for arrays of uniforms. In the future, please be more careful when reading answers. Or failing that, check your OpenGL errors before asking questions.
Dmitry
Updated on November 30, 2020Comments
-
Dmitry over 3 years
Tried to do just like here: Pass array to shader And like here: Passing an array of vectors to a uniform
But still no luck. I think I do just like there but it doesn't work:
Here's my JavaScript code:
shaderProgram.lightsUniform = gl.getUniformLocation(shaderProgram, "lights"); // Getting location gl.uniform1f(shaderProgram.lightsUniform, new Float32Array([3,1,2,3,4,5])); // Let's try to send some light (currently each light is one float) as array.
Vertex Shader Code:
uniform float lights[6]; // Declaration ... vLight *= lights[0]; // Let's try to mutliply our light by the first array item. There should be 3.0.
Summary: I send an array to shader with non-zero-floats.
Result: totally black! I.e. lights[0] contains 0.0 but expected 3.0. If I try lights[1], lights[2] etc. they all give the same result!
Let's now try to pass just one float. I change
gl.uniform1f(shaderProgram.lightsUniform, new Float32Array([3,1,2,3,4,5]));
to
gl.uniform1f(shaderProgram.lightsUniform, 3); // I want to send just float 3.0
Summary: instead of sending array I send just float 3.0.
Result: works! lights[0] contains 3.0 (but I sent just float, not an array).
What do I do wrong? How do I pass to shader array of uniforms?