Merge branch 'tizen' into devel/new_mesh
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / motion-stretch-effect.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/public-api/shader-effects/motion-stretch-effect.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/active-constraint.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/public-api/actors/image-actor.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace
34 {
35
36 struct MatrixFromPropertiesConstraint
37 {
38   MatrixFromPropertiesConstraint()
39   {
40   }
41
42   Matrix operator()( const Matrix& current,
43                      const PropertyInput& propertyPosition,
44                      const PropertyInput& propertyOrientation,
45                      const PropertyInput& propertyScale )
46   {
47     Matrix mat4( false );
48     mat4.SetTransformComponents( propertyScale.GetVector3(),
49                                  propertyOrientation.GetQuaternion(),
50                                  propertyPosition.GetVector3() );
51
52     return mat4;
53   }
54 };
55
56 const std::string MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME( "uGeometryStretchFactor" );
57 const std::string MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME( "uSpeedScalingFactor" );
58 const std::string MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME( "uObjectFadeStart" );
59 const std::string MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME( "uObjectFadeEnd" );
60 const std::string MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME( "uAlphaScale" );
61 const std::string MOTION_STRETCH_MODELVIEW_LASTFRAME( "uModelLastFrame" );  ///< Matrix
62
63 ////////////////////////////////////////////////////
64 //
65 // Motion stretch shader / actor tweaking parameters
66 //
67
68 // half width and half height respectively of actor, corresponding to values in vertex attribute stream
69 // Note that these values work for normal image actor (verts +/- 0.5) but a grid or a nine square seemsi
70 // to have verts in pixel space (e.g. 256,256). Need to fix this somehow,
71 // either in Dali or by passing uniforms which we can use to 'normalise' the verts in the vertex shader
72 const Vector2 MOTION_STRETCH_ACTOR_VERTEX( 0.5f, 0.5f );
73
74 const float MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR = 0.5f; // scaling factor for how much to stretch actor geom as it moves
75 const float MOTION_STRETCH_SPEED_SCALING_FACTOR = 0.5f;        // scales the speed, producing a number affecting how much the actor stretches & fades at the edges
76
77 const Vector2 MOTION_STRETCH_OBJECT_FADE_END( MOTION_STRETCH_ACTOR_VERTEX );             // displacement from center at which actor fully fades to zero alpha
78 const Vector2 MOTION_STRETCH_OBJECT_FADE_START( MOTION_STRETCH_OBJECT_FADE_END * 0.5f ); // displacement from center at which actor start to fade from full alpha
79
80 const float MOTION_STRETCH_ALPHA_SCALE = 0.75f; // global scaler applied to actor alpha as it is stretched + moving
81
82 } // namespace
83
84
85 MotionStretchEffect::MotionStretchEffect()
86 {
87 }
88
89 // Call the Parent copy constructor to add reference to the implementation for this object
90 MotionStretchEffect::MotionStretchEffect( ShaderEffect handle )
91 :ShaderEffect( handle )
92 {
93 }
94
95 MotionStretchEffect::~MotionStretchEffect()
96 {
97 }
98
99 MotionStretchEffect MotionStretchEffect::Apply( RenderableActor renderable )
100 {
101   MotionStretchEffect newEffect = New();
102
103   ImageActor imageActor = ImageActor::DownCast(renderable);
104   if( imageActor )
105   {
106     imageActor.SetShaderEffect( newEffect );
107   }
108
109   Dali::Property::Index uModelProperty = newEffect.GetPropertyIndex( MOTION_STRETCH_MODELVIEW_LASTFRAME );
110
111   Constraint constraint = Constraint::New<Matrix>( uModelProperty,
112                                                    Source( renderable, Actor::Property::WORLD_MATRIX ),
113                                                    EqualToConstraint() );
114
115   // and set up constraint.
116   newEffect.ApplyConstraint(constraint);
117   return newEffect;
118 }
119
120 MotionStretchEffect MotionStretchEffect::New()
121 {
122   // Dali vertexSource prefix for reference:
123   // precision highp float;
124   // attribute vec3  aPosition;
125   // attribute vec2  aTexCoord;
126   // uniform   mat4  uMvpMatrix;
127   // uniform   mat4  uModelView;
128   // uniform   mat3  uNormalMatrix;
129   // uniform   mat4  uProjection;
130   // uniform   vec4  uColor;
131   // varying   vec2  vTexCoord;
132   std::string vertexSource;
133   vertexSource =
134     "precision mediump float;\n"
135     "uniform mat4  uModelLastFrame;\n"
136     "uniform float uTimeDelta;\n"
137
138     "uniform float uGeometryStretchFactor;\n"
139     "uniform float uSpeedScalingFactor;\n"
140
141     // outputs
142     "varying vec2 vModelSpaceCenterToPos;\n"
143     "varying vec2 vScreenSpaceVelocityVector;\n"
144     "varying float vSpeed;\n"
145
146     "void main()\n"
147     "{\n"
148     // get view space position of vertex this frame and last frame
149     " vec4 vertex = vec4(aPosition, 1.0);\n"
150     " vec4 viewSpaceVertex = uModelView * vertex;\n"
151     " vec4 viewSpaceVertexLastFrame = uViewMatrix * uModelLastFrame * vertex;\n"
152
153     // work out vertex's last movement in view space
154     " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
155     " float reciprocalTimeDelta = 1.0 / ((uTimeDelta > 0.0) ? uTimeDelta : 0.01);\n"
156
157     // get clip space position of vertex this frame and last frame
158     " vec4 clipSpaceVertex = uMvpMatrix * vertex;\n"
159     " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
160
161     // decide how much this vertex is 'trailing', i.e. at the back of the object relative to its direction of motion. We do this
162     // by assuming the objects model space origin is at its center and taking the dot product of the vector from center to vertex with the motion direction
163     " float t = 0.0;\n"
164     " float posDeltaLength = length(viewSpacePosDelta);\n"
165     " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
166     " {\n"
167     "   vec4 viewSpaceCenterToPos = uModelView * vec4(aPosition, 0.0);\n"
168     "   float centerToVertexDist = length(viewSpaceCenterToPos);\n"
169     "   if(centerToVertexDist > 0.001)\n" // avoid div by 0 if object has vertex at model space origin
170     "   {\n"
171     "     vec3 viewSpacePosDeltaNormalised = viewSpacePosDelta / posDeltaLength;\n"
172     "     vec3 viewSpaceCenterToPosNormalised = viewSpaceCenterToPos.xyz / centerToVertexDist;\n"
173     "     t = (dot(viewSpacePosDeltaNormalised, viewSpaceCenterToPosNormalised) * 0.5 ) + 0.5;\n" // scale and bias from [-1..1] to [0..1]
174     "   }\n"
175     " }\n"
176     // output vertex position lerped with its last position, based on how much it is trailing,
177     // this stretches the geom back along where it has just been, giving a warping effect
178     // We raise t to a power in order that non-trailing vertices are effected much more than trailing ones
179     // Note: we must take account of time delta to convert position delta into a velocity, so changes are smooth (take into account frame time correctly)
180     " gl_Position = mix(clipSpaceVertexLastFrame, clipSpaceVertex, t * t * t * uGeometryStretchFactor * reciprocalTimeDelta);\n"
181
182     // work out vertex's last movement in normalised device coordinates [-1..1] space, i.e. perspective divide
183     " vec2 ndcVertex = clipSpaceVertex.xy / clipSpaceVertex.w;\n"
184     " vec2 ndcVertexLastFrame = clipSpaceVertexLastFrame.xy / clipSpaceVertexLastFrame.w;\n"
185     // scale and bias so that a value of 1.0 corresponds to screen size (NDC is [-1..1] = 2)
186     " vScreenSpaceVelocityVector = ((ndcVertex - ndcVertexLastFrame) * 0.5 * reciprocalTimeDelta);\n"
187     " vScreenSpaceVelocityVector.y = -vScreenSpaceVelocityVector.y;\n" // TODO negated due to y being inverted in our coordinate system?
188     // calculate a scaling factor proportional to velocity, which we can use to tweak how things look
189     " vSpeed = length(vScreenSpaceVelocityVector) * uSpeedScalingFactor;\n"
190     " vSpeed = clamp(vSpeed, 0.0, 1.0);\n"
191
192     // provide fragment shader with vector from center of object to pixel (assumes the objects model space origin is at its center and verts have same z)
193     " vModelSpaceCenterToPos = aPosition.xy;\n"
194
195     " vTexCoord = aTexCoord;\n"
196     "}\n";
197
198
199   // Dali fragmentSource prefix for reference:
200   // precision highp     float;
201   // uniform   sampler2D sTexture;
202   // uniform   sampler2D sEffect;
203   // uniform   vec4      uColor;
204   // varying   vec2      vTexCoord;
205   std::string fragmentSource;
206   fragmentSource =
207     "precision mediump float;\n"
208
209     "uniform vec2 uObjectFadeStart;\n"
210     "uniform vec2 uObjectFadeEnd;\n"
211     "uniform float uAlphaScale;\n"
212
213     // inputs
214     "varying vec2 vModelSpaceCenterToPos;\n"
215     "varying vec2 vScreenSpaceVelocityVector;\n"
216     "varying float vSpeed;\n"
217
218     "void main()\n"
219     "{\n"
220     // calculate an alpha value that will fade the object towards its extremities, we need this to avoid an unsightly hard edge between color values of
221     // the stretched object and the background. Use smoothstep also to hide any hard edges (discontinuities) in rate of change of this alpha gradient
222     " vec2 centerToPixel = abs( vModelSpaceCenterToPos );\n"
223     " vec2 fadeToEdges = smoothstep(0.0, 1.0, 1.0 - ((centerToPixel - uObjectFadeStart) / (uObjectFadeEnd - uObjectFadeStart)));\n"
224     " float fadeToEdgesScale = fadeToEdges.x * fadeToEdges.y * uAlphaScale;\n" // apply global scaler
225     " fadeToEdgesScale = mix(1.0, fadeToEdgesScale, vSpeed);\n" // fade proportional to speed, so opaque when at rest
226
227     // standard actor texel
228     " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
229     " gl_FragColor = colActor;\n"
230     " gl_FragColor.a *= fadeToEdgesScale;\n" // fade actor to its edges based on speed of motion
231     " gl_FragColor *= uColor;\n"
232     "}";
233
234   // NOTE: we must turn on alpha blending for the actor (HINT_BLENDING)
235   ShaderEffect shader = ShaderEffect::New(
236     vertexSource, fragmentSource, GeometryType( GEOMETRY_TYPE_IMAGE ),
237     ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ) );
238
239
240
241   MotionStretchEffect handle( shader );
242
243
244   //////////////////////////////////////
245   // Register uniform properties
246   //
247   //
248
249   // factors that scale the look, defaults
250   handle.SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR );
251   handle.SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_SPEED_SCALING_FACTOR );
252   handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_START );
253   handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_END );
254   handle.SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, MOTION_STRETCH_ALPHA_SCALE );
255   handle.SetUniform( MOTION_STRETCH_MODELVIEW_LASTFRAME, Matrix::IDENTITY );
256
257   return handle;
258 }
259
260 void MotionStretchEffect::SetGeometryStretchFactor( float scalingFactor )
261 {
262   SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
263 }
264
265 void MotionStretchEffect::SetSpeedScalingFactor( float scalingFactor )
266 {
267   SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
268 }
269
270 void MotionStretchEffect::SetObjectFadeStart( Vector2 displacement )
271 {
272   SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, displacement );
273 }
274
275 void MotionStretchEffect::SetObjectFadeEnd( Vector2 displacement )
276 {
277   SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, displacement );
278 }
279
280 void MotionStretchEffect::SetAlphaScale( float alphaScale )
281 {
282   SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, alphaScale );
283 }
284
285 const std::string& MotionStretchEffect::GetGeometryStretchFactorPropertyName() const
286 {
287   return MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME;
288 }
289
290 const std::string& MotionStretchEffect::GetSpeedScalingFactorPropertyName() const
291 {
292   return MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME;
293 }
294
295 const std::string& MotionStretchEffect::GetObjectFadeStartPropertyName() const
296 {
297   return MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME;
298 }
299
300 const std::string& MotionStretchEffect::GetObjectFadeEndPropertyName() const
301 {
302   return MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME;
303 }
304
305 const std::string& MotionStretchEffect::GetAlphaScalePropertyName() const
306 {
307   return MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME;
308 }
309
310 }
311
312 }