2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "bubble-effect.h"
33 const std::string MAGNIFICATIOB_PROPERTY_NAME( "uMagnification" );
34 const float EACH_WIDTH_PER_SHAPE(32.0f);
38 BubbleEffect::BubbleEffect()
43 BubbleEffect::BubbleEffect( ShaderEffect handle )
44 : ShaderEffect( handle ),
49 BubbleEffect::~BubbleEffect()
53 BubbleEffect BubbleEffect::New( unsigned int numberOfBubble, const std::string& shapeImagePath)
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"
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"
90 " gl_Position = vec4(0.0);\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"
102 " if(mod(aPosition.z,24.0) < 1.0 )\n"
104 " position.xy *= uMagnification;\n"
107 " float percentage = uPercentage[idx]*min(uMagnification,2.5);\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"
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"
125 vertexShaderStringStream << vertexShader;
127 std::string fragmentShader(
128 " varying float vPercentage;\n"
129 " varying vec2 vEffectTexCoord;\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"
141 ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
142 GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
143 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
144 BubbleEffect handle( shaderEffect );
146 handle.mNumberOfBubbles = numberOfBubble;
147 handle.SetMovementArea( Stage::GetCurrent().GetSize() );
150 handle.SetUniform( "uGravity", 50.f );
151 handle.SetUniform( "uMagnification", 1.f );
152 handle.SetUniform( "uDynamicScale", 1.f );
154 //Get pixel width of the shape
155 float width = Image::GetImageSize(shapeImagePath).width;
156 handle.SetUniform( "uShapeWidth", (width/EACH_WIDTH_PER_SHAPE) );
159 for( unsigned int i=0; i<numberOfBubble; i++ )
161 handle.SetPercentage( i, 0.f);
162 handle.SetStartAndEndPosition( i, zeroVector );
168 void BubbleEffect::SetMovementArea( const Vector2& movementArea )
170 if( movementArea == mMovementArea )
175 mMovementArea = movementArea;
176 SetUniform( "uInvertedMovementArea", Vector2(1.f,1.f) / mMovementArea );
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));
191 void BubbleEffect::SetStartAndEndPosition( unsigned int index, const Vector4& startAndEndPosition )
193 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
194 std::ostringstream oss;
195 oss<< "uStartAndEndPos["<< index << "]";
196 SetUniform( oss.str(), startAndEndPosition );
199 void BubbleEffect::SetPercentage( unsigned int index, float percentage )
201 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
202 SetUniform( GetPercentagePropertyName(index), percentage );
205 void BubbleEffect::SetGravity( float gravity )
207 SetUniform( "uGravity", gravity );
210 void BubbleEffect::SetShapeImageWidth( float imageWidth )
212 SetUniform( "uShapeWidth", (imageWidth/EACH_WIDTH_PER_SHAPE) );
215 void BubbleEffect::SetDynamicScale( float scale )
217 SetUniform( "uDynamicScale", scale );
220 void BubbleEffect::SetMagnification( float magnification )
222 SetUniform( MAGNIFICATIOB_PROPERTY_NAME, magnification );
225 std::string BubbleEffect::GetPercentagePropertyName( unsigned int index ) const
227 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
228 std::ostringstream oss;
229 oss<< "uPercentage["<< index << "]";
233 std::string BubbleEffect::GetMagnificationPropertyName() const
235 return MAGNIFICATIOB_PROPERTY_NAME;
238 void BubbleEffect::ResetParameters()
240 SetMagnification( 1.f );
242 for( unsigned int i=0; i<mNumberOfBubbles; i++ )
244 SetPercentage( i, 1.f);
245 SetStartAndEndPosition( i, zeroVector );
250 } // namespace Toolkit