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