Uniforms
Set a shader's uniforms by name, with numbers, arrays, typed arrays or textures, whatever their GLSL type.
When a renderer is made, it asks the program for its active uniforms and their types. setUniforms then calls the right gl.uniform* for each: uniform1f for a float, uniform3fv for a vec3, uniformMatrix4fv for a mat4, and so on. You don't look up locations or pick functions.
import { createContext, createRenderer } from 'wtc-simplegl'
// Uniforms of several types, set with setUniforms every frame: a float, a
// vec2, a vec3 array and an int.
const vert = /* glsl */ `#version 300 es
in vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
}`
const frag = /* glsl */ `#version 300 es
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec3 u_palette[3];
uniform int u_bands;
out vec4 colour;
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float wave = 0.5 + 0.5 * sin(uv.x * 6.283 + u_time) * 0.4 + uv.y * 0.6;
// Pick a colour from the palette by band
int band = int(floor(wave * float(u_bands))) % 3;
float edge = fract(wave * float(u_bands));
float alpha = smoothstep(0.0, 0.04, edge) * smoothstep(1.0, 0.96, edge);
colour = vec4(u_palette[band] * alpha, alpha);
}`
export default function (container) {
const ac = new AbortController()
const canvas = document.createElement('canvas')
container.appendChild(canvas)
const gl = createContext({
canvas,
signal: ac.signal,
attributes: { alpha: true, premultipliedAlpha: true, antialias: true }
})
const quad = createRenderer(gl, {
vert,
frag,
count: 3,
attributes: {
a_position: { data: new Float32Array([-1, -1, 3, -1, -1, 3]), size: 2 }
}
})
// Set once: a uniform keeps its value until it's set again
quad.setUniforms({
u_palette: new Float32Array([0.29, 0.62, 1, 1, 0.45, 0.7, 1, 0.8, 0.3]),
u_bands: 7
})
return {
frame(time) {
quad.bindVAOs()
quad.setUniforms({
u_resolution: [gl.canvas.width, gl.canvas.height],
u_time: time
})
quad.render()
},
dispose() {
ac.abort()
quad.delete()
gl.getExtension('WEBGL_lose_context')?.loseContext()
canvas.remove()
}
}
}Setting them
renderer.setUniforms({
u_time: time,
u_resolution: [gl.canvas.width, gl.canvas.height],
u_colour: [0.29, 0.62, 1],
u_matrix: new Float32Array(16)
})setUniforms is bindUniform for several at once; renderer.bindUniform('u_time', time) sets one. A uniform keeps its value until it's set again, so set constants once and only what changes each frame.
Names the shader doesn't use are skipped without an error. GLSL compilers remove uniforms that don't affect the output, so a uniform you've declared but not used yet isn't there to set. It also means one object of shared values can go to every renderer, and each takes what it needs:
const shared = { u_time: time, u_resolution: [gl.canvas.width, gl.canvas.height] }
background.setUniforms(shared)
particles.setUniforms({ ...shared, u_size: 3 })null and undefined values are skipped too.
Types
| GLSL | Value |
|---|---|
float, int, uint, bool | A number (or true/false for bool). |
vec2–vec4, ivec*, uvec*, bvec* | An array or typed array of 2–4 numbers. |
mat2–mat4, mat2x3 and the other non-square matrices | An array or Float32Array, column by column. |
An array, such as vec3 u_palette[3] | One flat array or typed array with every element in order. |
sampler2D and the other samplers | A texture or framebuffer target. See Textures. |
Arrays can be set by their plain name: WebGL reports vec3 u_palette[3] as u_palette[0], and wtc-simplegl answers to both.
Matrices aren't transposed, so they're in column-major order, as GLSL and most matrix libraries expect.
Which program
Uniforms belong to a program, and WebGL sets them on whichever program is current. bindUniform makes its renderer's program current first if it isn't, so you can set uniforms on any renderer at any time, in any order.
Uniform blocks
Uniforms in a uniform block (uniform Settings { ... }) aren't in renderer.uniforms and can't be set this way: they come from a buffer. Bind one with gl.uniformBlockBinding and gl.bindBufferBase(gl.UNIFORM_BUFFER, ...), using renderer.program.
Reading what's there
renderer.uniforms holds every active uniform, by name, with its location, type and size:
console.log(Object.keys(renderer.uniforms)) // which uniforms survived compilationSee also
- Textures
- Pointer, time and resolution, for the uniforms most effects use
- API: Renderer, ActiveUniform