1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
7 #ifndef IS_REQUIRED_BLUR
8 #define IS_REQUIRED_BLUR 0
11 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
12 INPUT mediump vec2 vPosition;
13 INPUT mediump vec2 vRectSize;
14 INPUT mediump vec2 vOptRectSize;
15 #if IS_REQUIRED_ROUNDED_CORNER
16 INPUT mediump vec4 vCornerRadius;
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
28 uniform mediump float blurRadius;
32 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
33 // Global values both rounded corner and borderline use
35 // radius of rounded corner on this quadrant
36 mediump float gRadius = 0.0;
38 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
39 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
40 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
41 mediump float gCenterPosition = 0.0;
42 // relative coordinate of gFragmentPosition from gCenterPosition.
43 mediump vec2 gDiff = vec2(0.0, 0.0);
44 // potential value what our algorithm use.
45 mediump float gPotential = 0.0;
47 // threshold of potential
48 mediump float gPotentialRange = 0.0;
49 mediump float gMaxOutlinePotential = 0.0;
50 mediump float gMinOutlinePotential = 0.0;
51 mediump float gMaxInlinePotential = 0.0;
52 mediump float gMinInlinePotential = 0.0;
54 void calculateCornerRadius()
56 #if IS_REQUIRED_ROUNDED_CORNER
59 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
60 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
61 sign(vPosition.y) * 0.5 + 0.5
66 void calculatePosition()
68 gFragmentPosition = abs(vPosition) - vRectSize;
69 gCenterPosition = -gRadius;
70 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
71 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
73 gDiff = gFragmentPosition - gCenterPosition;
76 void calculatePotential()
78 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
81 void setupMinMaxPotential()
83 gPotentialRange = 1.0;
85 gMaxOutlinePotential = gRadius + gPotentialRange;
86 gMinOutlinePotential = gRadius - gPotentialRange;
88 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
89 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
90 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
92 gMaxInlinePotential = gMaxOutlinePotential;
93 gMinInlinePotential = gMinOutlinePotential;
96 // reduce defect near edge of rounded corner.
97 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
98 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
101 void PreprocessPotential()
103 calculateCornerRadius();
105 calculatePotential();
107 setupMinMaxPotential();
111 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
112 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
114 mediump float potential = gPotential;
116 // default opacity of borderline is 0.0
117 mediump float borderlineOpacity = 0.0;
119 // calculate borderline opacity by potential
120 if(potential > gMinInlinePotential)
122 // potential is inside borderline range.
123 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
126 //calculate inside of borderline when outilneColor.a < 1.0
127 if(borderlineColor.a < 1.0)
129 mediump float tCornerRadius = -gCenterPosition;
130 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
131 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
132 if(potential > MaxTexturelinePotential)
134 // potential is out of texture range. use borderline color instead of texture
135 textureColor = vec4(borderlineColor.xyz, 0.0);
137 else if(potential > MinTexturelinePotential)
139 // potential is in texture range
140 textureColor = mix(textureColor, vec4(borderlineColor.xyz, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
142 borderlineOpacity *= borderlineColor.a;
143 return mix(textureColor, vec4(borderlineColor.xyz, 1.0), borderlineOpacity);
145 return mix(textureColor, borderlineColor, borderlineOpacity);
149 #if !IS_REQUIRED_BLUR && IS_REQUIRED_ROUNDED_CORNER
150 mediump float calculateCornerOpacity()
152 mediump float potential = gPotential;
154 // default opacity is 1.0
155 mediump float opacity = 1.0;
157 // calculate borderline opacity by potential
158 if(potential > gMaxOutlinePotential)
160 // potential is out of borderline range. just discard here
163 else if(potential > gMinOutlinePotential)
165 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
172 mediump float calculateBlurOpacity()
174 // Don't use borderline!
175 mediump vec2 v = gDiff;
176 mediump float cy = gRadius + blurRadius;
177 mediump float cr = gRadius + blurRadius;
179 #if IS_REQUIRED_ROUNDED_CORNER
180 // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
181 cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
183 v = vec2(min(v.x, v.y), max(v.x, v.y));
186 mediump float potential = 0.0;
187 mediump float alias = min(gRadius, 1.0);
188 mediump float potentialMin = cy + gRadius - blurRadius - alias;
189 mediump float potentialMax = cy + gRadius + blurRadius + alias;
191 // move center of circles for reduce defact
192 mediump float cyDiff = min(cy, 0.2 * blurRadius);
196 mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
198 if(diffFromBaseline > 0.0)
200 // out of calculation bound.
203 // for anti-alias when blurRaidus = 0.0
204 mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
205 mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
206 potentialMin += potentialDiff;
207 potentialMax -= potentialDiff;
211 // get some circle centered (x, x) and radius (r = cr / cy * x)
212 // s.t. point v is on that circle
213 // highest point of that circle is (x, x + r) and potential is x + r
215 // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
216 #if IS_REQUIRED_ROUNDED_CORNER
217 // NOTE : lowspec HW cannot calculate here. need to reduce numeric error
218 mediump float A = (cr * cr - 2.0 * cy * cy);
219 mediump float B = cy * (v.x + v.y);
220 mediump float V = dot(v,v);
221 mediump float D = B * B + A * V;
222 potential = V * (cr + cy) / (sqrt(D) + B);
224 // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
225 // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
226 // = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
227 // = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
228 // ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^4)) (Taylor series)
229 // ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
230 // Note : This simplify need cause we should use it on lowspec HW.
231 mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
232 potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
236 return 1.0 - smoothstep(potentialMin, potentialMax, potential);
242 lowp vec4 targetColor = vec4(mixColor, 1.0);
244 #if IS_REQUIRED_BLUR || IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
245 // skip most potential calculate for performance
246 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
248 OUT_COLOR = targetColor * uColor;
251 PreprocessPotential();
254 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
255 targetColor = convertBorderlineColor(targetColor);
257 OUT_COLOR = targetColor * uColor;
260 mediump float opacity = calculateBlurOpacity();
261 OUT_COLOR.a *= opacity;
262 #elif IS_REQUIRED_ROUNDED_CORNER
263 mediump float opacity = calculateCornerOpacity();
264 OUT_COLOR.a *= opacity;