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 ATLAS_DEFAULT_WARP
8 #define ATLAS_DEFAULT_WARP 0
10 #ifndef ATLAS_CUSTOM_WARP
11 #define ATLAS_CUSTOM_WARP 0
14 INPUT mediump vec2 vTexCoord;
15 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
16 INPUT mediump vec2 vPosition;
17 INPUT mediump vec2 vRectSize;
18 INPUT mediump vec2 vOptRectSize;
19 #if IS_REQUIRED_ROUNDED_CORNER
20 INPUT mediump vec4 vCornerRadius;
24 uniform sampler2D sTexture;
25 #if ATLAS_DEFAULT_WARP
26 uniform mediump vec4 uAtlasRect;
27 #elif ATLAS_CUSTOM_WARP
28 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
29 uniform lowp vec2 wrapMode;
32 uniform lowp vec4 uColor;
33 uniform lowp vec3 mixColor;
34 uniform lowp float preMultipliedAlpha;
35 #if IS_REQUIRED_BORDERLINE
36 uniform mediump float borderlineWidth;
37 uniform mediump float borderlineOffset;
38 uniform lowp vec4 borderlineColor;
39 uniform lowp vec4 uActorColor;
43 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
46 if( wrap > 1.5 ) /* REFLECT */
47 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
48 else /* warp is 0 or 1 */
49 coord = mix(coordinate, fract(coordinate), wrap);
50 return clamp(mix(range.x, range.y, coord), range.x, range.y);
54 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
55 // Global values both rounded corner and borderline use
57 // radius of rounded corner on this quadrant
58 mediump float gRadius = 0.0;
60 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
61 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
62 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
63 mediump float gCenterPosition = 0.0;
64 // relative coordinate of gFragmentPosition from gCenterPosition.
65 mediump vec2 gDiff = vec2(0.0, 0.0);
66 // potential value what our algorithm use.
67 mediump float gPotential = 0.0;
69 // threshold of potential
70 mediump float gPotentialRange = 0.0;
71 mediump float gMaxOutlinePotential = 0.0;
72 mediump float gMinOutlinePotential = 0.0;
73 mediump float gMaxInlinePotential = 0.0;
74 mediump float gMinInlinePotential = 0.0;
76 void calculateCornerRadius()
78 #if IS_REQUIRED_ROUNDED_CORNER
81 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
82 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
83 sign(vPosition.y) * 0.5 + 0.5
88 void calculatePosition()
90 gFragmentPosition = abs(vPosition) - vRectSize;
91 gCenterPosition = -gRadius;
92 #if IS_REQUIRED_BORDERLINE
93 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
95 gDiff = gFragmentPosition - gCenterPosition;
98 void calculatePotential()
100 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
103 void setupMinMaxPotential()
105 gPotentialRange = 1.0;
107 gMaxOutlinePotential = gRadius + gPotentialRange;
108 gMinOutlinePotential = gRadius - gPotentialRange;
110 #if IS_REQUIRED_BORDERLINE
111 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
112 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
114 gMaxInlinePotential = gMaxOutlinePotential;
115 gMinInlinePotential = gMinOutlinePotential;
118 // reduce defect near edge of rounded corner.
119 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
120 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
123 void PreprocessPotential()
125 calculateCornerRadius();
127 calculatePotential();
129 setupMinMaxPotential();
133 #if IS_REQUIRED_BORDERLINE
134 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
136 mediump float potential = gPotential;
138 // default opacity of borderline is 0.0
139 mediump float borderlineOpacity = 0.0;
141 // calculate borderline opacity by potential
142 if(potential > gMinInlinePotential)
144 // potential is inside borderline range.
145 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
147 // Muliply borderlineWidth to resolve very thin borderline
148 borderlineOpacity *= min(1.0, borderlineWidth);
151 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
152 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
153 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
155 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
156 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
157 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
158 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
160 mediump float tCornerRadius = -gCenterPosition;
161 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
162 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
163 if(potential > MaxTexturelinePotential)
165 // potential is out of texture range.
166 textureColor = vec4(0.0);
170 // potential is in texture range.
171 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
172 textureColor.a *= textureAlphaScale;
173 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
176 borderlineColorAlpha *= borderlineOpacity;
177 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
178 // We use pre-multiplied color to reduce operations.
179 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
181 // Manual blend operation with premultiplied colors.
182 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
183 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
184 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
185 // Else, return vec4((rgb*alpha) / alpha, alpha)
187 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
188 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
189 // TODO : Need to find some way without division
190 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
192 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
196 #if IS_REQUIRED_ROUNDED_CORNER
197 mediump float calculateCornerOpacity()
199 mediump float potential = gPotential;
201 // default opacity is 1.0
202 mediump float opacity = 1.0;
204 // calculate borderline opacity by potential
205 if(potential > gMaxOutlinePotential)
207 // potential is out of borderline range. just discard here
210 else if(potential > gMinOutlinePotential)
212 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
220 #if ATLAS_DEFAULT_WARP
221 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
222 #elif ATLAS_CUSTOM_WARP
223 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
224 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
226 mediump vec2 texCoord = vTexCoord;
229 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
231 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
232 // skip most potential calculate for performance
233 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
235 OUT_COLOR = textureColor;
239 PreprocessPotential();
242 #if IS_REQUIRED_BORDERLINE
243 textureColor = convertBorderlineColor(textureColor);
245 OUT_COLOR = textureColor;
247 #if IS_REQUIRED_ROUNDED_CORNER
248 mediump float opacity = calculateCornerOpacity();
249 OUT_COLOR.a *= opacity;
250 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
253 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE