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/motion-blur-effect.h>
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
38 ////////////////////////////////////////////////////
40 // Motion blur shader / actor tweaking parameters
43 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
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 );
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
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
57 const float MOTION_BLUR_ALPHA_SCALE = 0.75f; // global scaler applied to actor alpha as it is blurred + moving
62 MotionBlurEffect::MotionBlurEffect()
66 //Call the Parent copy constructor to add reference to the implementation for this object
67 MotionBlurEffect::MotionBlurEffect( ShaderEffect handle )
68 :ShaderEffect( handle )
72 MotionBlurEffect::~MotionBlurEffect()
76 MotionBlurEffect MotionBlurEffect::Apply( Actor handle )
78 MotionBlurEffect newEffect = New( MOTION_BLUR_NUM_SAMPLES );
79 handle.SetShaderEffect( newEffect );
81 Property::Index uModelProperty = newEffect.GetPropertyIndex( MOTION_BLUR_MODEL_LASTFRAME );
83 Constraint constraint = Constraint::New<Matrix>( uModelProperty,
84 Source( handle, Actor::WORLD_MATRIX ),
85 EqualToConstraint() );
87 // and set up constraint.
88 newEffect.ApplyConstraint( constraint );
92 MotionBlurEffect MotionBlurEffect::New()
94 return New( MOTION_BLUR_NUM_SAMPLES );
97 MotionBlurEffect MotionBlurEffect::New( const unsigned int numBlurSamples )
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;
111 "uniform mat4 uModelLastFrame;\n"
112 "uniform float uTimeDelta;\n"
114 "uniform float uGeometryStretchFactor;\n"
115 "uniform float uSpeedScalingFactor;\n"
118 "varying vec2 vModelSpaceCenterToPos;\n"
119 "varying vec2 vScreenSpaceVelocityVector;\n"
120 "varying float vSpeed;\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"
130 // work out vertex's last movement in view space
131 " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
133 // get clip space position of vertex this frame and last frame
134 " vec4 clipSpaceVertex = uMvpMatrix * vertex;\n"
135 " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
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
140 " float posDeltaLength = length(viewSpacePosDelta);\n"
141 " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
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
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]
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"
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"
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"
170 " vTexCoord = aTexCoord;\n"
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;
182 "precision mediump float;\n"
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"
192 "varying vec2 vModelSpaceCenterToPos;\n"
193 "varying vec2 vScreenSpaceVelocityVector;\n"
194 "varying float vSpeed;\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
205 // scale velocity vector by user requirements
206 " vec2 velocity = vScreenSpaceVelocityVector * uBlurTexCoordScale;\n"
208 // standard actor texel
209 " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
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"
215 " float t = i * uRecipNumSamplesMinusOne;\n"
216 " col += texture2D(sTexture, vTexCoord + (velocity * t)) * uRecipNumSamples;\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"
223 // NOTE: we must turn on alpha blending for the actor (HINT_BLENDING)
224 ShaderEffect shader = ShaderEffect::New( vertexSource,
227 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID) );
229 MotionBlurEffect handle( shader );
232 //////////////////////////////////////
233 // Register uniform properties
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 );
252 MotionBlurEffect MotionBlurEffect::DownCast( ShaderEffect shaderEffect )
254 MotionBlurEffect handle = shaderEffect;
258 void MotionBlurEffect::SetNumSamples( int numSamples )
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 ) );
265 void MotionBlurEffect::SetTexcoordScale( float texcoordScale )
267 SetUniform( MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME, texcoordScale );
270 void MotionBlurEffect::SetGeometryStretchFactor( float scalingFactor )
272 SetUniform( MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
275 void MotionBlurEffect::SetSpeedScalingFactor( float scalingFactor )
277 SetUniform( MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
280 void MotionBlurEffect::SetObjectFadeStart( Vector2 displacement )
282 SetUniform( MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME, displacement );
285 void MotionBlurEffect::SetObjectFadeEnd( Vector2 displacement )
287 SetUniform( MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME, displacement );
290 void MotionBlurEffect::SetAlphaScale( float alphaScale )
292 SetUniform( MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME, alphaScale );
295 const std::string& MotionBlurEffect::GetTexcoordScalePropertyName() const
297 return MOTION_BLUR_TEXCOORD_SCALE_PROPERTY_NAME;
300 const std::string& MotionBlurEffect::GetGeometryStretchFactorPropertyName() const
302 return MOTION_BLUR_GEOM_STRETCH_SCALING_FACTOR_PROPERTY_NAME;
305 const std::string& MotionBlurEffect::GetSpeedScalingFactorPropertyName() const
307 return MOTION_BLUR_SPEED_SCALING_FACTOR_PROPERTY_NAME;
310 const std::string& MotionBlurEffect::GetObjectFadeStartPropertyName() const
312 return MOTION_BLUR_OBJECT_FADE_START_PROPERTY_NAME;
315 const std::string& MotionBlurEffect::GetObjectFadeEndPropertyName() const
317 return MOTION_BLUR_OBJECT_FADE_END_PROPERTY_NAME;
320 const std::string& MotionBlurEffect::GetAlphaScalePropertyName() const
322 return MOTION_BLUR_ALPHA_SCALE_PROPERTY_NAME;