DALi Version 1.1.30
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bubble-effect / bubble-effect.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_H_
2 #define __DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_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 <sstream>
23 #include <dali/devel-api/rendering/shader.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 /**
35  * Create the shader to be used by the material
36  * @param[in] numberOfBubble How many groups of uniforms are used to control the bubble movement.
37  * @return A handle to the newly created shader.
38  */
39 inline Shader CreateBubbleShader( unsigned int numBubble )
40 {
41   const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
42   attribute mediump float   aIndex;\n
43   attribute mediump vec2    aPosition;\n
44   attribute highp   vec2    aTexCoord;\n
45   varying   mediump vec2    vTexCoord;\n
46   uniform   mediump mat4    uMvpMatrix;\n
47   // the gravity applied to the y direction
48   uniform mediump float uGravity;\n
49   // xy: the emit position of the bubble; zw: the destination of the bubble.
50   // The bubble is moving from (xy) to (zw plus the y drop influenced by gravity).
51   uniform vec4 uStartEndPosition[NUMBER_OF_BUBBLE];\n
52   // The undergoing percentage of the bubble movement. 0.0: start from emit position, 1.0: reach the destination
53   uniform float uPercentage[NUMBER_OF_BUBBLE];\n
54   uniform vec2 uInvertedMovementArea;\n
55   // The bubble number is restricted by the available uniform num.
56   // To increase the displayed bubble, every uStartEndPosition and uPercentage uniform is applied to a small bunch of bubbles (9 here)
57   // The offset defines the random offset between bubbles within the bunch.
58   uniform vec2 uOffset[9];\n
59   // This uniform is used to change the bubble size during running time
60   uniform float uDynamicScale;\n
61   varying float vPercentage;\n
62   varying vec2  vEffectTexCoord;\n
63   void main()\n
64   {\n
65     vec4 position = vec4( aPosition, 0.0, 1.0 );\n
66     // The Z coordinate is used to record the bubble index within current mesh actor
67     int index = int(aIndex); \n
68     //for some i between 0 ~ NUMBER_OF_BUBBLE-1: i,i+NUMBER_OF_BUBBLE, i+NUMBER_OF_BUBBLE*2, ... (up to i+NUMBER_OF_BUBBLE*8) belongs to the same bunch.
69     int groupIdx = index / NUMBER_OF_BUBBLE;\n
70     // The bubbles within the same bunch applies the same uniforms uStartEndPosition[idx] & uPercentage[idx]
71     int idx = index - groupIdx*NUMBER_OF_BUBBLE;\n
72     float percentage = uPercentage[idx];
73     // early out if uPercentage is (zero || one) setting position to zero (zero sized triangles)
74     if( percentage <= 0.0 || percentage >= 1.0 )\n
75     {\n
76       gl_Position = vec4(0.0);\n
77       return;\n
78     }\n
79     vec4 startAndEnd = uStartEndPosition[idx];\n
80     // The final position is added up different offset for bubbles
81     startAndEnd.zw += uOffset[groupIdx];\n
82     \n
83     // increase the bubble size from 0% to 100% during the first 1/5 of movement & apply the dynamic scale
84     // the new xy value containes both the new scale and new bubble position
85     position.xy *= uDynamicScale*min(percentage*5.0, 1.0);\n
86     position.xy += mix(startAndEnd.xy, startAndEnd.zw, percentage);\n
87     // The gravity is g*t*t on the y direction
88     position.y += uGravity * pow(percentage, 2.0);\n
89     gl_Position = uMvpMatrix * position;\n
90     \n
91     // Add multiple bubble shapes in the effect
92     vTexCoord = aTexCoord;\n
93     vPercentage = percentage;\n
94     // Use the emit position color for the bubble
95     vEffectTexCoord = startAndEnd.xy * uInvertedMovementArea;\n
96   }\n
97   );
98
99   const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
100   varying mediump vec2  vTexCoord;\n
101   uniform lowp    vec4  uColor;\n
102   uniform sampler2D     sBackground;\n
103   uniform sampler2D     sBubbleShape;\n
104   varying mediump float vPercentage;\n
105   varying mediump vec2  vEffectTexCoord;\n
106   \n
107   void main()\n
108   {\n
109     // Get the emit pisition color, and Mix with the actor color
110     mediump vec4 fragColor = texture2D(sBackground, vEffectTexCoord)*uColor;\n
111     // Apply the shape defined by the texture contained in the material
112     // And make the opacity being 0.7, and animate from 0.7 to 0 during the last 1/3 of movement
113     fragColor.a  *= texture2D(sBubbleShape, vTexCoord).a * ( 2.1 - max( vPercentage*2.1, 1.4 ) );\n
114     gl_FragColor = fragColor;\n
115   }\n
116   );
117
118   std::ostringstream vertexShaderStringStream;
119   vertexShaderStringStream << "#define NUMBER_OF_BUBBLE "<< numBubble << "\n"
120                            << VERTEX_SHADER;
121   Shader shader = Shader::New( vertexShaderStringStream.str(), FRAGMENT_SHADER );
122
123   return shader;
124 }
125
126 } // namespace Internal
127
128 } // namespace Toolkit
129
130 } // namespace Dali
131 #endif /* __DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_H_ */