de807518883dd392874727b0c1a1fd0a4801d96c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / motion-blur-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-blur-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
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace
33 {
34
35 const std::string MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME( "uBlurTexCoordScale" );
36 const std::string MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME( "uGeometryStretchFactor" );
37 const std::string MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME( "uSpeedScalingFactor" );
38 const std::string MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME( "uObjectFadeStart" );
39 const std::string MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME( "uObjectFadeEnd" );
40 const std::string MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME( "uAlphaScale" );
41 const std::string MOTION_BLUR_NUM_SAMPLES_NAME( "uNumSamples" );
42 const std::string MOTION_BLUR_RECIP_NUM_SAMPLES_NAME( "uRecipNumSamples" );
43 const std::string MOTION_BLUR_RECIP_NUM_SAMPLES_MINUS_ONE_NAME( "uRecipNumSamplesMinusOne" );
44 const std::string MOTION_BLUR_MODEL_LASTFRAME( "uModelLastFrame" );  ///< Matrix
45
46 ////////////////////////////////////////////////////
47 //
48 // Motion blur shader / actor tweaking parameters
49 //
50
51 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
52
53 // half width and half height respectively of actor, corresponding to values in vertex attribute stream
54 // TODO: Note that these values work for normal image actor (verts +/- 0.5) but a grid or a nine square seems to have verts in pixel space (e.g. 256,256). Need to fix this somehow,
55 // either in Dali or by passing uniforms which we can use to 'normalise' the verts in the vertex shader
56 const Vector2 MOTION_BLUR_ACTOR_VERTEX( 0.5f, 0.5f );
57
58 const float MOTION_BLUR_TEXCOORD_SCALE = 0.125f;             // stretch texture reads along velocity vector, larger number means reads spaced further apart
59 const float MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR = 0.05f; // scaling factor for how much to stretch actor geom as it moves
60 const float MOTION_BLUR_SPEED_SCALING_FACTOR = 0.5f;         // scales the speed, producing a number affecting how much the actor blurs & fades at the edges
61
62 const Vector2 MOTION_BLUR_OBJECT_FADE_END( MOTION_BLUR_ACTOR_VERTEX );             // distance from center at which actor fully fades to zero alpha
63 const Vector2 MOTION_BLUR_OBJECT_FADE_START( MOTION_BLUR_OBJECT_FADE_END * 0.5f ); // distance from center at which actor start to fade from full alpha
64
65 const float MOTION_BLUR_ALPHA_SCALE = 0.75f; // global scaler applied to actor alpha as it is blurred + moving
66
67 } // namespace
68
69
70 MotionBlurEffect::MotionBlurEffect()
71 {
72 }
73
74 //Call the Parent copy constructor to add reference to the implementation for this object
75 MotionBlurEffect::MotionBlurEffect( ShaderEffect handle )
76 :ShaderEffect( handle )
77 {
78 }
79
80 MotionBlurEffect::~MotionBlurEffect()
81 {
82 }
83
84 MotionBlurEffect MotionBlurEffect::Apply( RenderableActor renderable )
85 {
86   MotionBlurEffect newEffect = New( MOTION_BLUR_NUM_SAMPLES );
87   renderable.SetShaderEffect( newEffect );
88
89   Dali::Property::Index uModelProperty = newEffect.GetPropertyIndex( MOTION_BLUR_MODEL_LASTFRAME );
90
91   Constraint constraint = Constraint::New<Matrix>( uModelProperty,
92                                                    Source( renderable, Actor::Property::WORLD_MATRIX ),
93                                                    EqualToConstraint() );
94
95   // and set up constraint.
96   newEffect.ApplyConstraint( constraint );
97   return newEffect;
98 }
99
100 MotionBlurEffect MotionBlurEffect::New()
101 {
102   return New( MOTION_BLUR_NUM_SAMPLES );
103 }
104
105 MotionBlurEffect MotionBlurEffect::New( unsigned int numBlurSamples )
106 {
107   // Dali vertexSource prefix for reference:
108   // precision highp float;
109   // attribute vec3  aPosition;
110   // attribute vec2  aTexCoord;
111   // uniform   mat4  uMvpMatrix;
112   // uniform   mat4  uModelView;
113   // uniform   mat3  uNormalMatrix;
114   // uniform   mat4  uProjection;
115   // uniform   vec4  uColor;
116   // varying   vec2  vTexCoord;
117   std::string vertexSource;
118   vertexSource =
119     "precision mediump float;\n"
120     "uniform mat4 uModelLastFrame;\n"
121     "uniform float uTimeDelta;\n"
122
123     "uniform float uGeometryStretchFactor;\n"
124     "uniform float uSpeedScalingFactor;\n"
125
126     // outputs
127     "varying vec2 vModelSpaceCenterToPos;\n"
128     "varying vec2 vScreenSpaceVelocityVector;\n"
129     "varying float vSpeed;\n"
130
131     "void main()\n"
132     "{\n"
133     // get view space position of vertex this frame and last frame
134     " vec4 vertex = vec4(aPosition, 1.0);\n"
135     " vec4 viewSpaceVertex = uModelView * vertex;\n"
136     " vec4 viewSpaceVertexLastFrame = (uViewMatrix * uModelLastFrame) * vertex;\n"
137     " float reciprocalTimeDelta = 1.0 / ((uTimeDelta > 0.0) ? uTimeDelta : 0.01);\n"
138
139     // work out vertex's last movement in view space
140     " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
141
142     // get clip space position of vertex this frame and last frame
143     " vec4 clipSpaceVertex = uMvpMatrix * vertex;\n"
144     " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
145
146     // decide how much this vertex is 'trailing', i.e. at the back of the object relative to its direction of motion. We do this
147     // 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
148     " float t = 0.0;\n"
149     " float posDeltaLength = length(viewSpacePosDelta);\n"
150     " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
151     " {\n"
152     "   vec4 viewSpaceCenterToPos = uModelView * vec4(aPosition, 0.0);\n"
153     "   float centerToVertexDist = length(viewSpaceCenterToPos);\n"
154     "   if(centerToVertexDist > 0.001)\n" // avoid div by 0 if object has vertex at model space origin
155     "   {\n"
156     "     vec3 viewSpacePosDeltaNormalised = viewSpacePosDelta / posDeltaLength;\n"
157     "     vec3 viewSpaceCenterToPosNormalised = viewSpaceCenterToPos.xyz / centerToVertexDist;\n"
158     "     t = (dot(viewSpacePosDeltaNormalised, viewSpaceCenterToPosNormalised) * 0.5 ) + 0.5;\n" // scale and bias from [-1..1] to [0..1]
159     "   }\n"
160     " }\n"
161     // output vertex position lerped with its last position, based on how much it is trailing,
162     // this stretches the geom back along where it has just been, giving a warping effect
163     // 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)
164     " gl_Position = mix(clipSpaceVertexLastFrame, clipSpaceVertex, t * uGeometryStretchFactor * reciprocalTimeDelta);\n"
165
166     // work out vertex's last movement in normalised device coordinates [-1..1] space, i.e. perspective divide
167     " vec2 ndcVertex = clipSpaceVertex.xy / clipSpaceVertex.w;\n"
168     " vec2 ndcVertexLastFrame = clipSpaceVertexLastFrame.xy / clipSpaceVertexLastFrame.w;\n"
169     // scale and bias so that a value of 1.0 corresponds to screen size (NDC is [-1..1] = 2)
170     " vScreenSpaceVelocityVector = ((ndcVertex - ndcVertexLastFrame) * 0.5 * reciprocalTimeDelta);\n"
171     " vScreenSpaceVelocityVector.y = -vScreenSpaceVelocityVector.y;\n" // TODO negated due to y being inverted in our coordinate system?
172     // calculate a scaling factor proportional to velocity, which we can use to tweak how things look
173     " vSpeed = length(vScreenSpaceVelocityVector) * uSpeedScalingFactor;\n"
174     " vSpeed = clamp(vSpeed, 0.0, 1.0);\n"
175
176     // 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)
177     " vModelSpaceCenterToPos = aPosition.xy;\n"
178
179     " vTexCoord = aTexCoord;\n"
180     "}\n";
181
182
183   // Dali fragmentSource prefix for reference:
184   // precision highp     float;
185   // uniform   sampler2D sTexture;
186   // uniform   sampler2D sEffect;
187   // uniform   vec4      uColor;
188   // varying   vec2      vTexCoord;
189   std::string fragmentSource;
190   fragmentSource =
191     "precision mediump float;\n"
192     "uniform vec2 uObjectFadeStart;\n"
193     "uniform vec2 uObjectFadeEnd;\n"
194     "uniform float uAlphaScale;\n"
195     "uniform float uBlurTexCoordScale;\n"
196     "uniform float uNumSamples;\n"
197     "uniform float uRecipNumSamples;\n"
198     "uniform float uRecipNumSamplesMinusOne;\n"
199     // inputs
200     "varying vec2 vModelSpaceCenterToPos;\n"
201     "varying vec2 vScreenSpaceVelocityVector;\n"
202     "varying float vSpeed;\n"
203
204     "void main()\n"
205     "{\n"
206     // 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
207     // the blurred object and the unblurred background. Use smoothstep also to hide any hard edges (discontinuities) in rate of change of this alpha gradient
208     " vec2 centerToPixel = abs(vModelSpaceCenterToPos);\n"
209     " vec2 fadeToEdges = smoothstep(0.0, 1.0, 1.0 - ((centerToPixel - uObjectFadeStart) / (uObjectFadeEnd - uObjectFadeStart)));\n"
210     " float fadeToEdgesScale = fadeToEdges.x * fadeToEdges.y * uAlphaScale;\n" // apply global scaler
211     " fadeToEdgesScale = mix(1.0, fadeToEdgesScale, vSpeed);\n" // fade proportional to speed, so opaque when at rest
212
213     // scale velocity vector by user requirements
214     " vec2 velocity = vScreenSpaceVelocityVector * uBlurTexCoordScale;\n"
215
216     // standard actor texel
217     " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
218
219     // blurred actor - gather texture samples from the actor texture in the direction of motion
220     " vec4 col = colActor * uRecipNumSamples;\n"
221     " for(float i = 1.0; i < uNumSamples; i += 1.0)\n"
222     " {\n"
223     "   float t = i * uRecipNumSamplesMinusOne;\n"
224     "   col += texture2D(sTexture, vTexCoord + (velocity * t)) * uRecipNumSamples;\n"
225     " }\n"
226     " gl_FragColor = mix(colActor, col, vSpeed);\n" // lerp blurred and non-blurred actor based on speed of motion
227     " gl_FragColor.a = colActor.a * fadeToEdgesScale;\n" // fade blurred actor to its edges based on speed of motion
228     " gl_FragColor *= uColor;\n"
229     "}\n";
230
231   // NOTE: we must turn on alpha blending for the actor (HINT_BLENDING)
232   ShaderEffect shader = ShaderEffect::New( vertexSource, fragmentSource,
233                                            GEOMETRY_TYPE_IMAGE,
234                                            ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID) );
235
236   MotionBlurEffect handle( shader );
237
238
239   //////////////////////////////////////
240   // Register uniform properties
241   //
242   //
243
244   // factors that scale the look, defaults
245   handle.SetUniform( MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME, MOTION_BLUR_TEXCOORD_SCALE );
246   handle.SetUniform( MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME, MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR );
247   handle.SetUniform( MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME, MOTION_BLUR_SPEED_SCALING_FACTOR );
248   handle.SetUniform( MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME, MOTION_BLUR_OBJECT_FADE_START );
249   handle.SetUniform( MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME, MOTION_BLUR_OBJECT_FADE_END );
250   handle.SetUniform( MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME, MOTION_BLUR_ALPHA_SCALE );
251   handle.SetUniform( MOTION_BLUR_NUM_SAMPLES_NAME, static_cast<float>( MOTION_BLUR_NUM_SAMPLES ) );
252   handle.SetUniform( MOTION_BLUR_RECIP_NUM_SAMPLES_NAME, 1.0f / static_cast<float>( MOTION_BLUR_NUM_SAMPLES ) );
253   handle.SetUniform( MOTION_BLUR_RECIP_NUM_SAMPLES_MINUS_ONE_NAME, 1.0f / static_cast<float>( MOTION_BLUR_NUM_SAMPLES - 1.0f ) );
254   handle.SetUniform( MOTION_BLUR_MODEL_LASTFRAME, Matrix::IDENTITY );
255
256   return handle;
257 }
258
259 MotionBlurEffect MotionBlurEffect::DownCast( ShaderEffect shaderEffect )
260 {
261   MotionBlurEffect handle = shaderEffect;
262   return handle;
263 }
264
265 void MotionBlurEffect::SetNumSamples( int numSamples )
266 {
267   SetUniform( MOTION_BLUR_NUM_SAMPLES_NAME, static_cast<float>( numSamples ) );
268   SetUniform( MOTION_BLUR_RECIP_NUM_SAMPLES_NAME, 1.0f / static_cast<float>( numSamples ) );
269   SetUniform( MOTION_BLUR_RECIP_NUM_SAMPLES_MINUS_ONE_NAME, 1.0f / static_cast<float>( numSamples - 1.0f ) );
270 }
271
272 void MotionBlurEffect::SetTexcoordScale( float texcoordScale )
273 {
274   SetUniform( MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME, texcoordScale );
275 }
276
277 void MotionBlurEffect::SetGeometryStretchFactor( float scalingFactor )
278 {
279   SetUniform( MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
280 }
281
282 void MotionBlurEffect::SetSpeedScalingFactor( float scalingFactor )
283 {
284   SetUniform( MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
285 }
286
287 void MotionBlurEffect::SetObjectFadeStart( Vector2 displacement )
288 {
289   SetUniform( MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME, displacement );
290 }
291
292 void MotionBlurEffect::SetObjectFadeEnd( Vector2 displacement )
293 {
294   SetUniform( MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME, displacement );
295 }
296
297 void MotionBlurEffect::SetAlphaScale( float alphaScale )
298 {
299   SetUniform( MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME, alphaScale );
300 }
301
302 const std::string& MotionBlurEffect::GetTexcoordScalePropertyName() const
303 {
304   return MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME;
305 }
306
307 const std::string& MotionBlurEffect::GetGeometryStretchFactorPropertyName() const
308 {
309   return MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME;
310 }
311
312 const std::string& MotionBlurEffect::GetSpeedScalingFactorPropertyName() const
313 {
314   return MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME;
315 }
316
317 const std::string& MotionBlurEffect::GetObjectFadeStartPropertyName() const
318 {
319   return MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME;
320 }
321
322 const std::string& MotionBlurEffect::GetObjectFadeEndPropertyName() const
323 {
324   return MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME;
325 }
326
327 const std::string& MotionBlurEffect::GetAlphaScalePropertyName() const
328 {
329   return MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME;
330 }
331
332 }
333
334 }