Scratch for Java

Shader()

A fragment shader a sprite or the stage can be drawn with.

A shader is a small program in GLSL, the language of the graphics card. It is run once for every pixel and decides the pixel's colour. Processing hands it the picture being drawn as texture, and where on that picture the pixel is as vertTexCoord:

#ifdef GL_ES
precision mediump float;
#endif
 
uniform sampler2D texture;
varying vec4 vertTexCoord;
 
void main(void) {
  vec4 color = texture2D(texture, vertTexCoord.st);
  float grey = (color.r + color.g + color.b) / 3.0;
  gl_FragColor = vec4(grey, grey, grey, color.a);
}

A shader is not made with new but added to a sprite or the stage with getShaders().add(...), which returns it. Its uniform values are given from Java with set(String, int).

In the browser the shader is translated for WebGL, which is stricter than a desktop graphics card: a float has to be written as 1.0, not 1. A vertex shader other than Processing's default one is ignored there.

Syntax

Java

new Shader(name, fragmentShaderPath, vertexShaderPath)
new Shader(shader)
Shader()

Create Shareable URL

Select Sections