Reference setUniform()

setUniform()

Sets the shader’s uniform (global) variables.

Shader programs run on the computer’s graphics processing unit (GPU). They live in part of the computer’s memory that’s completely separate from the sketch that runs them. Uniforms are global variables within a shader program. They provide a way to pass values from a sketch running on the CPU to a shader program running on the GPU.

The first parameter, uniformName, is a string with the uniform’s name. For the shader above, uniformName would be 'r'.

The second parameter, data, is the value that should be used to set the uniform. For example, calling myShader.setUniform('r', 0.5) would set the r uniform in the shader above to 0.5. data should match the uniform’s type. Numbers, strings, booleans, arrays, and many types of images can all be passed to a shader with setUniform().

Examples

Syntax

setUniform(uniformName, data)

Parameters

uniformName

name of the uniform. Must match the name used in the vertex and fragment shaders.

data

value to assign to the uniform. Must match the uniform’s data type.

Notice any errors or typos? Please let us know. Please feel free to edit src/webgl/p5.Shader.js and open a pull request!

Related References