Let borderline color doesn't affect to mixcolor alpha by uActorColor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / color-visual-shader.frag
1 #ifndef IS_REQUIRED_ROUNDED_CORNER
2 #define IS_REQUIRED_ROUNDED_CORNER 0
3 #endif
4 #ifndef IS_REQUIRED_BORDERLINE
5 #define IS_REQUIRED_BORDERLINE 0
6 #endif
7 #ifndef IS_REQUIRED_BLUR
8 #define IS_REQUIRED_BLUR 0
9 #endif
10
11 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
12 INPUT mediump vec2 vPosition;
13 INPUT mediump vec2 vRectSize;
14 INPUT mediump vec2 vOptRectSize;
15 #if IS_REQUIRED_ROUNDED_CORNER
16 INPUT mediump vec4 vCornerRadius;
17 #endif
18 #endif
19
20 uniform lowp vec4 uColor;
21 uniform lowp vec3 mixColor;
22 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
23 uniform mediump float borderlineWidth;
24 uniform mediump float borderlineOffset;
25 uniform lowp vec4 borderlineColor;
26 uniform lowp vec4 uActorColor;
27 #endif
28 #if IS_REQUIRED_BLUR
29 uniform mediump float blurRadius;
30 #endif
31
32
33 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE || IS_REQUIRED_BLUR
34 // Global values both rounded corner and borderline use
35
36 // radius of rounded corner on this quadrant
37 mediump float gRadius = 0.0;
38
39 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
40 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
41 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
42 mediump float gCenterPosition = 0.0;
43 // relative coordinate of gFragmentPosition from gCenterPosition.
44 mediump vec2 gDiff = vec2(0.0, 0.0);
45 // potential value what our algorithm use.
46 mediump float gPotential = 0.0;
47
48 // threshold of potential
49 mediump float gPotentialRange = 0.0;
50 mediump float gMaxOutlinePotential = 0.0;
51 mediump float gMinOutlinePotential = 0.0;
52 mediump float gMaxInlinePotential = 0.0;
53 mediump float gMinInlinePotential = 0.0;
54
55 void calculateCornerRadius()
56 {
57 #if IS_REQUIRED_ROUNDED_CORNER
58   gRadius =
59   mix(
60     mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
61     mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
62     sign(vPosition.y) * 0.5 + 0.5
63   );
64 #endif
65 }
66
67 void calculatePosition()
68 {
69   gFragmentPosition = abs(vPosition) - vRectSize;
70   gCenterPosition = -gRadius;
71 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
72   gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
73 #endif
74   gDiff = gFragmentPosition - gCenterPosition;
75 }
76
77 void calculatePotential()
78 {
79   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
80 }
81
82 void setupMinMaxPotential()
83 {
84   gPotentialRange = 1.0;
85
86   gMaxOutlinePotential = gRadius + gPotentialRange;
87   gMinOutlinePotential = gRadius - gPotentialRange;
88
89 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
90   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
91   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
92 #else
93   gMaxInlinePotential = gMaxOutlinePotential;
94   gMinInlinePotential = gMinOutlinePotential;
95 #endif
96
97   // reduce defect near edge of rounded corner.
98   gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
99   gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0);
100 }
101
102 void PreprocessPotential()
103 {
104   calculateCornerRadius();
105   calculatePosition();
106   calculatePotential();
107
108   setupMinMaxPotential();
109 }
110 #endif
111
112 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
113 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
114 {
115   mediump float potential = gPotential;
116
117   // default opacity of borderline is 0.0
118   mediump float borderlineOpacity = 0.0;
119
120   // calculate borderline opacity by potential
121   if(potential > gMinInlinePotential)
122   {
123     // potential is inside borderline range.
124     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
125   }
126
127   lowp vec3  BorderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
128   lowp float BorderlineColorAlpha = borderlineColor.a * uActorColor.a;
129
130   //calculate inside of borderline when outilneColor.a < 1.0
131   if(borderlineColor.a < 1.0)
132   {
133     mediump float tCornerRadius = -gCenterPosition;
134     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
135     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
136     if(potential > MaxTexturelinePotential)
137     {
138       // potential is out of texture range. use borderline color instead of texture
139       textureColor = vec4(BorderlineColorRGB, 0.0);
140     }
141     else if(potential > MinTexturelinePotential)
142     {
143       // potential is in texture range
144       textureColor = mix(textureColor, vec4(BorderlineColorRGB, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
145     }
146     // TODO : need to fix here when uColor.a = 0.0 and uActorColor.a != 0
147     borderlineOpacity *= borderlineColor.a;
148     return mix(textureColor, vec4(BorderlineColorRGB, 1.0), borderlineOpacity);
149   }
150   return mix(textureColor, vec4(BorderlineColorRGB, BorderlineColorAlpha), borderlineOpacity);
151 }
152 #endif
153
154 #if !IS_REQUIRED_BLUR && IS_REQUIRED_ROUNDED_CORNER
155 mediump float calculateCornerOpacity()
156 {
157   mediump float potential = gPotential;
158
159   // default opacity is 1.0
160   mediump float opacity = 1.0;
161
162   // calculate borderline opacity by potential
163   if(potential > gMaxOutlinePotential)
164   {
165     // potential is out of borderline range. just discard here
166     discard;
167   }
168   else if(potential > gMinOutlinePotential)
169   {
170     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
171   }
172   return opacity;
173 }
174 #endif
175
176 #if IS_REQUIRED_BLUR
177 mediump float calculateBlurOpacity()
178 {
179 // Don't use borderline!
180   mediump vec2 v = gDiff;
181   mediump float cy = gRadius + blurRadius;
182   mediump float cr = gRadius + blurRadius;
183
184 #if IS_REQUIRED_ROUNDED_CORNER
185   // This routine make perfect circle. If corner radius is not exist, we don't consider prefect circle.
186   cy = min(cy, min(vRectSize.x, vRectSize.y) - gRadius);
187 #endif
188   v = vec2(min(v.x, v.y), max(v.x, v.y));
189   v = v + cy;
190
191   mediump float potential = 0.0;
192   mediump float alias = min(gRadius, 1.0);
193   mediump float potentialMin = cy + gRadius - blurRadius - alias;
194   mediump float potentialMax = cy + gRadius + blurRadius + alias;
195
196   // move center of circles for reduce defact
197   mediump float cyDiff = min(cy, 0.2 * blurRadius);
198   cy -= cyDiff;
199   cr += cyDiff;
200
201   mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x;
202
203   if(diffFromBaseline > 0.0)
204   {
205     // out of calculation bound.
206     potential = v.y;
207
208     // for anti-alias when blurRaidus = 0.0
209     mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy));
210     mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale);
211     potentialMin += potentialDiff;
212     potentialMax -= potentialDiff;
213   }
214   else
215   {
216     // get some circle centered (x, x) and radius (r = cr / cy * x)
217     // s.t. point v is on that circle
218     // highest point of that circle is (x, x + r) and potential is x + r
219
220     // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2
221 #if IS_REQUIRED_ROUNDED_CORNER
222     // NOTE : lowspec HW cannot calculate here. need to reduce numeric error
223     mediump float A = (cr * cr - 2.0 * cy * cy);
224     mediump float B = cy * (v.x + v.y);
225     mediump float V = dot(v,v);
226     mediump float D = B * B + A * V;
227     potential = V * (cr + cy) / (sqrt(D) + B);
228 #else
229     // We can simplify this value cause cy = 0.8 * blurRadius, cr = 1.2 * blurRadius
230     // potential = 5.0*(sqrt(4.0*(v.x+v.y)^2 + dot(v,v)) - 2.0*(v.x+v.y));
231     //           = 10.0*(v.x+v.y) * (sqrt(1.0 + (length(v) / (2.0*(v.x+v.y)))^2) - 1.0);
232     //           = 10.0*(v.x+v.y) * (sqrt(1.25 - x + x^2) - 1.0);
233     //          ~= 10.0*(v.x+v.y) * (0.11803399 - 0.44721360x + 0.35777088x^2 - 0.14310x^3 + O(x^4)) (Taylor series)
234     //          ~= -1.0557281 * (v.x + v.y) + 2.236068 * length(v) - ~~~ (here, x <= 0.5 * (1.0 - sqrt(0.5)) < 0.1464467)
235     // Note : This simplify need cause we should use it on lowspec HW.
236     mediump float x = 0.5 * (1.0 - length(v) / (v.x + v.y));
237     potential = -1.0557281 * (v.x + v.y) + 2.236068 * length(v) + 10.0 * (v.x + v.y) * (0.35777088 - 0.14310 * x) * x * x;
238 #endif
239   }
240
241   return 1.0 - smoothstep(potentialMin, potentialMax, potential);
242 }
243 #endif
244
245 void main()
246 {
247   lowp vec4 targetColor = vec4(mixColor, 1.0) * uColor;
248
249 #if IS_REQUIRED_BLUR || IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
250   // skip most potential calculate for performance
251   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
252   {
253     OUT_COLOR = targetColor;
254     return;
255   }
256   PreprocessPotential();
257 #endif
258
259 #if !IS_REQUIRED_BLUR && IS_REQUIRED_BORDERLINE
260   targetColor = convertBorderlineColor(targetColor);
261 #endif
262   OUT_COLOR = targetColor;
263
264 #if IS_REQUIRED_BLUR
265   mediump float opacity = calculateBlurOpacity();
266   OUT_COLOR.a *= opacity;
267 #elif IS_REQUIRED_ROUNDED_CORNER
268   mediump float opacity = calculateCornerOpacity();
269   OUT_COLOR.a *= opacity;
270 #endif
271 }