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