Alpha value blend of mesh vertices in a shader alpha not blending properly

I am using a shader to crop a mesh, I check if the points are inside a cropbox and if they are not I set the colour of the vertex to (0.0, 0.0, 0.0, 0.0). However the resulting colour of the points outside the mesh still seems to blend strangely with anything behind it, instead of just being transparent the vertices that have the color (0.0, 0.0, 0.0, 0.0) seem to make anything behind them also transparent.

The vertex shader - I send the un-transformed position out to the fragment shader as I am using this with ofEasyCam and I need the points not transformed by the camera position to do the crop. I then check those points in the fragment shader and if they are outside of my cropbox I set them to be transparent.

#version 150

uniform mat4 matrix;
in vec4 position;
in vec2 texcoord;

uniform int mode;
uniform float FloatVert1;
uniform float FloatVert2;
uniform float FloatVert3;
uniform float FloatVert4;
uniform float FloatVert5;
uniform float FloatVert6;
uniform float time;

out vec2 texCoordVarying;
out vec4 positionVarying;

void main()

//FloatVert1 is Displacemnt timing
//FloatVert2 is Displacemnt amount
//FloatVert4 position x scale
//FloatVert5 position y scale
//FloatVert6 position z scale

    texCoordVarying = vec2(texcoord.x, texcoord.y);

    vec4 outPosition = matrix * vec4(-position[0] *FloatVert4, -position[1] * FloatVert5, position[2] * FloatVert6, position[3]);	

   	positionVarying = vec4(-position[0], -position[1], position[2], position[3]);

    gl_Position = outPosition;
#version 150

uniform sampler2DRect tex0;

in vec2 texCoordVarying;
in vec4 positionVarying;
out vec4 outputColor;

uniform int mode;
uniform float FloatFrag1;
uniform float FloatFrag2;
uniform float FloatFrag3;
uniform float FloatFrag4;
uniform float FloatFrag5;
uniform float FloatFrag6;
uniform float time;
uniform vec3 cropBoxPos;
uniform vec3 cropBoxSize;

void main()

	if((positionVarying[0] < cropBoxPos[0] + (cropBoxSize[0]/2.0)) && (positionVarying[0] > cropBoxPos[0] - (cropBoxSize[0]/2.0)) && (positionVarying[1] < cropBoxPos[1] + (cropBoxSize[1]/2.0)) && (positionVarying[1] > cropBoxPos[1] - (cropBoxSize[1]/2.0)) && (positionVarying[2] < cropBoxPos[2] + (cropBoxSize[2]/2.0)) && (positionVarying[2] > cropBoxPos[2] - (cropBoxSize[2]/2.0))){

	outputColor = col;
	outputColor = vec4 (1.0, 0.0, 0.0, 0.0);

Here is a sample where the background is yellow to see what happens - first with the alpha of the points outside the crop set to 0.0 you can see it makes everything behind the points with a 0.0 alpha also transparent as the background comes through.

Here is exactly the same code but instead the points outside the cropbox are set to (1.0,0.0,0.0,1.0).

Is there something else I should do when I am using a shader to crop to stop the alpha affecting everything that would be behind it - it is like it is some sort of blend mode.

Looks to me like depth testing is disabled on the client side (this is by default). Is there a ofEnableDepthTest() in setup()? This and ofEnableAlphaBlending() should do the trick.

Yes, I am not sharp today, I did not enable alpha blending but this outcome is interesting for future exploration. The depth testing was fine, the red is meant to be in front of the scene.

Actually on further checking ofEnableAlphaBlending() makes no difference. If I change the blend mode to additive then the colours of my mesh are messed up, but the pixels with alpha levels of 0 remain transparent.

For reference, blending and depth testing should be set up this way:


Alternatively, you can try to use discard; instead of outputColor = vec4 (1.0, 0.0, 0.0, 0.0); in your fragment shader if you only intend to hide the cropped pixels. This requires no alpha blending.