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
More by Jim
- Project C-43: the lost origins of asymmetric crypto
- How Hacker News stays interesting
- My parents are Flat-Earthers
- The dots do matter: how to scam a Gmail user
- The sorry state of OpenSSL usability
- I hate telephones
- The Three Ts of Time, Thought and Typing: measuring cost on the web
- Granddad died today
- Your syntax highlighter is wrong
I wrote this because I felt like it. This post is not associated with my employer. Found an error? Edit this page.