[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / dissolve-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <dali-toolkit/public-api/shader-effects/dissolve-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string DISTORTION_PROPERTY_NAME( "uPercentage" );
29
30 } // namespace
31
32 DissolveEffect::DissolveEffect()
33 {
34 }
35
36 //Call the Parent copy constructor to add reference to the implementation for this object
37 DissolveEffect::DissolveEffect(ShaderEffect handle)
38 :ShaderEffect(handle)
39 {
40 }
41
42 DissolveEffect::~DissolveEffect()
43 {
44 }
45
46
47 DissolveEffect DissolveEffect::New( bool useHighPrecision )
48 {
49   std::string prefixHighPrecision( "precision highp float;\n");
50   std::string prefixMediumPrecision( "precision mediump float;\n" );
51   std::string vertexShader(
52     "uniform float uPercentage;\n"
53     "uniform vec3 uSaddleParam;\n"
54     "uniform vec2 uTranslation;\n"
55     "uniform vec2 uRotation; \n"
56     "uniform float uToNext;\n"
57     "varying float vPercentage;\n"
58     "void main()\n"
59     "{\n"
60       "gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
61       "vTexCoord = aTexCoord;\n"
62       //Calculate the distortion value given the dissolve central line
63       "vec2 texCoor = vec2( (aTexCoord.s - sTextureRect.s ) / (sTextureRect.p - sTextureRect.s), (aTexCoord.t- sTextureRect.t)/(sTextureRect.q - sTextureRect.t) ); \n"
64       "vec2 value = texCoor + uTranslation; \n"
65       "mat2 rotateMatrix = mat2( uRotation.s, uRotation.t, -uRotation.t, uRotation.s ); \n"
66       "value = rotateMatrix * value; \n"
67       "if(uToNext == 1.0)  \n"
68       "  value.s = uSaddleParam[2] + value.s; \n"
69       "float delay = value.t*value.t / uSaddleParam[0] - value.s*value.s/uSaddleParam[1];\n"
70       "vPercentage = clamp( uPercentage*2.0 - 0.5*sin(delay*1.571) - 0.5, 0.0, 1.0 ); \n"
71     "}\n");
72   std::string fragmentShader(
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 ) * (sTextureRect.p - sTextureRect.s) - vTexCoord.s  + sTextureRect.s; \n"
82       "float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ) * (sTextureRect.q - sTextureRect.t) - vTexCoord.t + sTextureRect.t; \n"
83       "vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
84       "gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
85       "gl_FragColor.a *= 1.0 - vPercentage; \n"
86     "}" );
87
88   // Create the implementation, temporarily owned on stack,
89   Dali::ShaderEffect shaderEffectCustom;
90   if( useHighPrecision )
91   {
92     shaderEffectCustom =  Dali::ShaderEffect::New( vertexShader, prefixHighPrecision + fragmentShader,
93                                                GeometryType( GEOMETRY_TYPE_IMAGE),
94                                                ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
95   }
96   else
97   {
98     shaderEffectCustom =  Dali::ShaderEffect::New( vertexShader, prefixMediumPrecision + fragmentShader,
99                                                GeometryType( GEOMETRY_TYPE_IMAGE),
100                                                ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
101   }
102
103   /* Pass ownership to DissolveEffect through overloaded constructor, So that it now has access to the
104      Dali::ShaderEffect implementation */
105   Dali::Toolkit::DissolveEffect handle( shaderEffectCustom );
106
107   handle.SetUniform( DISTORTION_PROPERTY_NAME, 0.0f );
108   handle.SetProperty( ShaderEffect::GRID_DENSITY, Property::Value(50.0f) );
109
110   handle.SetCentralLine( Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f) );
111
112   return handle;
113 }
114
115 void DissolveEffect::SetCentralLine( const Vector2& position, const Vector2& displacement )
116 {
117   // the line passes through 'position' and has the direction of 'displacement'
118   float coefA, coefB, coefC; //line equation: Ax+By+C=0;
119   coefA = displacement.y;
120   coefB = -displacement.x;
121   coefC = -displacement.y*position.x + displacement.x*position.y;
122
123   float inversedAABB = 1.f / (coefA*coefA+coefB*coefB);
124   float inversedSqrtAABB = sqrtf(inversedAABB);
125   float saddleA;
126
127   //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
128   //z = y*y/a/a - x*x/b/b
129   //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
130
131   Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
132   Vector2 translation;
133   Vector2 rotation;
134   float toNext = -1.f;
135   if( displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f) )
136   {
137     toNext = 1.f;
138   }
139
140   if( (displacement.y * displacement.x < 0.0f) )
141   {
142     //distance from (0,0) to the line
143     float distanceTopLeft =  fabsf(coefC) * inversedSqrtAABB;
144     //distance from (1, 1 ) to the line
145     float distanceBottomRight = fabsf(coefA+coefB+coefC) * inversedSqrtAABB;
146     saddleA = std::max( distanceTopLeft, distanceBottomRight );
147
148     //foot of a perpendicular: (1,0) to the line
149     float footX1 = ( coefB*coefB - coefA*coefC) * inversedAABB;
150     float footY1 = (-coefA*coefB - coefB*coefC) * inversedAABB;
151     //foot of a perpendicular: (0,1) to the line
152     float footX2 = (-coefA*coefB - coefA*coefC) * inversedAABB;
153     float footY2 = ( coefA*coefA - coefB*coefC) * inversedAABB;
154     saddleParam[1] = (footX1-footX2)*(footX1-footX2) + (footY1-footY2)*(footY1-footY2);
155     translation = Vector2(-footX2,-footY2);
156   }
157   else
158   {
159     //distance from(1,0) to the line
160     float distanceTopRight = fabsf(coefA+coefC) * inversedSqrtAABB;
161     //distance from(0,1) to the line
162     float distanceBottomLeft = fabsf(coefB+coefC) * inversedSqrtAABB;
163     saddleA = std::max( distanceTopRight, distanceBottomLeft );
164     //foot of a perpendicular: (0,0) to the line
165     float footX3 = (-coefA*coefC) * inversedAABB;
166     float footY3 = (-coefB*coefC) * inversedAABB;
167     //foot of a perpendicular: (1.0,1.0) to the line
168     float footX4 = ( coefB*coefB - coefA*coefB - coefA*coefC) * inversedAABB;
169     float footY4 = (-coefA*coefB + coefA*coefA- coefB*coefC) * inversedAABB;
170     saddleParam[1] = (footX3-footX4)*(footX3-footX4) + (footY3-footY4)*(footY3-footY4);
171     translation = Vector2(-footX3, -footY3);
172   }
173
174   saddleParam[2] = sqrtf(saddleParam[1]);
175   saddleParam[0] = saddleA*saddleA;
176   rotation = Vector2(-displacement.x, displacement.y);
177   rotation.Normalize();
178
179   SetUniform( "uSaddleParam", saddleParam );
180   SetUniform( "uTranslation", translation );
181   SetUniform( "uRotation", rotation );
182   SetUniform( "uToNext", toNext );
183
184 }
185
186 void DissolveEffect::SetDistortion( float distortion )
187 {
188   SetUniform( DISTORTION_PROPERTY_NAME, distortion );
189 }
190
191 const std::string& DissolveEffect::GetDistortionPropertyName() const
192 {
193   return DISTORTION_PROPERTY_NAME;
194 }
195
196 } // namespace Toolkit
197
198 } // namespace Dali