2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/devel-api/shader-effects/dissolve-effect.h>
21 #include <dali/public-api/rendering/shader.h>
24 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
32 void DissolveEffectSetCentralLine( Actor& actor, const Vector2& position, const Vector2& displacement, float initialProgress )
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;
40 float inversedAABB = 1.f / (coefA * coefA + coefB * coefB);
41 float inversedSqrtAABB = sqrtf(inversedAABB);
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
48 Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
52 if(displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f))
57 if((displacement.y * displacement.x < 0.0f))
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);
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);
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);
91 saddleParam[2] = sqrtf(saddleParam[1]);
92 saddleParam[0] = saddleA * saddleA;
93 rotation = Vector2(-displacement.x, displacement.y);
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);
103 Property::Map CreateDissolveEffect( bool useHighPrecision )
105 const char* prefixHighPrecision("precision highp float;\n");
106 const char* prefixMediumPrecision("precision mediump float;\n");
108 const char* vertexShader = SHADER_DISSOLVE_EFFECT_VERT.data();
109 const char* fragmentShader = SHADER_DISSOLVE_EFFECT_FRAG.data();
113 Property::Map customShader;
115 std::string vertexShaderString;
116 std::string fragmentShaderString;
119 vertexShaderString.reserve(strlen(prefixHighPrecision) + strlen(vertexShader));
120 vertexShaderString.append(prefixHighPrecision);
122 fragmentShaderString.reserve(strlen(prefixHighPrecision) + strlen(fragmentShader));
123 fragmentShaderString.append(prefixHighPrecision);
127 vertexShaderString.reserve(strlen(prefixMediumPrecision) + strlen(vertexShader));
128 vertexShaderString.append(prefixMediumPrecision);
130 fragmentShaderString.reserve(strlen(prefixMediumPrecision) + strlen(fragmentShader));
131 fragmentShaderString.append(prefixMediumPrecision);
134 vertexShaderString.append(vertexShader);
135 fragmentShaderString.append(fragmentShader);
137 customShader[Visual::Shader::Property::VERTEX_SHADER] = vertexShaderString;
138 customShader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShaderString;
140 customShader[Visual::Shader::Property::SUBDIVIDE_GRID_X] = 20;
141 customShader[Visual::Shader::Property::SUBDIVIDE_GRID_Y] = 20;
143 customShader[Visual::Shader::Property::HINTS] = Shader::Hint::OUTPUT_IS_TRANSPARENT;
145 map[Toolkit::Visual::Property::SHADER] = customShader;
149 } // namespace Toolkit