1 #ifndef __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__
2 #define __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_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>
34 * @brief Creates a new PageTurnBookSpineEffect
35 * This is an assisting effect of PageTurnEffect to display a book spine on _static_ pages, and also to flip the image horizontally when needed.
37 * When the page is turned over in landscape, call
38 * SetIsBackImageVisible(true), this effect can display the back image
39 * correctly after the imageActor been rotated 180 degrees. To
40 * display the pages visually consistent with its turning state,
41 * please set the uniforms with the same values as the PageTurnEffect.
43 * Animatable/Constrainable uniforms:
44 * "uShadowWidth" - The width of shadow to be pageSize * shadowWidth. This shadow appears at the edges of the actor
45 * which is not visible on static pages
46 * "uSpineShadowParameter" - The two parameters are the major&minor radius (in pixels) to form an ellipse shape. The top-left
47 * quarter of this ellipse is used to calculate spine normal for simulating shadow
48 * "uIsBackImageVisible" - Set whether the current page is with its backside visible. Need to pass the parameter as true for
49 * the page which is turned over but still visible in Landscape
50 * "uPageWidth" - The page width of the PageTurnBookSpineEffect
52 * @return A handle to a newly allocated ShaderEffect
54 inline ShaderEffect CreatePageTurnBookSpineEffect()
56 std::string vertexSource(
57 "precision mediump float;\n"
58 "uniform float uShadowWidth;\n"
61 " gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
62 " vTexCoord.x = (aTexCoord.x-sTextureRect.s) /( 1.0 - uShadowWidth ) + sTextureRect.s;\n"
63 " vTexCoord.y = ( aTexCoord.y-sTextureRect.t-0.5*uShadowWidth*(sTextureRect.q-sTextureRect.t) )/( 1.0 - uShadowWidth ) + sTextureRect.t;\n"
66 // the simplified version of the fragment shader of page turn effect
67 std::string fragmentSource(
68 "precision mediump float;\n"
69 "uniform float uIsBackImageVisible;\n"
70 "uniform float uPageWidth;\n"
71 "uniform vec2 uSpineShadowParameter;\n"
74 // leave the border for display shadow, not visible( out of the screen ) when the page is static
75 " if( vTexCoord.y > sTextureRect.q || vTexCoord.y < sTextureRect.t || vTexCoord.x > sTextureRect.p )\n"
77 " gl_FragColor = vec4( 0.0 );\n"
81 // flip the image horizontally by changing the x component of the texture coordinate
82 " if( uIsBackImageVisible == 1.0 ) gl_FragColor = texture2D( sTexture, vec2( sTextureRect.p+sTextureRect.s-vTexCoord.x, vTexCoord.y ) ) * uColor; \n"
83 " else gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
85 // display book spine, a stripe of shadowed texture
86 " float pixelPos = (vTexCoord.x-sTextureRect.s)*uPageWidth; \n"
87 " if(pixelPos < uSpineShadowParameter.x) \n"
89 " float x = pixelPos - uSpineShadowParameter.x;\n"
90 " float y = sqrt( uSpineShadowParameter.x*uSpineShadowParameter.x - x*x );\n"
91 " vec2 spineNormal = normalize(vec2(uSpineShadowParameter.y*x/uSpineShadowParameter.x, y));\n"
92 " gl_FragColor.rgb *= spineNormal.y; \n"
97 const float DEFAULT_SHADOW_WIDTH(0.15f);
98 const Vector2 DEFAULT_SPINE_SHADOW_PARAMETER(50.0f, 20.0f);
100 ShaderEffect shaderEffect = ShaderEffect::New( vertexSource, fragmentSource );
102 shaderEffect.SetUniform( "uIsBackImageVisible", -1.f );
103 shaderEffect.SetUniform( "uShadowWidth", DEFAULT_SHADOW_WIDTH );
104 shaderEffect.SetUniform( "uSpineShadowParameter", DEFAULT_SPINE_SHADOW_PARAMETER );
106 float defaultPageWidth = Dali::Stage::GetCurrent().GetSize().x;
107 shaderEffect.SetUniform( "uPageWidth", defaultPageWidth/(1.f-DEFAULT_SHADOW_WIDTH) );
112 } //namespace Internal
114 } // namespace Toolkit
118 #endif /* __DALI_PAGE_TURN_BOOK_SPINE_EFFECT_H__ */