1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__
5 * Copyright (c) 2015 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.
22 #include <dali/public-api/shader-effects/shader-effect.h>
32 * @brief Create a new DissolveLocalEffect
34 * DissolveLocalEffect is a custom shader effect to achieve Dissolve effects in multiple small areas of Image actors
36 * Animatable/Constrainable uniforms:
38 * "uCenter" - The center positions of each dimples
39 * "uRadius" - The propagation radius of each dimple
40 * "uPercentage" - The distortion applied to the effect texture. A value of zero means no distortion
42 * @param[in] numberOfDimples The number of dimples
43 * @return A handle to a newly allocated ShaderEffect
45 inline ShaderEffect CreateDissolveLocalEffect( unsigned int numberOfDimples )
47 std::ostringstream vertexShaderStringStream;
48 vertexShaderStringStream << "#define NUMBER_OF_DIMPLE "<< numberOfDimples << "\n";
49 std::string vertexShader(
50 "precision highp float;\n"
51 "uniform vec2 uCenter[ NUMBER_OF_DIMPLE ];\n"
52 "uniform float uRadius[ NUMBER_OF_DIMPLE ]; \n"
53 "uniform float uPercentage[ NUMBER_OF_DIMPLE ]; \n"
54 "varying float vPercentage;\n"
57 " vec4 position = uModelView * vec4( aPosition, 1.0 );\n"
58 " float percentage = 0.0;\n"
59 " for( int i=0; i<NUMBER_OF_DIMPLE; ++i )\n"
61 " float distance = distance(uCenter[i], position.xy);\n"
62 " percentage = max(percentage, uPercentage[i] * cos(clamp( distance/uRadius[i], 0.0, 1.0 )*1.57) );"
64 " vPercentage = clamp( percentage, 0.0, 1.0 );\n"
65 " gl_Position = uProjection * position;\n"
66 " vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord );\n"
68 vertexShaderStringStream << vertexShader;
70 std::string fragmentShader(
71 "precision highp float;\n"
72 "uniform float uTransparency;\n"
73 "varying float vPercentage;\n"
74 "float rand(vec2 co) \n"
76 " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
80 //Calculate the randomness
81 " float offsetS = rand( vTexCoord * vPercentage ); \n"
82 " float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ); \n"
83 " vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
84 " gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
85 " gl_FragColor.a *= 1.0 - uTransparency*vPercentage; \n"
88 ShaderEffect shaderEffect = ShaderEffect::New(
89 vertexShaderStringStream.str(), fragmentShader,
90 ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
92 //Register uniform properties
93 std::ostringstream oss;
94 for( unsigned int i = 0; i < numberOfDimples; i++ )
97 oss<< "uCenter["<< i << "]";
98 shaderEffect.SetUniform(oss.str(), Vector2(0.f,0.f));
101 oss<< "uRadius["<< i << "]";
102 shaderEffect.SetUniform(oss.str(), 0.f);
105 oss<< "uPercentage["<< i << "]";
106 shaderEffect.SetUniform( oss.str(), 0.f );
109 shaderEffect.SetProperty( ShaderEffect::Property::GRID_DENSITY, Dali::Property::Value(5.f) );
110 shaderEffect.SetUniform( "uTransparency", 0.5f );
115 } // namespace Toolkit
119 #endif // __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__