shader/rectangle_frag.glsl

changeset 158
f650994ec543
parent 157
d6c2f028d8c9
equal deleted inserted replaced
157:d6c2f028d8c9 158:f650994ec543
4 uniform vec4 color; 4 uniform vec4 color;
5 uniform vec2 size; 5 uniform vec2 size;
6 #ifndef FILL 6 #ifndef FILL
7 uniform float thickness; 7 uniform float thickness;
8 #endif 8 #endif
9 #ifdef ROUNDED_CORNERS
10 uniform float radius;
11 #endif
9 12
10 void main(void) { 13 void main(void) {
11 #ifdef FILL 14 #ifdef ROUNDED_CORNERS
15 // Calculate distances from each corner
16 vec2 corner_distances[4];
17 corner_distances[0] = uvcoord - vec2(radius, radius); // top-left
18 corner_distances[1] = uvcoord - vec2(size.x - radius, radius); // top-right
19 corner_distances[2] = uvcoord - vec2(radius, size.y - radius); // bottom-left
20 corner_distances[3] = uvcoord - vec2(size.x - radius, size.y - radius); // bottom-right
21
22 // Check if we're in a corner region
23 bool in_corner_region = false;
24 int corner_idx = -1;
25
26 for (int i = 0; i < 4; i++) {
27 vec2 test_point = corner_distances[i];
28 vec2 corner_test = vec2(0.0);
29
30 if (i == 0 && test_point.x < 0.0 && test_point.y < 0.0) { // top-left
31 corner_test = test_point;
32 corner_idx = i;
33 in_corner_region = true;
34 break;
35 } else if (i == 1 && test_point.x > 0.0 && test_point.y < 0.0) { // top-right
36 corner_test = test_point;
37 corner_idx = i;
38 in_corner_region = true;
39 break;
40 } else if (i == 2 && test_point.x < 0.0 && test_point.y > 0.0) { // bottom-left
41 corner_test = test_point;
42 corner_idx = i;
43 in_corner_region = true;
44 break;
45 } else if (i == 3 && test_point.x > 0.0 && test_point.y > 0.0) { // bottom-right
46 corner_test = test_point;
47 corner_idx = i;
48 in_corner_region = true;
49 break;
50 }
51 }
52
53 #ifdef FILL
54 // For filled rectangle with rounded corners
55 if (in_corner_region) {
56 float dist = length(corner_distances[corner_idx]);
57 if (dist > radius) {
58 discard; // Outside the rounded corner
59 }
60 } else if (uvcoord.x < 0.0 || uvcoord.y < 0.0 || uvcoord.x > size.x || uvcoord.y > size.y) {
61 discard; // Outside the rectangle
62 }
12 diffuse = color; 63 diffuse = color;
13 #else 64 #else // no FILL
65 // For outlined rectangle with rounded corners
66 if (in_corner_region) {
67 float dist = length(corner_distances[corner_idx]);
68 if (dist > radius) {
69 discard; // Outside the rounded corner
70 } else if (dist < radius - thickness) {
71 discard; // Inside the outline
72 }
73 diffuse = color;
74 } else if (any(lessThan(uvcoord, vec2(thickness))) || any(greaterThan(uvcoord, size - thickness))) {
75 // On a straight edge
76 if (uvcoord.x >= 0.0 && uvcoord.y >= 0.0 && uvcoord.x <= size.x && uvcoord.y <= size.y) {
77 diffuse = color;
78 } else {
79 discard;
80 }
81 } else {
82 discard; // Inside the outline
83 }
84 #endif // FILL
85 #else // no ROUNDED_CORNERS
86 #ifdef FILL
87 diffuse = color;
88 #else // no FILL
14 if (any(notEqual(1.0-step(thickness, uvcoord)+step(size-thickness, uvcoord), vec2(0.0)))) { 89 if (any(notEqual(1.0-step(thickness, uvcoord)+step(size-thickness, uvcoord), vec2(0.0)))) {
15 diffuse = color; 90 diffuse = color;
16 } else { 91 } else {
17 discard; 92 discard;
18 } 93 }
19 #endif 94 #endif // FILL
95 #endif // ROUNDED_CORNERS
20 } 96 }

mercurial