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 uniform sampler2D sTexture;
12 #ifdef IS_REQUIRED_YUV_TO_RGB
13 uniform sampler2D sTextureU;
14 uniform sampler2D sTextureV;
17 #ifdef IS_REQUIRED_ALPHA_MASKING
18 uniform sampler2D sMaskTexture;
19 INPUT mediump vec2 vMaskTexCoord;
22 #ifdef ATLAS_DEFAULT_WARP
23 uniform mediump vec4 uAtlasRect;
24 #elif defined(ATLAS_CUSTOM_WARP)
25 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
26 uniform lowp vec2 wrapMode;
29 uniform lowp vec4 uColor;
30 uniform lowp vec3 mixColor;
31 uniform lowp float preMultipliedAlpha;
32 #ifdef IS_REQUIRED_BORDERLINE
33 uniform mediump float borderlineWidth;
34 uniform mediump float borderlineOffset;
35 uniform lowp vec4 borderlineColor;
36 uniform lowp vec4 uActorColor;
39 #ifdef ATLAS_CUSTOM_WARP
40 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
43 if( wrap > 1.5 ) /* REFLECT */
44 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
45 else /* warp is 0 or 1 */
46 coord = mix(coordinate, fract(coordinate), wrap);
47 return clamp(mix(range.x, range.y, coord), range.x, range.y);
51 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
52 // Global values both rounded corner and borderline use
54 // radius of rounded corner on this quadrant
55 mediump float gRadius = 0.0;
57 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
58 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
59 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
60 mediump float gCenterPosition = 0.0;
61 // relative coordinate of gFragmentPosition from gCenterPosition.
62 mediump vec2 gDiff = vec2(0.0, 0.0);
63 // potential value what our algorithm use.
64 mediump float gPotential = 0.0;
66 // threshold of potential
67 mediump float gPotentialRange = 0.0;
68 mediump float gMaxOutlinePotential = 0.0;
69 mediump float gMinOutlinePotential = 0.0;
70 mediump float gMaxInlinePotential = 0.0;
71 mediump float gMinInlinePotential = 0.0;
73 void calculateCornerRadius()
75 #ifdef IS_REQUIRED_ROUNDED_CORNER
78 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
79 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
80 sign(vPosition.y) * 0.5 + 0.5
85 void calculatePosition()
87 gFragmentPosition = abs(vPosition) - vRectSize;
88 gCenterPosition = -gRadius;
89 #ifdef IS_REQUIRED_BORDERLINE
90 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
92 gDiff = gFragmentPosition - gCenterPosition;
95 void calculatePotential()
97 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
100 void setupMinMaxPotential()
102 // Set soft anti-alias range at most 2% of visual size
103 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.02);
105 gMaxOutlinePotential = gRadius + gPotentialRange;
106 gMinOutlinePotential = gRadius - gPotentialRange;
108 #ifdef IS_REQUIRED_BORDERLINE
109 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
110 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
112 gMaxInlinePotential = gMaxOutlinePotential;
113 gMinInlinePotential = gMinOutlinePotential;
116 // reduce defect near edge of rounded corner.
117 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
118 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
121 void PreprocessPotential()
123 calculateCornerRadius();
125 calculatePotential();
127 setupMinMaxPotential();
131 #ifdef IS_REQUIRED_BORDERLINE
132 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
134 mediump float potential = gPotential;
136 // default opacity of borderline is 0.0
137 mediump float borderlineOpacity = 0.0;
139 // calculate borderline opacity by potential
140 if(potential > gMinInlinePotential)
142 // potential is inside borderline range.
143 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
145 // Muliply borderlineWidth to resolve very thin borderline
146 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
149 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
150 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
151 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
153 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
154 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
155 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
156 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
158 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
159 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
160 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
161 if(potential > MaxTexturelinePotential)
163 // potential is out of texture range.
164 textureColor = vec4(0.0);
168 // potential is in texture range.
169 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
170 textureColor.a *= textureAlphaScale;
171 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
174 borderlineColorAlpha *= borderlineOpacity;
175 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
176 // We use pre-multiplied color to reduce operations.
177 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
179 // Manual blend operation with premultiplied colors.
180 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
181 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
182 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
183 // Else, return vec4((rgb*alpha) / alpha, alpha)
185 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
186 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
187 // TODO : Need to find some way without division
188 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
190 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
194 #ifdef IS_REQUIRED_ROUNDED_CORNER
195 mediump float calculateCornerOpacity()
197 mediump float potential = gPotential;
199 // default opacity is 1.0
200 mediump float opacity = 1.0;
202 // calculate borderline opacity by potential
203 if(potential > gMaxOutlinePotential)
205 // potential is out of borderline range. just discard here
208 else if(potential > gMinOutlinePotential)
210 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
216 #ifdef IS_REQUIRED_YUV_TO_RGB
217 lowp vec3 ConvertYuvToRgb(mediump vec2 texCoord)
219 lowp float y = texture(sTexture, texCoord).r;
220 lowp float u = texture(sTextureU, texCoord).r - 0.5;
221 lowp float v = texture(sTextureV, texCoord).r - 0.5;
223 rgb.r = y + (1.403 * v);
224 rgb.g = y - (0.344 * u) - (0.714 * v);
225 rgb.b = y + (1.770 * u);
232 #ifdef ATLAS_DEFAULT_WARP
233 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
234 #elif defined(ATLAS_CUSTOM_WARP)
235 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
236 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
238 mediump vec2 texCoord = vTexCoord;
241 #ifdef IS_REQUIRED_YUV_TO_RGB
242 lowp vec4 textureColor = vec4(ConvertYuvToRgb(texCoord), 1.0) * vec4( mixColor, 1.0 ) * uColor;
244 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
247 #ifdef IS_REQUIRED_ALPHA_MASKING
248 mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
249 textureColor.a *= maskAlpha;
250 textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
253 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
254 // skip most potential calculate for performance
255 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
257 OUT_COLOR = textureColor;
261 PreprocessPotential();
264 #ifdef IS_REQUIRED_BORDERLINE
265 textureColor = convertBorderlineColor(textureColor);
267 OUT_COLOR = textureColor;
269 #ifdef IS_REQUIRED_ROUNDED_CORNER
270 mediump float opacity = calculateCornerOpacity();
271 OUT_COLOR.a *= opacity;
272 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
275 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)