# OpenGL ES 2.0: Controlling Transparency in Fragment Shader

The following is the OpenGL ES 2.0 simple GLSL Fragment Shader, I use to place textures on polygons, to render 2D sprites.

varying mediump vec2 TextureCoordOut;
uniform sampler2D Sampler;

void main()
{
gl_FragColor = texture2D(Sampler, TextureCoordOut);
//gl_FragColor = vec4(texture2D(Sampler, TextureCoordOut).xyz, TextureCoordOut.w * 0.5);

}


The fragment shader places voxels with alpha information taken from the source 2D texutre(.png image). Apart from alpha information, I need to control overall polygon/sprite transparency to achieve Fade In/Fade Out effects.

Could you show me, please, how to modify the above shader to control the overall transparency, besides the alpha information?

Note: The commented out line is used for my attempts to achieve the transparency. I wish to combine both the alpha information with the overall polygon/sprite transparency.

Thanks.

This is probably cleaner solution, which also doesn't require to ask texture unit two times.

void main()
{
gl_FragColor = texture2D(Sampler, TextureCoordOut);
gl_FragColor.a *= 0.5;
}


(maybe compiler does that for you, but what if not. Especially on the opengl-es device performance difference may be observed)

• +1, Agreed, and I will modify my code according to your idea. Let me mar yor answer as the Accepted Answer as well. – Bunkai.Satori May 27 '11 at 2:29
• I was under the impression that you cannot set gl_FragColor multiple times. Once you set it, the color is fed into the frame buffer. Is that a limitation on newer version of OpenGL? Maybe ES? – Utkarsh Sinha Jun 1 '13 at 22:50

Apparently, I found the soultion.

This is the correct line in Fragment Shader:

    gl_FragColor = vec4(texture2D(Sampler, TextureCoordOut).xyz, texture2D(Sampler, TextureCoordOut).w * 0.5);


Thanks anyway.