2 * Copyright (c) 2015 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.
20 #include <dali/public-api/animation/constraint.h>
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/object/property-map.h>
25 #include <dali-toolkit/internal/controls/page-turn-view/page-turn-effect.h>
28 using namespace Dali::Toolkit;
32 #define DALI_COMPOSE_SHADER(STR) #STR
33 const char * const PROPERTY_COMMON_PARAMETERS( "uCommonParameters" );
34 const char * const PROPERTY_ORIGINAL_CENTER( "originalCenter" );
35 const char * const PROPERTY_CURRENT_CENTER( "currentCenter" );
39 * This constraint updates the common parameter values used by every vertex.
40 * By using constraint, they are calculate once in CPU then pass into the vertex shader as uniforms
42 struct CommonParametersConstraint
44 CommonParametersConstraint( float pageHeight )
45 : mPageHeight( pageHeight )
48 void operator()( Dali::Matrix& current, const PropertyInputContainer& inputs )
50 const Vector2& originalCenter = inputs[0]->GetVector2();
51 Vector2 currentCenter = inputs[1]->GetVector2();
53 // calculate the curve direction and the vanishing point
54 // here, the vanishing point is the intersection of spine with the line passing through original center and vertical to curve direction
55 Vector2 curveDirection( currentCenter - originalCenter );
56 curveDirection.Normalize();
57 if( fabs(curveDirection.y) < 0.01f) // eliminate the possibility of division by zero in the next step
59 curveDirection.y = 0.01f;
61 float vanishingPointY = originalCenter.y + curveDirection.x * originalCenter.x / curveDirection.y;
63 float curveEndY, cosTheta ,sinTheta ,translateX, translateY;
64 // when the vanishing point is very far away, make it infinitely, in this case, the page bent horizontally
65 const float THRESHOLD(20.0);
66 if( fabs(vanishingPointY-mPageHeight*0.5f) >= mPageHeight*THRESHOLD )
68 curveDirection = Vector2(-1.f,0.f);
69 currentCenter.y = originalCenter.y;
71 curveEndY = originalCenter.y;
74 translateX = currentCenter.x - originalCenter.x;
75 translateY = vanishingPointY;
79 curveEndY = currentCenter.y - curveDirection.y * (currentCenter.x/curveDirection.x) ;
80 Vector2 v1( currentCenter.x, currentCenter.y - vanishingPointY );
82 Vector2 v2( originalCenter.x, originalCenter.y - vanishingPointY );
84 cosTheta = v1.x*v2.x + v1.y*v2.y;
85 sinTheta = ( vanishingPointY > mPageHeight*0.5f ) ? sqrt(1.0-cosTheta*cosTheta) : -sqrt(1.0-cosTheta*cosTheta);
86 translateX = currentCenter.x - cosTheta*originalCenter.x - sinTheta*( originalCenter.y-vanishingPointY );
87 translateY = currentCenter.y + sinTheta*originalCenter.x - cosTheta*( originalCenter.y-vanishingPointY );
90 float originalLength = fabs(originalCenter.x/curveDirection.x);
91 float currentLength = fabs(currentCenter.x/curveDirection.x);
92 float curveHeight = 0.45f*sqrt(originalLength*originalLength - currentLength*currentLength);
94 float* parameterArray = current.AsFloat();
95 parameterArray[0] = cosTheta;
96 parameterArray[1] = -sinTheta;
97 parameterArray[2] = originalCenter.x;
98 parameterArray[3] = originalCenter.y;
99 parameterArray[4] = sinTheta;
100 parameterArray[5] = cosTheta;
101 parameterArray[6] = currentCenter.x;
102 parameterArray[7] = currentCenter.y;
103 parameterArray[8] = translateX;
104 parameterArray[9] = translateY;
105 parameterArray[10] = vanishingPointY;
106 parameterArray[11] = curveEndY;
107 parameterArray[12] = curveDirection.x;
108 parameterArray[13] = curveDirection.y;
109 parameterArray[14] = curveHeight;
110 parameterArray[15] = currentLength;
116 void Dali::Toolkit::Internal::PageTurnApplyInternalConstraint( Actor& actor, float pageHeight )
118 Constraint constraint = Constraint::New<Dali::Matrix>( actor, actor.GetPropertyIndex( PROPERTY_COMMON_PARAMETERS ) , CommonParametersConstraint( pageHeight ) );
119 constraint.AddSource( LocalSource( actor.GetPropertyIndex( PROPERTY_ORIGINAL_CENTER ) ) );
120 constraint.AddSource( LocalSource( actor.GetPropertyIndex( PROPERTY_CURRENT_CENTER ) ) );
124 Property::Map Dali::Toolkit::Internal::CreatePageTurnEffect()
126 const char* vertexShader = DALI_COMPOSE_SHADER(
128 * The common parameters for all the vertices, calculate in CPU then pass into the shader as uniforms
130 * first part of the page, (outside the the line passing through original center and vertical to curve direction)
131 * no Z change, only 2D rotation and translation
132 * ([0][0],[0][1],[1][0],[1][1]) mat2 rotateMatrix
133 * ([2][0],[2][1]) vec2 translationVector
135 * ([0][2],[0][3]) vec2 originalCenter: Typically the press down position of the Pan Gesture
136 * ([1][2],[1][3]) vec2 currentCenter: Typically the current position of the Pan Gesture
137 * ([3][0],[3][1]) vec2 curveDirection: The normalized vector pointing from original center to current center
138 * ([2][2]) float vanishingPointY: The Y coordinate of the intersection of the spine
139 * and the line which goes through the original center and is vertical to the curveDirection
140 * ([2][3]) float curveEndY: The Y coordinate of intersection of the spine and the line through both original and current center
141 * ([3][2]) float curveHeight: The height of the interpolated hermite curve.
142 * ([3][3]) float currentLength: The length from the current center to the curveEnd.
144 precision mediump float;\n
146 attribute mediump vec2 aPosition;\n
148 uniform mediump mat4 uMvpMatrix;\n
149 uniform mediump mat3 uNormalMatrix;\n
150 uniform mediump mat4 uModelView;\n
152 uniform mat4 uCommonParameters;\n
154 uniform vec3 uSize;\n
155 uniform float uIsTurningBack;\n
156 uniform float uTextureWidth;\n
157 varying vec3 vNormal;\n
158 varying vec4 vPosition;\n
159 varying mediump vec2 vTexCoord;\n
163 vec4 position = vec4( aPosition*uSize.xy, 0.0, 1.0);\n
164 vec2 currentCenter = vec2( uCommonParameters[1][2], uCommonParameters[1][3]);\n
165 vec2 originalCenter = vec2( uCommonParameters[0][2], uCommonParameters[0][3]);\n
166 vec3 normal = vec3(0.0,0.0,1.0);\n
168 if(currentCenter.x < originalCenter.x)\n
170 // change the coordinate origin from the center of the page to its top-left
171 position.xy += uSize.xy * 0.5;\n
172 vec2 curveDirection = vec2( uCommonParameters[3]);\n
173 vec3 vanishingPoint = vec3(0.0, uCommonParameters[2][2], 0.0);\n
174 // first part of the page, (outside the the line passing through original center and vertical to curve direction)
175 //no Z change, only 2D rotation and translation
176 if( dot(curveDirection, position.xy - originalCenter) < 0.0 )
178 position.y -= vanishingPoint.y;\n
179 position.xy = mat2(uCommonParameters)*position.xy + vec2( uCommonParameters[2]);\n
181 // second part of the page, bent as a ruled surface
184 // calculate on the flat plane, between
185 // the first line passing through current vertex and vanishing point
186 // the second line passing through original center and current center
187 vec2 curveEnd = vec2( 0.0, uCommonParameters[2][3] );\n
188 vec2 curFlatDirection = vec2(0.0,1.0);\n
189 float lengthFromCurve = position.y - originalCenter.y;\n
190 float lengthOnCurve = position.x;\n
191 if(currentCenter.y != originalCenter.y)\n
193 curFlatDirection = normalize(position.xy - vanishingPoint.xy);\n
194 lengthFromCurve = (curveEnd.x*curveDirection.y-curveEnd.y*curveDirection.x-position.x*curveDirection.y+position.y*curveDirection.x)
195 / (curFlatDirection.x*curveDirection.y-curFlatDirection.y*curveDirection.x);\n
196 lengthOnCurve = length(position.xy+lengthFromCurve*curFlatDirection-curveEnd);\n
199 // define the control points of hermite curve, composed with two segments
200 // calculation is carried out on the 2D plane which is passing through both current and original center and vertical to the image plane
201 float currentLength = uCommonParameters[3][3];\n
202 float originalLength = abs(originalCenter.x/curveDirection.x);\n
203 float height = uCommonParameters[3][2];\n
204 float percentage = currentLength/originalLength;\n
205 //vec2 SegmentOneControlPoint0 = vec2(0.0, 0.0);
206 vec2 SegmentOneControlPoint1 = vec2((0.65*percentage - 0.15)*originalLength, (0.8 + 0.2 * percentage)*height); \n
207 vec2 SegmentTwoControlPoint0 = SegmentOneControlPoint1;\n
208 vec2 SegmentTwoControlPoint1 = vec2(currentLength, 0.0); \n
209 vec2 SegmentOneTangentVector0 = SegmentOneControlPoint1;\n
210 vec2 SegmentOneTangentVector1 = vec2(0.5*originalLength,0.0);\n
211 vec2 SegmentTwoTangentVector0 = SegmentOneTangentVector1;\n
212 vec2 SegmentTwoTangentVector1 = SegmentOneTangentVector1;\n
214 // calculate the corresponding curve point position and its tangent vector
215 // it is a linear mapping onto nonlinear curves, might cause some unwanted deformation
216 // but as there are no analytical method to calculate the curve length on arbitrary segment
217 // no efficient way to solve this nonlinear mapping, Numerical approximation would cost too much computation in shader
220 float t0 = lengthOnCurve / originalLength;\n
226 curvePoint2D = (-2.0*t_3+3.0*t_2)*SegmentOneControlPoint1
227 + (t_3-2.0*t_2+t)*SegmentOneTangentVector0 + (t_3-t_2)*SegmentOneTangentVector1;\n
228 tangent = (-6.0*t_2+6.0*t)*SegmentOneControlPoint1
229 + (3.0*t_2-4.0*t+1.0)*SegmentOneTangentVector0 + (3.0*t_2-2.0*t)*SegmentOneTangentVector1;\n
233 float t = 2.0*t0-1.0;\n
236 curvePoint2D = (2.0*t_3-3.0*t_2+1.0)*SegmentTwoControlPoint0 + (-2.0*t_3+3.0*t_2)*SegmentTwoControlPoint1
237 + (t_3-2.0*t_2+t)*SegmentTwoTangentVector0 + (t_3-t_2)*SegmentTwoTangentVector1;\n
238 tangent = (6.0*t_2-6.0*t)*SegmentTwoControlPoint0 + (-6.0*t_2+6.0*t)*SegmentTwoControlPoint1
239 + (3.0*t_2-4.0*t+1.0)*SegmentTwoTangentVector0 + (3.0*t_2-2.0*t)*SegmentTwoTangentVector1;\n
240 // a trick to eliminate some optical illusion caused by the gradient matter of normal in per-fragment shading
241 // which is caused by linear interpolation of normal vs. nonlinear lighting
242 // will notice some artifact in the areas with dramatically normal changes, so compress the normal differences here
243 tangent.y *= min(1.0, length(position.xyz - vanishingPoint) / uSize.y ); \n
245 vec3 curvePoint = vec3(curveEnd - curvePoint2D.x*curveDirection,max(0.0,curvePoint2D.y));\n
246 vec3 tangentVector = vec3(-tangent.x*curveDirection,tangent.y);\n
248 // locate the new vertex position on the line passing through both vanishing point and the calculated curve point position
249 vec3 curLiftDirection = vec3(0.0,-1.0,0.0);\n
250 if(currentCenter.y != originalCenter.y)\n
252 curLiftDirection = normalize(curvePoint - vanishingPoint);\n
253 tangentVector *= (curveDirection.y > 0.0) ? -1.0 : 1.0;\n
254 // an heuristic adjustment here, to compensate the linear parameter mapping onto the nonlinear curve
255 float Y0 = position.y - curveDirection.y * (position.x/curveDirection.x); \n
258 if(abs(Y0-vanishingPoint.y) > abs(curveEnd.y-vanishingPoint.y)) \n
260 proportion = abs(curveEnd.y - Y0) / (abs(curveEnd.y-Y0)+abs(curveEnd.y - vanishingPoint.y)); \n
261 refLength = proportion*length(originalCenter-vanishingPoint.xy) / (proportion-1.0); \n
265 proportion = abs(curveEnd.y - Y0) / abs(curveEnd.y - vanishingPoint.y);\n
266 refLength = proportion*length(originalCenter-vanishingPoint.xy); \n
268 float Y1 = currentCenter.y - (normalize(currentCenter-vanishingPoint.xy)).y * refLength; \n
269 position.y = mix(Y0, Y1, t0); \n
271 position.xz = curvePoint.xz - lengthFromCurve*curLiftDirection.xz;\n
272 // calculate the normal vector, will be used for lighting
273 normal = cross(curLiftDirection, normalize(tangentVector));\n
274 // the signature of Z is decided by the page turning direction:
275 // from left to right(negative); from right to left (positive)
276 position.z *= -uIsTurningBack;\n
277 normal.xy *= -uIsTurningBack;\n
279 // change the coordinate origin from the top-left of the page to its center
280 position.xy -= uSize.xy * 0.5; \n
282 vNormal = uNormalMatrix * normal;\n
283 gl_Position = uMvpMatrix * position;
284 // varying parameters for fragment shader
285 vTexCoord = aPosition + vec2(0.5);\n
286 vTexCoord.x /= uTextureWidth;
287 vPosition = uModelView * position;\n
291 const char* fragmentShader = DALI_COMPOSE_SHADER(
292 precision mediump float;\n
294 varying mediump vec2 vTexCoord;\n
296 uniform sampler2D sTexture;\n
297 uniform lowp vec4 uColor;\n
298 uniform vec3 uSize;\n
299 uniform vec2 uSpineShadowParameter;\n
300 varying vec3 vNormal;\n
301 varying vec4 vPosition;\n
305 // need to re-normalize the interpolated normal
306 vec3 normal = normalize( vNormal );\n
307 // display page content
309 // display back image of the page, flip the texture
310 if( dot(vPosition.xyz, normal) > 0.0 ) texel = texture2D( sTexture, vec2( 1.0 - vTexCoord.x, vTexCoord.y ) );\n
311 // display front image of the page
312 else texel = texture2D( sTexture, vTexCoord );\n
314 // display book spine, a stripe of shadowed texture
315 float pixelPos = vTexCoord.x * uSize.x; \n
316 float spineShadowCoef = 1.0; \n
317 if( pixelPos < uSpineShadowParameter.x ) \n
319 float x = pixelPos - uSpineShadowParameter.x;\n
320 float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n
321 spineShadowCoef = normalize( vec2( uSpineShadowParameter.y*x/uSpineShadowParameter.x, y ) ).y;\n
323 // calculate the lighting
324 // set the ambient color as vec3(0.4);
325 float lightColor = abs( normal.z ) * 0.6 + 0.4;\n
326 gl_FragColor = vec4( ( spineShadowCoef * lightColor ) * texel.rgb , texel.a ) * uColor;\n
332 Property::Map customShader;
334 customShader[ "vertexShader" ] = vertexShader;
335 customShader[ "fragmentShader" ] = fragmentShader;
336 customShader[ "subdivideGridX" ] = 20;
337 customShader[ "subdivideGridY" ] = 20;
339 map[ "shader" ] = customShader;