Health circle
This shader can be filled by a configurable amount, which makes it useful for representing stats such as health.
Shader code
shader_type canvas_item;
uniform float width = 0.3;
uniform float gradient_ease = 5.0;
uniform float fill_ratio = 0.8;
void fragment() {
float fill_angle = fill_ratio * 3.141592656 * 2.0;
vec2 uv = UV * 2.0 - 1.0;
if (atan(uv.x, uv.y) + 3.141592656 < fill_angle) {
float inner_width = 1.0 - width;
inner_width *= inner_width;
float d = uv.x * uv.x + uv.y * uv.y;
if (d >= inner_width && d <= 1.0) {
float w = abs((1.0 + inner_width) / 2.0 - d) / (1.0 - inner_width);
w = 1.0 - pow(w + 0.5, gradient_ease);
COLOR = vec4(vec3(1.0), min(1.0, w));
} else {
COLOR.a = 0.0;
}
} else {
COLOR.a = 0.0;
}
}
I get an error when trying to use this…
error(16): Expected boolean expression
Ah, there is an ‘=’ instead of a ‘==’ there. I’m not sure how that snuck in…EDIT: it actually needs to be
if (d <= 1.0 && d >= inner_width)
instead ofif (d = inner_width)
. I guess that for some reason the things between the angle brackets got interpreted as HTML and got removed? :thinking:EDIT 2: Indeed, the site is removing it automatically.
Thanks for letting me know!
Thanks for following up! I appreciate it!
Man, this shader is underrated! I was just thinking about how to write a health circle in GDScript and I couldn’t think of a good way (I also suck at math…) when all of a sudden I stumbled across this. Very good stuff!