Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a "halo" shader in unity using GLSL. And I've come to a roadblock.

What I need to do is take a texture, like the following, and make it transparent according to the darkness of it. And I don't want a cutout, because that cuts it off at a hard edge.

Default particle

This line of code doesn't seem to work.

gl_FragColor = texture2D( vec4( _MainTex.r, _MainTex.g, _MainTex.b, _MainTex.a), vec2(textureCoordinates));

share|improve this question
have you able to found ans? – idurvesh Jan 22 at 3:53

Normally you store the transparency of a texture in the alpha channel, you can use an external editor like Paint.Net (free) or Photoshop to edit the alpha channels of a picture.

However since you're using shaders there is another trick you can do. A color is totally black if all it's color channels (R/G/B) are 0. It's gray when it's 128/128/128 and white when it's 255/255/255. If you want to determine the blackness you can take the average of the RGB channels, however a picture with bright red but no green or blue will still mean 'pretty black' and will still be 2/3rds transparent. However just try it for fun change your blend shader code to and see how it looks:

float blackness = (_Maintex.r + _Maintex.g + _Maintex.b) / 3.0;
gl_FragColor = texture2D( vec4( _MainTex.r, _MainTex.g, _MainTex.b, blackness), vec2(textureCoordinates));
share|improve this answer
I'd call that luminance or gray level or something, not 'blackness' (it's actually more like 'whiteness', since it's 0 for black and 1 for white). – Nathan Reed Jun 10 '12 at 5:20
can you explain how to do it in CG? – idurvesh Jan 22 at 3:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.