threshold shader

edited April 2014 in GLSL / Shaders

Can someone help me finish a threshold shader? This is how far i got (don't laugh):

PShader threshold;  
PImage img;
boolean enabled = true;

void setup() {
  size(640, 360, P2D);
  img = loadImage("leaves.jpg");      
  threshold = loadShader("threshold.glsl");
}

void draw() {
  if (enabled == true) {
    shader(threshold);

    float midPoint = norm(mouseX, 0, width);
    midPoint = constrain(midPoint, 0, 1);

    threshold.set(midPoint);

  }
  image(img, 0, 0);
}

void mousePressed() {
  enabled = !enabled;
  if (!enabled == true) {
    resetShader();
  }
}



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

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;
uniform vec2 texOffset;

varying vec4 vertColor;
varying vec4 vertTexCoord;


uniform float midPoint;

void main(void) {


  //pixel.a = someValue > 4.5 ? 1.0 : 0.5; 
  //pixel.a = someValue > 4.5 ? (someValue > 6.0 ? 1.0 : 0.7) : 0.5;

  //vec4 sum = 8.0 * col4 - (col0 + col1 + col2 + col3 + col5 + col6 + col7 + col8); 

  //vec2 tc4 = vertTexCoord.st + vec2(         0.0,          0.0);

  //gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;

  gl_FragColor = // either black or white depending on midpoint

  // btw, would an inverse option slow things down?
}

It would be really nice btw if all filters: http://processing.org/reference/filter_.html Had also a shader version, now there are some awsome shaders in processing but they are more a show off. They don't seem very usefull apart from a few.

Answers

  • Adapted Code

    // Sketch
    
    PShader threshold; 
    PImage img;
    boolean enabled;
    
    void setup() {
      size(640, 360, P2D);
      img = loadImage("leaves.jpg");     
      threshold = loadShader("threshold.glsl");
    }
    
    void draw() {
      if (enabled) threshold.set("midPoint", constrain(float(mouseX)/width, 0, 1));
      image(img, 0, 0);
    }
    
    void mousePressed() {
      if (enabled = !enabled) shader(threshold); else resetShader();
    }
    
    // Shader
    
    #define PROCESSING_TEXTURE_SHADER
    
    uniform sampler2D texture;
    varying vec4 vertTexCoord;
    uniform float midPoint;
    
    void main() {
      vec4 texColor = texture2D(texture, vertTexCoord.st).rgba;
      float brightness = (0.2126*texColor.r) + (0.7152*texColor.g) + (0.0722*texColor.b);
      if (brightness > midPoint) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
      } else {
        gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
      }
    }
    
  • Thanks!

    Where are those small numbers coming from? 0.2126texColor.r) + (0.7152texColor.g) + (0.0722*texColor.b

  • To threshold you have to compare against something. Your shader didn't contain such as value and your description didn't mention the distinguishable/threshold feature. In my opinion the first contender would be the brightness/lightness/luminance. To calculate that based on the rgb color values you need a formula:

  • In my opinion the first contender would be the brightness/lightness/luminance.

    I didn't think of anything else :) And i love how fast glgs shaders are :D

Sign In or Register to comment.