3D Health Bar
World’s simplest health bar.
- Works best on QuadMesh or Sprite3D since these allow billboarding (optional).
- Allows transparancy through Alpha Channel.
- No textures: Just colors.
- Uses an instance variable for health, so you don’t have to make the material unique: It will display amount of health automatically for each instance.
Shader parameters:
- Use Billboard: If true, the Mesh will always face the camera from any angle.
- Color: Color indicating the amount of health.
- Background Color: The background color of the full healthbar, exposed when health is taken away.
Instance shader parameters:
- Health: Controls the lenght of the color, between 0 and 1. Since this is an instance parameter, the material does not have to be made unique.
Shader code
shader_type spatial;
render_mode unshaded;
uniform bool use_billboard;
uniform vec4 color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 background_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
instance uniform float health : hint_range(0.0, 1.0) = 0.5;
void vertex() {
if (use_billboard){
MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0], INV_VIEW_MATRIX[1], INV_VIEW_MATRIX[2], MODEL_MATRIX[3]);
}
}
void fragment() {
ALBEDO = UV.x < health ? color.rgb : background_color.rgb;
ALPHA = UV.x < health ? color.a : background_color.a;
}