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/public-api/shader-effects/swirl-effect.h>
29 const std::string ANGLE_PROPERTY_NAME( "uAngle" );
30 const std::string CENTER_PROPERTY_NAME( "uCenter" );
31 const std::string RADIUS_PROPERTY_NAME( "uRadius" );
35 SwirlEffect::SwirlEffect()
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 SwirlEffect::SwirlEffect(ShaderEffect handle)
45 SwirlEffect::~SwirlEffect()
50 SwirlEffect SwirlEffect::New(bool warp)
52 // append the default version
53 std::string fragmentShader(
54 "uniform mediump vec2 uTextureSize;\n"
55 "uniform highp float uRadius;\n"
56 "uniform highp float uAngle;\n"
57 "uniform mediump vec2 uCenter;\n"
60 " highp vec2 textureCenter = (sTextureRect.xy + sTextureRect.zw) * 0.5;\n"
61 " textureCenter = vTexCoord.st - textureCenter;\n"
62 " highp float distance = length(textureCenter);\n"
63 " if (distance >= uRadius)\n"
65 " highp float percent = (uRadius - distance) / uRadius;\n"
66 " highp float theta = percent * percent * uAngle * 4.0;\n"
67 " highp float sinTheta = sin(theta);\n"
68 " highp float cosTheta = cos(theta);\n" );
69 // if warp, loose the sign from sin
72 fragmentShader.append(
73 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, sinTheta) ), "
74 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
78 fragmentShader.append(
79 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, -sinTheta) ), "
80 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
82 fragmentShader.append(
83 " textureCenter += uCenter;\n"
84 " gl_FragColor = texture2D( sTexture, textureCenter ) * uColor;\n"
87 // Create the implementation, temporarily owned on stack,
88 Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New(
91 Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
92 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
94 /* Pass ownership to SwirlEffect through overloaded constructor, So that it now has access to the
95 Dali::ShaderEffect implementation */
96 Dali::Toolkit::SwirlEffect handle( shaderEffectCustom );
98 handle.SetUniform( ANGLE_PROPERTY_NAME, 0.0f );
99 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.5f, 0.5f) );
100 handle.SetUniform( RADIUS_PROPERTY_NAME, 1.0f );
105 void SwirlEffect::SetAngle(float angle)
107 SetUniform( ANGLE_PROPERTY_NAME, angle );
110 void SwirlEffect::SetCenter(const Vector2& center)
112 SetUniform( CENTER_PROPERTY_NAME, center );
115 void SwirlEffect::SetRadius(float radius)
117 SetUniform( RADIUS_PROPERTY_NAME, radius );
120 const std::string& SwirlEffect::GetAnglePropertyName() const
122 return ANGLE_PROPERTY_NAME;
125 const std::string& SwirlEffect::GetCenterPropertyName() const
127 return CENTER_PROPERTY_NAME;
130 const std::string& SwirlEffect::GetRadiusPropertyName() const
132 return RADIUS_PROPERTY_NAME;
135 } // namespace Toolkit