Class Name

PShader

Description

This class encapsulates a GLSL shader program, including a vertex and a fragment shader. It is compatible with P2D and P3D, but not with the default renderer. Use the loadShader() function to load your shader code. Note: It's strongly encouraged to use loadShader() to create a PShader object, rather than calling the PShader constructor manually.

Examples

  • PShader blur;
    
    void setup() {
      size(640, 360, P2D);
      // Shaders files must be in the "data" folder to load correctly
      blur = loadShader("blur.glsl"); 
      stroke(0, 102, 153);
      rectMode(CENTER);
    }
    
    void draw() {
      filter(blur);  
      rect(mouseX-75, mouseY, 150, 150); 
      ellipse(mouseX+75, mouseY, 150, 150);
    }
    
  • PImage tex;
    PShader deform;
    
    void setup() {
      size(640, 360, P2D);
      tex = loadImage("tex1.jpg");
      deform = loadShader("deform.glsl");
      deform.set("resolution", float(width), float(height));
    }
    
    void draw() {
      deform.set("time", millis() / 1000.0);
      deform.set("mouse", float(mouseX), float(mouseY));
      shader(deform);
      image(tex, 0, 0, width, height);
    }
    

Constructors

  • PShader()
  • PShader(parent)
  • PShader(parent, vertFilename, fragFilename)
  • PShader(parent, vertURL, fragURL)
  • PShader(parent, vertSource, fragSource)

Parameters

  • parent(PApplet)the parent program
  • vertFilename(String)name of the vertex shader
  • fragFilename(String)name of the fragment shader
  • vertURL(URL)network location of the vertex shader
  • fragURL(URL)network location of the fragment shader

Methods

  • set()Sets a variable within the shader