Take the 2-minute tour ×

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

1 Answer 1

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

Your Answer


