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