71ed07ced70b50875e2b8506a49efa6e7e7e964f
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / bendy-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_BENDY_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_BENDY_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  * Creates a new Bendy effect
32  *
33  * BendyEffect is a custom shader effect to achieve bendy effects in Image actors
34  *
35  * Animatable/Constrainable uniforms:
36  *  "uCenter"    - The center point of the bendy effect
37  *  "uDirection" - The direction of the bendy effect
38  *  "uRadius"    - The radius of the bendy effect
39  *
40  * @return A handle to a newly allocated ShaderEffect.
41  */
42 inline ShaderEffect CreateBendyEffect()
43 {
44   // append the default version
45   std::string vertextShader(
46       "uniform mediump   vec2  uCenter;\n"
47       "uniform mediump   vec2  uDirection;\n"
48       "uniform mediump   float uRadius;\n"
49       "\n"
50       "varying mediump   float vShade;\n"
51       "\n"
52       "void main()\n"
53       "{\n"
54       " mediump float lighting = 0.25;\n"
55       " mediump vec4 position = uModelView * vec4(aPosition,1.0);\n"
56       "\n"
57       " mediump vec2 d = position.xy - uCenter;\n"
58       " mediump float dist = max( 0.0, dot(d,uDirection) );\n"
59       " mediump float radius = max(0.0, uRadius - dist * 0.01);\n"
60       "\n"
61       " mediump float cs = cos(dist / radius / 2.0);\n"
62       " mediump float sn = sin(dist / radius / 2.0);\n"
63       "\n"
64       "position.xy = position.xy - uDirection * dist;\n"
65       "\n"
66       "position.xy += uDirection * sn * radius;\n"
67       "position.z += (1.0 - cs) * radius;\n"
68       "\n"
69       "gl_Position = uProjection * position;\n"
70       "\n"
71       "vShade = 1.0 - abs(sn) * lighting;\n"
72       "\n"
73       "vTexCoord = aTexCoord;\n"
74       "}" );
75
76   std::string fragmentShader(
77       "varying mediump float  vShade;\n"
78       "\n"
79       "void main()\n"
80       "{\n"
81       "  gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0);\n"
82       "}" );
83
84   // Create the implementation, temporarily owned on stack,
85   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
86       vertextShader,
87       fragmentShader,
88       ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_DEPTH_BUFFER ));
89
90   shaderEffect.SetUniform( "uCenter", Vector2(0.0f, 0.0f), ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
91   shaderEffect.SetUniform( "uDirection", Vector2(0.0f, 0.0f), ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION );
92   shaderEffect.SetUniform( "uRadius", 0.0f );
93
94
95   return shaderEffect;
96 }
97
98 } // namespace Toolkit
99
100 } // namespace Dali
101
102 #endif // __DALI_TOOLKIT_SHADER_EFFECT_BENDY_H__