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