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.
19 #include <dali-toolkit/public-api/shader-effects/motion-stretch-effect.h>
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>
35 struct MatrixFromPropertiesConstraint
37 MatrixFromPropertiesConstraint()
41 Matrix operator()( const Matrix& current,
42 const PropertyInput& propertyPosition,
43 const PropertyInput& propertyOrientation,
44 const PropertyInput& propertyScale )
47 mat4.SetTransformComponents( propertyScale.GetVector3(),
48 propertyOrientation.GetQuaternion(),
49 propertyPosition.GetVector3() );
55 const std::string MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME( "uGeometryStretchFactor" );
56 const std::string MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME( "uSpeedScalingFactor" );
57 const std::string MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME( "uObjectFadeStart" );
58 const std::string MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME( "uObjectFadeEnd" );
59 const std::string MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME( "uAlphaScale" );
60 const std::string MOTION_STRETCH_MODELVIEW_LASTFRAME( "uModelLastFrame" ); ///< Matrix
62 ////////////////////////////////////////////////////
64 // Motion stretch shader / actor tweaking parameters
67 // half width and half height respectively of actor, corresponding to values in vertex attribute stream
68 // Note that these values work for normal image actor (verts +/- 0.5) but a grid or a nine square seemsi
69 // to have verts in pixel space (e.g. 256,256). Need to fix this somehow,
70 // either in Dali or by passing uniforms which we can use to 'normalise' the verts in the vertex shader
71 const Vector2 MOTION_STRETCH_ACTOR_VERTEX( 0.5f, 0.5f );
73 const float MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR = 0.5f; // scaling factor for how much to stretch actor geom as it moves
74 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 const Vector2 MOTION_STRETCH_OBJECT_FADE_END( MOTION_STRETCH_ACTOR_VERTEX ); // displacement from center at which actor fully fades to zero alpha
77 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 const float MOTION_STRETCH_ALPHA_SCALE = 0.75f; // global scaler applied to actor alpha as it is stretched + moving
84 MotionStretchEffect::MotionStretchEffect()
88 // Call the Parent copy constructor to add reference to the implementation for this object
89 MotionStretchEffect::MotionStretchEffect( ShaderEffect handle )
90 :ShaderEffect( handle )
94 MotionStretchEffect::~MotionStretchEffect()
98 MotionStretchEffect MotionStretchEffect::Apply( RenderableActor renderable )
100 MotionStretchEffect newEffect = New();
101 renderable.SetShaderEffect( newEffect );
103 Property::Index uModelProperty = newEffect.GetPropertyIndex( MOTION_STRETCH_MODELVIEW_LASTFRAME );
105 Constraint constraint = Constraint::New<Matrix>( uModelProperty,
106 Source( renderable, Actor::WORLD_MATRIX ),
107 EqualToConstraint() );
109 // and set up constraint.
110 newEffect.ApplyConstraint(constraint);
114 MotionStretchEffect MotionStretchEffect::New()
116 // Dali vertexSource prefix for reference:
117 // precision highp float;
118 // attribute vec3 aPosition;
119 // attribute vec2 aTexCoord;
120 // uniform mat4 uMvpMatrix;
121 // uniform mat4 uModelView;
122 // uniform mat3 uNormalMatrix;
123 // uniform mat4 uProjection;
124 // uniform vec4 uColor;
125 // varying vec2 vTexCoord;
126 std::string vertexSource;
128 "precision mediump float;\n"
129 "uniform mat4 uModelLastFrame;\n"
130 "uniform float uTimeDelta;\n"
132 "uniform float uGeometryStretchFactor;\n"
133 "uniform float uSpeedScalingFactor;\n"
136 "varying vec2 vModelSpaceCenterToPos;\n"
137 "varying vec2 vScreenSpaceVelocityVector;\n"
138 "varying float vSpeed;\n"
142 // get view space position of vertex this frame and last frame
143 " vec4 vertex = vec4(aPosition, 1.0);\n"
144 " vec4 viewSpaceVertex = uModelView * vertex;\n"
145 " vec4 viewSpaceVertexLastFrame = uViewMatrix * uModelLastFrame * vertex;\n"
147 // work out vertex's last movement in view space
148 " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
149 " float reciprocalTimeDelta = 1.0 / ((uTimeDelta > 0.0) ? uTimeDelta : 0.01);\n"
151 // get clip space position of vertex this frame and last frame
152 " vec4 clipSpaceVertex = uMvpMatrix * vertex;\n"
153 " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
155 // decide how much this vertex is 'trailing', i.e. at the back of the object relative to its direction of motion. We do this
156 // 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
158 " float posDeltaLength = length(viewSpacePosDelta);\n"
159 " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
161 " vec4 viewSpaceCenterToPos = uModelView * vec4(aPosition, 0.0);\n"
162 " float centerToVertexDist = length(viewSpaceCenterToPos);\n"
163 " if(centerToVertexDist > 0.001)\n" // avoid div by 0 if object has vertex at model space origin
165 " vec3 viewSpacePosDeltaNormalised = viewSpacePosDelta / posDeltaLength;\n"
166 " vec3 viewSpaceCenterToPosNormalised = viewSpaceCenterToPos.xyz / centerToVertexDist;\n"
167 " t = (dot(viewSpacePosDeltaNormalised, viewSpaceCenterToPosNormalised) * 0.5 ) + 0.5;\n" // scale and bias from [-1..1] to [0..1]
170 // output vertex position lerped with its last position, based on how much it is trailing,
171 // this stretches the geom back along where it has just been, giving a warping effect
172 // We raise t to a power in order that non-trailing vertices are effected much more than trailing ones
173 // 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)
174 " gl_Position = mix(clipSpaceVertexLastFrame, clipSpaceVertex, t * t * t * uGeometryStretchFactor * reciprocalTimeDelta);\n"
176 // work out vertex's last movement in normalised device coordinates [-1..1] space, i.e. perspective divide
177 " vec2 ndcVertex = clipSpaceVertex.xy / clipSpaceVertex.w;\n"
178 " vec2 ndcVertexLastFrame = clipSpaceVertexLastFrame.xy / clipSpaceVertexLastFrame.w;\n"
179 // scale and bias so that a value of 1.0 corresponds to screen size (NDC is [-1..1] = 2)
180 " vScreenSpaceVelocityVector = ((ndcVertex - ndcVertexLastFrame) * 0.5 * reciprocalTimeDelta);\n"
181 " vScreenSpaceVelocityVector.y = -vScreenSpaceVelocityVector.y;\n" // TODO negated due to y being inverted in our coordinate system?
182 // calculate a scaling factor proportional to velocity, which we can use to tweak how things look
183 " vSpeed = length(vScreenSpaceVelocityVector) * uSpeedScalingFactor;\n"
184 " vSpeed = clamp(vSpeed, 0.0, 1.0);\n"
186 // 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)
187 " vModelSpaceCenterToPos = aPosition.xy;\n"
189 " vTexCoord = aTexCoord;\n"
193 // Dali fragmentSource prefix for reference:
194 // precision highp float;
195 // uniform sampler2D sTexture;
196 // uniform sampler2D sEffect;
197 // uniform vec4 uColor;
198 // varying vec2 vTexCoord;
199 std::string fragmentSource;
201 "precision mediump float;\n"
203 "uniform vec2 uObjectFadeStart;\n"
204 "uniform vec2 uObjectFadeEnd;\n"
205 "uniform float uAlphaScale;\n"
208 "varying vec2 vModelSpaceCenterToPos;\n"
209 "varying vec2 vScreenSpaceVelocityVector;\n"
210 "varying float vSpeed;\n"
214 // 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
215 // the stretched object and the background. Use smoothstep also to hide any hard edges (discontinuities) in rate of change of this alpha gradient
216 " vec2 centerToPixel = abs( vModelSpaceCenterToPos );\n"
217 " vec2 fadeToEdges = smoothstep(0.0, 1.0, 1.0 - ((centerToPixel - uObjectFadeStart) / (uObjectFadeEnd - uObjectFadeStart)));\n"
218 " float fadeToEdgesScale = fadeToEdges.x * fadeToEdges.y * uAlphaScale;\n" // apply global scaler
219 " fadeToEdgesScale = mix(1.0, fadeToEdgesScale, vSpeed);\n" // fade proportional to speed, so opaque when at rest
221 // standard actor texel
222 " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
223 " gl_FragColor = colActor;\n"
224 " gl_FragColor.a *= fadeToEdgesScale;\n" // fade actor to its edges based on speed of motion
225 " gl_FragColor *= uColor;\n"
228 // NOTE: we must turn on alpha blending for the actor (HINT_BLENDING)
229 ShaderEffect shader = ShaderEffect::New(
230 vertexSource, fragmentSource, GeometryType( GEOMETRY_TYPE_IMAGE ),
231 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ) );
235 MotionStretchEffect handle( shader );
238 //////////////////////////////////////
239 // Register uniform properties
243 // factors that scale the look, defaults
244 handle.SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR );
245 handle.SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_SPEED_SCALING_FACTOR );
246 handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_START );
247 handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_END );
248 handle.SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, MOTION_STRETCH_ALPHA_SCALE );
249 handle.SetUniform( MOTION_STRETCH_MODELVIEW_LASTFRAME, Matrix::IDENTITY );
254 void MotionStretchEffect::SetGeometryStretchFactor( float scalingFactor )
256 SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
259 void MotionStretchEffect::SetSpeedScalingFactor( float scalingFactor )
261 SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
264 void MotionStretchEffect::SetObjectFadeStart( Vector2 displacement )
266 SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, displacement );
269 void MotionStretchEffect::SetObjectFadeEnd( Vector2 displacement )
271 SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, displacement );
274 void MotionStretchEffect::SetAlphaScale( float alphaScale )
276 SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, alphaScale );
279 const std::string& MotionStretchEffect::GetGeometryStretchFactorPropertyName() const
281 return MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME;
284 const std::string& MotionStretchEffect::GetSpeedScalingFactorPropertyName() const
286 return MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME;
289 const std::string& MotionStretchEffect::GetObjectFadeStartPropertyName() const
291 return MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME;
294 const std::string& MotionStretchEffect::GetObjectFadeEndPropertyName() const
296 return MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME;
299 const std::string& MotionStretchEffect::GetAlphaScalePropertyName() const
301 return MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME;