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; // sampler1D?
12 uniform lowp vec4 uColor;
13 uniform lowp vec3 mixColor;
14 #ifdef IS_REQUIRED_BORDERLINE
15 uniform mediump float borderlineWidth;
16 uniform mediump float borderlineOffset;
17 uniform lowp vec4 borderlineColor;
18 uniform lowp vec4 uActorColor;
21 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
22 // Global values both rounded corner and borderline use
24 // radius of rounded corner on this quadrant
25 mediump float gRadius = 0.0;
27 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
28 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
29 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
30 mediump float gCenterPosition = 0.0;
31 // relative coordinate of gFragmentPosition from gCenterPosition.
32 mediump vec2 gDiff = vec2(0.0, 0.0);
33 // potential value what our algorithm use.
34 mediump float gPotential = 0.0;
36 // threshold of potential
37 mediump float gPotentialRange = 0.0;
38 mediump float gMaxOutlinePotential = 0.0;
39 mediump float gMinOutlinePotential = 0.0;
40 mediump float gMaxInlinePotential = 0.0;
41 mediump float gMinInlinePotential = 0.0;
43 void calculateCornerRadius()
45 #ifdef IS_REQUIRED_ROUNDED_CORNER
48 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
49 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
50 sign(vPosition.y) * 0.5 + 0.5
55 void calculatePosition()
57 gFragmentPosition = abs(vPosition) - vRectSize;
58 gCenterPosition = -gRadius;
59 #ifdef IS_REQUIRED_BORDERLINE
60 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
62 gDiff = gFragmentPosition - gCenterPosition;
65 void calculatePotential()
67 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
70 void setupMinMaxPotential()
72 gPotentialRange = 1.0;
74 gMaxOutlinePotential = gRadius + gPotentialRange;
75 gMinOutlinePotential = gRadius - gPotentialRange;
77 #ifdef IS_REQUIRED_BORDERLINE
78 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
79 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
81 gMaxInlinePotential = gMaxOutlinePotential;
82 gMinInlinePotential = gMinOutlinePotential;
85 // reduce defect near edge of rounded corner.
86 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
87 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
90 void PreprocessPotential()
92 calculateCornerRadius();
96 setupMinMaxPotential();
101 #ifdef IS_REQUIRED_BORDERLINE
102 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
104 mediump float potential = gPotential;
106 // default opacity of borderline is 0.0
107 mediump float borderlineOpacity = 0.0;
109 // calculate borderline opacity by potential
110 if(potential > gMinInlinePotential)
112 // potential is inside borderline range.
113 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
115 // Muliply borderlineWidth to resolve very thin borderline
116 borderlineOpacity *= min(1.0, borderlineWidth);
119 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
120 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
121 // NOTE : gradient-visual is always preMultiplied.
122 borderlineColorRGB *= borderlineColorAlpha;
124 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
125 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
126 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
127 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
129 mediump float tCornerRadius = -gCenterPosition;
130 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
131 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
132 if(potential > MaxTexturelinePotential)
134 // potential is out of texture range.
135 textureColor = vec4(0.0);
139 // potential is in texture range.
140 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
141 textureColor.a *= textureAlphaScale;
142 textureColor.rgb *= textureAlphaScale;
145 // NOTE : gradient-visual is always preMultiplied.
146 borderlineColorAlpha *= borderlineOpacity;
147 borderlineColorRGB *= borderlineOpacity;
148 // We use pre-multiplied color to reduce operations.
149 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
151 // Manual blend operation with premultiplied colors.
152 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
153 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
154 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
155 // Else, return vec4((rgb*alpha) / alpha, alpha)
157 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
158 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
159 return vec4(finalMultipliedRGB, finalAlpha);
161 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
165 #ifdef IS_REQUIRED_ROUNDED_CORNER
166 mediump float calculateCornerOpacity()
168 mediump float potential = gPotential;
170 // default opacity is 1.0
171 mediump float opacity = 1.0;
173 // calculate borderline opacity by potential
174 if(potential > gMaxOutlinePotential)
176 // potential is out of borderline range. just discard here
179 else if(potential > gMinOutlinePotential)
181 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
190 lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
192 lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
195 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
196 // skip most potential calculate for performance
197 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
199 OUT_COLOR = textureColor;
203 PreprocessPotential();
206 #ifdef IS_REQUIRED_BORDERLINE
207 textureColor = convertBorderlineColor(textureColor);
209 OUT_COLOR = textureColor;
211 #ifdef IS_REQUIRED_ROUNDED_CORNER
212 mediump float opacity = calculateCornerOpacity();
213 OUT_COLOR *= opacity;
216 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)