Learn more about Israeli war crimes in Gaza, funded by the USA, Germany, the UK and others.

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 gl_FragCoord. 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 mouse_pos. 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
Tagged .

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.