1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/shader-effects/shader-effect.h>
31 * @brief Creates a new SwirlEffect
33 * SwirlEffect is a custom shader effect to achieve swirl effects in Image actors.
35 * Animatable/Constrainable uniforms:
36 * "uAngle" - The angle of the swirl
37 * "uCenter" - The center of the swirl
38 * "uRadius" - The radius of the swirl
40 * @param[in] warp True if the effect should warp
41 * @return A handle to a newly allocated ShaderEffect
43 inline ShaderEffect CreateSwirlEffect( bool warp )
45 // append the default version
46 std::string fragmentShader(
47 "uniform mediump vec2 uTextureSize;\n"
48 "uniform highp float uRadius;\n"
49 "uniform highp float uAngle;\n"
50 "uniform mediump vec2 uCenter;\n"
53 " highp vec2 textureCenter = (sTextureRect.xy + sTextureRect.zw) * 0.5;\n"
54 " textureCenter = vTexCoord.st - textureCenter;\n"
55 " highp float distance = length(textureCenter);\n"
56 " if (distance >= uRadius)\n"
58 " highp float percent = (uRadius - distance) / uRadius;\n"
59 " highp float theta = percent * percent * uAngle * 4.0;\n"
60 " highp float sinTheta = sin(theta);\n"
61 " highp float cosTheta = cos(theta);\n" );
62 // if warp, loose the sign from sin
65 fragmentShader.append(
66 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, sinTheta) ), "
67 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
71 fragmentShader.append(
72 " textureCenter = vec2( dot( textureCenter, vec2(cosTheta, -sinTheta) ), "
73 " dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
75 fragmentShader.append(
76 " textureCenter += uCenter;\n"
77 " gl_FragColor = texture2D( sTexture, textureCenter ) * uColor;\n"
80 Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New(
83 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
85 shaderEffect.SetUniform( "uAngle", 0.0f );
86 shaderEffect.SetUniform( "uCenter", Vector2(0.5f, 0.5f) );
87 shaderEffect.SetUniform( "uRadius", 1.0f );
92 } // namespace Toolkit
96 #endif // __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__