(StyleManager) Stop throwing an exception if style-sheet not found
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / public-api / shader-effects / dissolve-local-effect.cpp
1 /*
2  * Copyright (c) 2014 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/public-api/shader-effects/dissolve-local-effect.h>
21
22 #include <dali-toolkit/public-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     "uniform vec2 uCenter[ NUMBER_OF_DIMPLE ];\n"
60     "uniform float uRadius[ NUMBER_OF_DIMPLE ]; \n"
61     "uniform float uPercentage[ NUMBER_OF_DIMPLE ]; \n"
62     "varying float vPercentage;\n"
63     "void main()\n"
64     "{\n"
65     "  vec4 position = uModelView * vec4( aPosition, 1.0 );\n"
66     "  float percentage = 0.0;\n"
67     "  for( int i=0; i<NUMBER_OF_DIMPLE; ++i )\n"
68     "  {\n"
69     "    float distance = distance(uCenter[i], position.xy);\n"
70     "    percentage = max(percentage, uPercentage[i] * cos(clamp( distance/uRadius[i], 0.0, 1.0 )*1.57) );"
71     "  }\n"
72     "  vPercentage = clamp( percentage, 0.0, 1.0 );\n"
73     "  gl_Position = uProjection * position;\n"
74     "  vTexCoord = aTexCoord;\n"
75     "}\n");
76   vertexShaderStringStream << vertexShader;
77
78   std::string fragmentShader(
79     "precision highp float;\n"
80     "uniform float uTransparency;\n"
81     "varying float vPercentage;\n"
82     "float rand(vec2 co) \n"
83     "{\n"
84     "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
85     "}\n"
86     "void main()\n"
87     "{\n"
88       //Calculate the randomness
89     "  float offsetS = rand( vTexCoord * vPercentage ); \n"
90     "  float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ); \n"
91     "  vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
92     "  gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
93     "  gl_FragColor.a *= 1.0 - uTransparency*vPercentage; \n"
94     "}\n");
95
96   ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
97                                                  GeometryType( GEOMETRY_TYPE_IMAGE),
98                                                  ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
99
100   DissolveLocalEffect handle( shaderEffect );
101   handle.mNumberOfDimples = numberOfDimples;
102
103   //Register uniform properties
104   for( unsigned int i = 0; i < numberOfDimples; i++ )
105   {
106     handle.SetCenter(i, Vector2(0.f,0.f));
107     handle.SetRadius(i, 0.f);
108     handle.SetDistortion( i, 0.f );
109   }
110   handle.SetProperty( ShaderEffect::GRID_DENSITY, Property::Value(5.f) );
111   handle.SetTransparency( 0.5f );
112
113   return handle;
114 }
115
116 unsigned int DissolveLocalEffect::GetNumberOfDimples() const
117 {
118   return mNumberOfDimples;
119 }
120
121 void DissolveLocalEffect::SetTransparency( float transparency)
122 {
123   SetUniform( TRANSPARENCY_PROPERTY_NAME, transparency );
124 }
125
126 void DissolveLocalEffect::SetCenter( unsigned int index, const Vector2& center )
127 {
128   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
129   SetUniform( GetCenterPropertyName( index ), center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
130 }
131
132 void DissolveLocalEffect::SetRadius( unsigned int index, float radius )
133 {
134   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
135   SetUniform( GetRadiusPropertyName( index ), radius );
136 }
137
138 void DissolveLocalEffect::SetDistortion( unsigned int index, float distortion )
139 {
140   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
141   SetUniform( GetDistortionPropertyName( index ), distortion );
142 }
143
144 std::string DissolveLocalEffect::GetCenterPropertyName( unsigned int index ) const
145 {
146   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
147   std::ostringstream oss;
148   oss<< CENTER_PROPERTY_NAME << "[" << index << "]";
149   return oss.str();
150 }
151
152 std::string DissolveLocalEffect::GetRadiusPropertyName( unsigned int index ) const
153 {
154   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
155   std::ostringstream oss;
156   oss<< RADIUS_PROPERTY_NAME << "[" << index << "]";
157   return oss.str();
158 }
159
160 std::string DissolveLocalEffect::GetDistortionPropertyName( unsigned int index ) const
161 {
162   DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
163   std::ostringstream oss;
164   oss<< DISTORTION_PROPERTY_NAME << "["<< index << "]";
165   return oss.str();
166 }
167
168 } // namespace Toolkit
169
170 } // namespace Dali