Class Shader

java.lang.Object
org.openpatch.scratch.extensions.shader.Shader

public class Shader extends Object
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.

  • Constructor Details

    • Shader

      public Shader(String name, String fragmentShaderPath, String vertexShaderPath)
      Constructor
      Parameters:
      name - unique name
      fragmentShaderPath - path to the fragment shader file
      vertexShaderPath - path to the vertex shader file, or null for Processing's default one
    • Shader

      public Shader(Shader shader)
      Copy constructor
      Parameters:
      shader - shader
  • Method Details

    • getName

      public String getName()
      Returns the name
      Returns:
      the name
    • setName

      public void setName(String name)
      Sets the name
      Parameters:
      name - unique name
    • getPShader

      public processing.opengl.PShader getPShader()
      Returns the shader
      Returns:
      the shader
    • set

      public void set(String name, int x)
      Sets a uniform of the shader: a value the shader reads but the program decides, such as the time for an animation or a position on the stage.

      The name is the one the shader file declares, uniform float blocks; is set with set("blocks", 20). There is a set for one or two numbers or booleans, a Vector2, a Color (as a vec3), and an array of numbers, ncoords at a time, for an array uniform like uniform vec3 lights[10];.

      A uniform keeps its value until it is set again, so a value that changes is set in run().

      Parameters:
      name - the name of the uniform in the shader file
      x - the value
    • set

      public void set(String name, boolean x)
    • set

      public void set(String name, double x)
    • set

      public void set(String name, int x, int y)
    • set

      public void set(String name, boolean x, boolean y)
    • set

      public void set(String name, double x, double y)
    • set

      public void set(String name, Vector2 vec)
    • set

      public void set(String name, int[] values, int ncoords)
    • set

      public void set(String name, double[] values, int ncoords)
    • set

      public void set(String name, Color c)