Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / bubble-effect / bubble-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 // EXTERNAL HEADERS
18 #include <sstream>
19
20 // CLASS HEADER
21 #include "bubble-effect.h"
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 namespace
30 {
31
32 const std::string MAGNIFICATIOB_PROPERTY_NAME( "uMagnification" );
33 const float EACH_WIDTH_PER_SHAPE(32.0f);
34
35 } // namespace
36
37 BubbleEffect::BubbleEffect()
38 : mNumberOfBubbles(0)
39 {
40 }
41
42 BubbleEffect::BubbleEffect( ShaderEffect handle )
43 : ShaderEffect( handle ),
44   mNumberOfBubbles(0)
45 {
46 }
47
48 BubbleEffect::~BubbleEffect()
49 {
50 }
51
52 BubbleEffect BubbleEffect::New( unsigned int numberOfBubble, const std::string& shapeImagePath)
53 {
54   std::ostringstream vertexShaderStringStream;
55   vertexShaderStringStream << "#define NUMBER_OF_BUBBLE "<< numberOfBubble << "\n";
56   std::string vertexShader(
57     // the gravity applied to the y direction
58     "  uniform float uGravity; \n"
59     // Width of the texture in pixels
60     "  uniform float uShapeWidth; \n"
61     // xy: the emit position of the bubble; zw: the destinationof the bubble.
62     // The bubble is moving from (xy) to (zw plus the y drop influenced by gravity).
63     "  uniform vec4 uStartAndEndPos[NUMBER_OF_BUBBLE];\n"
64     // The undergoing percentage of the bubble movement. 0.0: start from emit position, 1.0: reach the destination
65     "  uniform float uPercentage[NUMBER_OF_BUBBLE];\n"
66     "  uniform vec2 uInvertedMovementArea; \n"
67     // The bubble number is restricted by the available uniform num.
68     // To increase the displayed bubble, every uStartAndEndPos and uPercentage uniform is applied to a small bunch of bubbles (9 here)
69     // The offset defines the random offset between bubbles within the bunch.
70     "  uniform vec2 offset[9]; \n"
71     // This uniform is specially for increase part of the bubble size and spread the bubble to the whole screen when unlock to home screen
72     "  uniform float uMagnification; \n"
73     // This uniform is used to change the bubble size during running time
74     "  uniform float uDynamicScale; \n"
75     "  varying float vPercentage;\n"
76     "  varying vec2  vEffectTexCoord;\n"
77     "  void main()\n"
78     "  {\n"
79     "    mediump vec4 position = vec4( aPosition.xy, 0.0, 1.0 );\n"
80     // The Z coordinate is used to record the bubble index within current mesh actor
81     "    int zCoord = int(aPosition.z); \n"
82     // 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.
83     "    int groupIdx = zCoord / NUMBER_OF_BUBBLE;\n"
84     // The bubbles within the same bunch applies the same uniforms uStartAndEndPos[idx] & uPercentage[idx]
85     "    int idx = zCoord - groupIdx*NUMBER_OF_BUBBLE;\n"
86     // early out if uPercentage is (zero || one) setting position to zero (zero sized triangles)
87     "    if( uPercentage[idx] <= 0.0 || uPercentage[idx] >= 1.0 )\n"
88     "    {\n"
89     "      gl_Position = vec4(0.0);\n"
90     "      return;\n"
91     "    }\n"
92     "    vec4 startAndEnd = uStartAndEndPos[idx]; \n"
93     // The final position is added up different offset for bubbles
94     "    startAndEnd.zw += offset[groupIdx];\n"
95     // Notice: Only animate the uMagnification for unlock (bubble explosion animation)!
96     // In other cases, uMagnification = 1.0!
97     // Increase the Size of part of bubbles and increase the speed of movement for unlock.
98     // Performance acceptable: Branch on a uniform variable.
99     "    if( uMagnification > 1.0)\n"
100     "    {\n"
101     "      if(mod(aPosition.z,24.0) < 1.0 )\n"
102     "      {\n"
103     "        position.xy *= uMagnification;\n"
104     "      }\n"
105     "    }\n"
106     "    float percentage = uPercentage[idx]*min(uMagnification,2.5);\n"
107     "\n"
108     // increase the bubble size from 0% to 100% during the first 1/5 of movement & apply the dynamic scale
109     // the new xy value containes both the new scale and new bubble position
110     "    position.xy *= uDynamicScale*min(percentage*5.0, 1.0);\n"
111     "    position.xy += mix(startAndEnd.xy, startAndEnd.zw, percentage*uMagnification);\n"
112     // The gravity is g*t*t on the y direction
113     "    position.y += uGravity * pow(percentage, 2.0);\n"
114     "    gl_Position = uMvpMatrix * position;\n"
115     "\n"
116     // Add multiple bubble shapes in the effect
117     "    mediump float texCoordX = floor( mod(startAndEnd.z, uShapeWidth) );\n "
118     "    mediump float texCoordY = floor( mod(startAndEnd.w, uShapeWidth) );\n "
119     "    vTexCoord = vec2( (texCoordX + aTexCoord.x)/ uShapeWidth,(texCoordY + aTexCoord.y)/ uShapeWidth );\n"
120     "    vPercentage = percentage;\n"
121     // Use the emit position color for the bubble
122     "    vEffectTexCoord = startAndEnd.xy * uInvertedMovementArea;\n"
123     "  }\n" );
124   vertexShaderStringStream << vertexShader;
125
126   std::string fragmentShader(
127     "  varying float vPercentage;\n"
128     "  varying vec2  vEffectTexCoord;\n"
129     "\n"
130     "  void main()\n"
131     "  {\n"
132     // Get the emit pisition color, and Mix with the actor color
133     "    vec4 fragColor = texture2D(sEffect, vEffectTexCoord)*uColor;\n"
134     // Apply the shape defined by the texture contained in the material
135     // And make the opacity being 0.7, and animate from 0.7 to 0 during the last 1/5 of movement
136     "    fragColor.a  *= texture2D(sTexture, vTexCoord).a * ( 3.5 - max( vPercentage*3.5, 2.8 ) );\n"
137     "    gl_FragColor = fragColor;\n"
138     "  }\n");
139
140   ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
141                                                  GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
142                                                  ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
143   BubbleEffect handle( shaderEffect );
144
145   handle.mNumberOfBubbles = numberOfBubble;
146   handle.SetMovementArea( Stage::GetCurrent().GetSize() );
147
148
149   handle.SetUniform( "uGravity", 50.f );
150   handle.SetUniform( "uMagnification", 1.f );
151   handle.SetUniform( "uDynamicScale", 1.f );
152
153   //Get pixel width of the shape
154   float width = Image::GetImageSize(shapeImagePath).width;
155   handle.SetUniform( "uShapeWidth", (width/EACH_WIDTH_PER_SHAPE) );
156
157   Vector4 zeroVector;
158   for( unsigned int i=0; i<numberOfBubble; i++ )
159   {
160     handle.SetPercentage( i, 0.f);
161     handle.SetStartAndEndPosition( i, zeroVector );
162   }
163
164   return handle;
165 }
166
167 void BubbleEffect::SetMovementArea( const Vector2& movementArea )
168 {
169   if( movementArea == mMovementArea )
170   {
171     return;
172   }
173
174   mMovementArea = movementArea;
175   SetUniform( "uInvertedMovementArea", Vector2(1.f,1.f) / mMovementArea );
176
177   srand(time(NULL));
178   int offset = mMovementArea.Length() / 10.f;
179   SetUniform("offset[0]", Vector2(0.f,0.f));
180   SetUniform("offset[1]", Vector2(rand()%offset,rand()%offset) );
181   SetUniform("offset[2]", Vector2(rand()%offset,-rand()%offset) );
182   SetUniform("offset[3]", Vector2(-rand()%offset,rand()%offset) );
183   SetUniform("offset[4]", Vector2(-rand()%offset,-rand()%offset) );
184   SetUniform("offset[5]", Vector2(rand()%offset,0.f));
185   SetUniform("offset[6]", Vector2(-rand()%offset,0.f));
186   SetUniform("offset[7]", Vector2(0.f,rand()%offset));
187   SetUniform("offset[8]", Vector2(0.f,-rand()%offset));
188 }
189
190 void BubbleEffect::SetStartAndEndPosition( unsigned int index, const Vector4& startAndEndPosition )
191 {
192   DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
193   std::ostringstream oss;
194   oss<< "uStartAndEndPos["<< index << "]";
195   SetUniform( oss.str(), startAndEndPosition );
196 }
197
198 void BubbleEffect::SetPercentage( unsigned int index, float percentage )
199 {
200   DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
201   SetUniform( GetPercentagePropertyName(index), percentage );
202 }
203
204 void BubbleEffect::SetGravity( float gravity )
205 {
206   SetUniform( "uGravity", gravity );
207 }
208
209 void BubbleEffect::SetShapeImageWidth( float imageWidth )
210 {
211   SetUniform( "uShapeWidth", (imageWidth/EACH_WIDTH_PER_SHAPE) );
212 }
213
214 void BubbleEffect::SetDynamicScale( float scale )
215 {
216   SetUniform( "uDynamicScale", scale );
217 }
218
219 void BubbleEffect::SetMagnification( float magnification )
220 {
221   SetUniform( MAGNIFICATIOB_PROPERTY_NAME, magnification );
222 }
223
224 std::string BubbleEffect::GetPercentagePropertyName( unsigned int index ) const
225 {
226   DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
227   std::ostringstream oss;
228   oss<< "uPercentage["<< index << "]";
229   return oss.str();
230 }
231
232 std::string BubbleEffect::GetMagnificationPropertyName() const
233 {
234   return MAGNIFICATIOB_PROPERTY_NAME;
235 }
236
237 void BubbleEffect::ResetParameters()
238 {
239   SetMagnification( 1.f );
240   Vector4 zeroVector;
241   for( unsigned int i=0; i<mNumberOfBubbles; i++ )
242   {
243     SetPercentage( i, 1.f);
244     SetStartAndEndPosition( i, zeroVector );
245   }
246 }
247
248
249 } // namespace Toolkit
250
251 } // namespace Dali