Changed Toolkit shader effects to be a static function returning a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / bouncing-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_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 bouncing effect
32  *
33  * BouncingEffect is a custom overscroll effect with two waves appearing at two sides then moving towards center and overlapping.
34  *
35  * Usage Example:
36  *
37  *  // Create the an imageActor, set shader effect, and add it to the stage
38  *  ImageActor imageActor = ImageActor::New( BufferImage::New( 1, 1 ) );
39  *  imageActor.SetSize(720.f,58.f);
40  *  Toolkit::ShaderEffect bouncingEffect = CreateBouncingEffect( Vector4(0.f,1.f,1.f,0.5f) );
41  *  imageActor.SetShaderEffect( bouncingEffect );
42  *  imageActor.SetParentOrigin( ParentOrigin::CENTER );
43  *  Stage::GetCurrent().Add( imageActor );
44  *
45  *   // Start the animation
46  *   Animation animation = Animation::New(1.f);
47  *   animation.AnimateTo( Property( bouncingEffect,"uProgressRate" ),
48  *                        1.f, AlphaFunction::BOUNCE );
49  *   animation.Play();
50  *
51  * Animatable/Constrainable uniforms:
52  *  "uProgressRate" - The progress rate to the effect
53  *
54  * @param[in] color The color used on the bouncing stripe
55  * @return A handle to a newly allocated ShaderEffect
56  */
57
58 inline ShaderEffect CreateBouncingEffect(const Vector4& color)
59 {
60   std::string fragmentShader = DALI_COMPOSE_SHADER(
61       precision mediump float;\n
62       uniform float uProgressRate;\n
63       uniform vec4 uAssignedColor;\n
64       void main()\n
65       {\n
66         float progressRate = abs(uProgressRate)*0.5;\n
67         float amplitude = 0.15 - progressRate*0.15 ;\n
68         float x1 = 7.5 * (vTexCoord.x - progressRate);\n
69         float x2 = 7.5 * (vTexCoord.x - 1.0 + progressRate);\n
70         float height1 = max(0.00001, 0.3 - amplitude * ( exp(x1) + exp(-x1) ) );\n
71         float height2 = max(0.00001, 0.3 - amplitude * ( exp(x2) + exp(-x2) ) );\n
72         float height3 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x1*0.5) + exp(-x1*0.5) ) );\n
73         float height4 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x2*0.5) + exp(-x2*0.5) ) );\n
74         vec4 fragColor = vec4(0.0);\n
75         float y = vTexCoord.y/(height1+height2);\n
76         float y2 = vTexCoord.y/max(height3,height4);\n
77         float coef = max(height1,height2)*5.0/( 1.0+exp(y*12.0-6.0) );\n
78         float alpha = pow( max(0.0,(1.0-y2))*(1.0-min(abs(x1),abs(x2))/5.0), 2.0);\n
79         if( vTexCoord.y < 0.075 )\n
80         {\n
81           fragColor= mix(uAssignedColor, vec4(1.0), coef);\n
82           fragColor += (vec4(1.0)-fragColor) * alpha;\n
83         }\n
84         else if (y2<1.0)\n
85         {\n
86           fragColor =vec4(1.0,1.0,1.0, alpha + (1.0-alpha)*coef);\n
87           fragColor.rgb -= ( vec3(1.0)-uAssignedColor.rgb )*min(clamp(y*1.2-0.3, 0.0, 0.3),clamp(0.9-y*1.2,0.0,0.3));\n
88         }\n
89         fragColor.a *= 10.0*min(min(vTexCoord.x, 1.0-vTexCoord.x),0.1)*min(1.0, progressRate/0.2);\n
90         gl_FragColor =  fragColor;\n
91       }
92   );
93
94   ShaderEffect shaderEffect;
95   shaderEffect = ShaderEffect::New( "", fragmentShader,
96                                     GeometryType( GEOMETRY_TYPE_IMAGE),
97                                     ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
98
99   shaderEffect.SetUniform( "uAssignedColor", color );
100   shaderEffect.SetUniform( "uProgressRate", 0.0f );
101
102   return shaderEffect;
103 }
104
105
106 } // namespace Toolkit
107
108 } // namespace Dali
109
110 #endif // __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_H__