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;
42 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
45 if( wrap > 1.5 ) /* REFLECT */
46 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
47 else /* warp is 0 or 1 */
48 coord = mix(coordinate, fract(coordinate), wrap);
49 return clamp(mix(range.x, range.y, coord), range.x, range.y);
53 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
54 // Global values both rounded corner and borderline use
56 // radius of rounded corner on this quadrant
57 mediump float gRadius = 0.0;
59 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
60 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
61 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
62 mediump float gCenterPosition = 0.0;
63 // relative coordinate of gFragmentPosition from gCenterPosition.
64 mediump vec2 gDiff = vec2(0.0, 0.0);
65 // potential value what our algorithm use.
66 mediump float gPotential = 0.0;
68 // threshold of potential
69 mediump float gPotentialRange = 0.0;
70 mediump float gMaxOutlinePotential = 0.0;
71 mediump float gMinOutlinePotential = 0.0;
72 mediump float gMaxInlinePotential = 0.0;
73 mediump float gMinInlinePotential = 0.0;
75 void calculateCornerRadius()
77 #if IS_REQUIRED_ROUNDED_CORNER
80 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
81 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
82 sign(vPosition.y) * 0.5 + 0.5
87 void calculatePosition()
89 gFragmentPosition = abs(vPosition) - vRectSize;
90 gCenterPosition = -gRadius;
91 #if IS_REQUIRED_BORDERLINE
92 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
94 gDiff = gFragmentPosition - gCenterPosition;
97 void calculatePotential()
99 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
102 void setupMinMaxPotential()
104 gPotentialRange = 1.0;
106 gMaxOutlinePotential = gRadius + gPotentialRange;
107 gMinOutlinePotential = gRadius - gPotentialRange;
109 #if IS_REQUIRED_BORDERLINE
110 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
111 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
113 gMaxInlinePotential = gMaxOutlinePotential;
114 gMinInlinePotential = gMinOutlinePotential;
117 // reduce defect near edge of rounded corner.
118 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
119 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
122 void PreprocessPotential()
124 calculateCornerRadius();
126 calculatePotential();
128 setupMinMaxPotential();
132 #if IS_REQUIRED_BORDERLINE
133 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
135 mediump float potential = gPotential;
137 // default opacity of borderline is 0.0
138 mediump float borderlineOpacity = 0.0;
140 // calculate borderline opacity by potential
141 if(potential > gMinInlinePotential)
143 // potential is inside borderline range.
144 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
147 //calculate inside of borderline when outilneColor.a < 1.0
148 if(borderlineColor.a < 1.0)
150 mediump float tCornerRadius = -gCenterPosition;
151 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
152 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
153 lowp vec3 BorderlineColorRGB = borderlineColor.xyz;
154 BorderlineColorRGB *= mix(1.0, borderlineColor.a, preMultipliedAlpha);
155 if(potential > MaxTexturelinePotential)
157 // potential is out of texture range. use borderline color instead of texture
158 textureColor = vec4(BorderlineColorRGB, 0.0);
160 else if(potential > MinTexturelinePotential)
162 // potential is in texture range
163 textureColor = mix(textureColor, vec4(BorderlineColorRGB, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
165 borderlineOpacity *= borderlineColor.a;
166 return mix(textureColor, vec4(BorderlineColorRGB, 1.0), borderlineOpacity);
168 return mix(textureColor, borderlineColor, borderlineOpacity);
172 #if IS_REQUIRED_ROUNDED_CORNER
173 mediump float calculateCornerOpacity()
175 mediump float potential = gPotential;
177 // default opacity is 1.0
178 mediump float opacity = 1.0;
180 // calculate borderline opacity by potential
181 if(potential > gMaxOutlinePotential)
183 // potential is out of borderline range. just discard here
186 else if(potential > gMinOutlinePotential)
188 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
196 #if ATLAS_DEFAULT_WARP
197 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
198 #elif ATLAS_CUSTOM_WARP
199 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
200 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
202 mediump vec2 texCoord = vTexCoord;
205 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 );
207 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
208 // skip most potential calculate for performance
209 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
211 OUT_COLOR = textureColor * uColor;
214 PreprocessPotential();
217 #if IS_REQUIRED_BORDERLINE
218 textureColor = convertBorderlineColor(textureColor);
220 OUT_COLOR = textureColor * uColor;
222 #if IS_REQUIRED_ROUNDED_CORNER
223 mediump float opacity = calculateCornerOpacity();
224 OUT_COLOR.a *= opacity;
225 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);