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
- Your syntax highlighter is wrong
- Granddad died today
- The Three Ts of Time, Thought and Typing: measuring cost on the web
- I hate telephones
- The sorry state of OpenSSL usability
- The dots do matter: how to scam a Gmail user
- My parents are Flat-Earthers
- How Hacker News stays interesting
- Project C-43: the lost origins of asymmetric crypto
- The hacker hype cycle
- The inception bar: a new phishing method
- Time is running out to catch COVID-19
- A probabilistic pub quiz for nerds
- Smear phishing: a new Android vulnerability
Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.