Let borderline color doesn't affect to mixcolor alpha by uActorColor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / gradient-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 RADIAL
8 #define RADIAL 0
9 #endif
10
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;
18 #endif
19 #endif
20
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;
29 #endif
30
31 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
32 // Global values both rounded corner and borderline use
33
34 // radius of rounded corner on this quadrant
35 mediump float gRadius = 0.0;
36
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;
45
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;
52
53 void calculateCornerRadius()
54 {
55 #if IS_REQUIRED_ROUNDED_CORNER
56   gRadius =
57   mix(
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
61   );
62 #endif
63 }
64
65 void calculatePosition()
66 {
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;
71 #endif
72   gDiff = gFragmentPosition - gCenterPosition;
73 }
74
75 void calculatePotential()
76 {
77   gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
78 }
79
80 void setupMinMaxPotential()
81 {
82   gPotentialRange = 1.0;
83
84   gMaxOutlinePotential = gRadius + gPotentialRange;
85   gMinOutlinePotential = gRadius - gPotentialRange;
86
87 #if IS_REQUIRED_BORDERLINE
88   gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
89   gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
90 #else
91   gMaxInlinePotential = gMaxOutlinePotential;
92   gMinInlinePotential = gMinOutlinePotential;
93 #endif
94
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);
98 }
99
100 void PreprocessPotential()
101 {
102   calculateCornerRadius();
103   calculatePosition();
104   calculatePotential();
105
106   setupMinMaxPotential();
107 }
108 #endif
109
110
111 #if IS_REQUIRED_BORDERLINE
112 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
113 {
114   mediump float potential = gPotential;
115
116   // default opacity of borderline is 0.0
117   mediump float borderlineOpacity = 0.0;
118
119   // calculate borderline opacity by potential
120   if(potential > gMinInlinePotential)
121   {
122     // potential is inside borderline range.
123     borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
124   }
125
126   lowp vec3  BorderlineColorRGB   = borderlineColor.rgb * uActorColor.rgb;
127   lowp float BorderlineColorAlpha = borderlineColor.a * uActorColor.a;
128   // Gradient is always preMultiplied.
129   BorderlineColorRGB *= BorderlineColorAlpha;
130
131   //calculate inside of borderline when outilneColor.a < 1.0
132   if(borderlineColor.a < 1.0)
133   {
134     mediump float tCornerRadius = -gCenterPosition;
135     mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
136     mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
137     lowp vec3 BorderlineColorRGB = borderlineColor.xyz * borderlineColor.a;
138     if(potential > MaxTexturelinePotential)
139     {
140       // potential is out of texture range. use borderline color instead of texture
141       textureColor = vec4(BorderlineColorRGB, 0.0);
142     }
143     else if(potential > MinTexturelinePotential)
144     {
145       // potential is in texture range
146       textureColor = mix(textureColor, vec4(BorderlineColorRGB, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
147     }
148     // TODO : need to fix here when uColor.a = 0.0 and uActorColor.a != 0
149     borderlineOpacity *= borderlineColor.a;
150     return mix(textureColor, vec4(BorderlineColorRGB, 1.0), borderlineOpacity);
151   }
152   return mix(textureColor, vec4(BorderlineColorRGB, BorderlineColorAlpha), borderlineOpacity);
153 }
154 #endif
155
156 #if IS_REQUIRED_ROUNDED_CORNER
157 mediump float calculateCornerOpacity()
158 {
159   mediump float potential = gPotential;
160
161   // default opacity is 1.0
162   mediump float opacity = 1.0;
163
164   // calculate borderline opacity by potential
165   if(potential > gMaxOutlinePotential)
166   {
167     // potential is out of borderline range. just discard here
168     discard;
169   }
170   else if(potential > gMinOutlinePotential)
171   {
172     opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
173   }
174   return opacity;
175 }
176 #endif
177
178 void main()
179 {
180 #if RADIAL
181   lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0) * uColor;
182 #else
183   lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor;
184 #endif
185
186 #if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE
187   // skip most potential calculate for performance
188   if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
189   {
190     OUT_COLOR = textureColor;
191     return;
192   }
193   PreprocessPotential();
194 #endif
195
196 #if IS_REQUIRED_BORDERLINE
197   textureColor = convertBorderlineColor(textureColor);
198 #endif
199   OUT_COLOR = textureColor;
200
201 #if IS_REQUIRED_ROUNDED_CORNER
202   mediump float opacity = calculateCornerOpacity();
203   OUT_COLOR *= opacity;
204 #endif
205 }