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. The shader will be used for:
- Fills when a texture is enabled if it includes a uniform
sampler2D
. - Fills when lights are enabled if it includes the attribute
aNormal
, or if it has any of the following uniforms:uUseLighting
,uAmbientLightCount
,uDirectionalLightCount
,uPointLightCount
,uAmbientColor
,uDirectionalDiffuseColors
,uDirectionalSpecularColors
,uPointLightLocation
,uPointLightDiffuseColors
,uPointLightSpecularColors
,uLightingDirection
, oruSpecular
. - Fills whenever there are no lights or textures.
- Strokes if it includes the uniform
uStrokeWeight
.
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.
Examples
Syntax
shader(s)
Parameters
p5.Shader object to apply.
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.