1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/shader-effects/shader-effect.h>
31 * @brief Set the dissolve central line.
33 * Use one point (position) and one direction ( displacement ) vector to define this line
34 * As we use the texture coordinate as pixel position to calculate random offset,
35 * the line should passing through rectangle {(0,0),(0,1),(1,0),(1,1)},
36 * so make the position parameter with two component values between 0.0 to 1.0
37 * @param[in] dissolveEffect The shader effect
38 * @param[in] position The point ( locates within rectangle {(0,0),(0,1),(1,0),(1,1)} ) passed through by the central line
39 * @param[in] displacement The direction of the central line
41 inline void DissolveEffectSetCentralLine( ShaderEffect& dissolveEffect, const Vector2& position, const Vector2& displacement )
43 // the line passes through 'position' and has the direction of 'displacement'
44 float coefA, coefB, coefC; //line equation: Ax+By+C=0;
45 coefA = displacement.y;
46 coefB = -displacement.x;
47 coefC = -displacement.y*position.x + displacement.x*position.y;
49 float inversedAABB = 1.f / (coefA*coefA+coefB*coefB);
50 float inversedSqrtAABB = sqrtf(inversedAABB);
53 //saddle surface(Hyperbolic paraboloid)function, used to calculate the dissolve starting time
54 //z = y*y/a/a - x*x/b/b
55 //with our selection of parameters(a and b), this value for any texture coordinate is between -1.0 and 1.0
57 Vector3 saddleParam; // [0]: a*a, [1]: b*b, [2] b
61 if( displacement.x > 0.f || (EqualsZero(displacement.x) && displacement.y > 0.f) )
66 if( (displacement.y * displacement.x < 0.0f) )
68 //distance from (0,0) to the line
69 float distanceTopLeft = fabsf(coefC) * inversedSqrtAABB;
70 //distance from (1, 1 ) to the line
71 float distanceBottomRight = fabsf(coefA+coefB+coefC) * inversedSqrtAABB;
72 saddleA = std::max( distanceTopLeft, distanceBottomRight );
74 //foot of a perpendicular: (1,0) to the line
75 float footX1 = ( coefB*coefB - coefA*coefC) * inversedAABB;
76 float footY1 = (-coefA*coefB - coefB*coefC) * inversedAABB;
77 //foot of a perpendicular: (0,1) to the line
78 float footX2 = (-coefA*coefB - coefA*coefC) * inversedAABB;
79 float footY2 = ( coefA*coefA - coefB*coefC) * inversedAABB;
80 saddleParam[1] = (footX1-footX2)*(footX1-footX2) + (footY1-footY2)*(footY1-footY2);
81 translation = Vector2(-footX2,-footY2);
85 //distance from(1,0) to the line
86 float distanceTopRight = fabsf(coefA+coefC) * inversedSqrtAABB;
87 //distance from(0,1) to the line
88 float distanceBottomLeft = fabsf(coefB+coefC) * inversedSqrtAABB;
89 saddleA = std::max( distanceTopRight, distanceBottomLeft );
90 //foot of a perpendicular: (0,0) to the line
91 float footX3 = (-coefA*coefC) * inversedAABB;
92 float footY3 = (-coefB*coefC) * inversedAABB;
93 //foot of a perpendicular: (1.0,1.0) to the line
94 float footX4 = ( coefB*coefB - coefA*coefB - coefA*coefC) * inversedAABB;
95 float footY4 = (-coefA*coefB + coefA*coefA- coefB*coefC) * inversedAABB;
96 saddleParam[1] = (footX3-footX4)*(footX3-footX4) + (footY3-footY4)*(footY3-footY4);
97 translation = Vector2(-footX3, -footY3);
100 saddleParam[2] = sqrtf(saddleParam[1]);
101 saddleParam[0] = saddleA*saddleA;
102 rotation = Vector2(-displacement.x, displacement.y);
103 rotation.Normalize();
105 dissolveEffect.SetUniform( "uSaddleParam", saddleParam );
106 dissolveEffect.SetUniform( "uTranslation", translation );
107 dissolveEffect.SetUniform( "uRotation", rotation );
108 dissolveEffect.SetUniform( "uToNext", toNext );
111 * @brief Create a new Dissolve effect
113 * DissolveEffect is a custom shader effect to achieve Dissolve effects in Image actors.
115 * Animatable/Constrainable uniforms:
116 * "uPercentage" - This value is proportional to the distortion applied; a value of zero means no distortion.
118 * @param[in] useHighPrecision True if using high precision in fragment shader for fully random noise, false otherwise
119 * @return A handle to a newly allocated ShaderEffect
122 inline ShaderEffect CreateDissolveEffect(bool useHighPrecision = true)
124 std::string prefixHighPrecision( "precision highp float;\n");
125 std::string prefixMediumPrecision( "precision mediump float;\n" );
126 std::string vertexShader(
127 "uniform float uPercentage;\n"
128 "uniform vec3 uSaddleParam;\n"
129 "uniform vec2 uTranslation;\n"
130 "uniform vec2 uRotation; \n"
131 "uniform float uToNext;\n"
132 "varying float vPercentage;\n"
135 "gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
136 "vTexCoord = aTexCoord;\n"
137 //Calculate the distortion value given the dissolve central line
138 "vec2 texCoor = vec2( (aTexCoord.s - sTextureRect.s ) / (sTextureRect.p - sTextureRect.s), (aTexCoord.t- sTextureRect.t)/(sTextureRect.q - sTextureRect.t) ); \n"
139 "vec2 value = texCoor + uTranslation; \n"
140 "mat2 rotateMatrix = mat2( uRotation.s, uRotation.t, -uRotation.t, uRotation.s ); \n"
141 "value = rotateMatrix * value; \n"
142 "if(uToNext == 1.0) \n"
143 " value.s = uSaddleParam[2] + value.s; \n"
144 "float delay = value.t*value.t / uSaddleParam[0] - value.s*value.s/uSaddleParam[1];\n"
145 "vPercentage = clamp( uPercentage*2.0 - 0.5*sin(delay*1.571) - 0.5, 0.0, 1.0 ); \n"
147 std::string fragmentShader(
148 "varying float vPercentage;\n"
149 "float rand(vec2 co) \n"
151 " return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); \n"
155 //Calculate the randomness
156 "float offsetS = rand( vTexCoord * vPercentage ) * (sTextureRect.p - sTextureRect.s) - vTexCoord.s + sTextureRect.s; \n"
157 "float offsetT = rand( vec2(vTexCoord.t*vPercentage, vTexCoord.s * vPercentage) ) * (sTextureRect.q - sTextureRect.t) - vTexCoord.t + sTextureRect.t; \n"
158 "vec2 lookupCoord = vTexCoord + vec2(offsetS, offsetT) * vPercentage; \n"
159 "gl_FragColor = texture2D( sTexture, lookupCoord ) * uColor; \n"
160 "gl_FragColor.a *= 1.0 - vPercentage; \n"
163 // Create the implementation, temporarily owned on stack,
164 Dali::ShaderEffect shaderEffect;
165 if( useHighPrecision )
167 shaderEffect = Dali::ShaderEffect::New(
168 prefixHighPrecision+vertexShader, prefixHighPrecision + fragmentShader,
169 ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
173 shaderEffect = Dali::ShaderEffect::New(
174 prefixMediumPrecision+vertexShader, prefixMediumPrecision + fragmentShader,
175 ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ) );
178 shaderEffect.SetUniform( "uPercentage", 0.0f );
179 shaderEffect.SetProperty( ShaderEffect::Property::GRID_DENSITY, Dali::Property::Value(50.0f) );
181 DissolveEffectSetCentralLine( shaderEffect, Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f) );
187 } // namespace Toolkit
191 #endif // __DALI_TOOLKIT_SHADER_EFFECT_DISSOLVE_H__