2 * Copyright (c) 2014 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.
20 #include <dali-toolkit/public-api/shader-effects/dissolve-local-effect.h>
22 #include <dali-toolkit/public-api/shader-effects/dissolve-effect.h>
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" );
38 DissolveLocalEffect::DissolveLocalEffect()
39 : mNumberOfDimples( 1 )
43 //Call the Parent copy constructor to add reference to the implementation for this object
44 DissolveLocalEffect::DissolveLocalEffect( ShaderEffect handle )
45 : ShaderEffect( handle ),
50 DissolveLocalEffect::~DissolveLocalEffect()
54 DissolveLocalEffect DissolveLocalEffect::New( unsigned int numberOfDimples )
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"
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"
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) );"
73 " vPercentage = clamp( percentage, 0.0, 1.0 );\n"
74 " gl_Position = uProjection * position;\n"
75 " vTexCoord = aTexCoord;\n"
77 vertexShaderStringStream << vertexShader;
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"
85 " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \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"
97 ShaderEffect shaderEffect = ShaderEffect::New( vertexShaderStringStream.str(), fragmentShader,
98 GeometryType( GEOMETRY_TYPE_IMAGE),
99 ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
101 DissolveLocalEffect handle( shaderEffect );
102 handle.mNumberOfDimples = numberOfDimples;
104 //Register uniform properties
105 for( unsigned int i = 0; i < numberOfDimples; i++ )
107 handle.SetCenter(i, Vector2(0.f,0.f));
108 handle.SetRadius(i, 0.f);
109 handle.SetDistortion( i, 0.f );
111 handle.SetProperty( ShaderEffect::GRID_DENSITY, Property::Value(5.f) );
112 handle.SetTransparency( 0.5f );
117 unsigned int DissolveLocalEffect::GetNumberOfDimples() const
119 return mNumberOfDimples;
122 void DissolveLocalEffect::SetTransparency( float transparency)
124 SetUniform( TRANSPARENCY_PROPERTY_NAME, transparency );
127 void DissolveLocalEffect::SetCenter( unsigned int index, const Vector2& center )
129 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
130 SetUniform( GetCenterPropertyName( index ), center, ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION );
133 void DissolveLocalEffect::SetRadius( unsigned int index, float radius )
135 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
136 SetUniform( GetRadiusPropertyName( index ), radius );
139 void DissolveLocalEffect::SetDistortion( unsigned int index, float distortion )
141 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
142 SetUniform( GetDistortionPropertyName( index ), distortion );
145 std::string DissolveLocalEffect::GetCenterPropertyName( unsigned int index ) const
147 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
148 std::ostringstream oss;
149 oss<< CENTER_PROPERTY_NAME << "[" << index << "]";
153 std::string DissolveLocalEffect::GetRadiusPropertyName( unsigned int index ) const
155 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
156 std::ostringstream oss;
157 oss<< RADIUS_PROPERTY_NAME << "[" << index << "]";
161 std::string DissolveLocalEffect::GetDistortionPropertyName( unsigned int index ) const
163 DALI_ASSERT_ALWAYS( index < mNumberOfDimples );
164 std::ostringstream oss;
165 oss<< DISTORTION_PROPERTY_NAME << "["<< index << "]";
169 } // namespace Toolkit