Circular health bars

Started by
18 comments, last by rip-off 11 years, 10 months ago

Sorry about that, I'm referring to a bar similar to the teal bar in the thumbnail below. It starts on the right side, where it end in a small ball, and fills up along the black part all the way around the creature's head as your character's experience increases.
dHoEi.png
Thumbnail from The Witcher.


I'd like to clarify that the technique I described works for the shape(or any other irregular circle shape) provided in your example picture, not sure if that was clear from my previous post. Basically you use the angle gradient to get the right portion of the health you wish to display and then you mask pixels to form the shape you want.
Advertisement
I did something very similar in a pixel shader -- passed in the %age to fill as a parameter to the corners. Each pixel looked to see if it was inside the circle or outside and bailed if so. Then looked to see what angle round the circle it was, compared that to the display %age and picked an appropriate colour.

We also had a version which did a texture fetch based on the angle so we could pass gradients in as textures to use.

We could then put health bars on thousands of screen elements.
That pixel shader technique has the advantage of working with all kinds of crazy shapes, not just circles. You might be able to do the same thing without a shader by using alpha test.
In general, GUI stuff like this isn't done with a clever shader. The most commonly used tech for game GUIs is Flash, using ScaleForm to render it into the game (RAD game tools recently released Iggy, with equivalent functionality). In this case, the health bar -- whatever its shape -- would be an animation, with the player's health level determining which frame was shown. This lets your GUI artist use content creation tools he's comfortable with, and lets him iterate without constant support from a programmer.

Obviously a ScaleForm license is out of the reach of most indie devs, but if you're using UDK, good news -- it's in there. (Crytek might have it available too, but I'm not sure.)
I'd just use an alpha-masked texture. Draw out a nice alpha gradient along the path you want your health bar to follow, and build 2 textures one for the completely filled state and one for the completely empty state. Then pass in the % of life that the player has left to the shader, and have it use the empty texture for alpha values greater than this, and the filled texture for alpha values less than this (with perhaps some blending between them around the boundary). This way, you can have any shape that you want.
Hello everybody,

first of all, I must apology for posting in such an old topic. However, I've seen this article (and some others around the internet) a hundred times, and tried to learn some HLSL syntax, but I cannot figure out how to turn this idea into code. I have found some interesting stuff in other forums, but most of it deprecated. Since this is the best explanation I've read about how to do it so far, I've decided to ask here: can someone write an example of the pixel shader? Thank you in advance.

Greetings,

Aaron Contreras

[quote name='Lloyd040690' timestamp='1307423706' post='4820392']
Sorry about that, I'm referring to a bar similar to the teal bar in the thumbnail below. It starts on the right side, where it end in a small ball, and fills up along the black part all the way around the creature's head as your character's experience increases.
dHoEi.png
Thumbnail from The Witcher.


I'd like to clarify that the technique I described works for the shape(or any other irregular circle shape) provided in your example picture, not sure if that was clear from my previous post. Basically you use the angle gradient to get the right portion of the health you wish to display and then you mask pixels to form the shape you want.
[/quote]

And you don't have to use an angle gradient either, you can use a spiral gradient, linear gradients, or a custom drawn mask for some really wierd fill patterns.

Also, you don't have to use a pixelshader for it, you can place your mask texture as the alpha channel in the "fill" texture and use a fixed function alpha test.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Since this is the best explanation I've read about how to do it so far, I've decided to ask here: can someone write an example of the pixel shader? Thank you in advance.
Why not make your own thread, this one was dead a year ago

Hello everybody,

first of all, I must apology for posting in such an old topic. However, I've seen this article (and some others around the internet) a hundred times, and tried to learn some HLSL syntax, but I cannot figure out how to turn this idea into code. I have found some interesting stuff in other forums, but most of it deprecated. Since this is the best explanation I've read about how to do it so far, I've decided to ask here: can someone write an example of the pixel shader? Thank you in advance.

Greetings,

Aaron Contreras


i'm not certain about hlsl, but i imagine it shouldn't be too hard to get the idea from a glsl example, going off the second post, i'd draw a rectangle(or w/e shape really), and do something like so in shader:


uniform float Health; //Mapped health between 1-0, where 1 is full health, and 0 is none.
unifom sampler2D HealthTexture;
uniform vec4 g_Color; //global color(generally red for health)

in vec2 TexCoord;
out vec4 f_Color; //final output color
void main(){
f_Color = texture2D(HealthTexture, TexCoord);
if(f_Color.r<=1.0f-Health) discard; //discard the fragment if our health is too low, by doing 1-health(1-health so that we map from black->white in terms of loss health)
//alternativly, instead of discarding, we could do some other color like black, it's up to you.
f_Color*=g_color; //Now make it w/e color we want.
}


anywho, that's how i in vision it would be done.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Please do not resurrect old threads. Feel free to create a new thread if you wish.

This topic is closed to new replies.

Advertisement