1 INPUT mediump vec2 vTexCoord;
2 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
3 INPUT mediump vec2 vPosition;
4 INPUT mediump vec2 vRectSize;
5 INPUT mediump vec2 vOptRectSize;
6 #ifdef IS_REQUIRED_ROUNDED_CORNER
7 INPUT mediump vec4 vCornerRadius;
11 // scale factor to fit start and end position of gradient.
12 uniform mediump float uTextureCoordinateScaleFactor;
14 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
15 // Be used when we calculate anti-alias range near 1 pixel.
16 uniform highp vec3 uScale;
19 uniform sampler2D sTexture; // sampler1D?
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #ifdef IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
26 uniform lowp vec4 uActorColor;
29 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
30 // Global values both rounded corner and borderline use
32 // radius of rounded corner on this quadrant
33 mediump float gRadius = 0.0;
35 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
36 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
37 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
38 mediump float gCenterPosition = 0.0;
39 // relative coordinate of gFragmentPosition from gCenterPosition.
40 mediump vec2 gDiff = vec2(0.0, 0.0);
41 // potential value what our algorithm use.
42 mediump float gPotential = 0.0;
44 // threshold of potential
45 mediump float gPotentialRange = 0.0;
46 mediump float gMaxOutlinePotential = 0.0;
47 mediump float gMinOutlinePotential = 0.0;
48 mediump float gMaxInlinePotential = 0.0;
49 mediump float gMinInlinePotential = 0.0;
51 void calculateCornerRadius()
53 #ifdef IS_REQUIRED_ROUNDED_CORNER
56 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
57 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
58 sign(vPosition.y) * 0.5 + 0.5
63 void calculatePosition()
65 gFragmentPosition = abs(vPosition) - vRectSize;
66 gCenterPosition = -gRadius;
67 #ifdef IS_REQUIRED_BORDERLINE
68 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
70 gDiff = gFragmentPosition - gCenterPosition;
73 void calculatePotential()
75 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
78 void setupMinMaxPotential()
80 // Set soft anti-alias range at most 10% of visual size.
81 // The range should be inverse proportion with scale of view.
82 // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
83 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.2) / max(0.001, max(uScale.x, uScale.y));
85 gMaxOutlinePotential = gRadius + gPotentialRange;
86 gMinOutlinePotential = gRadius - gPotentialRange;
88 #ifdef 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();
112 #ifdef IS_REQUIRED_BORDERLINE
113 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
115 mediump float potential = gPotential;
117 // default opacity of borderline is 0.0
118 mediump float borderlineOpacity = 0.0;
120 // calculate borderline opacity by potential
121 if(potential > gMinInlinePotential)
123 // potential is inside borderline range.
124 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
126 // Muliply borderlineWidth to resolve very thin borderline
127 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
130 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
131 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
132 // NOTE : gradient-visual is always preMultiplied.
133 borderlineColorRGB *= borderlineColorAlpha;
135 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
136 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
137 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
138 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
140 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
141 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
142 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
143 if(potential > MaxTexturelinePotential)
145 // potential is out of texture range.
146 textureColor = vec4(0.0);
150 // potential is in texture range.
151 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
152 textureColor.a *= textureAlphaScale;
153 textureColor.rgb *= textureAlphaScale;
156 // NOTE : gradient-visual is always preMultiplied.
157 borderlineColorAlpha *= borderlineOpacity;
158 borderlineColorRGB *= borderlineOpacity;
159 // We use pre-multiplied color to reduce operations.
160 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
162 // Manual blend operation with premultiplied colors.
163 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
164 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
165 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
166 // Else, return vec4((rgb*alpha) / alpha, alpha)
168 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
169 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
170 return vec4(finalMultipliedRGB, finalAlpha);
172 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
176 #ifdef IS_REQUIRED_ROUNDED_CORNER
177 mediump float calculateCornerOpacity()
179 mediump float potential = gPotential;
181 // default opacity is 1.0
182 mediump float opacity = 1.0;
184 // calculate borderline opacity by potential
185 if(potential > gMaxOutlinePotential)
187 // potential is out of borderline range. just discard here
190 else if(potential > gMinOutlinePotential)
192 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
201 mediump float radialTexCoord = ((length(vTexCoord) - 0.5) * uTextureCoordinateScaleFactor) + 0.5;
202 lowp vec4 textureColor = TEXTURE(sTexture, vec2(radialTexCoord, 0.5)) * vec4(mixColor, 1.0) * uColor;
204 lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
207 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
208 // skip most potential calculate for performance
209 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
211 OUT_COLOR = textureColor;
215 PreprocessPotential();
218 #ifdef IS_REQUIRED_BORDERLINE
219 textureColor = convertBorderlineColor(textureColor);
221 OUT_COLOR = textureColor;
223 #ifdef IS_REQUIRED_ROUNDED_CORNER
224 mediump float opacity = calculateCornerOpacity();
225 OUT_COLOR *= opacity;
228 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)