Simple glow shader for HUD elements

Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
Post Reply
User avatar
kodi
 
 
Posts: 1355
Joined: Mon May 06, 2013 8:02 am

Simple glow shader for HUD elements

Post by kodi »

This is a shader meant for hud graphics that are supposed to glow. The larger you want your glow to be, the more transparent space you'll need to have around the edge of your graphic.
Image
The armor pieces on the little dude at the left edge of the screen shows the effect.

Code: Select all

//texture glow fragment shader by kodi
//use as you wish, no need to ask. Credits appreciated though!

//control the shader with these. Both radialSamples and size affect performance. The former should not be a lower value than the latter.
const int radialSamples = 16;
const int size = 8;
const float intensity = 0.005;

//full revolution in radians
const float twopi = 6.2831;

vec4 Process(vec4 color) 
{
	vec2 tc = gl_TexCoord[0].st; //coordinate of the fragment(pixel) in the image
	vec4 color1 = getTexel(tc); //texture color of fragment
	ivec2 texSize = textureSize(tex, 0); //texture size in pixels
	vec2 pixel = 1.0/vec2(texSize); //pixel size in a normalized 0-1.0 range
	vec4 newcolor = color1;
	if(color1.a <= 0) //if the fragment is transparent we attempt adding glow
	{
		for(int i=1; i<=radialSamples; i++) //in this loop, we rotate around the fragment to check color of neighbouring ones
		{
			float angle = twopi*(float(i)/float(radialSamples)); //angle, increases each time "i" does. 
			vec2 offset = vec2(cos(angle)*pixel.x,sin(angle)*pixel.y); //gets offset to a pixel adjacent to the original
			for(int j=1; j<=size; j++) //in this loop, we check the color of (size) amount of pixels in a straight line
			{
				vec4 glowSample = getTexel(tc+(offset*float(j))); //gets the color of a new sample   
				newcolor.rgb = newcolor.rgb + (glowSample.rgb*intensity);  //adds to the original
				newcolor.a = min(1.0, newcolor.a+(glowSample.a*intensity)); //adjusts and caps alpha
			}
		}
	}
	return newcolor;
}
Post Reply

Return to “Shaders”