Ensure the use of ShaderEffect in ImageActor is backwards-compatible
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / spot-effect.h
1 #ifndef __DALI_TOOLKIT_SHADER_EFFECT_SPOT_H__
2 #define __DALI_TOOLKIT_SHADER_EFFECT_SPOT_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 SpotEffect
32  *
33  * SpotEffect is a custom shader effect to achieve spot effects on Image actors
34  *
35  * Animatable/Constrainable uniforms:
36  *  "uCenter" - The center of the spot. Default value (0.0,0.0)
37  *  "uRadius" - The radius of the spot. Default value 0.0
38  *
39  * @return A handle to a newly allocated ShaderEffect
40  */
41 inline ShaderEffect CreateSpotEffect()
42 {
43   // append the default version
44   std::string vertexShader(
45       "uniform mediump   vec2  uCenter;\n"
46       "uniform mediump   float  uRadius;\n"
47       "varying mediump   float  vRange;\n"
48       "\n"
49       "void main()\n"
50       "{\n"
51       "  mediump vec4 world = vec4(aPosition, 1.0);\n"
52       "  \n"
53       "  mediump vec2 d = vec2(world.xy - uCenter);\n"
54       "  mediump float dist = length(d);\n"
55       "  \n"
56       "  mediump float range = (uRadius - dist) / (uRadius);\n"
57       "  vRange = max(0.1, range);\n"
58       "  \n"
59       "  gl_Position = uMvpMatrix * world;\n"
60       "  vTexCoord = mix( sTextureRect.xy, sTextureRect.zw, aTexCoord );\n"
61       "}");
62
63   std::string fragmentShader(
64       "varying mediump   float  vRange;\n"
65       "\n"
66       "void main()\n"
67       "{\n"
68       "  gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(vRange, vRange, vRange, 1.0) * uColor;\n"
69       "}" );
70
71   Dali::ShaderEffect shaderEffect =  Dali::ShaderEffect::New(
72       vertexShader, fragmentShader,
73       ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID ));
74
75   shaderEffect.SetUniform( "uCenter", Vector2(0.0f, 0.0f) );
76   shaderEffect.SetUniform( "uRadius", 0.0f );
77
78   return shaderEffect;
79 }
80
81 } // namespace Toolkit
82
83 } // namespace Dali
84
85 #endif // __DALI_TOOLKIT_SHADER_EFFECT_SPOT_H__