1 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
2 INPUT mediump vec2 vPosition;
3 INPUT mediump vec2 vRectSize;
4 INPUT mediump vec2 vOptRectSize;
5 #ifdef IS_REQUIRED_ROUNDED_CORNER
6 INPUT mediump vec4 vCornerRadius;
10 uniform lowp vec4 uColor;
11 uniform lowp vec3 mixColor;
12 #ifdef IS_REQUIRED_BLUR
13 uniform mediump float blurRadius;
14 #elif defined(IS_REQUIRED_BORDERLINE)
15 uniform mediump float borderlineWidth;
16 uniform mediump float borderlineOffset;
17 uniform lowp vec4 borderlineColor;
18 uniform lowp vec4 uActorColor;
22 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) || defined(IS_REQUIRED_BLUR)
23 // Global values both rounded corner and borderline use
25 // radius of rounded corner on this quadrant
26 mediump float gRadius = 0.0;
28 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
29 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
30 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
31 mediump float gCenterPosition = 0.0;
32 // relative coordinate of gFragmentPosition from gCenterPosition.
33 mediump vec2 gDiff = vec2(0.0, 0.0);
34 // potential value what our algorithm use.
35 mediump float gPotential = 0.0;
37 // threshold of potential
38 mediump float gPotentialRange = 0.0;
39 mediump float gMaxOutlinePotential = 0.0;
40 mediump float gMinOutlinePotential = 0.0;
41 mediump float gMaxInlinePotential = 0.0;
42 mediump float gMinInlinePotential = 0.0;
44 void calculateCornerRadius()
46 #ifdef IS_REQUIRED_ROUNDED_CORNER
49 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
50 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
51 sign(vPosition.y) * 0.5 + 0.5
56 void calculatePosition()
58 gFragmentPosition = abs(vPosition) - vRectSize;
59 gCenterPosition = -gRadius;
60 #ifdef IS_REQUIRED_BLUR
61 #elif defined(IS_REQUIRED_BORDERLINE)
62 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
64 gDiff = gFragmentPosition - gCenterPosition;
67 void calculatePotential()
69 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
72 void setupMinMaxPotential()
74 // Set soft anti-alias range at most 2% of visual size
75 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.02);
77 gMaxOutlinePotential = gRadius + gPotentialRange;
78 gMinOutlinePotential = gRadius - gPotentialRange;
80 #ifdef IS_REQUIRED_BLUR
81 gMaxInlinePotential = gMaxOutlinePotential;
82 gMinInlinePotential = gMinOutlinePotential;
83 #elif defined(IS_REQUIRED_BORDERLINE)
84 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
85 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
87 gMaxInlinePotential = gMaxOutlinePotential;
88 gMinInlinePotential = gMinOutlinePotential;
91 // reduce defect near edge of rounded corner.
92 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
93 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
96 void PreprocessPotential()
98 calculateCornerRadius();
100 calculatePotential();
102 setupMinMaxPotential();
106 #ifdef IS_REQUIRED_BLUR
107 #elif defined(IS_REQUIRED_BORDERLINE)
108 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
110 mediump float potential = gPotential;
112 // default opacity of borderline is 0.0
113 mediump float borderlineOpacity = 0.0;
115 // calculate borderline opacity by potential
116 if(potential > gMinInlinePotential)
118 // potential is inside borderline range.
119 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
121 // Muliply borderlineWidth to resolve very thin borderline
122 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
125 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
126 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
127 // NOTE : color-visual is always not preMultiplied.
129 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
130 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
131 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
132 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
134 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
135 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
136 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
137 if(potential > MaxTexturelinePotential)
139 // potential is out of texture range.
140 textureColor = vec4(0.0);
144 // potential is in texture range.
145 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
146 textureColor.a *= textureAlphaScale;
147 textureColor.rgb *= textureColor.a;
150 // NOTE : color-visual is always not preMultiplied.
151 borderlineColorAlpha *= borderlineOpacity;
152 borderlineColorRGB *= borderlineColorAlpha;
153 // We use pre-multiplied color to reduce operations.
154 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
156 // Manual blend operation with premultiplied colors.
157 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
158 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
159 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
160 // Else, return vec4((rgb*alpha) / alpha, alpha)
162 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
163 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
164 // TODO : Need to find some way without division
165 return vec4(finalMultipliedRGB / finalAlpha, finalAlpha);
167 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
171 #ifdef IS_REQUIRED_BLUR
172 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
173 mediump float calculateCornerOpacity()
175 mediump float potential = gPotential;
177 // default opacity is 1.0
178 mediump float opacity = 1.0;
180 // calculate borderline opacity by potential
181 if(potential > gMaxOutlinePotential)
183 // potential is out of borderline range. just discard here
186 else if(potential > gMinOutlinePotential)
188 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
194 #ifdef IS_REQUIRED_BLUR
195 mediump float calculateBlurOpacity()
197 // Don't use borderline!
198 mediump vec2 v = gDiff;
199 mediump float cy = gRadius + blurRadius;
200 mediump float cr = gRadius + blurRadius;
202 #ifdef IS_REQUIRED_ROUNDED_CORNER
203 // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
204 cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
206 v = vec2(min(v.x, v.y), max(v.x, v.y));
209 mediump float potential = 0.0;
210 mediump float alias = min(gRadius, 1.0);
211 mediump float potentialMin = cy + gRadius - blurRadius - alias;
212 mediump float potentialMax = cy + gRadius + blurRadius + alias;
214 // move center of circles for reduce defact
215 mediump float cyDiff = min(cy, 0.2 * blurRadius);
219 mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
221 if(diffFromBaseline > 0.0)
223 // out of calculation bound.
226 // for anti-alias when blurRaidus = 0.0
227 mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
228 mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
229 potentialMin += potentialDiff;
230 potentialMax -= potentialDiff;
234 // get some circle centered (x, x) and radius (r = cr / cy * x)
235 // s.t. point v is on that circle
236 // highest point of that circle is (x, x + r) and potential is x + r
238 // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
239 #ifdef IS_REQUIRED_ROUNDED_CORNER
240 // Note : lowspec HW cannot calculate here. need to reduce numeric error
241 highp float A = (cr * cr - 2.0 * cy * cy);
242 highp float B = cy * (v.x + v.y);
243 highp float V = dot(v,v);
244 highp float D = B * B + A * V;
245 potential = V * (cr + cy) / (sqrt(D) + B);
247 // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
248 // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
249 // = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
250 // = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
251 // ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^5)) (Taylor series)
252 // ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
253 // Note : This simplify need cause we should use it on lowspec HW.
254 mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
255 potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
259 return 1.0 - smoothstep(potentialMin, potentialMax, potential);
265 lowp vec4 targetColor = vec4(mixColor, 1.0) * uColor;
267 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
268 // skip most potential calculate for performance
269 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
271 OUT_COLOR = targetColor;
275 PreprocessPotential();
278 #ifdef IS_REQUIRED_BLUR
279 #elif defined(IS_REQUIRED_BORDERLINE)
280 targetColor = convertBorderlineColor(targetColor);
282 OUT_COLOR = targetColor;
284 #ifdef IS_REQUIRED_BLUR
285 mediump float opacity = calculateBlurOpacity();
286 OUT_COLOR.a *= opacity;
287 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
288 mediump float opacity = calculateCornerOpacity();
289 OUT_COLOR.a *= opacity;
292 #if defined(IS_REQUIRED_BLUR) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)