WebGL shader uniforms
Above you should see a square with some concentric circles. When you mouseover the square, the image changes depending on your mouse position! The code that generates this is in the textarea on the right.
WebGL shaders are pure functions. Every time the shader is run, the shader is given some inputs, and eventually the shader produces some output. This is its entire job; it has no side-effects.
There are a few kinds of shader input.
Yesterday I showed a fragment shader
which has access to the coordinate it’s rendering as
Another kind of input is a uniform.
A uniform is a variable which is global to a GL “program”.
A uniform is declared in the shader code with a name;
the above shader declares the uniform called
The uniform only changes its value when explicitly changed through the WebGL API:
const mousePosLoc = gl.getUniformLocation(prog, "mouse_pos"); // get pointer to variable gl.uniform2fv(mousePosLoc, [mousePos.x, mousePos.y]); // set new value
I wrote this because I felt like it. This post is not associated with my employer.