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 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_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 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
30 // Be used when we calculate anti-alias range near 1 pixel.
31 uniform highp vec3 uScale;
34 uniform lowp vec4 uColor;
35 uniform lowp vec3 mixColor;
36 uniform lowp float preMultipliedAlpha;
37 #ifdef IS_REQUIRED_BORDERLINE
38 uniform mediump float borderlineWidth;
39 uniform mediump float borderlineOffset;
40 uniform lowp vec4 borderlineColor;
41 uniform lowp vec4 uActorColor;
44 #ifdef ATLAS_CUSTOM_WARP
45 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
48 if( wrap > 1.5 ) /* REFLECT */
49 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
50 else /* warp is 0 or 1 */
51 coord = mix(coordinate, fract(coordinate), wrap);
52 return clamp(mix(range.x, range.y, coord), range.x, range.y);
56 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
57 // Global values both rounded corner and borderline use
59 // radius of rounded corner on this quadrant
60 mediump float gRadius = 0.0;
62 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
63 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
64 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
65 mediump float gCenterPosition = 0.0;
66 // relative coordinate of gFragmentPosition from gCenterPosition.
67 mediump vec2 gDiff = vec2(0.0, 0.0);
68 // potential value what our algorithm use.
69 mediump float gPotential = 0.0;
71 // threshold of potential
72 mediump float gPotentialRange = 0.0;
73 mediump float gMaxOutlinePotential = 0.0;
74 mediump float gMinOutlinePotential = 0.0;
75 mediump float gMaxInlinePotential = 0.0;
76 mediump float gMinInlinePotential = 0.0;
78 void calculateCornerRadius()
80 #ifdef IS_REQUIRED_ROUNDED_CORNER
83 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
84 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
85 sign(vPosition.y) * 0.5 + 0.5
90 void calculatePosition()
92 gFragmentPosition = abs(vPosition) - vRectSize;
93 gCenterPosition = -gRadius;
94 #ifdef IS_REQUIRED_BORDERLINE
95 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
97 gDiff = gFragmentPosition - gCenterPosition;
100 void calculatePotential()
102 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
105 void setupMinMaxPotential()
107 // Set soft anti-alias range at most 10% of visual size.
108 // The range should be inverse proportion with scale of view.
109 // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
110 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.2) / max(0.001, max(uScale.x, uScale.y));
112 gMaxOutlinePotential = gRadius + gPotentialRange;
113 gMinOutlinePotential = gRadius - gPotentialRange;
115 #ifdef IS_REQUIRED_BORDERLINE
116 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
117 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
119 gMaxInlinePotential = gMaxOutlinePotential;
120 gMinInlinePotential = gMinOutlinePotential;
123 // reduce defect near edge of rounded corner.
124 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
125 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
128 void PreprocessPotential()
130 calculateCornerRadius();
132 calculatePotential();
134 setupMinMaxPotential();
138 #ifdef IS_REQUIRED_BORDERLINE
139 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
141 mediump float potential = gPotential;
143 // default opacity of borderline is 0.0
144 mediump float borderlineOpacity = 0.0;
146 // calculate borderline opacity by potential
147 if(potential > gMinInlinePotential)
149 // potential is inside borderline range.
150 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
152 // Muliply borderlineWidth to resolve very thin borderline
153 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
156 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
157 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
158 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
160 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
161 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
162 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
163 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
165 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
166 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
167 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
168 if(potential > MaxTexturelinePotential)
170 // potential is out of texture range.
171 textureColor = vec4(0.0);
175 // potential is in texture range.
176 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
177 textureColor.a *= textureAlphaScale;
178 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
181 borderlineColorAlpha *= borderlineOpacity;
182 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
183 // We use pre-multiplied color to reduce operations.
184 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
186 // Manual blend operation with premultiplied colors.
187 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
188 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
189 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
190 // Else, return vec4((rgb*alpha) / alpha, alpha)
192 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
193 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
194 // TODO : Need to find some way without division
195 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
197 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
201 #ifdef IS_REQUIRED_ROUNDED_CORNER
202 mediump float calculateCornerOpacity()
204 mediump float potential = gPotential;
206 // default opacity is 1.0
207 mediump float opacity = 1.0;
209 // calculate borderline opacity by potential
210 if(potential > gMaxOutlinePotential)
212 // potential is out of borderline range. just discard here
215 else if(potential > gMinOutlinePotential)
217 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
223 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
224 lowp vec4 ConvertYuvToRgba(mediump vec2 texCoord)
226 #ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
227 // Special case when shader use YUV but actual textures are not YUV format.
228 // In this case, just resturn sTexture.
229 if(textureSize(sTextureU, 0) != textureSize(sTextureV, 0))
231 return texture(sTexture, texCoord);
235 lowp float y = texture(sTexture, texCoord).r;
236 lowp float u = texture(sTextureU, texCoord).r - 0.5;
237 lowp float v = texture(sTextureV, texCoord).r - 0.5;
239 rgba.r = y + (1.403 * v);
240 rgba.g = y - (0.344 * u) - (0.714 * v);
241 rgba.b = y + (1.770 * u);
249 #ifdef ATLAS_DEFAULT_WARP
250 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
251 #elif defined(ATLAS_CUSTOM_WARP)
252 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
253 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
255 mediump vec2 texCoord = vTexCoord;
258 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
259 lowp vec4 textureColor = ConvertYuvToRgba(texCoord) * vec4( mixColor, 1.0 ) * uColor;
261 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
264 #ifdef IS_REQUIRED_ALPHA_MASKING
265 mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
266 textureColor.a *= maskAlpha;
267 textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
270 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
271 // skip most potential calculate for performance
272 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
274 OUT_COLOR = textureColor;
278 PreprocessPotential();
281 #ifdef IS_REQUIRED_BORDERLINE
282 textureColor = convertBorderlineColor(textureColor);
284 OUT_COLOR = textureColor;
286 #ifdef IS_REQUIRED_ROUNDED_CORNER
287 mediump float opacity = calculateCornerOpacity();
288 OUT_COLOR.a *= opacity;
289 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
292 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)