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;
13 #ifdef IS_REQUIRED_ALPHA_MASKING
14 uniform sampler2D sMaskTexture;
15 INPUT mediump vec2 vMaskTexCoord;
18 #ifdef ATLAS_DEFAULT_WARP
19 uniform mediump vec4 uAtlasRect;
20 #elif defined(ATLAS_CUSTOM_WARP)
21 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
22 uniform lowp vec2 wrapMode;
25 uniform lowp vec4 uColor;
26 uniform lowp vec3 mixColor;
27 uniform lowp float preMultipliedAlpha;
28 #ifdef IS_REQUIRED_BORDERLINE
29 uniform mediump float borderlineWidth;
30 uniform mediump float borderlineOffset;
31 uniform lowp vec4 borderlineColor;
32 uniform lowp vec4 uActorColor;
35 #ifdef ATLAS_CUSTOM_WARP
36 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
39 if( wrap > 1.5 ) /* REFLECT */
40 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
41 else /* warp is 0 or 1 */
42 coord = mix(coordinate, fract(coordinate), wrap);
43 return clamp(mix(range.x, range.y, coord), range.x, range.y);
47 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
48 // Global values both rounded corner and borderline use
50 // radius of rounded corner on this quadrant
51 mediump float gRadius = 0.0;
53 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
54 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
55 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
56 mediump float gCenterPosition = 0.0;
57 // relative coordinate of gFragmentPosition from gCenterPosition.
58 mediump vec2 gDiff = vec2(0.0, 0.0);
59 // potential value what our algorithm use.
60 mediump float gPotential = 0.0;
62 // threshold of potential
63 mediump float gPotentialRange = 0.0;
64 mediump float gMaxOutlinePotential = 0.0;
65 mediump float gMinOutlinePotential = 0.0;
66 mediump float gMaxInlinePotential = 0.0;
67 mediump float gMinInlinePotential = 0.0;
69 void calculateCornerRadius()
71 #ifdef IS_REQUIRED_ROUNDED_CORNER
74 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
75 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
76 sign(vPosition.y) * 0.5 + 0.5
81 void calculatePosition()
83 gFragmentPosition = abs(vPosition) - vRectSize;
84 gCenterPosition = -gRadius;
85 #ifdef IS_REQUIRED_BORDERLINE
86 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
88 gDiff = gFragmentPosition - gCenterPosition;
91 void calculatePotential()
93 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
96 void setupMinMaxPotential()
98 gPotentialRange = 1.0;
100 gMaxOutlinePotential = gRadius + gPotentialRange;
101 gMinOutlinePotential = gRadius - gPotentialRange;
103 #ifdef IS_REQUIRED_BORDERLINE
104 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
105 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
107 gMaxInlinePotential = gMaxOutlinePotential;
108 gMinInlinePotential = gMinOutlinePotential;
111 // reduce defect near edge of rounded corner.
112 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
113 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
116 void PreprocessPotential()
118 calculateCornerRadius();
120 calculatePotential();
122 setupMinMaxPotential();
126 #ifdef IS_REQUIRED_BORDERLINE
127 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
129 mediump float potential = gPotential;
131 // default opacity of borderline is 0.0
132 mediump float borderlineOpacity = 0.0;
134 // calculate borderline opacity by potential
135 if(potential > gMinInlinePotential)
137 // potential is inside borderline range.
138 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
140 // Muliply borderlineWidth to resolve very thin borderline
141 borderlineOpacity *= min(1.0, borderlineWidth);
144 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
145 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
146 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
148 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
149 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
150 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
151 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
153 mediump float tCornerRadius = -gCenterPosition;
154 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
155 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
156 if(potential > MaxTexturelinePotential)
158 // potential is out of texture range.
159 textureColor = vec4(0.0);
163 // potential is in texture range.
164 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
165 textureColor.a *= textureAlphaScale;
166 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
169 borderlineColorAlpha *= borderlineOpacity;
170 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
171 // We use pre-multiplied color to reduce operations.
172 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
174 // Manual blend operation with premultiplied colors.
175 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
176 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
177 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
178 // Else, return vec4((rgb*alpha) / alpha, alpha)
180 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
181 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
182 // TODO : Need to find some way without division
183 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
185 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
189 #ifdef IS_REQUIRED_ROUNDED_CORNER
190 mediump float calculateCornerOpacity()
192 mediump float potential = gPotential;
194 // default opacity is 1.0
195 mediump float opacity = 1.0;
197 // calculate borderline opacity by potential
198 if(potential > gMaxOutlinePotential)
200 // potential is out of borderline range. just discard here
203 else if(potential > gMinOutlinePotential)
205 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
213 #ifdef ATLAS_DEFAULT_WARP
214 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
215 #elif defined(ATLAS_CUSTOM_WARP)
216 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
217 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
219 mediump vec2 texCoord = vTexCoord;
222 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
224 #ifdef IS_REQUIRED_ALPHA_MASKING
225 mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
226 textureColor.a *= maskAlpha;
227 textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
230 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
231 // skip most potential calculate for performance
232 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
234 OUT_COLOR = textureColor;
238 PreprocessPotential();
241 #ifdef IS_REQUIRED_BORDERLINE
242 textureColor = convertBorderlineColor(textureColor);
244 OUT_COLOR = textureColor;
246 #ifdef IS_REQUIRED_ROUNDED_CORNER
247 mediump float opacity = calculateCornerOpacity();
248 OUT_COLOR.a *= opacity;
249 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
252 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)