Moving visuals into new folder with same level as controls
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / dissolve-local-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/shader-effects/shader-effect.h>
23 #include <sstream>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 /**
32  * @brief Create a new DissolveLocalEffect
33  *
34  * DissolveLocalEffect is a custom shader effect to achieve Dissolve effects in multiple small areas of Image actors
35  *
36  * Animatable/Constrainable uniforms:
37  *  "uTransparency"
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
41  *
42  * @param[in] numberOfDimples The number of dimples
43  * @return A handle to a newly allocated ShaderEffect
44  */
45 inline ShaderEffect CreateDissolveLocalEffect( unsigned int numberOfDimples )
46 {
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"
55       "void main()\n"
56       "{\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"
60       "  {\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) );"
63       "  }\n"
64       "  vPercentage = clamp( percentage, 0.0, 1.0 );\n"
65       "  gl_Position = uProjection * position;\n"
66       "  vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord );\n"
67       "}\n");
68   vertexShaderStringStream << vertexShader;
69
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"
75       "{\n"
76       "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
77       "}\n"
78       "void main()\n"
79       "{\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"
86       "}\n");
87
88   ShaderEffect shaderEffect = ShaderEffect::New(
89       vertexShaderStringStream.str(), fragmentShader,
90       ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
91
92   //Register uniform properties
93   std::ostringstream oss;
94   for( unsigned int i = 0; i < numberOfDimples; i++ )
95   {
96     oss.str("");
97     oss<< "uCenter["<< i << "]";
98     shaderEffect.SetUniform(oss.str(), Vector2(0.f,0.f));
99
100     oss.str("");
101     oss<< "uRadius["<< i << "]";
102     shaderEffect.SetUniform(oss.str(), 0.f);
103
104     oss.str("");
105     oss<< "uPercentage["<< i << "]";
106     shaderEffect.SetUniform( oss.str(), 0.f );
107   }
108
109   shaderEffect.SetProperty( ShaderEffect::Property::GRID_DENSITY, Dali::Property::Value(5.f) );
110   shaderEffect.SetUniform( "uTransparency", 0.5f );
111
112   return shaderEffect;
113 }
114
115 } // namespace Toolkit
116
117 } // namespace Dali
118
119 #endif // __DALI_TOOLKIT_SHADER_EFFECT_LOCAL_DISSOLVE_H__