Hey all. I’m trying to use blendMode() in a pretty specific way, but I’m getting results I wouldn’t expect. I would like to draw random red rectangles (255,0,0) and green rectangles (0,255,0) and then use a fragment shader to assign a color to a pixel based on whether it is red, green, red and green, or neither. Here’s my processing code.
PShader testShader;
void setup() {
size(1000,1000,P2D);
testShader = loadShader("test.frag");
}
void draw() {
noStroke();
shader(testShader);
blendMode(REPLACE);
fill(0,0);
rect(0,0,width,height);
blendMode(ADD);
fill(255,0,0);
rect(0,0,750,750);
fill(0,255,0);
rect(250,250,1000,1000);
}
And here is my shader…
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
varying vec4 vertColor;
varying vec4 vertTexCoord;
void main() {
vec3 color;
if (vertColor[0] == 1 && vertColor[1] == 1) {
color = vec3(151,171,128);
}
else
if (vertColor[0] == 1 && vertColor[1] == 0) {
color = vec3(182,217,208);
} else
if (vertColor[0] == 0 && vertColor[1] == 1) {
color = vec3(217,106,73);
} else
if (vertColor[0] == 0 && vertColor[1] == 0) {
color = vec3(26,71,84);
}
gl_FragColor = vec4(color/255.,1.);
gl_FragColor = vertColor;
}
If I uncomment that last line and set gl_FragColor to vertColor, I get a red box in the top left, a green box in the bottom right, and a yellow box where they overlap. This is what I’d expect, and the yellow in the middle looks like (255,255,0).
If I comment that last line, the pixel color should be set to one of the four color values I have hard coded. The background would be a dark teal, the top left would be a light blue, the bottom right would be an orange/red, and the middle would be great. The dark teal is the only one of these colors that look the way I’d expect. The blue and red are lighter than expected, and the green color is totally white.
I can’t tell what’s going on, but I would expect vertColor to be (1,1,0) in the middle, thus resulting in a green pixel due to the logic I’ve implemented.
Alternatively, if there is another way to write to just the red channel of a canvas, I would love to know about that. I’m essentially trying to write multiple monochrome canvases so that I can do things with those multiple monochrome canvases in glsl.