forked from exeldro/obs-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cut_rect_per_corner.shader
127 lines (125 loc) · 4.78 KB
/
cut_rect_per_corner.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
uniform int corner_tl;
uniform int corner_tr;
uniform int corner_br;
uniform int corner_bl;
uniform int border_thickness;
uniform float4 border_color;
uniform float border_alpha_start = 1.0;
uniform float border_alpha_end = 0.0;
uniform float alpha_cut_off = 0.5;
float4 mainImage(VertData v_in) : TARGET
{
float4 pixel = image.Sample(textureSampler, v_in.uv);
int closedEdgeX = 0;
int closedEdgeY = 0;
if(pixel.a < alpha_cut_off){
return float4(1.0,0.0,0.0,0.0);
}
int corner_top = corner_tl>corner_tr?corner_tl:corner_tr;
int corner_right = corner_tr>corner_br?corner_tr:corner_br;
int corner_bottom = corner_bl>corner_br?corner_bl:corner_br;
int corner_left = corner_tl>corner_bl?corner_tl:corner_bl;
if(image.Sample(textureSampler, v_in.uv + float2(corner_right*uv_pixel_interval.x,0)).a < alpha_cut_off){
closedEdgeX = corner_right;
}else if(image.Sample(textureSampler, v_in.uv + float2(-corner_left*uv_pixel_interval.x,0)).a < alpha_cut_off){
closedEdgeX = -corner_left;
}
if(image.Sample(textureSampler, v_in.uv + float2(0,corner_bottom*uv_pixel_interval.y)).a < alpha_cut_off){
closedEdgeY = corner_bottom;
}else if(image.Sample(textureSampler, v_in.uv + float2(0,-corner_top*uv_pixel_interval.y)).a < alpha_cut_off){
closedEdgeY = -corner_top;
}
if(closedEdgeX == 0 && closedEdgeY == 0){
return pixel;
}
if(closedEdgeX != 0){
[loop] for(int x = 1;x<corner_right;x++){
if(image.Sample(textureSampler, v_in.uv + float2(x*uv_pixel_interval.x, 0)).a < alpha_cut_off){
closedEdgeX = x;
break;
}
}
[loop] for(int x = 1;x<corner_left;x++){
if(image.Sample(textureSampler, v_in.uv + float2(-x*uv_pixel_interval.x, 0)).a < alpha_cut_off){
closedEdgeX = -x;
break;
}
}
}
if(closedEdgeY != 0){
[loop] for(int y = 1;y<corner_bottom;y++){
if(image.Sample(textureSampler, v_in.uv + float2(0, y*uv_pixel_interval.y)).a < alpha_cut_off){
closedEdgeY = y;
break;
}
}
[loop] for(int y = 1;y<corner_top;y++){
if(image.Sample(textureSampler, v_in.uv + float2(0, -y*uv_pixel_interval.y)).a < alpha_cut_off){
closedEdgeY = -y;
break;
}
}
}
int closedEdgeXabs = closedEdgeX < 0 ? -closedEdgeX : closedEdgeX;
int closedEdgeYabs = closedEdgeY < 0 ? -closedEdgeY : closedEdgeY;
int corner_radius = 0;
if(closedEdgeX < 0 && closedEdgeY < 0){
corner_radius = corner_tl;
}else if(closedEdgeX > 0 && closedEdgeY < 0){
corner_radius = corner_tr;
}else if(closedEdgeX > 0 && closedEdgeY > 0){
corner_radius = corner_br;
}else if(closedEdgeX < 0 && closedEdgeY > 0){
corner_radius = corner_bl;
}
if(closedEdgeXabs > corner_radius && closedEdgeYabs > corner_radius){
return pixel;
}
if(closedEdgeXabs == 0){
if(closedEdgeYabs <= border_thickness){
float4 fade_color = border_color;
fade_color.a = border_alpha_end + ((float)closedEdgeYabs / (float)border_thickness)*(border_alpha_start-border_alpha_end);
return fade_color;
}else{
return pixel;
}
}
if(closedEdgeYabs == 0){
if(closedEdgeXabs <= border_thickness){
float4 fade_color = border_color;
fade_color.a = border_alpha_end + ((float)closedEdgeXabs / (float)border_thickness)*(border_alpha_start-border_alpha_end);
return fade_color;
}else{
return pixel;
}
}
if(closedEdgeXabs > corner_radius){
if(closedEdgeYabs <= border_thickness){
float4 fade_color = border_color;
fade_color.a = border_alpha_end + ((float)closedEdgeYabs / (float)border_thickness)*(border_alpha_start-border_alpha_end);
return fade_color;
}else{
return pixel;
}
}
if(closedEdgeYabs > corner_radius){
if(closedEdgeXabs <= border_thickness){
float4 fade_color = border_color;
fade_color.a = border_alpha_end + ((float)closedEdgeXabs / (float)border_thickness)*(border_alpha_start-border_alpha_end);
return fade_color;
}else{
return pixel;
}
}
float d = closedEdgeXabs+closedEdgeYabs;
if(d>corner_radius){
if(d-corner_radius <= border_thickness){
float4 fade_color = border_color;
fade_color.a = border_alpha_end + ((d-corner_radius)/ (float)border_thickness)*(border_alpha_start-border_alpha_end);
return fade_color;
}else{
return pixel;
}
}
return float4(0.0,0.0,0.0,0.0);
}