Fix resource ready state
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / shader-effects / motion-stretch-effect.h
1 #ifndef DALI_TOOLKIT_SHADER_EFFECT_MOTION_STRETCH_H
2 #define DALI_TOOLKIT_SHADER_EFFECT_MOTION_STRETCH_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/object/property-map.h>
25 #include <dali/public-api/rendering/shader.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/visuals/visual-properties.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 /**
35  * @brief Set the properties for the motion stretch
36  */
37 inline void SetMotionStretchProperties(Actor& actor)
38 {
39   actor.RegisterProperty("uGeometryStretchFactor", 0.5f);
40   actor.RegisterProperty("uSpeedScalingFactor", 0.5f);
41   actor.RegisterProperty("uObjectFadeStart", Vector2(0.25f, 0.25f));
42   actor.RegisterProperty("uObjectFadeEnd", Vector2(0.5f, 0.5f));
43   actor.RegisterProperty("uAlphaScale", 0.75f);
44   Property::Index uModelProperty = actor.RegisterProperty("uModelLastFrame", Matrix::IDENTITY);
45
46   Constraint constraint = Constraint::New<Matrix>(actor, uModelProperty, EqualToConstraint());
47   constraint.AddSource(Source(actor, Actor::Property::WORLD_MATRIX));
48   constraint.Apply();
49 }
50
51 /**
52  * @brief Creates a new MotionStretchEffect
53  *
54  * Motion stretch shader works on a per object basis. Objects will stretch in the direction of motion when they move, or if the camera moves.
55  *
56  * Animatable/Constrainable uniforms:
57  *  "uGeometryStretchFactor"  - This scales the amount the geometry stretches along the motion velocity vector.
58  *                              A smaller value means the geometry stretches less, larger it stretches more. Default 0.5.
59  *  "uSpeedScalingFactor"     - This value is used to control how much to fade the actor near the edges, based on the
60  *                              speed the actor is moving. When the actor is at rest this is not applied. Default 0.5.
61  *  "uObjectFadeStart"        - The displacement from the centre of the actor that the actor will start to fade towards
62  *                              its edges. This is used to prevent an unsightly hard edge between the stretched actor and
63  *                              the scene. Depends on the values of the vertices in the vertex stream. When the actor is at
64  *                              rest this is not applied. Default Vector2(0.25, 0.25), which is half way towards the edge for
65  *                              an ImageVisual::QUAD.
66  *  "uObjectFadeEnd"          - The displacement from the centre of the actor that the actor will finish fading towards its edges.
67  *                              This is used to prevent an unsightly hard edge between the stretched actor and the scene. Depends
68  *                              on the values of the vertices in the vertex stream. When the actor is at rest this is not applied.
69  *                              Default 0.5, which is all the way towards the edge for an ImageVisual::QUAD.
70  *  "uAlphaScale"             - Global scaler applied to the alpha of the actor. Used to make the stretched actor a bit more subtle
71  *                              and reveal a bit of the background behind it as it moves. When the actor is at rest this is not
72  *                              applied. Default 0.75.
73  *  "uModelLastFrame"         - The model to world space transformation matrix of the actor in the previous frame.
74  *
75  * @return The newly created Property::Map with the motion stretch effect
76  */
77 inline Property::Map CreateMotionStretchEffect()
78 {
79   std::string vertexSource;
80   vertexSource =
81     "precision mediump float;\n"
82
83     "attribute vec2 aPosition;\n"
84
85     "uniform mat4 uMvpMatrix;\n"
86     "uniform mat4 uModelView;\n"
87     "uniform mat4 uViewMatrix;\n"
88     "uniform mat4 uProjection;\n"
89     "uniform vec3 uSize;\n"
90
91     "uniform mat4  uModelLastFrame;\n"
92     "float timeDelta = 0.0167;\n"
93
94     "uniform float uGeometryStretchFactor;\n"
95     "uniform float uSpeedScalingFactor;\n"
96
97     // outputs
98     "varying vec2 vModelSpaceCenterToPos;\n"
99     "varying vec2 vScreenSpaceVelocityVector;\n"
100     "varying float vSpeed;\n"
101     "varying vec2 vTexCoord;\n"
102
103     "void main()\n"
104     "{\n"
105     // get view space position of vertex this frame and last frame
106     " vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n"
107     " vertexPosition.xyz *= uSize;\n"
108
109     " vec4 viewSpaceVertex = uModelView * vertexPosition;\n"
110     " vec4 viewSpaceVertexLastFrame = uViewMatrix * uModelLastFrame * vertexPosition;\n"
111
112     // work out vertex's last movement in view space
113     " vec3 viewSpacePosDelta = viewSpaceVertex.xyz - viewSpaceVertexLastFrame.xyz;\n"
114     " float reciprocalTimeDelta = 1.0 / timeDelta;\n"
115
116     // get clip space position of vertex this frame and last frame
117     " vec4 clipSpaceVertex = uMvpMatrix * vertexPosition;\n"
118     " vec4 clipSpaceVertexLastFrame = uProjection * viewSpaceVertexLastFrame;\n"
119
120     // decide how much this vertex is 'trailing', i.e. at the back of the object relative to its direction of motion. We do this
121     // 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
122     " float t = 0.0;\n"
123     " float posDeltaLength = length(viewSpacePosDelta);\n"
124     " if(posDeltaLength > 0.001)\n" // avoid div by 0 if object has barely moved
125     " {\n"
126     "   vec4 viewSpaceCenterToPos = uModelView * vec4(aPosition, 0.0, 0.0);\n"
127     "   float centerToVertexDist = length(viewSpaceCenterToPos);\n"
128     "   if(centerToVertexDist > 0.001)\n" // avoid div by 0 if object has vertex at model space origin
129     "   {\n"
130     "     vec3 viewSpacePosDeltaNormalised = viewSpacePosDelta / posDeltaLength;\n"
131     "     vec3 viewSpaceCenterToPosNormalised = viewSpaceCenterToPos.xyz / centerToVertexDist;\n"
132     "     t = (dot(viewSpacePosDeltaNormalised, viewSpaceCenterToPosNormalised) * 0.5 ) + 0.5;\n" // scale and bias from [-1..1] to [0..1]
133     "   }\n"
134     " }\n"
135     // output vertex position lerped with its last position, based on how much it is trailing,
136     // this stretches the geom back along where it has just been, giving a warping effect
137     // We raise t to a power in order that non-trailing vertices are effected much more than trailing ones
138     // 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)
139     " gl_Position = mix(clipSpaceVertexLastFrame, clipSpaceVertex, t * t * t * uGeometryStretchFactor * reciprocalTimeDelta);\n"
140
141     // work out vertex's last movement in normalised device coordinates [-1..1] space, i.e. perspective divide
142     " vec2 ndcVertex = clipSpaceVertex.xy / clipSpaceVertex.w;\n"
143     " vec2 ndcVertexLastFrame = clipSpaceVertexLastFrame.xy / clipSpaceVertexLastFrame.w;\n"
144     // scale and bias so that a value of 1.0 corresponds to screen size (NDC is [-1..1] = 2)
145     " vScreenSpaceVelocityVector = ((ndcVertex - ndcVertexLastFrame) * 0.5 * reciprocalTimeDelta);\n"
146     " vScreenSpaceVelocityVector.y = -vScreenSpaceVelocityVector.y;\n" // TODO negated due to y being inverted in our coordinate system?
147     // calculate a scaling factor proportional to velocity, which we can use to tweak how things look
148     " vSpeed = length(vScreenSpaceVelocityVector) * uSpeedScalingFactor;\n"
149     " vSpeed = clamp(vSpeed, 0.0, 1.0);\n"
150
151     // 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)
152     " vModelSpaceCenterToPos = viewSpaceVertex.xy;\n"
153
154     " vec2 texCoord = aPosition + vec2(0.5);"
155     " vTexCoord = texCoord;\n"
156     "}\n";
157
158   std::string fragmentSource;
159   fragmentSource =
160     "precision mediump float;\n"
161
162     "uniform sampler2D sTexture;\n"
163     "uniform vec4 uColor;\n"
164
165     "uniform vec2 uObjectFadeStart;\n"
166     "uniform vec2 uObjectFadeEnd;\n"
167     "uniform float uAlphaScale;\n"
168
169     // inputs
170     "varying vec2 vModelSpaceCenterToPos;\n"
171     "varying vec2 vScreenSpaceVelocityVector;\n"
172     "varying float vSpeed;\n"
173     "varying vec2 vTexCoord;\n"
174
175     "void main()\n"
176     "{\n"
177     // 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
178     // the stretched object and the background. Use smoothstep also to hide any hard edges (discontinuities) in rate of change of this alpha gradient
179     " vec2 centerToPixel = abs( vModelSpaceCenterToPos );\n"
180     " vec2 fadeToEdges = smoothstep(0.0, 1.0, 1.0 - ((centerToPixel - uObjectFadeStart) / (uObjectFadeEnd - uObjectFadeStart)));\n"
181     " float fadeToEdgesScale = fadeToEdges.x * fadeToEdges.y * uAlphaScale;\n" // apply global scaler
182     " fadeToEdgesScale = mix(1.0, fadeToEdgesScale, vSpeed);\n"                // fade proportional to speed, so opaque when at rest
183
184     // standard actor texel
185     " vec4 colActor = texture2D(sTexture, vTexCoord);\n"
186     " gl_FragColor = colActor;\n"
187     " gl_FragColor.a *= fadeToEdgesScale;\n" // fade actor to its edges based on speed of motion
188     " gl_FragColor *= uColor;\n"
189     "}";
190
191   Property::Map map;
192
193   Property::Map customShader;
194   customShader[Visual::Shader::Property::VERTEX_SHADER]   = vertexSource;
195   customShader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentSource;
196
197   customShader[Visual::Shader::Property::SUBDIVIDE_GRID_X] = 10;
198   customShader[Visual::Shader::Property::SUBDIVIDE_GRID_Y] = 10;
199
200   customShader[Visual::Shader::Property::HINTS] = Shader::Hint::OUTPUT_IS_TRANSPARENT;
201
202   map[Toolkit::Visual::Property::SHADER] = customShader;
203   return map;
204 }
205
206 } // namespace Toolkit
207
208 } // namespace Dali
209
210 #endif // DALI_TOOLKIT_SHADER_EFFECT_MOTION_STRETCH_H