Need example code for texture shader

Hi. I trying to draw texture on shader, but its not work. What I do wrong? bg drawn correct.

PShader mShader;
    PImage bg;
    PImage tex;
    void setup() {
      size(640, 360, P2D);
      bg = loadImage("bg.jpg");
      tex = loadImage("smoke.png");
      mShader = loadShader("texfrag.glsl", "texvert.glsl");
      mShader.set("texture", tex);
    void draw() {


#ifdef GL_ES
precision mediump float;
precision mediump int;

uniform sampler2D texture;

varying vec4 vertColor;
varying vec4 vertTexCoord;

void main() {
  gl_FragColor = texture2D(texture, * vertColor;


uniform mat4 transform;
uniform mat4 texMatrix;

attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;

varying vec4 vertColor;
varying vec4 vertTexCoord;
uniform sampler2D texture;

void main() {
  gl_Position = transform * position;
  vertColor = color;
  vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);

Or need sample code for texture draw on shader.


  • what exactly are you drawing? there's nothing after the shader in draw. try drawing a rectangle.

    look at the example in the reference. it sets the shader and then draws an image. every pixel in the image is affected by the shader.

  • I want to draw the image and morph it. I read all reference but not found how to draw simple 2D texture on shader.

  • edited January 2018 Answer ✓

    Correct way:

    1. set shader setting
    2. draw (shaded) thing

    Like in the reference example:

    void draw() {
      image(img, 0, 0);

    What you are doing:

    void draw() {
      image(bg,0,0);    // draw unshaded image
      shader(mShader);  // set shader, then don't draw anything

    ...that is wrong.

  • Check this in

    Section: texture shaders, code listing 5.1

    Also you should check as it might do what you want in case you are feeding your texture externally.


  • edited January 2018

    Thanks, I set image(tex,0,0); after shader(mShader); and it works, but bg also morphed :|

  • Actual code is more useful than short descriptions of your problem.

Sign In or Register to comment.