2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
17 #include <dali-toolkit/public-api/shader-effects/swirl-effect.h>
28 const std::string ANGLE_PROPERTY_NAME( "uAngle" );
29 const std::string CENTER_PROPERTY_NAME( "uCenter" );
30 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
34 SwirlEffect::SwirlEffect()
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 SwirlEffect::SwirlEffect(ShaderEffect handle)
44 SwirlEffect::~SwirlEffect()
49 SwirlEffect SwirlEffect::New(bool warp)
51 // append the default version
52 std::string fragmentShader(
53 "uniform vec2 uTextureSize;\n"
54 "uniform float uRadius;\n"
55 "uniform float uAngle;\n"
56 "uniform vec2 uCenter;\n"
59 " vec2 textureCenter = (sTextureRect.xy + sTextureRect.zw) * 0.5;\n"
60 " textureCenter = vTexCoord.st - textureCenter;\n"
61 " float distance = length(textureCenter);\n"
62 " if (distance >= uRadius)\n"
64 " float percent = (uRadius - distance) / uRadius;\n"
65 " float theta = percent * percent * uAngle * 4.0;\n"
66 " float sinTheta = sin(theta);\n"
67 " float cosTheta = cos(theta);\n" );
68 // if warp, loose the sign from sin
71 fragmentShader.append(
72 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, sinTheta) ), "
73 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
77 fragmentShader.append(
78 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, -sinTheta) ), "
79 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
81 fragmentShader.append(
82 " textureCenter += uCenter;\n"
83 " gl_FragColor = texture2D( sTexture, textureCenter ) * uColor;\n"
86 // Create the implementation, temporarily owned on stack,
87 Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New(
90 Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
91 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
93 /* Pass ownership to SwirlEffect through overloaded constructor, So that it now has access to the
94 Dali::ShaderEffect implementation */
95 Dali::Toolkit::SwirlEffect handle( shaderEffectCustom );
97 handle.SetUniform( ANGLE_PROPERTY_NAME, 0.0f );
98 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.5f, 0.5f) );
99 handle.SetUniform( RADIUS_PROPERTY_NAME, 1.0f );
104 void SwirlEffect::SetAngle(float angle)
106 SetUniform( ANGLE_PROPERTY_NAME, angle );
109 void SwirlEffect::SetCenter(const Vector2& center)
111 SetUniform( CENTER_PROPERTY_NAME, center );
114 void SwirlEffect::SetRadius(float radius)
116 SetUniform( RADIUS_PROPERTY_NAME, radius );
119 const std::string& SwirlEffect::GetAnglePropertyName() const
121 return ANGLE_PROPERTY_NAME;
124 const std::string& SwirlEffect::GetCenterPropertyName() const
126 return CENTER_PROPERTY_NAME;
129 const std::string& SwirlEffect::GetRadiusPropertyName() const
131 return RADIUS_PROPERTY_NAME;
134 } // namespace Toolkit