|
1 |
|
2 layout(location = 0) out vec4 diffuse; |
|
3 in vec2 uvcoord; |
|
4 |
|
5 uniform vec2 radii; |
|
6 #ifdef FILL |
|
7 uniform vec4 color; |
|
8 #endif |
|
9 #ifdef BORDER |
|
10 uniform float thickness; |
|
11 uniform vec4 border_color; |
|
12 #endif |
|
13 |
|
14 void main(void) { |
|
15 // Calculate center of the ellipse |
|
16 vec2 center = radii; |
|
17 |
|
18 // Calculate position relative to center |
|
19 vec2 pos = uvcoord - center; |
|
20 |
|
21 // For an ellipse, we need to calculate normalized distance based on the formula: |
|
22 // (x/a)² + (y/b)² = 1 where a and b are the semi-axes |
|
23 vec2 normalized = pos / center; |
|
24 |
|
25 // Calculate squared distance in ellipse space |
|
26 // Value = 1.0 exactly at the ellipse boundary |
|
27 float dist_squared = dot(normalized, normalized); |
|
28 |
|
29 #ifndef BORDER |
|
30 // For filled ellipse |
|
31 if (dist_squared > 1.0) { |
|
32 discard; // Outside the ellipse |
|
33 } |
|
34 diffuse = color; |
|
35 #else // BORDER |
|
36 // For outlined ellipse |
|
37 // Calculate inner border threshold based on thickness |
|
38 // We need to determine the inner ellipse boundary |
|
39 float border_ratio = thickness / min(center.x, center.y); |
|
40 |
|
41 // The inner ellipse has a smaller radius but follows the same equation |
|
42 // We need to calculate what normalized value corresponds to the inner ellipse |
|
43 float inner_threshold = 1.0 - border_ratio; |
|
44 inner_threshold = inner_threshold * inner_threshold; // Square it because we're comparing to dist_squared |
|
45 |
|
46 if (dist_squared > 1.0) { |
|
47 discard; // Outside the ellipse |
|
48 } else if (dist_squared > inner_threshold) { |
|
49 // In the border region |
|
50 diffuse = border_color; |
|
51 } else { |
|
52 // Inside the outline |
|
53 #ifdef FILL |
|
54 diffuse = color; |
|
55 #else |
|
56 discard; |
|
57 #endif |
|
58 } |
|
59 #endif // BORDER |
|
60 } |