Shader problem with two textures

edited August 2017 in GLSL / Shaders

Hello, I try to mix two images with shader, that work but when I want display the image of origine in a same time, the texturing one is always under shader effect, i don't know if it's understable. So I write a little code to make that more understable. plus a link on github too

Processing

PImage src_1 ;
PImage src_2 ;
PShader rope_shader_overlay;
void setup() {
  size(100,100,P2D);
  src_1 = new PImage(width,height);
  src_2 = new PImage(width,height);
  rope_shader_overlay = loadShader("rope_frag_overlay.glsl");

  change_pixel();
}

void draw() {
  background(0);
    change_pixel();
  PImage work = new PImage(src_2.width, src_2.height);
  // work.pixels = src_2.pixels ;
  for(int i = 0 ; i < work.pixels.length ;i++) {
    work.pixels[i] = src_2.pixels[i];
  }

 PImage display = overlay(work, src_1, .5,.6,.3,1);

  // image(display, 10,10);
  image(display,0,0);
  image(src_1, -width + width/3, 0);
  image(src_2, width -width/3, 0); // this image have a same appearence than shader.
}


PImage overlay(PImage tex, PImage inc, float... ratio) {
  shader(rope_shader_overlay);
  rope_shader_overlay.set("texture",tex);
  rope_shader_overlay.set("incrustation",inc);
  rope_shader_overlay.set("ratio",ratio[0],ratio[1],ratio[2],ratio[3]);
  return tex;
}

void change_pixel() {
  src_1.loadPixels() ;
  src_2.loadPixels() ;
  for(int i = 0 ; i < src_1.pixels.length ; i++) {
    src_1.pixels [i] = color(random(255),random(255),random(255));
    src_2.pixels [i] = color(abs(sin(frameCount *.01)) *255,abs(sin(frameCount *.02)) *255,abs(sin(frameCount *.001)) *255);
  }
  src_1.updatePixels();
  src_2.updatePixels();
}

GLSL

/**
ROPE - Romanesco processing environment – 
* Copyleft (c) 2014-2017 
* Stan le Punk > http://stanlepunk.xyz/
Shader mix from incrustation to texture
Overlay effect
v 0.0.1
*/
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_TEXTURE_SHADER

varying vec4 vertTexCoord;
uniform sampler2D texture;
uniform sampler2D incrustation;
uniform vec4 ratio;   




void main() {
  vec4 a = vec4(texture2D(texture,vertTexCoord.st));
  vec4 b = vec4(texture2D(incrustation,vertTexCoord.st) *ratio);
  vec4 rgba = a+b;
  gl_FragColor = rgba;
}

Answers

  • edited August 2017 Answer ✓

    The shader can be set once and will be used every time. To view src_2 before the shader, first remove this line from inside overlay() on line 32:

    shader(rope_shader_overlay);

    Then change the second half of the draw function to look like this:

    void draw() {
      ...
      PImage display = overlay(work, src_1, .5,.6,.3,1);
    
      shader(rope_shader_overlay);
      image(display,0,0); // These are drawn with the shader
      image(src_1, -width + width/3, 0);
    
      resetShader();
      image(src_2, width -width/3, 0); // This is drawn normally
    }
    
  • Thx a lot, I totally forget the resetShader() method

Sign In or Register to comment.