2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
21 #include "bubble-effect.h"
32 const std::string MAGNIFICATIOB_PROPERTY_NAME( "uMagnification" );
33 const float EACH_WIDTH_PER_SHAPE(32.0f);
37 BubbleEffect::BubbleEffect()
42 BubbleEffect::BubbleEffect( ShaderEffect handle )
43 : ShaderEffect( handle ),
48 BubbleEffect::~BubbleEffect()
52 BubbleEffect BubbleEffect::New( unsigned int numberOfBubble, const std::string& shapeImagePath)
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"
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"
89 " gl_Position = vec4(0.0);\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"
101 " if(mod(aPosition.z,24.0) < 1.0 )\n"
103 " position.xy *= uMagnification;\n"
106 " float percentage = uPercentage[idx]*min(uMagnification,2.5);\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"
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"
124 vertexShaderStringStream << vertexShader;
126 std::string fragmentShader(
127 " varying float vPercentage;\n"
128 " varying vec2 vEffectTexCoord;\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"
140 ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
141 GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
142 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
143 BubbleEffect handle( shaderEffect );
145 handle.mNumberOfBubbles = numberOfBubble;
146 handle.SetMovementArea( Stage::GetCurrent().GetSize() );
149 handle.SetUniform( "uGravity", 50.f );
150 handle.SetUniform( "uMagnification", 1.f );
151 handle.SetUniform( "uDynamicScale", 1.f );
153 //Get pixel width of the shape
154 float width = Image::GetImageSize(shapeImagePath).width;
155 handle.SetUniform( "uShapeWidth", (width/EACH_WIDTH_PER_SHAPE) );
158 for( unsigned int i=0; i<numberOfBubble; i++ )
160 handle.SetPercentage( i, 0.f);
161 handle.SetStartAndEndPosition( i, zeroVector );
167 void BubbleEffect::SetMovementArea( const Vector2& movementArea )
169 if( movementArea == mMovementArea )
174 mMovementArea = movementArea;
175 SetUniform( "uInvertedMovementArea", Vector2(1.f,1.f) / mMovementArea );
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));
190 void BubbleEffect::SetStartAndEndPosition( unsigned int index, const Vector4& startAndEndPosition )
192 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
193 std::ostringstream oss;
194 oss<< "uStartAndEndPos["<< index << "]";
195 SetUniform( oss.str(), startAndEndPosition );
198 void BubbleEffect::SetPercentage( unsigned int index, float percentage )
200 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
201 SetUniform( GetPercentagePropertyName(index), percentage );
204 void BubbleEffect::SetGravity( float gravity )
206 SetUniform( "uGravity", gravity );
209 void BubbleEffect::SetShapeImageWidth( float imageWidth )
211 SetUniform( "uShapeWidth", (imageWidth/EACH_WIDTH_PER_SHAPE) );
214 void BubbleEffect::SetDynamicScale( float scale )
216 SetUniform( "uDynamicScale", scale );
219 void BubbleEffect::SetMagnification( float magnification )
221 SetUniform( MAGNIFICATIOB_PROPERTY_NAME, magnification );
224 std::string BubbleEffect::GetPercentagePropertyName( unsigned int index ) const
226 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
227 std::ostringstream oss;
228 oss<< "uPercentage["<< index << "]";
232 std::string BubbleEffect::GetMagnificationPropertyName() const
234 return MAGNIFICATIOB_PROPERTY_NAME;
237 void BubbleEffect::ResetParameters()
239 SetMagnification( 1.f );
241 for( unsigned int i=0; i<mNumberOfBubbles; i++ )
243 SetPercentage( i, 1.f);
244 SetStartAndEndPosition( i, zeroVector );
249 } // namespace Toolkit