Sets the p5.Shader object to apply while drawing.
Shaders are programs that run on the graphics processing unit (GPU). They can process many pixels or vertices at the same time, making them fast for many graphics tasks. They’re written in a language called GLSL and run along with the rest of the code in a sketch. p5.Shader objects can be created using the createShader() and loadShader() functions.
The parameter, s
, is the p5.Shader object to apply. For example, calling shader(myShader)
applies myShader
to process each pixel on the canvas. This only changes the fill (the inner part of shapes), but does not affect the outlines (strokes) or any images drawn using the image()
function. The source code from a p5.Shader object's fragment and vertex shaders will be compiled the first time it's passed to shader()
. See MDN for more information about compiling shaders.
Calling resetShader() restores a sketch’s default shaders.
Note: Shaders can only be used in WebGL mode.
If you want to apply shaders to strokes or images, use the following methods:
- strokeShader(): Applies a shader to the stroke (outline) of shapes, allowing independent control over the stroke rendering using shaders.
- imageShader(): Applies a shader to images or textures, controlling how the shader modifies their appearance during rendering.
Examples
Syntax
shader(s)
Parameters
Related References
copyToContext
Copies the shader from one drawing context to another.
inspectHooks
Logs the hooks available in this shader, and their current implementation.
modify
Returns a new shader, based on the original, but with custom snippets of shader code replacing default behaviour.
setUniform
Sets the shader’s uniform (global) variables.