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