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 uniform sampler2D sTexture; // sampler1D?
15 uniform lowp vec4 uColor;
16 uniform lowp vec3 mixColor;
17 #ifdef IS_REQUIRED_BORDERLINE
18 uniform mediump float borderlineWidth;
19 uniform mediump float borderlineOffset;
20 uniform lowp vec4 borderlineColor;
21 uniform lowp vec4 uActorColor;
24 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
25 // Global values both rounded corner and borderline use
27 // radius of rounded corner on this quadrant
28 mediump float gRadius = 0.0;
30 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
31 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
32 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
33 mediump float gCenterPosition = 0.0;
34 // relative coordinate of gFragmentPosition from gCenterPosition.
35 mediump vec2 gDiff = vec2(0.0, 0.0);
36 // potential value what our algorithm use.
37 mediump float gPotential = 0.0;
39 // threshold of potential
40 mediump float gPotentialRange = 0.0;
41 mediump float gMaxOutlinePotential = 0.0;
42 mediump float gMinOutlinePotential = 0.0;
43 mediump float gMaxInlinePotential = 0.0;
44 mediump float gMinInlinePotential = 0.0;
46 void calculateCornerRadius()
48 #ifdef IS_REQUIRED_ROUNDED_CORNER
51 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
52 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
53 sign(vPosition.y) * 0.5 + 0.5
58 void calculatePosition()
60 gFragmentPosition = abs(vPosition) - vRectSize;
61 gCenterPosition = -gRadius;
62 #ifdef IS_REQUIRED_BORDERLINE
63 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
65 gDiff = gFragmentPosition - gCenterPosition;
68 void calculatePotential()
70 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
73 void setupMinMaxPotential()
75 // Set soft anti-alias range at most 2% of visual size
76 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.02);
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)