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