Add support for Animation::EndAction::BakeFinal to builder
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / dissolve-local-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 #include <sstream>
18
19 #include <dali-toolkit/public-api/shader-effects/dissolve-local-effect.h>
20
21 #include <dali-toolkit/public-api/shader-effects/dissolve-effect.h>
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 namespace
30 {
31   const std::string DISTORTION_PROPERTY_NAME( "uPercentage" );
32   const std::string CENTER_PROPERTY_NAME( "uCenter" );
33   const std::string RADIUS_PROPERTY_NAME( "uRadius" );
34   const std::string TRANSPARENCY_PROPERTY_NAME( "uTransparency" );
35 }
36
37 DissolveLocalEffect::DissolveLocalEffect()
38 {
39 }
40
41 //Call the Parent copy constructor to add reference to the implementation for this object
42 DissolveLocalEffect::DissolveLocalEffect( ShaderEffect handle )
43 : ShaderEffect( handle )
44 {
45 }
46
47 DissolveLocalEffect::~DissolveLocalEffect()
48 {
49 }
50
51 DissolveLocalEffect DissolveLocalEffect::New( unsigned int numberOfDimples )
52 {
53   std::ostringstream vertexShaderStringStream;
54   vertexShaderStringStream << "#define NUMBER_OF_DIMPLE "<< numberOfDimples << "\n";
55   std::string vertexShader(
56     "uniform vec2 uCenter[ NUMBER_OF_DIMPLE ];\n"
57     "uniform float uRadius[ NUMBER_OF_DIMPLE ]; \n"
58     "uniform float uPercentage[ NUMBER_OF_DIMPLE ]; \n"
59     "varying float vPercentage;\n"
60     "void main()\n"
61     "{\n"
62     "  vec4 position = uModelView * vec4( aPosition, 1.0 );\n"
63     "  float percentage = 0.0;\n"
64     "  for( int i=0; i<NUMBER_OF_DIMPLE; ++i )\n"
65     "  {\n"
66     "    float distance = distance(uCenter[i], position.xy);\n"
67     "    percentage = max(percentage, uPercentage[i] * cos(clamp( distance/uRadius[i], 0.0, 1.0 )*1.57) );"
68     "  }\n"
69     "  vPercentage = clamp( percentage, 0.0, 1.0 );\n"
70     "  gl_Position = uProjection * position;\n"
71     "  vTexCoord = aTexCoord;\n"
72     "}\n");
73   vertexShaderStringStream << vertexShader;
74
75   std::string fragmentShader(
76     "precision highp float;\n"
77     "uniform float uTransparency;\n"
78     "varying float vPercentage;\n"
79     "float rand(vec2 co) \n"
80     "{\n"
81     "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
82     "}\n"
83     "void main()\n"
84     "{\n"
85       //Calculate the randomness
86     "  float offsetS = rand( vTexCoord * vPercentage ); \n"
87     "  float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ); \n"
88     "  vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
89     "  gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
90     "  gl_FragColor.a *= 1.0 - uTransparency*vPercentage; \n"
91     "}\n");
92
93   ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
94                                                  GeometryType( GEOMETRY_TYPE_IMAGE),
95                                                  ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
96
97   DissolveLocalEffect handle( shaderEffect );
98   handle.mNumberOfDimples = numberOfDimples;
99
100   //Register uniform properties
101   for( unsigned int i = 0; i < numberOfDimples; i++ )
102   {
103     handle.SetCenter(i, Vector2(0.f,0.f));
104     handle.SetRadius(i, 0.f);
105     handle.SetDistortion( i, 0.f );
106   }
107   handle.SetProperty( ShaderEffect::GRID_DENSITY, Property::Value(5.f) );
108   handle.SetTransparency( 0.5f );
109
110   return handle;
111 }
112
113 unsigned int DissolveLocalEffect::GetNumberOfDimples() const
114 {
115   return mNumberOfDimples;
116 }
117
118 void DissolveLocalEffect::SetTransparency( float transparency)
119 {
120   SetUniform( TRANSPARENCY_PROPERTY_NAME, transparency );
121 }
122
123 void DissolveLocalEffect::SetCenter( unsigned int index, const Vector2& center )
124 {
125   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
126   SetUniform( GetCenterPropertyName( index ), center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
127 }
128
129 void DissolveLocalEffect::SetRadius( unsigned int index, float radius )
130 {
131   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
132   SetUniform( GetRadiusPropertyName( index ), radius );
133 }
134
135 void DissolveLocalEffect::SetDistortion( unsigned int index, float distortion )
136 {
137   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
138   SetUniform( GetDistortionPropertyName( index ), distortion );
139 }
140
141 std::string DissolveLocalEffect::GetCenterPropertyName( unsigned int index ) const
142 {
143   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
144   std::ostringstream oss;
145   oss<< CENTER_PROPERTY_NAME << "[" << index << "]";
146   return oss.str();
147 }
148
149 std::string DissolveLocalEffect::GetRadiusPropertyName( unsigned int index ) const
150 {
151   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
152   std::ostringstream oss;
153   oss<< RADIUS_PROPERTY_NAME << "[" << index << "]";
154   return oss.str();
155 }
156
157 std::string DissolveLocalEffect::GetDistortionPropertyName( unsigned int index ) const
158 {
159   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
160   std::ostringstream oss;
161   oss<< DISTORTION_PROPERTY_NAME << "["<< index << "]";
162   return oss.str();
163 }
164
165 } // namespace Toolkit
166
167 } // namespace Dali