1 #ifndef DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_H
2 #define DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_H
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
23 #include <dali/public-api/rendering/shader.h>
35 * Create the shader to be used by the renderer
36 * @param[in] numberOfBubble How many groups of uniforms are used to control the bubble movement.
37 * @return A handle to the newly created shader.
39 inline Shader CreateBubbleShader( unsigned int numBubble )
41 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
42 attribute mediump float aIndex;\n
43 attribute mediump vec2 aPosition;\n
44 attribute highp vec2 aTexCoord;\n
45 varying mediump vec2 vTexCoord;\n
46 uniform mediump mat4 uMvpMatrix;\n
47 // the gravity applied to the y direction
48 uniform mediump float uGravity;\n
49 // xy: the emit position of the bubble; zw: the destination of the bubble.
50 // The bubble is moving from (xy) to (zw plus the y drop influenced by gravity).
51 uniform vec4 uStartEndPosition[NUMBER_OF_BUBBLE];\n
52 // The undergoing percentage of the bubble movement. 0.0: start from emit position, 1.0: reach the destination
53 uniform float uPercentage[NUMBER_OF_BUBBLE];\n
54 uniform vec2 uInvertedMovementArea;\n
55 // The bubble number is restricted by the available uniform num.
56 // To increase the displayed bubble, every uStartEndPosition and uPercentage uniform is applied to a small bunch of bubbles (9 here)
57 // The offset defines the random offset between bubbles within the bunch.
58 uniform vec2 uOffset[9];\n
59 // This uniform is used to change the bubble size during running time
60 uniform float uDynamicScale;\n
61 varying float vPercentage;\n
62 varying vec2 vEffectTexCoord;\n
65 vec4 position = vec4( aPosition, 0.0, 1.0 );\n
66 // The Z coordinate is used to record the bubble index within current mesh actor
67 int index = int(aIndex); \n
68 //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.
69 int groupIdx = index / NUMBER_OF_BUBBLE;\n
70 // The bubbles within the same bunch applies the same uniforms uStartEndPosition[idx] & uPercentage[idx]
71 int idx = index - groupIdx*NUMBER_OF_BUBBLE;\n
72 float percentage = uPercentage[idx];
73 // early out if uPercentage is (zero || one) setting position to zero (zero sized triangles)
74 if( percentage <= 0.0 || percentage >= 1.0 )\n
76 gl_Position = vec4(0.0);\n
79 vec4 startAndEnd = uStartEndPosition[idx];\n
80 // The final position is added up different offset for bubbles
81 startAndEnd.zw += uOffset[groupIdx];\n
83 // increase the bubble size from 0% to 100% during the first 1/5 of movement & apply the dynamic scale
84 // the new xy value containes both the new scale and new bubble position
85 position.xy *= uDynamicScale*min(percentage*5.0, 1.0);\n
86 position.xy += mix(startAndEnd.xy, startAndEnd.zw, percentage);\n
87 // The gravity is g*t*t on the y direction
88 position.y += uGravity * pow(percentage, 2.0);\n
89 gl_Position = uMvpMatrix * position;\n
91 // Add multiple bubble shapes in the effect
92 vTexCoord = aTexCoord;\n
93 vPercentage = percentage;\n
94 // Use the emit position color for the bubble
95 vEffectTexCoord = startAndEnd.xy * uInvertedMovementArea + vec2(0.5);\n
99 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
100 varying mediump vec2 vTexCoord;\n
101 uniform lowp vec4 uColor;\n
102 uniform sampler2D sBackground;\n
103 uniform sampler2D sBubbleShape;\n
104 varying mediump float vPercentage;\n
105 varying mediump vec2 vEffectTexCoord;\n
109 // Get the emit pisition color, and Mix with the actor color
110 mediump vec4 fragColor = texture2D(sBackground, vEffectTexCoord)*uColor;\n
111 // Apply the shape defined by the texture contained sBubbleShape
112 // And make the opacity being 0.7, and animate from 0.7 to 0 during the last 1/3 of movement
113 fragColor.a *= texture2D(sBubbleShape, vTexCoord).a * ( 2.1 - max( vPercentage*2.1, 1.4 ) );\n
114 gl_FragColor = fragColor;\n
118 std::ostringstream vertexShaderStringStream;
119 vertexShaderStringStream << "#define NUMBER_OF_BUBBLE "<< numBubble << "\n"
121 Shader shader = Shader::New( vertexShaderStringStream.str(), FRAGMENT_SHADER );
126 } // namespace Internal
128 } // namespace Toolkit
131 #endif // DALI_TOOLKIT_INTERNAL_BUBBLE_EFFECT_H