Merge "Support scroll in web view." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / dissolve-effect.cpp
1 /*
2  * Copyright (c) 2021 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 // CLASS HEADER
18 #include <dali-toolkit/devel-api/shader-effects/dissolve-effect.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/rendering/shader.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 void DissolveEffectSetCentralLine( Actor& actor, const Vector2& position, const Vector2& displacement, float initialProgress )
33 {
34   // the line passes through 'position' and has the direction of 'displacement'
35   float coefA, coefB, coefC; //line equation: Ax+By+C=0;
36   coefA = displacement.y;
37   coefB = -displacement.x;
38   coefC = -displacement.y * position.x + displacement.x * position.y;
39
40   float inversedAABB     = 1.f / (coefA * coefA + coefB * coefB);
41   float inversedSqrtAABB = sqrtf(inversedAABB);
42   float saddleA;
43
44   //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
45   //z = y*y/a/a - x*x/b/b
46   //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
47
48   Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
49   Vector2 translation;
50   Vector2 rotation;
51   float   toNext = -1.f;
52   if(displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f))
53   {
54     toNext = 1.f;
55   }
56
57   if((displacement.y * displacement.x < 0.0f))
58   {
59     //distance from (0,0) to the line
60     float distanceTopLeft = fabsf(coefC) * inversedSqrtAABB;
61     //distance from (1, 1 ) to the line
62     float distanceBottomRight = fabsf(coefA + coefB + coefC) * inversedSqrtAABB;
63     saddleA                   = std::max(distanceTopLeft, distanceBottomRight);
64
65     //foot of a perpendicular: (1,0) to the line
66     float footX1 = (coefB * coefB - coefA * coefC) * inversedAABB;
67     float footY1 = (-coefA * coefB - coefB * coefC) * inversedAABB;
68     //foot of a perpendicular: (0,1) to the line
69     float footX2   = (-coefA * coefB - coefA * coefC) * inversedAABB;
70     float footY2   = (coefA * coefA - coefB * coefC) * inversedAABB;
71     saddleParam[1] = (footX1 - footX2) * (footX1 - footX2) + (footY1 - footY2) * (footY1 - footY2);
72     translation    = Vector2(-footX2, -footY2);
73   }
74   else
75   {
76     //distance from(1,0) to the line
77     float distanceTopRight = fabsf(coefA + coefC) * inversedSqrtAABB;
78     //distance from(0,1) to the line
79     float distanceBottomLeft = fabsf(coefB + coefC) * inversedSqrtAABB;
80     saddleA                  = std::max(distanceTopRight, distanceBottomLeft);
81     //foot of a perpendicular: (0,0) to the line
82     float footX3 = (-coefA * coefC) * inversedAABB;
83     float footY3 = (-coefB * coefC) * inversedAABB;
84     //foot of a perpendicular: (1.0,1.0) to the line
85     float footX4   = (coefB * coefB - coefA * coefB - coefA * coefC) * inversedAABB;
86     float footY4   = (-coefA * coefB + coefA * coefA - coefB * coefC) * inversedAABB;
87     saddleParam[1] = (footX3 - footX4) * (footX3 - footX4) + (footY3 - footY4) * (footY3 - footY4);
88     translation    = Vector2(-footX3, -footY3);
89   }
90
91   saddleParam[2] = sqrtf(saddleParam[1]);
92   saddleParam[0] = saddleA * saddleA;
93   rotation       = Vector2(-displacement.x, displacement.y);
94   rotation.Normalize();
95
96   actor.RegisterProperty("uSaddleParam", saddleParam);
97   actor.RegisterProperty("uTranslation", translation);
98   actor.RegisterProperty("uRotation", rotation);
99   actor.RegisterProperty("uToNext", toNext);
100   actor.RegisterProperty("uPercentage", initialProgress, Dali::Property::ANIMATABLE);
101 }
102
103 Property::Map CreateDissolveEffect( bool useHighPrecision )
104 {
105   const char* prefixHighPrecision("precision highp float;\n");
106   const char* prefixMediumPrecision("precision mediump float;\n");
107
108   const char* vertexShader = SHADER_DISSOLVE_EFFECT_VERT.data();
109   const char* fragmentShader = SHADER_DISSOLVE_EFFECT_FRAG.data();
110
111   Property::Map map;
112
113   Property::Map customShader;
114
115   std::string vertexShaderString;
116   std::string fragmentShaderString;
117   if(useHighPrecision)
118   {
119     vertexShaderString.reserve(strlen(prefixHighPrecision) + strlen(vertexShader));
120     vertexShaderString.append(prefixHighPrecision);
121
122     fragmentShaderString.reserve(strlen(prefixHighPrecision) + strlen(fragmentShader));
123     fragmentShaderString.append(prefixHighPrecision);
124   }
125   else
126   {
127     vertexShaderString.reserve(strlen(prefixMediumPrecision) + strlen(vertexShader));
128     vertexShaderString.append(prefixMediumPrecision);
129
130     fragmentShaderString.reserve(strlen(prefixMediumPrecision) + strlen(fragmentShader));
131     fragmentShaderString.append(prefixMediumPrecision);
132   }
133
134   vertexShaderString.append(vertexShader);
135   fragmentShaderString.append(fragmentShader);
136
137   customShader[Visual::Shader::Property::VERTEX_SHADER]   = vertexShaderString;
138   customShader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShaderString;
139
140   customShader[Visual::Shader::Property::SUBDIVIDE_GRID_X] = 20;
141   customShader[Visual::Shader::Property::SUBDIVIDE_GRID_Y] = 20;
142
143   customShader[Visual::Shader::Property::HINTS] = Shader::Hint::OUTPUT_IS_TRANSPARENT;
144
145   map[Toolkit::Visual::Property::SHADER] = customShader;
146   return map;
147 }
148
149 } // namespace Toolkit
150
151 } // namespace Dali