2 * Copyright (c) 2015 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.
19 #include "bubble-effect.h"
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/images/image.h>
35 const std::string MAGNIFICATIOB_PROPERTY_NAME( "uMagnification" );
36 const float EACH_WIDTH_PER_SHAPE(32.0f);
40 BubbleEffect::BubbleEffect()
45 BubbleEffect::BubbleEffect( ShaderEffect handle )
46 : ShaderEffect( handle ),
51 BubbleEffect::~BubbleEffect()
55 BubbleEffect BubbleEffect::New( unsigned int numberOfBubble)
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"
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"
91 " gl_Position = vec4(0.0);\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"
103 " if(mod(aPosition.z,24.0) < 1.0 )\n"
105 " position.xy *= uMagnification;\n"
108 " float percentage = uPercentage[idx]*min(uMagnification,2.5);\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"
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"
124 vertexShaderStringStream << vertexShader;
126 std::string fragmentShader(
127 " precision mediump float;\n"
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(
142 vertexShaderStringStream.str(),
144 GeometryType( GEOMETRY_TYPE_TEXTURED_MESH),
145 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
147 BubbleEffect handle( shaderEffect );
149 handle.mNumberOfBubbles = numberOfBubble;
150 handle.SetMovementArea( Stage::GetCurrent().GetSize() );
152 handle.SetUniform( "uGravity", 50.f );
153 handle.SetUniform( "uMagnification", 1.f );
154 handle.SetUniform( "uDynamicScale", 1.f );
157 for( unsigned int i=0; i<numberOfBubble; i++ )
159 handle.SetPercentage( i, 0.f);
160 handle.SetStartAndEndPosition( i, zeroVector );
166 void BubbleEffect::SetMovementArea( const Vector2& movementArea )
168 if( movementArea == mMovementArea )
173 mMovementArea = movementArea;
174 SetUniform( "uInvertedMovementArea", Vector2(1.f,1.f) / mMovementArea );
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));
189 void BubbleEffect::SetStartAndEndPosition( unsigned int index, const Vector4& startAndEndPosition )
191 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
192 std::ostringstream oss;
193 oss<< "uStartAndEndPos["<< index << "]";
194 SetUniform( oss.str(), startAndEndPosition );
197 void BubbleEffect::SetPercentage( unsigned int index, float percentage )
199 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
200 SetUniform( GetPercentagePropertyName(index), percentage );
203 void BubbleEffect::SetGravity( float gravity )
205 SetUniform( "uGravity", gravity );
208 void BubbleEffect::SetDynamicScale( float scale )
210 SetUniform( "uDynamicScale", scale );
213 void BubbleEffect::SetMagnification( float magnification )
215 SetUniform( MAGNIFICATIOB_PROPERTY_NAME, magnification );
218 std::string BubbleEffect::GetPercentagePropertyName( unsigned int index ) const
220 DALI_ASSERT_ALWAYS( index < mNumberOfBubbles );
221 std::ostringstream oss;
222 oss<< "uPercentage["<< index << "]";
226 std::string BubbleEffect::GetMagnificationPropertyName() const
228 return MAGNIFICATIOB_PROPERTY_NAME;
231 void BubbleEffect::ResetParameters()
233 SetMagnification( 1.f );
235 for( unsigned int i=0; i<mNumberOfBubbles; i++ )
237 SetPercentage( i, 1.f);
238 SetStartAndEndPosition( i, zeroVector );
243 } // namespace Toolkit