1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
7 #ifndef IS_REQUIRED_ALPHA_MASKING
8 #define IS_REQUIRED_ALPHA_MASKING 0
10 #ifndef ATLAS_DEFAULT_WARP
11 #define ATLAS_DEFAULT_WARP 0
13 #ifndef ATLAS_CUSTOM_WARP
14 #define ATLAS_CUSTOM_WARP 0
17 INPUT mediump vec2 vTexCoord;
18 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
19 INPUT mediump vec2 vPosition;
20 INPUT mediump vec2 vRectSize;
21 INPUT mediump vec2 vOptRectSize;
22 #if IS_REQUIRED_ROUNDED_CORNER
23 INPUT mediump vec4 vCornerRadius;
27 uniform sampler2D sTexture;
29 #if IS_REQUIRED_ALPHA_MASKING
30 uniform sampler2D sMaskTexture;
31 INPUT mediump vec2 vMaskTexCoord;
34 #if ATLAS_DEFAULT_WARP
35 uniform mediump vec4 uAtlasRect;
36 #elif ATLAS_CUSTOM_WARP
37 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
38 uniform lowp vec2 wrapMode;
41 uniform lowp vec4 uColor;
42 uniform lowp vec3 mixColor;
43 uniform lowp float preMultipliedAlpha;
44 #if IS_REQUIRED_BORDERLINE
45 uniform mediump float borderlineWidth;
46 uniform mediump float borderlineOffset;
47 uniform lowp vec4 borderlineColor;
48 uniform lowp vec4 uActorColor;
52 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
55 if( wrap > 1.5 ) /* REFLECT */
56 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
57 else /* warp is 0 or 1 */
58 coord = mix(coordinate, fract(coordinate), wrap);
59 return clamp(mix(range.x, range.y, coord), range.x, range.y);
63 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
64 // Global values both rounded corner and borderline use
66 // radius of rounded corner on this quadrant
67 mediump float gRadius = 0.0;
69 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
70 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
71 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
72 mediump float gCenterPosition = 0.0;
73 // relative coordinate of gFragmentPosition from gCenterPosition.
74 mediump vec2 gDiff = vec2(0.0, 0.0);
75 // potential value what our algorithm use.
76 mediump float gPotential = 0.0;
78 // threshold of potential
79 mediump float gPotentialRange = 0.0;
80 mediump float gMaxOutlinePotential = 0.0;
81 mediump float gMinOutlinePotential = 0.0;
82 mediump float gMaxInlinePotential = 0.0;
83 mediump float gMinInlinePotential = 0.0;
85 void calculateCornerRadius()
87 #if IS_REQUIRED_ROUNDED_CORNER
90 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
91 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
92 sign(vPosition.y) * 0.5 + 0.5
97 void calculatePosition()
99 gFragmentPosition = abs(vPosition) - vRectSize;
100 gCenterPosition = -gRadius;
101 #if IS_REQUIRED_BORDERLINE
102 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
104 gDiff = gFragmentPosition - gCenterPosition;
107 void calculatePotential()
109 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
112 void setupMinMaxPotential()
114 gPotentialRange = 1.0;
116 gMaxOutlinePotential = gRadius + gPotentialRange;
117 gMinOutlinePotential = gRadius - gPotentialRange;
119 #if IS_REQUIRED_BORDERLINE
120 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
121 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
123 gMaxInlinePotential = gMaxOutlinePotential;
124 gMinInlinePotential = gMinOutlinePotential;
127 // reduce defect near edge of rounded corner.
128 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
129 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
132 void PreprocessPotential()
134 calculateCornerRadius();
136 calculatePotential();
138 setupMinMaxPotential();
142 #if IS_REQUIRED_BORDERLINE
143 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
145 mediump float potential = gPotential;
147 // default opacity of borderline is 0.0
148 mediump float borderlineOpacity = 0.0;
150 // calculate borderline opacity by potential
151 if(potential > gMinInlinePotential)
153 // potential is inside borderline range.
154 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
156 // Muliply borderlineWidth to resolve very thin borderline
157 borderlineOpacity *= min(1.0, borderlineWidth);
160 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
161 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
162 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
164 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
165 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
166 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
167 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
169 mediump float tCornerRadius = -gCenterPosition;
170 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
171 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
172 if(potential > MaxTexturelinePotential)
174 // potential is out of texture range.
175 textureColor = vec4(0.0);
179 // potential is in texture range.
180 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
181 textureColor.a *= textureAlphaScale;
182 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
185 borderlineColorAlpha *= borderlineOpacity;
186 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
187 // We use pre-multiplied color to reduce operations.
188 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
190 // Manual blend operation with premultiplied colors.
191 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
192 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
193 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
194 // Else, return vec4((rgb*alpha) / alpha, alpha)
196 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
197 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
198 // TODO : Need to find some way without division
199 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
201 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
205 #if IS_REQUIRED_ROUNDED_CORNER
206 mediump float calculateCornerOpacity()
208 mediump float potential = gPotential;
210 // default opacity is 1.0
211 mediump float opacity = 1.0;
213 // calculate borderline opacity by potential
214 if(potential > gMaxOutlinePotential)
216 // potential is out of borderline range. just discard here
219 else if(potential > gMinOutlinePotential)
221 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
229 #if ATLAS_DEFAULT_WARP
230 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
231 #elif ATLAS_CUSTOM_WARP
232 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
233 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
235 mediump vec2 texCoord = vTexCoord;
238 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
240 #if IS_REQUIRED_ALPHA_MASKING
241 mediump float maskAlpha = TEXTURE(sMaskTexture, vMaskTexCoord).a;
242 textureColor.a *= maskAlpha;
243 textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
246 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
247 // skip most potential calculate for performance
248 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
250 OUT_COLOR = textureColor;
254 PreprocessPotential();
257 #if IS_REQUIRED_BORDERLINE
258 textureColor = convertBorderlineColor(textureColor);
260 OUT_COLOR = textureColor;
262 #if IS_REQUIRED_ROUNDED_CORNER
263 mediump float opacity = calculateCornerOpacity();
264 OUT_COLOR.a *= opacity;
265 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
268 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE