2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/devel-api/shader-effects/distance-field-effect.h>
29 const std::string COLOR_PROPERTY_NAME( "uColor" );
30 const std::string SMOOTHING_PROPERTY_NAME( "uSmoothing" );
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" );
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" );
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" );
48 DistanceFieldEffect::DistanceFieldEffect()
52 //Call the Parent copy constructor to add reference to the implementation for this object
53 DistanceFieldEffect::DistanceFieldEffect(ShaderEffect handle)
58 DistanceFieldEffect::~DistanceFieldEffect()
62 DistanceFieldEffect DistanceFieldEffect::New()
64 std::string fragmentShaderPrefix(
65 "#extension GL_OES_standard_derivatives : enable\n"
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"
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"
88 " if (uDoShadow == 0.0)\n"
90 " mediump float alpha = uColor.a * alphaFactor;\n"
91 " lowp vec4 rgb = uColor;\n"
93 " if (uDoOutline > 0.0)\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"
101 " if (uDoGlow > 0.0)\n"
103 " rgb = mix(uGlowColor, rgb, alphaFactor);\n"
104 " alpha = smoothstep(uGlowBoundary, uSmoothing, distance);\n"
107 " // set fragment color\n"
108 " color = vec4(rgb.rgb, alpha);\n"
111 " else // (uDoShadow > 0.0)\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"
117 " // inside object, outside shadow\n"
118 " if (inText == 1.0)\n"
122 " // inside object, outside shadow\n"
123 " else if ((inText != 0.0) && (inShadow == 0.0))\n"
126 " color.a *= inText;\n"
128 " // outside object, completely inside shadow\n"
129 " else if ((inText == 0.0) && (inShadow == 1.0))\n"
131 " color = uShadowColor;\n"
133 " // inside object, completely inside shadow\n"
134 " else if ((inText != 0.0) && (inShadow == 1.0))\n"
136 " color = mix(uShadowColor, uColor, inText);\n"
137 " color.a = uShadowColor.a;\n"
139 " // inside object, inside shadow's border\n"
140 " else if ((inText != 0.0) && (inShadow != 0.0))\n"
142 " color = mix(uShadowColor, uColor, inText);\n"
143 " color.a *= max(inText, inShadow);\n"
145 " // inside shadow's border\n"
146 " else if (inShadow != 0.0)\n"
148 " color = uShadowColor;\n"
149 " color.a *= inShadow;\n"
151 " // outside shadow and object\n"
159 " gl_FragColor = color;\n"
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));
170 /* Pass ownership to DistanceFieldEffect through overloaded constructor, So that it now has access to the
171 Dali::ShaderEffect implementation */
173 // TODO: move default values to... Constants?
174 Dali::Toolkit::DistanceFieldEffect handle( shaderEffect );
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));
183 // TODO: find a way to set the shadow offset in texel coordinates instead of UVs.
184 handle.SetShadowOffset(Vector2(0.05f, 0.05f));
187 handle.SetOutline(false);
188 handle.SetGlow(false);
189 handle.SetShadow(false);
195 void DistanceFieldEffect::SetGlowColor(const Vector4& color)
197 SetUniform(GLOW_COLOR_PROPERTY_NAME, color);
200 void DistanceFieldEffect::SetGlow(bool glowEnable)
202 const float a = glowEnable ? 1.0f : 0.0f;
203 SetUniform(GLOW_ENABLE_PROPERTY_NAME, a);
206 void DistanceFieldEffect::SetGlowBoundary(float glowBoundary)
208 SetUniform(GLOW_BOUNDARY_PROPERTY_NAME, glowBoundary);
211 void DistanceFieldEffect::SetOutline(bool outlineEnable)
213 const float a = outlineEnable ? 1.0f : 0.0f;
214 SetUniform(OUTLINE_ENABLE_PROPERTY_NAME, a);
217 void DistanceFieldEffect::SetOutlineColor(const Vector4& color)
219 SetUniform(OUTLINECOLOR_PROPERTY_NAME, color);
222 void DistanceFieldEffect::SetOutlineParams(const Vector2& outlineParams)
224 SetUniform(OUTLINE_SIZE_PROPERTY_NAME, outlineParams);
227 void DistanceFieldEffect::SetShadow(bool shadowEnable)
235 const float a = shadowEnable ? 1.0f : 0.0f;
236 SetUniform(SHADOW_ENABLE_PROPERTY_NAME, a);
239 void DistanceFieldEffect::SetShadowColor(const Vector4& color)
241 SetUniform(SHADOW_COLOR_PROPERTY_NAME, color);
244 void DistanceFieldEffect::SetShadowOffset(const Vector2& offset)
246 SetUniform(SHADOW_OFFSET_PROPERTY_NAME, offset);
249 void DistanceFieldEffect::SetSmoothingEdge(const float smoothing)
251 SetUniform(SMOOTHING_PROPERTY_NAME, smoothing);
254 const std::string& DistanceFieldEffect::GetColorPropertyName() const
256 return COLOR_PROPERTY_NAME;
259 const std::string& DistanceFieldEffect::GetOutlineColorPropertyName() const
261 return OUTLINECOLOR_PROPERTY_NAME;
264 const std::string& DistanceFieldEffect::GetShadowColorPropertyName() const
266 return SHADOW_COLOR_PROPERTY_NAME;
269 const std::string& DistanceFieldEffect::GetShadowOffsetPropertyName() const
271 return SHADOW_OFFSET_PROPERTY_NAME;
274 const std::string& DistanceFieldEffect::GetGlowColorPropertyName() const
276 return GLOW_COLOR_PROPERTY_NAME;
279 const std::string& DistanceFieldEffect::GetGlowBoundaryPropertyName() const
281 return GLOW_BOUNDARY_PROPERTY_NAME;
284 const std::string& DistanceFieldEffect::GetOutlineSizePropertyName() const
286 return OUTLINE_SIZE_PROPERTY_NAME;
289 const std::string& DistanceFieldEffect::GetOutlineEnablePropertyName() const
291 return OUTLINE_ENABLE_PROPERTY_NAME;
294 const std::string& DistanceFieldEffect::GetGlowEnablePropertyName() const
296 return GLOW_ENABLE_PROPERTY_NAME;
299 const std::string& DistanceFieldEffect::GetShadowEnablePropertyName() const
301 return SHADOW_ENABLE_PROPERTY_NAME;
304 const std::string& DistanceFieldEffect::GetSmoothingPropertyName() const
306 return SMOOTHING_PROPERTY_NAME;
309 } // namespace Toolkit