Merge branch 'devel/new_mesh' into devel/master
[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 <dali/public-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 /**
31  * @brief Set the dissolve central line.
32  *
33  * Use one point (position) and one direction ( displacement ) vector to define this line
34  * As we use the texture coordinate as pixel position to calculate random offset,
35  * the line should passing through rectangle {(0,0),(0,1),(1,0),(1,1)},
36  * so make the position parameter with two component values between 0.0 to 1.0
37  * @param[in] dissolveEffect The shader effect
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  */
41 inline void DissolveEffectSetCentralLine( ShaderEffect& dissolveEffect, const Vector2& position, const Vector2& displacement )
42 {
43   // the line passes through 'position' and has the direction of 'displacement'
44     float coefA, coefB, coefC; //line equation: Ax+By+C=0;
45     coefA = displacement.y;
46     coefB = -displacement.x;
47     coefC = -displacement.y*position.x + displacement.x*position.y;
48
49     float inversedAABB = 1.f / (coefA*coefA+coefB*coefB);
50     float inversedSqrtAABB = sqrtf(inversedAABB);
51     float saddleA;
52
53     //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
54     //z = y*y/a/a - x*x/b/b
55     //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
56
57     Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
58     Vector2 translation;
59     Vector2 rotation;
60     float toNext = -1.f;
61     if( displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f) )
62     {
63       toNext = 1.f;
64     }
65
66     if( (displacement.y * displacement.x < 0.0f) )
67     {
68       //distance from (0,0) to the line
69       float distanceTopLeft =  fabsf(coefC) * inversedSqrtAABB;
70       //distance from (1, 1 ) to the line
71       float distanceBottomRight = fabsf(coefA+coefB+coefC) * inversedSqrtAABB;
72       saddleA = std::max( distanceTopLeft, distanceBottomRight );
73
74       //foot of a perpendicular: (1,0) to the line
75       float footX1 = ( coefB*coefB - coefA*coefC) * inversedAABB;
76       float footY1 = (-coefA*coefB - coefB*coefC) * inversedAABB;
77       //foot of a perpendicular: (0,1) to the line
78       float footX2 = (-coefA*coefB - coefA*coefC) * inversedAABB;
79       float footY2 = ( coefA*coefA - coefB*coefC) * inversedAABB;
80       saddleParam[1] = (footX1-footX2)*(footX1-footX2) + (footY1-footY2)*(footY1-footY2);
81       translation = Vector2(-footX2,-footY2);
82     }
83     else
84     {
85       //distance from(1,0) to the line
86       float distanceTopRight = fabsf(coefA+coefC) * inversedSqrtAABB;
87       //distance from(0,1) to the line
88       float distanceBottomLeft = fabsf(coefB+coefC) * inversedSqrtAABB;
89       saddleA = std::max( distanceTopRight, distanceBottomLeft );
90       //foot of a perpendicular: (0,0) to the line
91       float footX3 = (-coefA*coefC) * inversedAABB;
92       float footY3 = (-coefB*coefC) * inversedAABB;
93       //foot of a perpendicular: (1.0,1.0) to the line
94       float footX4 = ( coefB*coefB - coefA*coefB - coefA*coefC) * inversedAABB;
95       float footY4 = (-coefA*coefB + coefA*coefA- coefB*coefC) * inversedAABB;
96       saddleParam[1] = (footX3-footX4)*(footX3-footX4) + (footY3-footY4)*(footY3-footY4);
97       translation = Vector2(-footX3, -footY3);
98     }
99
100     saddleParam[2] = sqrtf(saddleParam[1]);
101     saddleParam[0] = saddleA*saddleA;
102     rotation = Vector2(-displacement.x, displacement.y);
103     rotation.Normalize();
104
105     dissolveEffect.SetUniform( "uSaddleParam", saddleParam );
106     dissolveEffect.SetUniform( "uTranslation", translation );
107     dissolveEffect.SetUniform( "uRotation", rotation );
108     dissolveEffect.SetUniform( "uToNext", toNext );
109 }
110 /**
111  * @brief Create a new Dissolve effect
112  *
113  *  DissolveEffect is a custom shader effect to achieve Dissolve effects in Image actors.
114  *
115  *  Animatable/Constrainable uniforms:
116  *    "uPercentage" - This value is proportional to the distortion applied; a value of zero means no distortion.
117  *
118  *  @param[in] useHighPrecision True if using high precision in fragment shader for fully random noise, false otherwise
119  *  @return A handle to a newly allocated ShaderEffect
120  */
121
122 inline ShaderEffect CreateDissolveEffect(bool useHighPrecision = true)
123 {
124   std::string prefixHighPrecision( "precision highp float;\n");
125     std::string prefixMediumPrecision( "precision mediump float;\n" );
126     std::string vertexShader(
127       "uniform float uPercentage;\n"
128       "uniform vec3 uSaddleParam;\n"
129       "uniform vec2 uTranslation;\n"
130       "uniform vec2 uRotation; \n"
131       "uniform float uToNext;\n"
132       "varying float vPercentage;\n"
133       "void main()\n"
134       "{\n"
135         "gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
136         "vTexCoord = aTexCoord;\n"
137         //Calculate the distortion value given the dissolve central line
138         "vec2 texCoor = vec2( (aTexCoord.s - sTextureRect.s ) / (sTextureRect.p - sTextureRect.s), (aTexCoord.t- sTextureRect.t)/(sTextureRect.q - sTextureRect.t) ); \n"
139         "vec2 value = texCoor + uTranslation; \n"
140         "mat2 rotateMatrix = mat2( uRotation.s, uRotation.t, -uRotation.t, uRotation.s ); \n"
141         "value = rotateMatrix * value; \n"
142         "if(uToNext == 1.0)  \n"
143         "  value.s = uSaddleParam[2] + value.s; \n"
144         "float delay = value.t*value.t / uSaddleParam[0] - value.s*value.s/uSaddleParam[1];\n"
145         "vPercentage = clamp( uPercentage*2.0 - 0.5*sin(delay*1.571) - 0.5, 0.0, 1.0 ); \n"
146       "}\n");
147     std::string fragmentShader(
148       "varying float vPercentage;\n"
149       "float rand(vec2 co) \n"
150       "{\n"
151       "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
152       "}\n"
153       "void main()\n"
154       "{\n"
155         //Calculate the randomness
156         "float offsetS = rand( vTexCoord * vPercentage ) * (sTextureRect.p - sTextureRect.s) - vTexCoord.s  + sTextureRect.s; \n"
157         "float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ) * (sTextureRect.q - sTextureRect.t) - vTexCoord.t + sTextureRect.t; \n"
158         "vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
159         "gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
160         "gl_FragColor.a *= 1.0 - vPercentage; \n"
161       "}" );
162
163     // Create the implementation, temporarily owned on stack,
164     Dali::ShaderEffect shaderEffect;
165     if( useHighPrecision )
166     {
167       shaderEffect =  Dali::ShaderEffect::New(
168           prefixHighPrecision+vertexShader, prefixHighPrecision + fragmentShader,
169           ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
170     }
171     else
172     {
173       shaderEffect =  Dali::ShaderEffect::New(
174           prefixMediumPrecision+vertexShader, prefixMediumPrecision + fragmentShader,
175           ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
176     }
177
178     shaderEffect.SetUniform( "uPercentage", 0.0f );
179     shaderEffect.SetProperty( ShaderEffect::Property::GRID_DENSITY, Dali::Property::Value(50.0f) );
180
181     DissolveEffectSetCentralLine( shaderEffect, Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f) );
182
183     return shaderEffect;
184
185 }
186
187 } // namespace Toolkit
188
189 } // namespace Dali
190
191 #endif // __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__