Class Shader

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

public final class Shader extends Object
A shader: a small program the graphics card runs for every pixel of a sprite (or of the whole stage) to change how it looks. You write only the body; every shader gets the same header:
uniform sampler2D costume;   // the costume (or the stage's picture)
uniform vec2 resolution;     // its size in pixels
uniform float time;          // game time in seconds
in vec2 uv;                  // where in the costume: (0, 0) top left, (1, 1) bottom right
out vec4 color;              // the colour to draw
void main() {
  vec4 c = texture(costume, uv);
  float g = (c.r + c.g + c.b) / 3.0;
  color = vec4(g, g, g, c.a);
}

Colours have straight (not premultiplied) alpha. Values of your own come in through uniforms you declare and set with set(String, double) and friends; the header's time and resolution are filled in for you and cannot be set.

  • Method Details

    • getName

      public String getName()
      Returns:
      the shader's name
    • setName

      public void setName(String name)
      Parameters:
      name - a new name
    • set

      public void set(String uniform, double value)
      Parameters:
      uniform - the uniform's name in the shader
      value - an int or float uniform's value
    • set

      public void set(String uniform, int value)
      Parameters:
      uniform - the uniform's name
      value - an int uniform's value
    • set

      public void set(String uniform, boolean value)
      Parameters:
      uniform - the uniform's name
      value - a bool uniform's value
    • set

      public void set(String uniform, double x, double y)
      Parameters:
      uniform - the uniform's name
      x - a vec2's x
      y - a vec2's y
    • set

      public void set(String uniform, Vector2 value)
      Parameters:
      uniform - the uniform's name
      value - a vec2
    • set

      public void set(String uniform, Color color)
      Parameters:
      uniform - the uniform's name
      color - a vec4: red, green, blue and alpha from 0 to 1
    • set

      public void set(String uniform, double[] values, int perElement)
      An array uniform such as uniform vec3 lights[100];.
      Parameters:
      uniform - the uniform's name
      values - all values, one element after the other
      perElement - values per element: 1 for float[], 3 for vec3[]