aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3d/qquick3dmorphtarget.cpp
blob: d7d2cc516045f29a59a9469cc1befc78283ea063 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dmorphtarget_p.h"

#include <QtQuick3DRuntimeRender/private/qssgrendermorphtarget_p.h>
#include <QtQml/QQmlFile>

QT_BEGIN_NAMESPACE

/*!
    \qmltype MorphTarget
    \inherits Object3D
    \inqmlmodule QtQuick3D
    \brief Defines the properties of a morph target.

    Each \e MorphTarget is a morph target for a \l{Morphing Animation}{vertex animation}. The degree
    of morphing is controlled by changing the \l {weight} property.

    \qml
    MorphTarget {
        id: morphtarget0
        attributes: MorphTarget.Position | MorphTarget.Normal
        weight: 0.5
    }
    \endqml

    The \l {Qt Quick 3D - Morphing Example}{morphing example} shows how to use a morph target.
*/

QQuick3DMorphTarget::QQuick3DMorphTarget(QQuick3DObject *parent)
    : QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::MorphTarget)), parent) {}

QQuick3DMorphTarget::~QQuick3DMorphTarget()
{
}

/*!
    \qmlproperty real MorphTarget::weight

    Specifies the weight of the current morph target. The weight is the multiplication factor used
    by the linear interpolation. A weight of 1 means that this target is fully applied. A weight of
    0 means that it has no influence.
*/

float QQuick3DMorphTarget::weight() const
{
    return m_weight;
}

/*!
    \qmlproperty enumeration MorphTarget::attributes

    Specifies the set of attributes of the current morph target.
    In order to animate vertex attributes in morphing, the mesh must
    contain those target attributes and the morph target must have the
    attributes enabled.

    The attributes for a morph target are specified by OR-ing together the following values:
    \value MorphTarget.Position animates the vertex positions
    \value MorphTarget.Normal animates the normal vectors
    \value MorphTarget.Tangent animates the tangent vectors
    \value MorphTarget.Binormal animates the binormal vectors
    \value MorphTarget.TexCoord0 animates the texture coordinate 0 vectors
    \value MorphTarget.TexCoord1 animates the texture coordinate 1 vectors
    \value MorphTarget.Color animates the vertex color vectors
*/

QQuick3DMorphTarget::MorphTargetAttributes QQuick3DMorphTarget::attributes() const
{
    return m_attributes;
}

void QQuick3DMorphTarget::markAllDirty()
{
    m_dirtyAttributes = 0xffffffff;
    QQuick3DObject::markAllDirty();
}

void QQuick3DMorphTarget::setWeight(float weight)
{
    if (m_weight == weight)
        return;

    m_weight = weight;
    emit weightChanged();
    markDirty(WeightDirty);
}

void QQuick3DMorphTarget::setAttributes(MorphTargetAttributes attributes)
{
    if (m_attributes == attributes)
        return;

    m_attributes = attributes;
    m_numAttribs = 0;
    int flags = attributes;
    while (flags) {
        m_numAttribs += flags & 0x1;
        flags >>= 1;
    }
    emit attributesChanged();
    markDirty(MorphTargetAttributesDirty);
}

QSSGRenderGraphObject *QQuick3DMorphTarget::updateSpatialNode(QSSGRenderGraphObject *node)
{
    if (!node) {
        markAllDirty();
        node = new QSSGRenderMorphTarget();
    }
    QQuick3DObject::updateSpatialNode(node);
    auto modelNode = static_cast<QSSGRenderMorphTarget *>(node);
    if (m_dirtyAttributes & WeightDirty)
        modelNode->weight = m_weight;
    if (m_dirtyAttributes & MorphTargetAttributesDirty)
        modelNode->attributes = QSSGRenderMorphTarget::InputAttributes(int(m_attributes));

    m_dirtyAttributes = 0;

    return modelNode;
}

void QQuick3DMorphTarget::markDirty(QQuick3DMorphTarget::QSSGMorphTargetDirtyType type)
{
    if (!(m_dirtyAttributes & quint32(type))) {
        m_dirtyAttributes |= quint32(type);
        update();
    }
}

size_t QQuick3DMorphTarget::numAttribs()
{
    return m_numAttribs;
}

QT_END_NAMESPACE