1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
11 INPUT mediump vec2 vTexCoord;
12 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
13 INPUT mediump vec2 vPosition;
14 INPUT mediump vec2 vRectSize;
15 INPUT mediump vec2 vOptRectSize;
16 #if IS_REQUIRED_ROUNDED_CORNER
17 INPUT mediump vec4 vCornerRadius;
21 uniform sampler2D sTexture; // sampler1D?
22 uniform lowp vec4 uColor;
23 uniform lowp vec3 mixColor;
24 #if IS_REQUIRED_BORDERLINE
25 uniform mediump float borderlineWidth;
26 uniform mediump float borderlineOffset;
27 uniform lowp vec4 borderlineColor;
28 uniform lowp vec4 uActorColor;
31 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
32 // Global values both rounded corner and borderline use
34 // radius of rounded corner on this quadrant
35 mediump float gRadius = 0.0;
37 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
38 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
39 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
40 mediump float gCenterPosition = 0.0;
41 // relative coordinate of gFragmentPosition from gCenterPosition.
42 mediump vec2 gDiff = vec2(0.0, 0.0);
43 // potential value what our algorithm use.
44 mediump float gPotential = 0.0;
46 // threshold of potential
47 mediump float gPotentialRange = 0.0;
48 mediump float gMaxOutlinePotential = 0.0;
49 mediump float gMinOutlinePotential = 0.0;
50 mediump float gMaxInlinePotential = 0.0;
51 mediump float gMinInlinePotential = 0.0;
53 void calculateCornerRadius()
55 #if IS_REQUIRED_ROUNDED_CORNER
58 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
59 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
60 sign(vPosition.y) * 0.5 + 0.5
65 void calculatePosition()
67 gFragmentPosition = abs(vPosition) - vRectSize;
68 gCenterPosition = -gRadius;
69 #if IS_REQUIRED_BORDERLINE
70 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
72 gDiff = gFragmentPosition - gCenterPosition;
75 void calculatePotential()
77 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
80 void setupMinMaxPotential()
82 gPotentialRange = 1.0;
84 gMaxOutlinePotential = gRadius + gPotentialRange;
85 gMinOutlinePotential = gRadius - gPotentialRange;
87 #if IS_REQUIRED_BORDERLINE
88 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
89 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
91 gMaxInlinePotential = gMaxOutlinePotential;
92 gMinInlinePotential = gMinOutlinePotential;
95 // reduce defect near edge of rounded corner.
96 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
97 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
100 void PreprocessPotential()
102 calculateCornerRadius();
104 calculatePotential();
106 setupMinMaxPotential();
111 #if IS_REQUIRED_BORDERLINE
112 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
114 mediump float potential = gPotential;
116 // default opacity of borderline is 0.0
117 mediump float borderlineOpacity = 0.0;
119 // calculate borderline opacity by potential
120 if(potential > gMinInlinePotential)
122 // potential is inside borderline range.
123 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
125 // Muliply borderlineWidth to resolve very thin borderline
126 borderlineOpacity *= min(1.0, borderlineWidth);
129 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
130 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
131 // NOTE : gradient-visual is always preMultiplied.
132 borderlineColorRGB *= borderlineColorAlpha;
134 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
135 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
136 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
137 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
139 mediump float tCornerRadius = -gCenterPosition;
140 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
141 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
142 if(potential > MaxTexturelinePotential)
144 // potential is out of texture range.
145 textureColor = vec4(0.0);
149 // potential is in texture range.
150 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
151 textureColor.a *= textureAlphaScale;
152 textureColor.rgb *= textureAlphaScale;
155 // NOTE : gradient-visual is always preMultiplied.
156 borderlineColorAlpha *= borderlineOpacity;
157 borderlineColorRGB *= borderlineOpacity;
158 // We use pre-multiplied color to reduce operations.
159 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
161 // Manual blend operation with premultiplied colors.
162 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
163 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
164 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
165 // Else, return vec4((rgb*alpha) / alpha, alpha)
167 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
168 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
169 return vec4(finalMultipliedRGB, finalAlpha);
171 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
175 #if IS_REQUIRED_ROUNDED_CORNER
176 mediump float calculateCornerOpacity()
178 mediump float potential = gPotential;
180 // default opacity is 1.0
181 mediump float opacity = 1.0;
183 // calculate borderline opacity by potential
184 if(potential > gMaxOutlinePotential)
186 // potential is out of borderline range. just discard here
189 else if(potential > gMinOutlinePotential)
191 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
200 lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
202 lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
205 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
206 // skip most potential calculate for performance
207 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
209 OUT_COLOR = textureColor;
213 PreprocessPotential();
216 #if IS_REQUIRED_BORDERLINE
217 textureColor = convertBorderlineColor(textureColor);
219 OUT_COLOR = textureColor;
221 #if IS_REQUIRED_ROUNDED_CORNER
222 mediump float opacity = calculateCornerOpacity();
223 OUT_COLOR *= opacity;
226 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE