Simple Pie Chart
Displays a pie chart based on an array of colors and percentages.
The last color’s percentage is the remainder after the other four are added. Percentages adding to more than 1 may be unpredictable.
Supports a mild blur effect to avoid hard edges in color and transparency.
I had thought I wanted little pie charts in my game. Turns out I don’t need it, but maybe someone else will find a use.
If changing the number of colors, need to manually change the array sizes, either due to a limitation of godot’s shader language or a limitation of my ability to use it.
Shader code
shader_type canvas_item;
const int num_colors = 5;
const int num_shares = num_colors - 1;
uniform vec4 colors[5] : source_color;
uniform float shares[4]; // The last color gets the remainder
uniform float relative_size : hint_range(0.0, 1.0, 0.1) = 1.;
uniform float blur_strength : hint_range(0., .1, .005) = .01;
void fragment() {
float angle = (atan(.5-UV.y, UV.x-.5) + PI)/(2.*PI);
float r = length(UV-vec2(.5));
float tot_shares = 0.;
COLOR = mix(colors[num_colors - 1],colors[0], smoothstep(blur_strength, .0, 1. - angle));
for (int i = 0; i < num_colors-1; i++) {
tot_shares += shares[i];
if (angle < tot_shares) {
COLOR = mix(colors[i],colors[i+1], smoothstep(blur_strength, .0, abs(angle-tot_shares)));
break;
}
}
COLOR.a = min(COLOR.a, smoothstep(0, blur_strength*2., relative_size*.5 - r));
}