Changed Toolkit shader effects to be a static function returning a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / ripple-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_RIPPLE_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_RIPPLE_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 RippleEffect
32  *
33  * RippleEffect is a custom shader effect to achieve ripple effects on Image actors.
34  *
35  * Animatable/Constrainable uniforms:
36  *  "uAmplitude"  - The amplitude of the effect
37  *  "uCenter"     - The center point of the effect as screen coordinates
38  *  "uTime"       - The time duration for the ripple
39  *
40  * @return A handle to a newly allocated ShaderEffect
41  */
42 inline ShaderEffect CreateRippleEffect()
43 {
44   std::string vertexShader(
45       "precision mediump float;\n"
46       "uniform mediump   vec2  uCenter;\n"
47       "uniform mediump   float uTime;\n"
48       "uniform mediump   float uAmplitude;\n"
49       "varying mediump   float vLight;\n"
50       "varying mediump   float vShade;\n"
51       "void main()\n"
52       "{\n"
53       "float lighting = uAmplitude * 0.02;\n"
54       "float waveLength = uAmplitude * 0.0016;\n"
55       "vec4 world = uModelView * vec4(aPosition,1.0);\n"
56       "vec2 d = vec2(world.x - uCenter.x, world.y - uCenter.y);\n"
57       "float dist = length(d);\n"
58       "float amplitude = cos(uTime - dist*waveLength);\n"
59       "float slope     = sin(uTime - dist*waveLength);\n"
60       "world.z += amplitude * uAmplitude;\n"
61       "gl_Position = uProjection * world;\n"
62       "vec2 lightDirection = vec2(-0.707,0.707);\n"
63       "float dot = 0.0;\n"
64       "if(dist > 0.0)\n"
65       "{\n"
66       "  dot = dot(normalize(d),lightDirection) * lighting;\n"
67       "}\n"
68       "vShade = 1.0 - (dot * slope);\n"
69       "vLight = max(0.0, dot * -slope);\n"
70       "vTexCoord = aTexCoord;\n"
71       "}" );
72
73   // append the default version
74   std::string imageFragmentShader(
75       "precision mediump float;\n"
76       "varying mediump float  vLight;\n"
77       "varying mediump float  vShade;\n"
78       "void main()\n"
79       "{\n"
80       "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0) + vec4(vLight, vLight, vLight,0.0);\n"
81       "}" );
82
83
84   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
85       vertexShader, imageFragmentShader,
86       GeometryType(GEOMETRY_TYPE_IMAGE), ShaderEffect::GeometryHints(ShaderEffect::HINT_GRID) );
87
88
89   shaderEffect.SetUniform( "uAmplitude", 0.0f );
90   shaderEffect.SetUniform( "uCenter", Vector2(0.0f, 0.0f));
91   shaderEffect.SetUniform( "uTime", 0.0f );
92
93   return shaderEffect;
94 }
95
96 } // namespace Toolkit
97
98 } // namespace Dali
99
100 #endif // __DALI_TOOLKIT_SHADER_EFFECT_RIPPLE_H__