Merge "Fix for font validation." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / iris-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_IRIS_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_IRIS_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/shader-effects/shader-effect.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 /**
31  * @brief Creates a new IrisEffect
32  *
33  * IrisEffect is a custom shader effect to achieve iris effects in Image actors
34  *
35  * Animatable/Constrainable uniforms:
36  *
37  *  "uRadius"       - The radius of the iris effect in texture coordinate distance,
38  *                    i.e. 0.0 (no circle) to 1.0 (complete circle), to > 1.0 (extending outside of texture).
39  *                    @note For Atlas Textures results may be unpredictable.
40  *
41  *  "uBlendFactor"  - The blend factor of the iris effect. The lower the value, the larger the blending portion
42  *                    (between Opaque & Transparent). Blending will account for 1 / blendFactor of the radius
43  *                    of the texture.
44  *
45  *  "uCenter"       - The center point of the iris (in texture coordinates)
46  *
47  * @return A handle to a newly allocated ShaderEffect
48  */
49 inline ShaderEffect CreateIrisEffect()
50 {
51   // append the default version
52   std::string vertexShader(
53       "uniform mediump vec2 uCenter;\n"
54       "varying mediump vec2 vRelativePosition;\n"
55       "\n"
56       "void main()\n"
57       "{\n"
58       "    mediump vec4 world = uModelView * vec4(aPosition, 1.0);\n"
59       "    gl_Position = uProjection * world;\n"
60       "    \n"
61       "    vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord );\n"
62       "    vRelativePosition = vTexCoord - uCenter;\n"
63       "}\n");
64
65   std::string fragmentShader(
66       "uniform mediump float uRadius;                                                           \n"
67       "uniform mediump float uBlendFactor;                                                      \n"
68       "varying mediump vec2 vRelativePosition;                                                  \n"
69       "void main()                                                                      \n"
70       "{                                                                                \n"
71       "   mediump float delta = (length(vRelativePosition) - uRadius);                          \n"
72       "   delta = clamp(0.0 - delta * uBlendFactor, 0.0, 1.0);                          \n"
73       "   gl_FragColor = texture2D(sTexture, vTexCoord) * uColor;                       \n"
74       "   gl_FragColor.a *= delta;                                                      \n"
75       "}                                                                                \n"
76   );
77
78   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
79       vertexShader,
80       fragmentShader,
81       ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ));
82
83   shaderEffect.SetUniform( "uRadius", 0.0f );
84   shaderEffect.SetUniform( "uBlendFactor", 100.0f );
85   shaderEffect.SetUniform( "uCenter", Vector2(0.5f, 0.5f) );
86
87   return shaderEffect;
88 }
89
90 }
91 }
92
93 #endif