2 * Copyright (c) 2014 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/public-api/shader-effects/distance-field-effect.h>
20 #define STRINGIFY(...) #__VA_ARGS__
31 const std::string COLOR_PROPERTY_NAME( "uColor" );
32 const std::string SMOOTHING_PROPERTY_NAME( "uSmoothing" );
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" );
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" );
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" );
50 DistanceFieldEffect::DistanceFieldEffect()
54 //Call the Parent copy constructor to add reference to the implementation for this object
55 DistanceFieldEffect::DistanceFieldEffect(ShaderEffect handle)
60 DistanceFieldEffect::~DistanceFieldEffect()
64 DistanceFieldEffect DistanceFieldEffect::New()
66 std::string fragmentShaderPrefix(
67 "#extension GL_OES_standard_derivatives : enable\n"
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"
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"
90 " if (uDoShadow == 0.0)\n"
92 " mediump float alpha = uColor.a * alphaFactor;\n"
93 " lowp vec4 rgb = uColor;\n"
95 " if (uDoOutline > 0.0)\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"
103 " if (uDoGlow > 0.0)\n"
105 " rgb = mix(uGlowColor, rgb, alphaFactor);\n"
106 " alpha = smoothstep(uGlowBoundary, uSmoothing, distance);\n"
109 " // set fragment color\n"
110 " color = vec4(rgb.rgb, alpha);\n"
113 " else // (uDoShadow > 0.0)\n"
115 " 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"
119 " // inside object, outside shadow\n"
120 " if (inText == 1.0)\n"
124 " // inside object, outside shadow\n"
125 " else if ((inText != 0.0) && (inShadow == 0.0))\n"
128 " color.a *= inText;\n"
130 " // outside object, completely inside shadow\n"
131 " else if ((inText == 0.0) && (inShadow == 1.0))\n"
133 " color = uShadowColor;\n"
135 " // inside object, completely inside shadow\n"
136 " else if ((inText != 0.0) && (inShadow == 1.0))\n"
138 " color = mix(uShadowColor, uColor, inText);\n"
139 " color.a = uShadowColor.a;\n"
141 " // inside object, inside shadow's border\n"
142 " else if ((inText != 0.0) && (inShadow != 0.0))\n"
144 " color = mix(uShadowColor, uColor, inText);\n"
145 " color.a *= max(inText, inShadow);\n"
147 " // inside shadow's border\n"
148 " else if (inShadow != 0.0)\n"
150 " color = uShadowColor;\n"
151 " color.a *= inShadow;\n"
153 " // outside shadow and object\n"
161 " gl_FragColor = color;\n"
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));
172 /* Pass ownership to DistanceFieldEffect through overloaded constructor, So that it now has access to the
173 Dali::ShaderEffect implementation */
175 // TODO: move default values to... Constants?
176 Dali::Toolkit::DistanceFieldEffect handle( shaderEffect );
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));
185 // TODO: find a way to set the shadow offset in texel coordinates instead of UVs.
186 handle.SetShadowOffset(Vector2(0.05f, 0.05f));
189 handle.SetOutline(false);
190 handle.SetGlow(false);
191 handle.SetShadow(false);
197 void DistanceFieldEffect::SetGlowColor(const Vector4& color)
199 SetUniform(GLOW_COLOR_PROPERTY_NAME, color);
202 void DistanceFieldEffect::SetGlow(bool glowEnable)
204 const float a = glowEnable ? 1.0f : 0.0f;
205 SetUniform(GLOW_ENABLE_PROPERTY_NAME, a);
208 void DistanceFieldEffect::SetGlowBoundary(float glowBoundary)
210 SetUniform(GLOW_BOUNDARY_PROPERTY_NAME, glowBoundary);
213 void DistanceFieldEffect::SetOutline(bool outlineEnable)
215 const float a = outlineEnable ? 1.0f : 0.0f;
216 SetUniform(OUTLINE_ENABLE_PROPERTY_NAME, a);
219 void DistanceFieldEffect::SetOutlineColor(const Vector4& color)
221 SetUniform(OUTLINECOLOR_PROPERTY_NAME, color);
224 void DistanceFieldEffect::SetOutlineParams(const Vector2& outlineParams)
226 SetUniform(OUTLINE_SIZE_PROPERTY_NAME, outlineParams);
229 void DistanceFieldEffect::SetShadow(bool shadowEnable)
237 const float a = shadowEnable ? 1.0f : 0.0f;
238 SetUniform(SHADOW_ENABLE_PROPERTY_NAME, a);
241 void DistanceFieldEffect::SetShadowColor(const Vector4& color)
243 SetUniform(SHADOW_COLOR_PROPERTY_NAME, color);
246 void DistanceFieldEffect::SetShadowOffset(const Vector2& offset)
248 SetUniform(SHADOW_OFFSET_PROPERTY_NAME, offset);
251 void DistanceFieldEffect::SetSmoothingEdge(const float smoothing)
253 SetUniform(SMOOTHING_PROPERTY_NAME, smoothing);
256 const std::string& DistanceFieldEffect::GetColorPropertyName() const
258 return COLOR_PROPERTY_NAME;
261 const std::string& DistanceFieldEffect::GetOutlineColorPropertyName() const
263 return OUTLINECOLOR_PROPERTY_NAME;
266 const std::string& DistanceFieldEffect::GetShadowColorPropertyName() const
268 return SHADOW_COLOR_PROPERTY_NAME;
271 const std::string& DistanceFieldEffect::GetShadowOffsetPropertyName() const
273 return SHADOW_OFFSET_PROPERTY_NAME;
276 const std::string& DistanceFieldEffect::GetGlowColorPropertyName() const
278 return GLOW_COLOR_PROPERTY_NAME;
281 const std::string& DistanceFieldEffect::GetGlowBoundaryPropertyName() const
283 return GLOW_BOUNDARY_PROPERTY_NAME;
286 const std::string& DistanceFieldEffect::GetOutlineSizePropertyName() const
288 return OUTLINE_SIZE_PROPERTY_NAME;
291 const std::string& DistanceFieldEffect::GetOutlineEnablePropertyName() const
293 return OUTLINE_ENABLE_PROPERTY_NAME;
296 const std::string& DistanceFieldEffect::GetGlowEnablePropertyName() const
298 return GLOW_ENABLE_PROPERTY_NAME;
301 const std::string& DistanceFieldEffect::GetShadowEnablePropertyName() const
303 return SHADOW_ENABLE_PROPERTY_NAME;
306 const std::string& DistanceFieldEffect::GetSmoothingPropertyName() const
308 return SMOOTHING_PROPERTY_NAME;
311 } // namespace Toolkit