summaryrefslogtreecommitdiffstats
path: root/src/animation/frontend/qblendedclipanimator.cpp
blob: 28d00fd320253eaeee28e9999d266518b1754b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include "qblendedclipanimator.h"
#include "qblendedclipanimator_p.h"
#include <Qt3DAnimation/qabstractclipblendnode.h>
#include <Qt3DAnimation/qchannelmapper.h>
#include <Qt3DAnimation/qclock.h>

QT_BEGIN_NAMESPACE

namespace Qt3DAnimation {

QBlendedClipAnimatorPrivate::QBlendedClipAnimatorPrivate()
    : Qt3DAnimation::QAbstractClipAnimatorPrivate()
    , m_blendTreeRoot(nullptr)
{
}

/*!
    \qmltype BlendedClipAnimator
    \instantiates Qt3DAnimation::QBlendedClipAnimator
    \inqmlmodule Qt3D.Animation
    \inherits AbstractClipAnimator
    \since 5.9

    \brief BlendedClipAnimator is a component providing animation playback capabilities of a tree
    of blend nodes.

    An instance of BlendedClipAnimator can be aggregated by an Entity to add the ability to play
    back animation clips and to apply the calculated animation values to properties of QObjects.

    Whereas a ClipAnimator gets its animation data from a single animation clip,
    BlendedClipAnimator can blend together multiple clips. The animation data is obtained by
    evaluating a so called \e {blend tree}. A blend tree is a hierarchical tree structure where the
    leaf nodes are value nodes that encapsulate an animation clip (AbstractAnimationClip); and the
    internal nodes represent blending operations that operate on the nodes pointed to by their
    operand properties.

    To associate a blend tree with a BlendedClipAnimator, set the animator's blendTree property to
    point at the root node of your blend tree:

    \badcode
    BlendedClipAnimator {
        blendTree: AdditiveClipBlend {
            ....
        }
    }
    \endcode

    A blend tree can be constructed from the following node types:

    \note The blend node tree should only be edited when the animator is not running.

    \list
    \li Qt3D.Animation.ClipBlendValue
    \li Qt3D.Animation.LerpClipBlend
    \li Qt3D.Animation.AdditiveClipBlend
    \endlist

    Additional node types will be added over time.

    As an example consider the following blend tree:

    \badcode
        Clip0----
                |
                Lerp Node----
                |           |
        Clip1----           Additive Node
                            |
                    Clip2----
    \endcode

    This can be created and used as follows:

    \badcode
    BlendedClipAnimator {
        blendTree: AdditiveClipBlend {
            baseClip: LerpClipBlend {
                startClip: ClipBlendValue {
                    clip: AnimationClipLoader { source: "walk.json" }
                }

                endClip: ClipBlendValue {
                    clip: AnimationClipLoader { source: "run.json" }
                }
            }

            additiveClip: ClipBlendValue {
                clip: AnimationClipLoader { source: "wave-arm.json" }
            }
        }

        channelMapper: ChannelMapper {...}
        running: true
    }
    \endcode

    By authoring a set of animation clips and blending between them dynamically at runtime with a
    blend tree, we open up a huge set of possible resulting animations. As some simple examples of
    the above blend tree, where alpha is the additive factor and beta is the lerp blend factor we
    can get a 2D continuum of possible animations:

    \badcode
    (alpha = 0, beta = 1) Running, No arm waving --- (alpha = 1, beta = 1) Running, Arm waving
            |                                               |
            |                                               |
            |                                               |
    (alpha = 0, beta = 0) Walking, No arm waving --- (alpha = 0, beta = 1) Running, No arm waving
    \endcode

    More complex blend trees offer even more flexibility for combining your animation clips. Note
    that the values used to control the blend tree (alpha and beta above) are simple properties on
    the blend nodes. This means, that these properties themselves can also be controlled by
    the animation framework.
*/

/*!
    \class Qt3DAnimation::QBlendedClipAnimator
    \inherits Qt3DAnimation::QAbstractClipAnimator

    \inmodule Qt3DAnimation
    \since 5.9

    \brief QBlendedClipAnimator is a component providing animation playback capabilities of a tree
    of blend nodes.

    An instance of QBlendedClipAnimator can be aggregated by a QEntity to add the ability to play
    back animation clips and to apply the calculated animation values to properties of QObjects.

    Whereas a QClipAnimator gets its animation data from a single animation clip,
    QBlendedClipAnimator can blend together multiple clips. The animation data is obtained by
    evaluating a so called \e {blend tree}. A blend tree is a hierarchical tree structure where the
    leaf nodes are value nodes that encapsulate an animation clip (QAbstractAnimationClip); and the
    internal nodes represent blending operations that operate on the nodes pointed to by their
    operand properties.

    To associate a blend tree with a QBlendedClipAnimator, set the animator's blendTree property to
    point at the root node of your blend tree:

    \badcode
    auto blendTreeRoot = new QAdditiveClipBlend();
    ...
    auto animator = new QBlendedClipAnimator();
    animator->setBlendTree(blendTreeRoot);
    \endcode

    A blend tree can be constructed from the following node types:

    \note The blend node tree should only be edited when the animator is not running.

    \list
    \li Qt3DAnimation::QClipBlendValue
    \li Qt3DAnimation::QLerpClipBlend
    \li Qt3DAnimation::QAdditiveClipBlend
    \endlist

    Additional node types will be added over time.

    As an example consider the following blend tree:

    \badcode
        Clip0----
                |
                Lerp Node----
                |           |
        Clip1----           Additive Node
                            |
                    Clip2----
    \endcode

    This can be created and used as follows:

    \code
    // Create leaf nodes of blend tree
    auto clip0 = new QClipBlendValue(
        new QAnimationClipLoader(QUrl::fromLocalFile("walk.json")));
    auto clip1 = new QClipBlendValue(
        new QAnimationClipLoader(QUrl::fromLocalFile("run.json")));
    auto clip2 = new QClipBlendValue(
        new QAnimationClipLoader(QUrl::fromLocalFile("wave-arm.json")));

    // Create blend tree inner nodes
    auto lerpNode = new QLerpClipBlend();
    lerpNode->setStartClip(clip0);
    lerpNode->setEndClip(clip1);
    lerpNode->setBlendFactor(0.5f); // Half-walk, half-run

    auto additiveNode = new QAdditiveClipBlend();
    additiveNode->setBaseClip(lerpNode); // Comes from lerp sub-tree
    additiveNode->setAdditiveClip(clip2);
    additiveNode->setAdditiveFactor(1.0f); // Wave arm fully

    // Run the animator
    auto animator = new QBlendedClipAnimator();
    animator->setBlendTree(additiveNode);
    animator->setChannelMapper(...);
    animator->setRunning(true);
    \endcode

    By authoring a set of animation clips and blending between them dynamically at runtime with a
    blend tree, we open up a huge set of possible resulting animations. As some simple examples of
    the above blend tree, where alpha is the additive factor and beta is the lerp blend factor we
    can get a 2D continuum of possible animations:

    \badcode
    (alpha = 0, beta = 1) Running, No arm waving --- (alpha = 1, beta = 1) Running, Arm waving
            |                                               |
            |                                               |
            |                                               |
    (alpha = 0, beta = 0) Walking, No arm waving --- (alpha = 0, beta = 1) Running, No arm waving
    \endcode

    More complex blend trees offer even more flexibility for combining your animation clips. Note
    that the values used to control the blend tree (alpha and beta above) are simple properties on
    the blend nodes. This means, that these properties themselves can also be controlled by
    the animation framework.

*/
QBlendedClipAnimator::QBlendedClipAnimator(Qt3DCore::QNode *parent)
    : Qt3DAnimation::QAbstractClipAnimator(*new QBlendedClipAnimatorPrivate, parent)
{
}

/*! \internal */
QBlendedClipAnimator::QBlendedClipAnimator(QBlendedClipAnimatorPrivate &dd, Qt3DCore::QNode *parent)
    : Qt3DAnimation::QAbstractClipAnimator(dd, parent)
{
}

QBlendedClipAnimator::~QBlendedClipAnimator()
{
}

/*!
    \qmlproperty AbstractClipBlendNode Qt3D.Animation::BlendedClipAnimator::blendTree

    This property holds the root of the animation blend tree that will
    be evaluated before being interpolated by the animator.
*/
/*!
    \property QBlendedClipAnimator::blendTree

    This property holds the root of the animation blend tree that will be evaluated before being
    interpolated by the animator.
*/
QAbstractClipBlendNode *QBlendedClipAnimator::blendTree() const
{
    Q_D(const QBlendedClipAnimator);
    return d->m_blendTreeRoot;
}

void QBlendedClipAnimator::setBlendTree(QAbstractClipBlendNode *blendTree)
{
    Q_D(QBlendedClipAnimator);
    if (d->m_blendTreeRoot == blendTree)
        return;

    if (d->m_blendTreeRoot)
        d->unregisterDestructionHelper(d->m_blendTreeRoot);

    if (blendTree != nullptr && blendTree->parent() == nullptr)
        blendTree->setParent(this);

    d->m_blendTreeRoot = blendTree;

    if (d->m_blendTreeRoot)
        d->registerDestructionHelper(d->m_blendTreeRoot, &QBlendedClipAnimator::setBlendTree, d->m_blendTreeRoot);

    emit blendTreeChanged(blendTree);
}

/*! \internal */
Qt3DCore::QNodeCreatedChangeBasePtr QBlendedClipAnimator::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QBlendedClipAnimatorData>::create(this);
    QBlendedClipAnimatorData &data = creationChange->data;
    Q_D(const QBlendedClipAnimator);
    data.blendTreeRootId = Qt3DCore::qIdForNode(d->m_blendTreeRoot);
    data.mapperId = Qt3DCore::qIdForNode(d->m_mapper);
    data.clockId = Qt3DCore::qIdForNode(d->m_clock);
    data.running = d->m_running;
    data.loops = d->m_loops;
    data.normalizedTime = d->m_normalizedTime;
    return creationChange;
}

} // namespace Qt3DAnimation

QT_END_NAMESPACE