2 * Copyright (c) 2014 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.
18 #include <dali-toolkit/public-api/shader-effects/ripple-effect.h>
30 const std::string AMPLITUDE_PROPERTY_NAME( "uAmplitude" );
31 const std::string CENTER_PROPERTY_NAME( "uCenter" );
32 const std::string TIME_PROPERTY_NAME( "uTime" );
36 RippleEffect::RippleEffect()
40 //Call the Parent copy constructor to add reference to the implementation for this object
41 RippleEffect::RippleEffect(ShaderEffect handle)
46 RippleEffect::~RippleEffect()
51 RippleEffect RippleEffect::New()
54 std::string vertexShader(
55 "precision mediump float;\n"
56 "uniform mediump vec2 uCenter;\n"
57 "uniform mediump float uTime;\n"
58 "uniform mediump float uAmplitude;\n"
59 "uniform mediump float uLighting;\n"
60 "uniform mediump float uWaveLength;\n"
61 "varying mediump float vLight;\n"
62 "varying mediump float vShade;\n"
65 "float lighting = uAmplitude * 0.02;\n"
66 "float waveLength = uAmplitude * 0.0016;\n"
67 "vec4 world = uModelView * vec4(aPosition,1.0);\n"
68 "vec2 d = vec2(world.x - uCenter.x, world.y - uCenter.y);\n"
69 "float dist = length(d);\n"
70 "float amplitude = cos(uTime - dist*waveLength);\n"
71 "float slope = sin(uTime - dist*waveLength);\n"
72 "world.z += amplitude * uAmplitude;\n"
73 "gl_Position = uProjection * world;\n"
74 "vec2 lightDirection = vec2(-0.707,0.707);\n"
78 " dot = dot(normalize(d),lightDirection) * lighting;\n"
80 "vShade = 1.0 - (dot * slope);\n"
81 "vLight = max(0.0, dot * -slope);\n"
82 "vTexCoord = aTexCoord;\n"
85 // append the default version
86 std::string imageFragmentShader(
87 "precision mediump float;\n"
88 "varying mediump float vLight;\n"
89 "varying mediump float vShade;\n"
92 " gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(vShade,vShade,vShade,1.0) + vec4(vLight, vLight, vLight,0.0);\n"
95 // Create the implementation, temporarily owned on stack
96 Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New(
97 vertexShader, imageFragmentShader, GeometryType(GEOMETRY_TYPE_IMAGE), GeometryHints(HINT_GRID) );
99 /* Pass ownership to RippleEffect through overloaded constructor, So that it now has access to the
100 Dali::ShaderEffect implementation */
101 Dali::Toolkit::RippleEffect handle( shaderEffect );
103 handle.SetUniform( AMPLITUDE_PROPERTY_NAME, 0.0f );
104 handle.SetUniform( CENTER_PROPERTY_NAME, Vector2(0.0f, 0.0f));
105 handle.SetUniform( TIME_PROPERTY_NAME, 0.0f );
110 void RippleEffect::SetAmplitude(float amplitude)
112 SetUniform( AMPLITUDE_PROPERTY_NAME, amplitude );
115 void RippleEffect::SetCenter(const Vector2& center)
117 SetUniform( CENTER_PROPERTY_NAME, center );
120 void RippleEffect::SetTime(float time)
122 SetUniform( TIME_PROPERTY_NAME, time );
125 const std::string& RippleEffect::GetAmplitudePropertyName() const
127 return AMPLITUDE_PROPERTY_NAME;
130 const std::string& RippleEffect::GetCenterPropertyName() const
132 return CENTER_PROPERTY_NAME;
135 const std::string& RippleEffect::GetTimePropertyName() const
137 return TIME_PROPERTY_NAME;
140 } // namespace Toolkit