2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
17 #include <dali-toolkit/public-api/shader-effects/ripple-effect.h>
29 const std::string AMPLITUDE_PROPERTY_NAME( "uAmplitude" );
30 const std::string CENTER_PROPERTY_NAME( "uCenter" );
31 const std::string TIME_PROPERTY_NAME( "uTime" );
35 RippleEffect::RippleEffect()
39 //Call the Parent copy constructor to add reference to the implementation for this object
40 RippleEffect::RippleEffect(ShaderEffect handle)
45 RippleEffect::~RippleEffect()
50 RippleEffect RippleEffect::New()
53 std::string vertexShader(
54 "precision mediump float;\n"
55 "uniform mediump vec2 uCenter;\n"
56 "uniform mediump float uTime;\n"
57 "uniform mediump float uAmplitude;\n"
58 "uniform mediump float uLighting;\n"
59 "uniform mediump float uWaveLength;\n"
60 "varying mediump float vLight;\n"
61 "varying mediump float vShade;\n"
64 "float lighting = uAmplitude * 0.02;\n"
65 "float waveLength = uAmplitude * 0.0016;\n"
66 "vec4 world = uModelView * vec4(aPosition,1.0);\n"
67 "vec2 d = vec2(world.x - uCenter.x, world.y - uCenter.y);\n"
68 "float dist = length(d);\n"
69 "float amplitude = cos(uTime - dist*waveLength);\n"
70 "float slope = sin(uTime - dist*waveLength);\n"
71 "world.z += amplitude * uAmplitude;\n"
72 "gl_Position = uProjection * world;\n"
73 "vec2 lightDirection = vec2(-0.707,0.707);\n"
77 " dot = dot(normalize(d),lightDirection) * lighting;\n"
79 "vShade = 1.0 - (dot * slope);\n"
80 "vLight = max(0.0, dot * -slope);\n"
81 "vTexCoord = aTexCoord;\n"
84 // append the default version
85 std::string imageFragmentShader(
86 "precision mediump float;\n"
87 "varying mediump float vLight;\n"
88 "varying mediump float vShade;\n"
91 " gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0) + vec4(vLight, vLight, vLight,0.0);\n"
94 // Create the implementation, temporarily owned on stack
95 Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New(
96 vertexShader, imageFragmentShader, vertexShader, "", GeometryHints(HINT_GRID) );
98 /* Pass ownership to RippleEffect through overloaded constructor, So that it now has access to the
99 Dali::ShaderEffect implementation */
100 Dali::Toolkit::RippleEffect handle( shaderEffect );
102 handle.SetUniform( AMPLITUDE_PROPERTY_NAME, 0.0f );
103 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f));
104 handle.SetUniform( TIME_PROPERTY_NAME, 0.0f );
109 void RippleEffect::SetAmplitude(float amplitude)
111 SetUniform( AMPLITUDE_PROPERTY_NAME, amplitude );
114 void RippleEffect::SetCenter(const Vector2& center)
116 SetUniform( CENTER_PROPERTY_NAME, center );
119 void RippleEffect::SetTime(float time)
121 SetUniform( TIME_PROPERTY_NAME, time );
124 const std::string& RippleEffect::GetAmplitudePropertyName() const
126 return AMPLITUDE_PROPERTY_NAME;
129 const std::string& RippleEffect::GetCenterPropertyName() const
131 return CENTER_PROPERTY_NAME;
134 const std::string& RippleEffect::GetTimePropertyName() const
136 return TIME_PROPERTY_NAME;
139 } // namespace Toolkit