Merge "Changed styles to use case-insensitive matching" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / swirl-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 /**
31  * @brief Creates a new SwirlEffect
32  *
33  * SwirlEffect is a custom shader effect to achieve swirl effects in Image actors.
34  *
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
39  *
40  * @param[in] warp True if the effect should warp
41  * @return A handle to a newly allocated ShaderEffect
42  */
43 inline ShaderEffect CreateSwirlEffect( bool warp )
44 {
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"
51       "void main()\n"
52       "{\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"
57       "     discard;\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
63   if( warp )
64   {
65     fragmentShader.append(
66         "  textureCenter = vec2( dot( textureCenter, vec2(cosTheta, sinTheta) ), "
67         "                        dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
68   }
69   else
70   {
71     fragmentShader.append(
72         "  textureCenter = vec2( dot( textureCenter, vec2(cosTheta, -sinTheta) ), "
73         "                        dot( textureCenter, vec2(sinTheta, cosTheta) ) );\n" );
74   }
75   fragmentShader.append(
76       "  textureCenter += uCenter;\n"
77       "  gl_FragColor = texture2D( sTexture, textureCenter ) * uColor;\n"
78       "}" );
79
80   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
81       "",
82       fragmentShader,
83       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ));
84
85   shaderEffect.SetUniform( "uAngle", 0.0f );
86   shaderEffect.SetUniform( "uCenter", Vector2(0.5f, 0.5f) );
87   shaderEffect.SetUniform( "uRadius", 1.0f );
88
89   return shaderEffect;
90 }
91
92 } // namespace Toolkit
93
94 } // namespace Dali
95
96 #endif // __DALI_TOOLKIT_SHADER_EFFECT_SWIRL_H__