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 // Set soft anti-alias range at most 2% of visual size
73 gPotentialRange = min(1.0, max(vRectSize.x, vRectSize.y) * 0.02);
75 gMaxOutlinePotential = gRadius + gPotentialRange;
76 gMinOutlinePotential = gRadius - gPotentialRange;
78 #ifdef IS_REQUIRED_BORDERLINE
79 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
80 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
82 gMaxInlinePotential = gMaxOutlinePotential;
83 gMinInlinePotential = gMinOutlinePotential;
86 // reduce defect near edge of rounded corner.
87 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
88 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
91 void PreprocessPotential()
93 calculateCornerRadius();
97 setupMinMaxPotential();
102 #ifdef IS_REQUIRED_BORDERLINE
103 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
105 mediump float potential = gPotential;
107 // default opacity of borderline is 0.0
108 mediump float borderlineOpacity = 0.0;
110 // calculate borderline opacity by potential
111 if(potential > gMinInlinePotential)
113 // potential is inside borderline range.
114 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
116 // Muliply borderlineWidth to resolve very thin borderline
117 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
120 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
121 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
122 // NOTE : gradient-visual is always preMultiplied.
123 borderlineColorRGB *= borderlineColorAlpha;
125 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
126 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
127 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
128 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
130 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
131 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
132 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
133 if(potential > MaxTexturelinePotential)
135 // potential is out of texture range.
136 textureColor = vec4(0.0);
140 // potential is in texture range.
141 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
142 textureColor.a *= textureAlphaScale;
143 textureColor.rgb *= textureAlphaScale;
146 // NOTE : gradient-visual is always preMultiplied.
147 borderlineColorAlpha *= borderlineOpacity;
148 borderlineColorRGB *= borderlineOpacity;
149 // We use pre-multiplied color to reduce operations.
150 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
152 // Manual blend operation with premultiplied colors.
153 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
154 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
155 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
156 // Else, return vec4((rgb*alpha) / alpha, alpha)
158 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
159 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
160 return vec4(finalMultipliedRGB, finalAlpha);
162 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
166 #ifdef IS_REQUIRED_ROUNDED_CORNER
167 mediump float calculateCornerOpacity()
169 mediump float potential = gPotential;
171 // default opacity is 1.0
172 mediump float opacity = 1.0;
174 // calculate borderline opacity by potential
175 if(potential > gMaxOutlinePotential)
177 // potential is out of borderline range. just discard here
180 else if(potential > gMinOutlinePotential)
182 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
191 lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
193 lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
196 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
197 // skip most potential calculate for performance
198 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
200 OUT_COLOR = textureColor;
204 PreprocessPotential();
207 #ifdef IS_REQUIRED_BORDERLINE
208 textureColor = convertBorderlineColor(textureColor);
210 OUT_COLOR = textureColor;
212 #ifdef IS_REQUIRED_ROUNDED_CORNER
213 mediump float opacity = calculateCornerOpacity();
214 OUT_COLOR *= opacity;
217 #if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)