Property enum name changes in dali-core: Toolkit changes for compiling
[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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <dali-toolkit/public-api/shader-effects/dissolve-effect.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string DISTORTION_PROPERTY_NAME( "uPercentage" );
30
31 } // namespace
32
33 DissolveEffect::DissolveEffect()
34 {
35 }
36
37 //Call the Parent copy constructor to add reference to the implementation for this object
38 DissolveEffect::DissolveEffect(ShaderEffect handle)
39 :ShaderEffect(handle)
40 {
41 }
42
43 DissolveEffect::~DissolveEffect()
44 {
45 }
46
47
48 DissolveEffect DissolveEffect::New( bool useHighPrecision )
49 {
50   std::string prefixHighPrecision( "precision highp float;\n");
51   std::string prefixMediumPrecision( "precision mediump float;\n" );
52   std::string vertexShader(
53     "uniform float uPercentage;\n"
54     "uniform vec3 uSaddleParam;\n"
55     "uniform vec2 uTranslation;\n"
56     "uniform vec2 uRotation; \n"
57     "uniform float uToNext;\n"
58     "varying float vPercentage;\n"
59     "void main()\n"
60     "{\n"
61       "gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
62       "vTexCoord = aTexCoord;\n"
63       //Calculate the distortion value given the dissolve central line
64       "vec2 texCoor = vec2( (aTexCoord.s - sTextureRect.s ) / (sTextureRect.p - sTextureRect.s), (aTexCoord.t- sTextureRect.t)/(sTextureRect.q - sTextureRect.t) ); \n"
65       "vec2 value = texCoor + uTranslation; \n"
66       "mat2 rotateMatrix = mat2( uRotation.s, uRotation.t, -uRotation.t, uRotation.s ); \n"
67       "value = rotateMatrix * value; \n"
68       "if(uToNext == 1.0)  \n"
69       "  value.s = uSaddleParam[2] + value.s; \n"
70       "float delay = value.t*value.t / uSaddleParam[0] - value.s*value.s/uSaddleParam[1];\n"
71       "vPercentage = clamp( uPercentage*2.0 - 0.5*sin(delay*1.571) - 0.5, 0.0, 1.0 ); \n"
72     "}\n");
73   std::string fragmentShader(
74     "varying float vPercentage;\n"
75     "float rand(vec2 co) \n"
76     "{\n"
77     "  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
78     "}\n"
79     "void main()\n"
80     "{\n"
81       //Calculate the randomness
82       "float offsetS = rand( vTexCoord * vPercentage ) * (sTextureRect.p - sTextureRect.s) - vTexCoord.s  + sTextureRect.s; \n"
83       "float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ) * (sTextureRect.q - sTextureRect.t) - vTexCoord.t + sTextureRect.t; \n"
84       "vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
85       "gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
86       "gl_FragColor.a *= 1.0 - vPercentage; \n"
87     "}" );
88
89   // Create the implementation, temporarily owned on stack,
90   Dali::ShaderEffect shaderEffectCustom;
91   if( useHighPrecision )
92   {
93     shaderEffectCustom =  Dali::ShaderEffect::New( prefixHighPrecision+vertexShader, prefixHighPrecision + fragmentShader,
94                                                GeometryType( GEOMETRY_TYPE_IMAGE),
95                                                ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
96   }
97   else
98   {
99     shaderEffectCustom =  Dali::ShaderEffect::New( prefixMediumPrecision+vertexShader, prefixMediumPrecision + fragmentShader,
100                                                GeometryType( GEOMETRY_TYPE_IMAGE),
101                                                ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
102   }
103
104   /* Pass ownership to DissolveEffect through overloaded constructor, So that it now has access to the
105      Dali::ShaderEffect implementation */
106   Dali::Toolkit::DissolveEffect handle( shaderEffectCustom );
107
108   handle.SetUniform( DISTORTION_PROPERTY_NAME, 0.0f );
109   handle.SetProperty( ShaderEffect::Property::GRID_DENSITY, Dali::Property::Value(50.0f) );
110
111   handle.SetCentralLine( Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f) );
112
113   return handle;
114 }
115
116 void DissolveEffect::SetCentralLine( const Vector2& position, const Vector2& displacement )
117 {
118   // the line passes through 'position' and has the direction of 'displacement'
119   float coefA, coefB, coefC; //line equation: Ax+By+C=0;
120   coefA = displacement.y;
121   coefB = -displacement.x;
122   coefC = -displacement.y*position.x + displacement.x*position.y;
123
124   float inversedAABB = 1.f / (coefA*coefA+coefB*coefB);
125   float inversedSqrtAABB = sqrtf(inversedAABB);
126   float saddleA;
127
128   //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
129   //z = y*y/a/a - x*x/b/b
130   //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
131
132   Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
133   Vector2 translation;
134   Vector2 rotation;
135   float toNext = -1.f;
136   if( displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f) )
137   {
138     toNext = 1.f;
139   }
140
141   if( (displacement.y * displacement.x < 0.0f) )
142   {
143     //distance from (0,0) to the line
144     float distanceTopLeft =  fabsf(coefC) * inversedSqrtAABB;
145     //distance from (1, 1 ) to the line
146     float distanceBottomRight = fabsf(coefA+coefB+coefC) * inversedSqrtAABB;
147     saddleA = std::max( distanceTopLeft, distanceBottomRight );
148
149     //foot of a perpendicular: (1,0) to the line
150     float footX1 = ( coefB*coefB - coefA*coefC) * inversedAABB;
151     float footY1 = (-coefA*coefB - coefB*coefC) * inversedAABB;
152     //foot of a perpendicular: (0,1) to the line
153     float footX2 = (-coefA*coefB - coefA*coefC) * inversedAABB;
154     float footY2 = ( coefA*coefA - coefB*coefC) * inversedAABB;
155     saddleParam[1] = (footX1-footX2)*(footX1-footX2) + (footY1-footY2)*(footY1-footY2);
156     translation = Vector2(-footX2,-footY2);
157   }
158   else
159   {
160     //distance from(1,0) to the line
161     float distanceTopRight = fabsf(coefA+coefC) * inversedSqrtAABB;
162     //distance from(0,1) to the line
163     float distanceBottomLeft = fabsf(coefB+coefC) * inversedSqrtAABB;
164     saddleA = std::max( distanceTopRight, distanceBottomLeft );
165     //foot of a perpendicular: (0,0) to the line
166     float footX3 = (-coefA*coefC) * inversedAABB;
167     float footY3 = (-coefB*coefC) * inversedAABB;
168     //foot of a perpendicular: (1.0,1.0) to the line
169     float footX4 = ( coefB*coefB - coefA*coefB - coefA*coefC) * inversedAABB;
170     float footY4 = (-coefA*coefB + coefA*coefA- coefB*coefC) * inversedAABB;
171     saddleParam[1] = (footX3-footX4)*(footX3-footX4) + (footY3-footY4)*(footY3-footY4);
172     translation = Vector2(-footX3, -footY3);
173   }
174
175   saddleParam[2] = sqrtf(saddleParam[1]);
176   saddleParam[0] = saddleA*saddleA;
177   rotation = Vector2(-displacement.x, displacement.y);
178   rotation.Normalize();
179
180   SetUniform( "uSaddleParam", saddleParam );
181   SetUniform( "uTranslation", translation );
182   SetUniform( "uRotation", rotation );
183   SetUniform( "uToNext", toNext );
184
185 }
186
187 void DissolveEffect::SetDistortion( float distortion )
188 {
189   SetUniform( DISTORTION_PROPERTY_NAME, distortion );
190 }
191
192 const std::string& DissolveEffect::GetDistortionPropertyName() const
193 {
194   return DISTORTION_PROPERTY_NAME;
195 }
196
197 } // namespace Toolkit
198
199 } // namespace Dali