Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / distance-field-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <dali-toolkit/public-api/shader-effects/distance-field-effect.h>
18
19 #define STRINGIFY(...) #__VA_ARGS__
20
21 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace
28 {
29 // generic uniforms
30 const std::string COLOR_PROPERTY_NAME( "uColor" );
31 const std::string SMOOTHING_PROPERTY_NAME( "uSmoothing" );
32
33 // outline uniforms
34 const std::string OUTLINE_ENABLE_PROPERTY_NAME( "uDoOutline" );
35 const std::string OUTLINECOLOR_PROPERTY_NAME( "uOutlineColor" );
36 const std::string OUTLINE_SIZE_PROPERTY_NAME( "uOutlineParams" );
37
38 // glow related
39 const std::string GLOW_ENABLE_PROPERTY_NAME( "uDoGlow" );
40 const std::string GLOW_COLOR_PROPERTY_NAME( "uGlowColor" );
41 const std::string GLOW_BOUNDARY_PROPERTY_NAME( "uGlowBoundary" );
42
43 // shadow related
44 const std::string SHADOW_ENABLE_PROPERTY_NAME( "uDoShadow" );
45 const std::string SHADOW_COLOR_PROPERTY_NAME( "uShadowColor" );
46 const std::string SHADOW_OFFSET_PROPERTY_NAME( "uShadowOffset" );
47 } // namespace
48
49 DistanceFieldEffect::DistanceFieldEffect()
50 {
51 }
52
53 //Call the Parent copy constructor to add reference to the implementation for this object
54 DistanceFieldEffect::DistanceFieldEffect(ShaderEffect handle)
55 :ShaderEffect(handle)
56 {
57 }
58
59 DistanceFieldEffect::~DistanceFieldEffect()
60 {
61 }
62
63 DistanceFieldEffect DistanceFieldEffect::New()
64 {
65   std::string fragmentShaderPrefix(
66       "#extension GL_OES_standard_derivatives : enable\n"
67       "\n"
68       );
69
70   std::string fragmentShader(
71       "uniform mediump float uSmoothing;\n"
72       "uniform mediump float uGlowBoundary;\n"
73       "uniform mediump vec2  uOutlineParams;\n"
74       "uniform lowp    vec4  uOutlineColor;\n"
75       "uniform lowp    vec4  uShadowColor;\n"
76       "uniform mediump vec2  uShadowOffset;\n"
77       "uniform lowp    vec4  uGlowColor;\n"
78       "uniform lowp    float uDoOutline;\n"
79       "uniform lowp    float uDoShadow;\n"
80       "uniform lowp    float uDoGlow;\n"
81       "\n"
82       "void main()\n"
83       "{\n"
84       "  // sample distance field\n"
85       "  mediump float distance = texture2D(sTexture, vTexCoord).a;\n"
86       "  mediump float smoothWidth = fwidth(distance);\n"
87       "  mediump float alphaFactor = smoothstep(uSmoothing - smoothWidth, uSmoothing + smoothWidth, distance);\n"
88       "  lowp    vec4  color;\n"
89       "  if (uDoShadow == 0.0)\n"
90       "  {\n"
91       "    mediump float alpha = uColor.a * alphaFactor;\n"
92       "    lowp    vec4  rgb = uColor;\n"
93       "\n"
94       "    if (uDoOutline > 0.0)\n"
95       "    {\n"
96       "      mediump float outlineWidth = uOutlineParams[1] + smoothWidth;\n"
97       "      mediump float outlineBlend = smoothstep(uOutlineParams[0] - outlineWidth, uOutlineParams[0] + outlineWidth, distance);\n"
98       "      alpha = smoothstep(uSmoothing - smoothWidth, uSmoothing + smoothWidth, distance);\n"
99       "      rgb = mix(uOutlineColor, uColor, outlineBlend);\n"
100       "    }\n"
101       "\n"
102       "    if (uDoGlow > 0.0)\n"
103       "    {\n"
104       "      rgb = mix(uGlowColor, rgb, alphaFactor);\n"
105       "      alpha = smoothstep(uGlowBoundary, uSmoothing, distance);\n"
106       "    }\n"
107       "\n"
108       "    // set fragment color\n"
109       "    color = vec4(rgb.rgb, alpha);\n"
110       "  }\n"
111       "\n"
112       "  else // (uDoShadow > 0.0)\n"
113       "  {\n"
114       "    float shadowDistance = texture2D(sTexture, vTexCoord - uShadowOffset).a;\n"
115       "    mediump float inText = alphaFactor;\n"
116       "    mediump float inShadow = smoothstep(uSmoothing - smoothWidth, uSmoothing + smoothWidth, shadowDistance);\n"
117       "\n"
118       "    // inside object, outside shadow\n"
119       "    if (inText == 1.0)\n"
120       "    {\n"
121       "      color = uColor;\n"
122       "    }\n"
123       "    // inside object, outside shadow\n"
124       "    else if ((inText != 0.0) && (inShadow == 0.0))\n"
125       "    {\n"
126       "      color = uColor;\n"
127       "      color.a *= inText;\n"
128       "    }\n"
129       "    // outside object, completely inside shadow\n"
130       "    else if ((inText == 0.0) && (inShadow == 1.0))\n"
131       "    {\n"
132       "      color = uShadowColor;\n"
133       "    }\n"
134       "    // inside object, completely inside shadow\n"
135       "    else if ((inText != 0.0) && (inShadow == 1.0))\n"
136       "    {\n"
137       "      color = mix(uShadowColor, uColor, inText);\n"
138       "      color.a = uShadowColor.a;\n"
139       "    }\n"
140       "    // inside object, inside shadow's border\n"
141       "    else if ((inText != 0.0) && (inShadow != 0.0))\n"
142       "    {\n"
143       "      color = mix(uShadowColor, uColor, inText);\n"
144       "      color.a *= max(inText, inShadow);\n"
145       "    }\n"
146       "    // inside shadow's border\n"
147       "    else if (inShadow != 0.0)\n"
148       "    {\n"
149       "      color = uShadowColor;\n"
150       "      color.a *= inShadow;\n"
151       "    }\n"
152       "    // outside shadow and object\n"
153       "    else \n"
154       "    {\n"
155       "      color.a = 0.0;\n"
156       "    }\n"
157       "\n"
158       "  }\n"
159       "\n"
160       "  gl_FragColor = color;\n"
161       "\n"
162       "}\n"
163       );
164
165   // Create the implementation, temporarily owned on stack
166   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::NewWithPrefix("", "",
167                                                                        fragmentShaderPrefix, fragmentShader,
168                                                                        Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
169                                                                        ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING));
170
171   /* Pass ownership to DistanceFieldEffect through overloaded constructor, So that it now has access to the
172      Dali::ShaderEffect implementation */
173
174   // TODO: move default values to... Constants?
175   Dali::Toolkit::DistanceFieldEffect handle( shaderEffect );
176
177   handle.SetSmoothingEdge(0.5f);
178   handle.SetOutlineColor(Color::BLACK);
179   handle.SetOutlineParams(Vector2(0.51f, 0.0f));
180   handle.SetGlowBoundary(0.4f);
181   handle.SetGlowColor(Color::GREEN);
182   handle.SetShadowColor(Vector4(0.0f, 0.0f, 0.0f, 0.4f));
183
184   // TODO: find a way to set the shadow offset in texel coordinates instead of UVs.
185   handle.SetShadowOffset(Vector2(0.05f, 0.05f));
186
187   // Default:
188   handle.SetOutline(false);
189   handle.SetGlow(false);
190   handle.SetShadow(false);
191
192   return handle;
193
194 }
195
196 void DistanceFieldEffect::SetGlowColor(const Vector4& color)
197 {
198   SetUniform(GLOW_COLOR_PROPERTY_NAME, color);
199 }
200
201 void DistanceFieldEffect::SetGlow(bool glowEnable)
202 {
203   const float a = glowEnable ? 1.0f : 0.0f;
204   SetUniform(GLOW_ENABLE_PROPERTY_NAME, a);
205 }
206
207 void DistanceFieldEffect::SetGlowBoundary(float glowBoundary)
208 {
209   SetUniform(GLOW_BOUNDARY_PROPERTY_NAME, glowBoundary);
210 }
211
212 void DistanceFieldEffect::SetOutline(bool outlineEnable)
213 {
214   const float a = outlineEnable ? 1.0f : 0.0f;
215   SetUniform(OUTLINE_ENABLE_PROPERTY_NAME, a);
216 }
217
218 void DistanceFieldEffect::SetOutlineColor(const Vector4& color)
219 {
220   SetUniform(OUTLINECOLOR_PROPERTY_NAME, color);
221 }
222
223 void DistanceFieldEffect::SetOutlineParams(const Vector2& outlineParams)
224 {
225   SetUniform(OUTLINE_SIZE_PROPERTY_NAME, outlineParams);
226 }
227
228 void DistanceFieldEffect::SetShadow(bool shadowEnable)
229 {
230   if (shadowEnable)
231   {
232     SetGlow(false);
233     SetOutline(false);
234   }
235
236   const float a = shadowEnable ? 1.0f : 0.0f;
237   SetUniform(SHADOW_ENABLE_PROPERTY_NAME, a);
238 }
239
240 void DistanceFieldEffect::SetShadowColor(const Vector4& color)
241 {
242   SetUniform(SHADOW_COLOR_PROPERTY_NAME, color);
243 }
244
245 void DistanceFieldEffect::SetShadowOffset(const Vector2& offset)
246 {
247   SetUniform(SHADOW_OFFSET_PROPERTY_NAME, offset);
248 }
249
250 void DistanceFieldEffect::SetSmoothingEdge(const float smoothing)
251 {
252   SetUniform(SMOOTHING_PROPERTY_NAME, smoothing);
253 }
254
255 const std::string& DistanceFieldEffect::GetColorPropertyName() const
256 {
257   return COLOR_PROPERTY_NAME;
258 }
259
260 const std::string& DistanceFieldEffect::GetOutlineColorPropertyName() const
261 {
262   return OUTLINECOLOR_PROPERTY_NAME;
263 }
264
265 const std::string& DistanceFieldEffect::GetShadowColorPropertyName() const
266 {
267   return SHADOW_COLOR_PROPERTY_NAME;
268 }
269
270 const std::string& DistanceFieldEffect::GetShadowOffsetPropertyName() const
271 {
272   return SHADOW_OFFSET_PROPERTY_NAME;
273 }
274
275 const std::string& DistanceFieldEffect::GetGlowColorPropertyName() const
276 {
277   return GLOW_COLOR_PROPERTY_NAME;
278 }
279
280 const std::string& DistanceFieldEffect::GetGlowBoundaryPropertyName() const
281 {
282   return GLOW_BOUNDARY_PROPERTY_NAME;
283 }
284
285 const std::string& DistanceFieldEffect::GetOutlineSizePropertyName() const
286 {
287   return OUTLINE_SIZE_PROPERTY_NAME;
288 }
289
290 const std::string& DistanceFieldEffect::GetOutlineEnablePropertyName() const
291 {
292   return OUTLINE_ENABLE_PROPERTY_NAME;
293 }
294
295 const std::string& DistanceFieldEffect::GetGlowEnablePropertyName() const
296 {
297   return GLOW_ENABLE_PROPERTY_NAME;
298 }
299
300 const std::string& DistanceFieldEffect::GetShadowEnablePropertyName() const
301 {
302   return SHADOW_ENABLE_PROPERTY_NAME;
303 }
304
305 const std::string& DistanceFieldEffect::GetSmoothingPropertyName() const
306 {
307   return SMOOTHING_PROPERTY_NAME;
308 }
309
310 } // namespace Toolkit
311
312 } // namespace Dali