Use an extra texture with a shader

I wan't to use an extra texture with a shader. For some reason I get this error:

The shader doesn't have a uniform called "textureB" OR the uniform was removed during compilation because it was unused.

I know what the error means, but in this case it doesn't make sense to me. What is going wrong?

PShader psBlendLightest;

PGraphics pg;

PGraphics textureB;


void settings() {
  size(512, 512, P2D);
}

void setup() {
  psBlendLightest = loadShader("http://localhost:8888/shaders/blendLightest.glsl"); // set with processing LIGHTEST

  pg = createGraphics(512, 512, P2D);
  textureB = createGraphics(512, 512, P2D);

  pg.beginDraw();
  pg.background(0);
  pg.fill(255);
  pg.quad(25, 25, 400, 40, 450, 500, 40, 300);
  pg.endDraw();


  textureB.beginDraw();
  textureB.background(0);
  textureB.fill(255);
  textureB.ellipse(256, 256, 400, 400);
  textureB.endDraw();


}


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

  psBlendLightest.set("textureB", textureB);
  shader(psBlendLightest);

  image(pg, 0, 0);  

}

The shader:

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;
varying vec4 vertTexCoord;


uniform sampler2D textureB;



void main() {

  vec3 texColorA = texture2D(texture, vertTexCoord.st).rgb;
  vec3 texColorB = texture2D(textureB, vertTexCoord.st).rgb;

  gl_FragColor = vec4(max(texColorA, texColorB), 1.0);
}

Answers

  • edited October 2015

    First of all you wanna use filter() instead of shader() or Processing won't set the texture uniform.

    Well, your code works when using new PShader() directly:

    PShader psBlendLightest;
    PGraphics pg, textureB;
    
    void setup() {
        size(512, 512, P2D);
        pg = createGraphics(512, 512, P2D);
        pg.beginDraw();
        pg.background(0);
        pg.fill(255);
        pg.quad(25, 25, 400, 40, 450, 500, 40, 300);
        pg.endDraw();
        pg.updatePixels();
        textureB = createGraphics(512, 512, P2D);
        textureB.beginDraw();
        textureB.background(0);
        textureB.fill(255);
        textureB.ellipse(256, 256, 400, 400);
        textureB.endDraw();
        textureB.updatePixels();
        //psBlendLightest = loadShader("blendLightest.frag"); // Doesn't work
        //psBlendLightest = loadShader("blendLightest.frag", "blendLightest.vert"); // Doesn't work
        psBlendLightest = new PShader(this, new String[] {
            "uniform mat4 transform;",
    
            "attribute vec4 vertex;",
            "attribute vec4 texCoord;",
    
            "varying vec4 vertTexCoord;",
    
            "void main() {",
                "vertTexCoord = vec4(vec2(texCoord.x, 1.0 - texCoord.y), 1.0, 1.0);",
                "gl_Position = transform * vertex;",
            "}"
        },
        new String[] {
            "uniform sampler2D texture;",
            "uniform sampler2D textureB;",
    
            "varying vec4 vertTexCoord;",
    
            "void main() {",
                "vec3 texColorA = texture2D(texture, vertTexCoord.xy).rgb;",
                "vec3 texColorB = texture2D(textureB, vertTexCoord.xy).rgb;",
                "gl_FragColor = vec4(max(texColorA, texColorB), 1.0);",
            "}"
        });
    }
    
    void draw() {
        image(pg, 0, 0);
        psBlendLightest.set("textureB", textureB);
        filter(psBlendLightest);
    }
    

    blendLightest.vert

    uniform mat4 transform;
    
    attribute vec4 vertex;
    attribute vec4 texCoord;
    
    varying vec4 vertTexCoord;
    
    void main() {
        vertTexCoord = vec4(vec2(texCoord.x, 1.0 - texCoord.y), 1.0, 1.0);
        gl_Position = transform * vertex;
    }
    

    blendLightest.frag

    uniform sampler2D texture;
    uniform sampler2D textureB;
    
    varying vec4 vertTexCoord;
    
    void main() {
        vec3 texColorA = texture2D(texture, vertTexCoord.xy).rgb;
        vec3 texColorB = texture2D(textureB, vertTexCoord.xy).rgb;
        gl_FragColor = vec4(max(texColorA, texColorB), 1.0);
    }
    

    But even if you include a custom vertex shader loadShader() will "optimize" textureB away. Looks like a bug to me.

  • I can't get it to work. Maybe due i'm on a mac again. I opened an issue.

    https://github.com/processing/processing/issues/3968

Sign In or Register to comment.