参考 shader()

shader()

设置要在绘制时应用的 p5.Shader 对象。

着色器是在图形处理单元 (GPU) 上运行的程序。 它们可以同时处理许多像素或顶点,从而可以快速执行许多图形任务。 它们是用一种名为 GLSL 并与绘图中的其余代码一起运行。p5.Shader 对象可以使用 createShader()loadShader() 函数。

参数 s 是要应用的 p5.Shader 对象。 例如,调用 shader(myShader) 应用 myShader 来处理画布上的每个像素。 该着色器将用于:

  • 如果纹理包含统一的纹理,则在启用纹理时填充 sampler2D。li>
  • 如果包含该属性,则在启用灯光时填充 aNormal,或者如果它具有以下任何 uniform:uUseLightinguAmbientLightCountuDirectionalLightCountuPointLightCountuAmbientColoruDirectionalDiffuseColorsuDirectionalSpecularColorsuPointLightLocationuPointLightDiffuseColorsuPointLightSpecularColorsuLightingDirection,或 uSpecular
  • 当没有灯光或纹理时进行填充。
  • Strokes if it includes the uniform uStrokeWeight.

源代码来自一个 p5.Shader 对象的片段和顶点着色器将在第一次传递给 shader() 时进行编译。参见 MDN 了解有关编译着色器的更多信息。

调用 resetShader() 可恢复绘图的默认着色器。

注意:着色器只能在 WebGL 模式下使用。

示例

语法

shader(s)

参数

s

应用对象 p5.Shader

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

相关参考