Skip to main content

Uniforms

Uniforms are values that are passed to the vertex and fragment shaders that are the same for all vertices/fragments.

How to use them:

// look up the location of the uniform
const timeLocation = gl.getUniformLocation(program, "u_time");

const ellapsedTime = performance.now();
// before drawing, we'd set the uniform
gl.uniform1f(timeLocation, ellapsedTime);

Types of uniforms:

  • integer or float → i/f
  • 1, 2, 3, or 4 dimensions → 1/2/3/4
  • single value or vector → v

Like this we can set the uniform using the following methods:

// v0, v1 -> floating point numbers
gl.uniform2f(uniformLocation, v0, v1);
gl.uniform3fv(uniformLocation, new Float32Array([v0, v1, v2]));

If we want to pass a matrix we use gl.uniformMatrix[234]fv. See documentation