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