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