Shader compilation tool for dali-toolkit
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / graphics / shaders / distance-field-effect.frag
1 varying mediump vec2 vTexCoord;
2
3 uniform mediump float uGlowBoundary;
4 uniform mediump vec2  uOutlineParams;
5 uniform lowp    vec4  uOutlineColor;
6 uniform lowp    vec4  uShadowColor;
7 uniform mediump vec2  uShadowOffset;
8 uniform lowp    vec4  uGlowColor;
9 uniform lowp    float uDoOutline;
10 uniform lowp    float uDoShadow;
11 uniform lowp    float uDoGlow;
12
13 uniform sampler2D sTexture;
14 uniform lowp vec4 uColor;
15
16 void main()
17 {
18   // sample distance field
19   mediump float smoothing = 0.5;
20
21   mediump float distance = texture2D(sTexture, vTexCoord).a;
22   mediump float smoothWidth = fwidth(distance);
23   mediump float alphaFactor = smoothstep(smoothing - smoothWidth, smoothing + smoothWidth, distance);
24   lowp    vec4  color;
25
26   if (uDoShadow == 0.0)
27   {
28     mediump float alpha = uColor.a * alphaFactor;
29     lowp    vec4  rgb = uColor;
30
31     if (uDoOutline > 0.0)
32     {
33       mediump float outlineWidth = uOutlineParams[1] + smoothWidth;
34       mediump float outlineBlend = smoothstep(uOutlineParams[0] - outlineWidth, uOutlineParams[0] + outlineWidth, distance);
35       alpha = smoothstep(smoothing - smoothWidth, smoothing + smoothWidth, distance);
36       rgb = mix(uOutlineColor, uColor, outlineBlend);
37     }
38
39     if (uDoGlow > 0.0)
40     {
41       rgb = mix(uGlowColor, rgb, alphaFactor);
42       alpha = smoothstep(uGlowBoundary, smoothing, distance);
43     }
44
45     // set fragment color
46     color = vec4(rgb.rgb, alpha);
47   }
48
49   else // (uDoShadow > 0.0)
50   {
51     mediump float shadowDistance = texture2D(sTexture, vTexCoord - uShadowOffset).a;
52     mediump float inText = alphaFactor;
53     mediump float inShadow = smoothstep(smoothing - smoothWidth, smoothing + smoothWidth, shadowDistance);
54
55     // inside object, outside shadow
56     if (inText == 1.0)
57     {
58       color = uColor;
59     }
60     // inside object, outside shadow
61     else if ((inText != 0.0) && (inShadow == 0.0))
62     {
63       color = uColor;
64       color.a *= inText;
65     }
66     // outside object, completely inside shadow
67     else if ((inText == 0.0) && (inShadow == 1.0))
68     {
69       color = uShadowColor;
70     }
71     // inside object, completely inside shadow
72     else if ((inText != 0.0) && (inShadow == 1.0))
73     {
74       color = mix(uShadowColor, uColor, inText);
75       color.a = uShadowColor.a;
76     }
77     // inside object, inside shadow's border
78     else if ((inText != 0.0) && (inShadow != 0.0))
79     {
80       color = mix(uShadowColor, uColor, inText);
81       color.a *= max(inText, inShadow);
82     }
83     // inside shadow's border
84     else if (inShadow != 0.0)
85     {
86       color = uShadowColor;
87       color.a *= inShadow;
88     }
89     // outside shadow and object
90     else
91     {
92       color.a = 0.0;
93     }
94   }
95
96   gl_FragColor = color;
97 }