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 INPUT mediump float vAliasMargin;
7 #ifdef IS_REQUIRED_ROUNDED_CORNER
8 INPUT mediump vec4 vCornerRadius;
12 // scale factor to fit start and end position of gradient.
13 uniform mediump float uTextureCoordinateScaleFactor;
15 uniform sampler2D sTexture; // sampler1D?
16 uniform lowp vec4 uColor;
17 uniform lowp vec3 mixColor;
18 #ifdef IS_REQUIRED_BORDERLINE
19 uniform mediump float borderlineWidth;
20 uniform mediump float borderlineOffset;
21 uniform lowp vec4 borderlineColor;
22 uniform lowp vec4 uActorColor;
25 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
26 // Global values both rounded corner and borderline use
28 // radius of rounded corner on this quadrant
29 mediump float gRadius = 0.0;
31 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
32 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
33 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
34 mediump float gCenterPosition = 0.0;
35 // relative coordinate of gFragmentPosition from gCenterPosition.
36 mediump vec2 gDiff = vec2(0.0, 0.0);
37 // potential value what our algorithm use.
38 mediump float gPotential = 0.0;
40 // threshold of potential
41 mediump float gPotentialRange = 0.0;
42 mediump float gMaxOutlinePotential = 0.0;
43 mediump float gMinOutlinePotential = 0.0;
44 mediump float gMaxInlinePotential = 0.0;
45 mediump float gMinInlinePotential = 0.0;
47 void calculateCornerRadius()
49 #ifdef IS_REQUIRED_ROUNDED_CORNER
52 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
53 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
54 sign(vPosition.y) * 0.5 + 0.5
59 void calculatePosition()
61 gFragmentPosition = abs(vPosition) - vRectSize;
62 gCenterPosition = -gRadius;
63 #ifdef IS_REQUIRED_BORDERLINE
64 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
66 gDiff = gFragmentPosition - gCenterPosition;
69 void calculatePotential()
71 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
74 void setupMinMaxPotential()
76 gPotentialRange = vAliasMargin;
78 gMaxOutlinePotential = gRadius + gPotentialRange;
79 gMinOutlinePotential = gRadius - gPotentialRange;
81 #ifdef IS_REQUIRED_BORDERLINE
82 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
83 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
85 gMaxInlinePotential = gMaxOutlinePotential;
86 gMinInlinePotential = gMinOutlinePotential;
89 // reduce defect near edge of rounded corner.
90 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
91 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
94 void PreprocessPotential()
96 calculateCornerRadius();
100 setupMinMaxPotential();
105 #ifdef IS_REQUIRED_BORDERLINE
106 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
108 mediump float potential = gPotential;
110 // default opacity of borderline is 0.0
111 mediump float borderlineOpacity = 0.0;
113 // calculate borderline opacity by potential
114 if(potential > gMinInlinePotential)
116 // potential is inside borderline range.
117 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
119 // Muliply borderlineWidth to resolve very thin borderline
120 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
123 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
124 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
125 // NOTE : gradient-visual is always preMultiplied.
126 borderlineColorRGB *= borderlineColorAlpha;
128 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
129 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
130 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
131 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
133 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
134 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
135 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
136 if(potential > MaxTexturelinePotential)
138 // potential is out of texture range.
139 textureColor = vec4(0.0);
143 // potential is in texture range.
144 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
145 textureColor.a *= textureAlphaScale;
146 textureColor.rgb *= textureAlphaScale;
149 // NOTE : gradient-visual is always preMultiplied.
150 borderlineColorAlpha *= borderlineOpacity;
151 borderlineColorRGB *= borderlineOpacity;
152 // We use pre-multiplied color to reduce operations.
153 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
155 // Manual blend operation with premultiplied colors.
156 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
157 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
158 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
159 // Else, return vec4((rgb*alpha) / alpha, alpha)
161 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
162 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
163 return vec4(finalMultipliedRGB, finalAlpha);
165 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
169 #ifdef IS_REQUIRED_ROUNDED_CORNER
170 mediump float calculateCornerOpacity()
172 mediump float potential = gPotential;
174 // default opacity is 1.0
175 mediump float opacity = 1.0;
177 // calculate borderline opacity by potential
178 if(potential > gMaxOutlinePotential)
180 // potential is out of borderline range. just discard here
183 else if(potential > gMinOutlinePotential)
185 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
194 mediump float radialTexCoord = ((length(vTexCoord) - 0.5) * uTextureCoordinateScaleFactor) + 0.5;
195 lowp vec4 textureColor = TEXTURE(sTexture, vec2(radialTexCoord, 0.5)) * vec4(mixColor, 1.0) * uColor;
197 lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
200 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
201 // skip most potential calculate for performance
202 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
204 OUT_COLOR = textureColor;
208 PreprocessPotential();
211 #ifdef IS_REQUIRED_BORDERLINE
212 textureColor = convertBorderlineColor(textureColor);
214 OUT_COLOR = textureColor;
216 #ifdef IS_REQUIRED_ROUNDED_CORNER
217 mediump float opacity = calculateCornerOpacity();
218 OUT_COLOR *= opacity;
221 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)