Quantcast
Channel: GLSL / Shaders - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 212

Blend results in LIGHTEST fashion

$
0
0

I use a shader to select a color.

Here it is for blueish for example: Screen Shot 2015-10-07 at 10.44.06 AM

Here for redish: Screen Shot 2015-10-07 at 10.44.28 AM

Now I wan't to blend the 2 in a LIGHTEST kind of fashion. I tried blend(img, 0, 0, 512, 512, 0, 0, 512, 512, LIGHTEST); but as expected this doesn't work. Even worse, it makes crash JRE a lot it seems! (Is this a processing bug?).

What would be a good way to blend those results? I'm looking for a solution that is scalable (so also use the color select for 7 colors for example).

If possible: -a fast solution (so gpu prefered) -no edits to the current shader (if that makes sense to do)

PImage img;
PShader shader;


void settings() {
  size(1512, 1000, P2D);
}


float[] targetColorRGB = new float[] {0.2, 0.4, 0.7};

float fuzzinessH = 0.1;
float fuzzinessS = 0.3;
float fuzzinessV = 0.3;

// for 2nd color:

float[] targetColor2RGB = new float[] {0.5, 0.3, 0.2};

float fuzziness2H = 0.1;
float fuzziness2S = 0.3;
float fuzziness2V = 0.3;


void setup() {

  img = loadImage("http://" + "localhost:8888/catalogtree/helipad_detection/testImg2.png");
  shader = loadShader("http://" + "localhost:8888/shaders/colorRangeHSV.glsl");

  colorMode(RGB, 1, 1, 1, 1);
}



void draw() {

  background(0);

  shader(shader); 
  shader.set("targetColor", targetColorRGB, 3);

  shader.set("fuzzinessH", fuzzinessH);
  shader.set("fuzzinessS", fuzzinessS);
  shader.set("fuzzinessV", fuzzinessV);
  image(img, 512, 0, height, height);  

  //resetShader();

  // now apply the shader again with different settings

  shader(shader); 
  shader.set("targetColor", targetColor2RGB, 3);

  shader.set("fuzzinessH", fuzziness2H);
  shader.set("fuzzinessS", fuzziness2S);
  shader.set("fuzzinessV", fuzziness2V);

  //image(img, 512, 0, height, height);

  // this also makes crash JRE a lot!
  blend(img, 0, 0, 512, 512, 0, 0, 512, 512, LIGHTEST);

  resetShader();
  // the original
  image(img, 0, 0, 512, 512);
}

The shader:

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

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;
varying vec4 vertTexCoord;

// excepts a color in RGB
uniform vec3 targetColor; 

uniform float fuzzinessH;
uniform float fuzzinessS;
uniform float fuzzinessV;



// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

float norm(in float value, in float start, in float stop) {
    return (value - start) / (stop - start);
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

void main() {

  vec3 texColor = texture2D(texture, vertTexCoord.st).rgb;

  vec3 hsvTexColor = rgb2hsv(texColor);
  vec3 hsvTargetColor = rgb2hsv(targetColor);

  float distanceH = distance(hsvTexColor.r, hsvTargetColor.r);
  float distanceS = distance(hsvTexColor.g, hsvTargetColor.g);
  float distanceV = distance(hsvTexColor.b, hsvTargetColor.b);


  /*
  vec3 result;

  if (distanceH < fuzzinessH && distanceS < fuzzinessS && distanceV < fuzzinessV) {

    float normH = norm(distanceH, fuzzinessH, 0);
    float normS = norm(distanceS, fuzzinessS, 0);
    float normV = norm(distanceV, fuzzinessV, 0);

    result = vec3(min(normH, min(normS, normV)));
  }
  else {
    result = vec3(0);
  }
  */

  // the result looks the same without any if statements
  // so we go for that cause shaders without if statements tend to be faster!

  float normH = norm(distanceH, fuzzinessH, 0);
  float normS = norm(distanceS, fuzzinessS, 0);
  float normV = norm(distanceV, fuzzinessV, 0);

  vec3 result = vec3(min(normH, min(normS, normV)));


  gl_FragColor = vec4(result, 1.0);
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Viewing all articles
Browse latest Browse all 212

Trending Articles