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