e160532aa76f0d3061d27f8b45915c67204820f1
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / dissolve-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_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 <string.h>
23 #include <dali/devel-api/rendering/shader.h>
24
25 namespace
26 {
27   template < typename T>
28   void SafeSetCustomProperty( Dali::Actor& actor, const std::string& name, const T& value )
29   {
30     Dali::Property::Index index = actor.GetPropertyIndex( name );
31     if ( Dali::Property::INVALID_INDEX == index )
32     {
33       index = actor.RegisterProperty( name, value );
34     }
35     else
36     {
37       actor.SetProperty( index, value );
38     }
39   }
40
41   template < typename T>
42   void SafeSetCustomProperty( Dali::Actor& actor, const std::string& name, const T& value, Dali::Property::AccessMode accessMode )
43   {
44     Dali::Property::Index index = actor.GetPropertyIndex( name );
45     if ( Dali::Property::INVALID_INDEX == index )
46     {
47       index = actor.RegisterProperty( name, value, accessMode );
48     }
49     else
50     {
51       actor.SetProperty( index, value );
52     }
53   }
54
55 };
56
57 namespace Dali
58 {
59
60 namespace Toolkit
61 {
62
63 /**
64  * @brief Set the dissolve central line.
65  *
66  * Use one point (position) and one direction ( displacement ) vector to define this line
67  * As we use the texture coordinate as pixel position to calculate random offset,
68  * the line should passing through rectangle {(0,0),(0,1),(1,0),(1,1)},
69  * so make the position parameter with two component values between 0.0 to 1.0
70  * @param[in] position The point ( locates within rectangle {(0,0),(0,1),(1,0),(1,1)} ) passed through by the central line
71  * @param[in] displacement The direction of the central line
72  * @param[in] initialProgress, the normalised initial progress of the shader
73  */
74 inline void DissolveEffectSetCentralLine( Actor& actor, const Vector2& position, const Vector2& displacement, float initialProgress )
75 {
76   // the line passes through 'position' and has the direction of 'displacement'
77     float coefA, coefB, coefC; //line equation: Ax+By+C=0;
78     coefA = displacement.y;
79     coefB = -displacement.x;
80     coefC = -displacement.y*position.x + displacement.x*position.y;
81
82     float inversedAABB = 1.f / (coefA*coefA+coefB*coefB);
83     float inversedSqrtAABB = sqrtf(inversedAABB);
84     float saddleA;
85
86     //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
87     //z = y*y/a/a - x*x/b/b
88     //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
89
90     Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
91     Vector2 translation;
92     Vector2 rotation;
93     float toNext = -1.f;
94     if( displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f) )
95     {
96       toNext = 1.f;
97     }
98
99     if( (displacement.y * displacement.x < 0.0f) )
100     {
101       //distance from (0,0) to the line
102       float distanceTopLeft =  fabsf(coefC) * inversedSqrtAABB;
103       //distance from (1, 1 ) to the line
104       float distanceBottomRight = fabsf(coefA+coefB+coefC) * inversedSqrtAABB;
105       saddleA = std::max( distanceTopLeft, distanceBottomRight );
106
107       //foot of a perpendicular: (1,0) to the line
108       float footX1 = ( coefB*coefB - coefA*coefC) * inversedAABB;
109       float footY1 = (-coefA*coefB - coefB*coefC) * inversedAABB;
110       //foot of a perpendicular: (0,1) to the line
111       float footX2 = (-coefA*coefB - coefA*coefC) * inversedAABB;
112       float footY2 = ( coefA*coefA - coefB*coefC) * inversedAABB;
113       saddleParam[1] = (footX1-footX2)*(footX1-footX2) + (footY1-footY2)*(footY1-footY2);
114       translation = Vector2(-footX2,-footY2);
115     }
116     else
117     {
118       //distance from(1,0) to the line
119       float distanceTopRight = fabsf(coefA+coefC) * inversedSqrtAABB;
120       //distance from(0,1) to the line
121       float distanceBottomLeft = fabsf(coefB+coefC) * inversedSqrtAABB;
122       saddleA = std::max( distanceTopRight, distanceBottomLeft );
123       //foot of a perpendicular: (0,0) to the line
124       float footX3 = (-coefA*coefC) * inversedAABB;
125       float footY3 = (-coefB*coefC) * inversedAABB;
126       //foot of a perpendicular: (1.0,1.0) to the line
127       float footX4 = ( coefB*coefB - coefA*coefB - coefA*coefC) * inversedAABB;
128       float footY4 = (-coefA*coefB + coefA*coefA- coefB*coefC) * inversedAABB;
129       saddleParam[1] = (footX3-footX4)*(footX3-footX4) + (footY3-footY4)*(footY3-footY4);
130       translation = Vector2(-footX3, -footY3);
131     }
132
133     saddleParam[2] = sqrtf(saddleParam[1]);
134     saddleParam[0] = saddleA*saddleA;
135     rotation = Vector2(-displacement.x, displacement.y);
136     rotation.Normalize();
137
138     SafeSetCustomProperty( actor, "uSaddleParam", saddleParam );
139     SafeSetCustomProperty( actor, "uTranslation", translation );
140     SafeSetCustomProperty( actor, "uRotation", rotation );
141     SafeSetCustomProperty( actor, "uToNext", toNext );
142     SafeSetCustomProperty( actor, "uPercentage", initialProgress, Dali::Property::ANIMATABLE );
143 }
144 /**
145  * @brief Create a new Dissolve effect
146  *
147  *  DissolveEffect is a custom shader effect to achieve Dissolve effects in Image actors.
148  *
149  *  Animatable/Constrainable uniforms:
150  *    "uPercentage" - This value is proportional to the distortion applied; a value of zero means no distortion.
151  *
152  *  @param[in] useHighPrecision True if using high precision in fragment shader for fully random noise, false otherwise
153  *  @return The newly created Property::Map with the dissolve effect
154  */
155
156 inline Property::Map CreateDissolveEffect( bool useHighPrecision = true )
157 {
158   const char* prefixHighPrecision( "precision highp float;\n");
159   const char* prefixMediumPrecision( "precision mediump float;\n" );
160
161   const char* vertexShader( DALI_COMPOSE_SHADER(
162     attribute mediump vec2 aPosition;\n
163     \n
164     uniform mediump mat4 uMvpMatrix;\n
165     uniform vec3 uSize;\n
166     uniform vec4 uTextureRect;
167     \n
168     uniform float uPercentage;\n
169     uniform vec3 uSaddleParam;\n
170     uniform vec2 uTranslation;\n
171     uniform vec2 uRotation; \n
172     uniform float uToNext;\n
173     \n
174     varying float vPercentage;\n
175     varying vec2 vTexCoord;\n
176
177     void main()\n
178     {\n
179       mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
180       vertexPosition.xyz *= uSize;\n
181       vertexPosition = uMvpMatrix * vertexPosition;\n
182       gl_Position = vertexPosition;\n
183
184       vec2 texCoord = aPosition + vec2(0.5);
185       vTexCoord = texCoord;\n
186       //Calculate the distortion value given the dissolve central line
187       vec2 value = texCoord + uTranslation; \n
188       mat2 rotateMatrix = mat2( uRotation.s, uRotation.t, -uRotation.t, uRotation.s ); \n
189       value = rotateMatrix * value; \n
190       if(uToNext == 1.0)  \n
191         value.s = uSaddleParam[2] + value.s; \n
192       float delay = value.t*value.t / uSaddleParam[0] - value.s*value.s/uSaddleParam[1];\n
193       vPercentage = clamp( uPercentage*2.0 - 0.5*sin(delay*1.571) - 0.5, 0.0, 1.0 ); \n
194     })
195   );
196
197   const char* fragmentShader( DALI_COMPOSE_SHADER(
198     varying float vPercentage;\n
199     varying mediump vec2 vTexCoord;\n
200     \n
201     uniform sampler2D sTexture;\n
202     uniform lowp vec4 uColor;\n
203     uniform vec4 uTextureRect;
204     \n
205     float rand(vec2 co) \n
206     {\n
207       return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n
208     }\n
209     \n
210     void main()\n
211     {\n
212
213       //Calculate the randomness
214       float offsetS = rand( vTexCoord * vPercentage ) - vTexCoord.s; \n
215       float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ) - vTexCoord.t; \n
216       vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n
217       gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n
218       gl_FragColor.a *= 1.0 - vPercentage; \n
219     } )
220   );
221
222   Property::Map map;
223
224   Property::Map customShader;
225
226   std::string vertexShaderString;
227   std::string fragmentShaderString;
228   if( useHighPrecision )
229   {
230     vertexShaderString.reserve(strlen( prefixHighPrecision ) + strlen( vertexShader ));
231     vertexShaderString.append( prefixHighPrecision );
232
233     fragmentShaderString.reserve(strlen( prefixHighPrecision ) + strlen( fragmentShader ));
234     fragmentShaderString.append( prefixHighPrecision );
235   }
236   else
237   {
238     vertexShaderString.reserve(strlen( prefixMediumPrecision ) + strlen( vertexShader ));
239     vertexShaderString.append( prefixMediumPrecision );
240
241     fragmentShaderString.reserve(strlen( prefixMediumPrecision ) + strlen( fragmentShader ));
242     fragmentShaderString.append( prefixMediumPrecision );
243   }
244
245   vertexShaderString.append( vertexShader );
246   fragmentShaderString.append( fragmentShader );
247
248   customShader[ "vertex-shader" ] = vertexShaderString;
249   customShader[ "fragment-shader" ] = fragmentShaderString;
250
251   customShader[ "subdivide-grid-x" ] = 20;
252   customShader[ "subdivide-grid-y" ] = 20;
253
254   customShader[ "hints" ] = "output-is-transparent";
255
256   map[ "shader" ] = customShader;
257   return map;
258 }
259
260 } // namespace Toolkit
261
262 } // namespace Dali
263
264 #endif // __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__