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.
18 #include <dali-toolkit/public-api/shader-effects/motion-stretch-effect.h>
28 struct MatrixFromPropertiesConstraint
30 MatrixFromPropertiesConstraint()
34 Matrix operator()( const Matrix& current,
35 const PropertyInput& propertyPosition,
36 const PropertyInput& propertyOrientation,
37 const PropertyInput& propertyScale )
40 mat4.SetTransformComponents( propertyScale.GetVector3(),
41 propertyOrientation.GetQuaternion(),
42 propertyPosition.GetVector3() );
48 const std::string MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME( "uGeometryStretchFactor" );
49 const std::string MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME( "uSpeedScalingFactor" );
50 const std::string MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME( "uObjectFadeStart" );
51 const std::string MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME( "uObjectFadeEnd" );
52 const std::string MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME( "uAlphaScale" );
53 const std::string MOTION_STRETCH_MODELVIEW_LASTFRAME( "uModelLastFrame" ); ///< Matrix
55 ////////////////////////////////////////////////////
57 // Motion stretch shader / actor tweaking parameters
60 // half width and half height respectively of actor, corresponding to values in vertex attribute stream
61 // Note that these values work for normal image actor (verts +/- 0.5) but a grid or a nine square seemsi
62 // to have verts in pixel space (e.g. 256,256). Need to fix this somehow,
63 // either in Dali or by passing uniforms which we can use to 'normalise' the verts in the vertex shader
64 const Vector2 MOTION_STRETCH_ACTOR_VERTEX( 0.5f, 0.5f );
66 const float MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR = 0.5f; // scaling factor for how much to stretch actor geom as it moves
67 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
69 const Vector2 MOTION_STRETCH_OBJECT_FADE_END( MOTION_STRETCH_ACTOR_VERTEX ); // displacement from center at which actor fully fades to zero alpha
70 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
72 const float MOTION_STRETCH_ALPHA_SCALE = 0.75f; // global scaler applied to actor alpha as it is stretched + moving
77 MotionStretchEffect::MotionStretchEffect()
81 // Call the Parent copy constructor to add reference to the implementation for this object
82 MotionStretchEffect::MotionStretchEffect( ShaderEffect handle )
83 :ShaderEffect( handle )
87 MotionStretchEffect::~MotionStretchEffect()
91 MotionStretchEffect MotionStretchEffect::Apply( Actor handle )
93 MotionStretchEffect newEffect = New();
94 handle.SetShaderEffect( newEffect );
96 Property::Index uModelProperty = newEffect.GetPropertyIndex( MOTION_STRETCH_MODELVIEW_LASTFRAME );
98 Constraint constraint = Constraint::New<Matrix>( uModelProperty,
99 Source( handle, Actor::WORLD_MATRIX ),
100 EqualToConstraint() );
102 // and set up constraint.
103 newEffect.ApplyConstraint(constraint);
107 MotionStretchEffect MotionStretchEffect::New()
109 // Dali vertexSource prefix for reference:
110 // precision highp float;
111 // attribute vec3 aPosition;
112 // attribute vec2 aTexCoord;
113 // uniform mat4 uMvpMatrix;
114 // uniform mat4 uModelView;
115 // uniform mat3 uNormalMatrix;
116 // uniform mat4 uProjection;
117 // uniform vec4 uColor;
118 // varying vec2 vTexCoord;
119 std::string vertexSource;
121 "uniform mat4 uModelLastFrame;\n"
122 "uniform float uTimeDelta;\n"
124 "uniform float uGeometryStretchFactor;\n"
125 "uniform float uSpeedScalingFactor;\n"
128 "varying vec2 vModelSpaceCenterToPos;\n"
129 "varying vec2 vScreenSpaceVelocityVector;\n"
130 "varying float vSpeed;\n"
134 // get view space position of vertex this frame and last frame
135 " vec4 vertex = vec4(aPosition, 1.0);\n"
136 " vec4 viewSpaceVertex = uModelView * vertex;\n"
137 " vec4 viewSpaceVertexLastFrame = uViewMatrix * uModelLastFrame * vertex;\n"
139 // work out vertex's last movement in view space
140 " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
141 " float reciprocalTimeDelta = 1.0 / ((uTimeDelta > 0.0) ? uTimeDelta : 0.01);\n"
143 // get clip space position of vertex this frame and last frame
144 " vec4 clipSpaceVertex = uMvpMatrix * vertex;\n"
145 " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
147 // decide how much this vertex is 'trailing', i.e. at the back of the object relative to its direction of motion. We do this
148 // 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
150 " float posDeltaLength = length(viewSpacePosDelta);\n"
151 " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
153 " vec4 viewSpaceCenterToPos = uModelView * vec4(aPosition, 0.0);\n"
154 " float centerToVertexDist = length(viewSpaceCenterToPos);\n"
155 " if(centerToVertexDist > 0.001)\n" // avoid div by 0 if object has vertex at model space origin
157 " vec3 viewSpacePosDeltaNormalised = viewSpacePosDelta / posDeltaLength;\n"
158 " vec3 viewSpaceCenterToPosNormalised = viewSpaceCenterToPos.xyz / centerToVertexDist;\n"
159 " t = (dot(viewSpacePosDeltaNormalised, viewSpaceCenterToPosNormalised) * 0.5 ) + 0.5;\n" // scale and bias from [-1..1] to [0..1]
162 // output vertex position lerped with its last position, based on how much it is trailing,
163 // this stretches the geom back along where it has just been, giving a warping effect
164 // We raise t to a power in order that non-trailing vertices are effected much more than trailing ones
165 // 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)
166 " gl_Position = mix(clipSpaceVertexLastFrame, clipSpaceVertex, t * t * t * uGeometryStretchFactor * reciprocalTimeDelta);\n"
168 // work out vertex's last movement in normalised device coordinates [-1..1] space, i.e. perspective divide
169 " vec2 ndcVertex = clipSpaceVertex.xy / clipSpaceVertex.w;\n"
170 " vec2 ndcVertexLastFrame = clipSpaceVertexLastFrame.xy / clipSpaceVertexLastFrame.w;\n"
171 // scale and bias so that a value of 1.0 corresponds to screen size (NDC is [-1..1] = 2)
172 " vScreenSpaceVelocityVector = ((ndcVertex - ndcVertexLastFrame) * 0.5 * reciprocalTimeDelta);\n"
173 " vScreenSpaceVelocityVector.y = -vScreenSpaceVelocityVector.y;\n" // TODO negated due to y being inverted in our coordinate system?
174 // calculate a scaling factor proportional to velocity, which we can use to tweak how things look
175 " vSpeed = length(vScreenSpaceVelocityVector) * uSpeedScalingFactor;\n"
176 " vSpeed = clamp(vSpeed, 0.0, 1.0);\n"
178 // 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)
179 " vModelSpaceCenterToPos = aPosition.xy;\n"
181 " vTexCoord = aTexCoord;\n"
185 // Dali fragmentSource prefix for reference:
186 // precision highp float;
187 // uniform sampler2D sTexture;
188 // uniform sampler2D sEffect;
189 // uniform vec4 uColor;
190 // varying vec2 vTexCoord;
191 std::string fragmentSource;
193 "precision mediump float;\n"
195 "uniform vec2 uObjectFadeStart;\n"
196 "uniform vec2 uObjectFadeEnd;\n"
197 "uniform float uAlphaScale;\n"
200 "varying vec2 vModelSpaceCenterToPos;\n"
201 "varying vec2 vScreenSpaceVelocityVector;\n"
202 "varying float vSpeed;\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 stretched object and the 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
213 // standard actor texel
214 " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
215 " gl_FragColor = colActor;\n"
216 " gl_FragColor.a *= fadeToEdgesScale;\n" // fade actor to its edges based on speed of motion
217 " gl_FragColor *= uColor;\n"
220 // NOTE: we must turn on alpha blending for the actor (HINT_BLENDING)
221 ShaderEffect shader = ShaderEffect::New( vertexSource,
223 GeometryType( GEOMETRY_TYPE_IMAGE ),
224 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING | ShaderEffect::HINT_GRID ) );
228 MotionStretchEffect handle( shader );
231 //////////////////////////////////////
232 // Register uniform properties
236 // factors that scale the look, defaults
237 handle.SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_GEOM_STRETCH_SCALING_FACTOR );
238 handle.SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, MOTION_STRETCH_SPEED_SCALING_FACTOR );
239 handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_START );
240 handle.SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, MOTION_STRETCH_OBJECT_FADE_END );
241 handle.SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, MOTION_STRETCH_ALPHA_SCALE );
242 handle.SetUniform( MOTION_STRETCH_MODELVIEW_LASTFRAME, Matrix::IDENTITY );
247 void MotionStretchEffect::SetGeometryStretchFactor( float scalingFactor )
249 SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
252 void MotionStretchEffect::SetSpeedScalingFactor( float scalingFactor )
254 SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor );
257 void MotionStretchEffect::SetObjectFadeStart( Vector2 displacement )
259 SetUniform( MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME, displacement );
262 void MotionStretchEffect::SetObjectFadeEnd( Vector2 displacement )
264 SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, displacement );
267 void MotionStretchEffect::SetAlphaScale( float alphaScale )
269 SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, alphaScale );
272 const std::string& MotionStretchEffect::GetGeometryStretchFactorPropertyName() const
274 return MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME;
277 const std::string& MotionStretchEffect::GetSpeedScalingFactorPropertyName() const
279 return MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME;
282 const std::string& MotionStretchEffect::GetObjectFadeStartPropertyName() const
284 return MOTION_STRETCH_OBJECT_FADE_START_PROPERTY_NAME;
287 const std::string& MotionStretchEffect::GetObjectFadeEndPropertyName() const
289 return MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME;
292 const std::string& MotionStretchEffect::GetAlphaScalePropertyName() const
294 return MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME;