Qt Quick 3D - Animazione di morphing personalizzata
Mostra come scrivere una geometria personalizzata in C++ con un morph target.

Questo esempio illustra come definire in C++ una geometria personalizzata complessa che contiene una forma di base e un morph target, con vettori normali per entrambi.
Geometria personalizzata
La parte principale di questo esempio consiste nella creazione di una geometria personalizzata con un morph target. A tal fine, creiamo una sottoclasse di ` QQuick3DGeometry`:
class MorphGeometry : public QQuick3DGeometry
{
Q_OBJECT
QML_NAMED_ELEMENT(MorphGeometry)
Q_PROPERTY(int gridSize READ gridSize WRITE setGridSize NOTIFY gridSizeChanged)
public:
MorphGeometry(QQuick3DObject *parent = nullptr);
int gridSize() { return m_gridSize; }
void setGridSize(int gridSize);
signals:
void gridSizeChanged();
private:
void calculateGeometry();
void updateData();
QList<QVector3D> m_positions;
QList<QVector3D> m_normals;
QList<QVector4D> m_colors;
QList<QVector3D> m_targetPositions;
QList<QVector3D> m_targetNormals;
QList<QVector4D> m_targetColors;
QList<quint32> m_indexes;
QByteArray m_vertexBuffer;
QByteArray m_indexBuffer;
QByteArray m_targetBuffer;
int m_gridSize = 50;
QVector3D boundsMin;
QVector3D boundsMax;
};Il costruttore definisce la struttura dei dati della mesh:
MorphGeometry::MorphGeometry(QQuick3DObject *parent)
: QQuick3DGeometry(parent)
{
updateData();
}La funzione ` updateData ` esegue il caricamento effettivo della geometria della mesh:
void MorphGeometry::updateData()
{
clear();
calculateGeometry();
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0,
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addAttribute(QQuick3DGeometry::Attribute::NormalSemantic, 3 * sizeof(float),
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addAttribute(QQuick3DGeometry::Attribute::ColorSemantic, 6 * sizeof(float),
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addTargetAttribute(0, QQuick3DGeometry::Attribute::PositionSemantic, 0);
addTargetAttribute(0, QQuick3DGeometry::Attribute::NormalSemantic, m_targetPositions.size() * sizeof(float) * 3);
addTargetAttribute(0, QQuick3DGeometry::Attribute::ColorSemantic,
m_targetPositions.size() * sizeof(float) * 3 + m_targetNormals.size() * sizeof(float) * 3);
addAttribute(QQuick3DGeometry::Attribute::IndexSemantic, 0,
QQuick3DGeometry::Attribute::ComponentType::U32Type);
const int numVertexes = m_positions.size();
m_vertexBuffer.resize(numVertexes * sizeof(Vertex));
Vertex *vert = reinterpret_cast<Vertex *>(m_vertexBuffer.data());
for (int i = 0; i < numVertexes; ++i) {
Vertex &v = vert[i];
v.position = m_positions[i];
v.normal = m_normals[i];
v.color = m_colors[i];
}
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetPositions.data()), m_targetPositions.size() * sizeof(QVector3D)));
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetNormals.data()), m_targetNormals.size() * sizeof(QVector3D)));
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetColors.data()), m_targetColors.size() * sizeof(QVector4D)));
setStride(sizeof(Vertex));
setVertexData(m_vertexBuffer);
setTargetData(m_targetBuffer);
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
setBounds(boundsMin, boundsMax);
m_indexBuffer = QByteArray(reinterpret_cast<char *>(m_indexes.data()), m_indexes.size() * sizeof(quint32));
setIndexData(m_indexBuffer);
}Chiamiamo ` updateData ` dal costruttore e quando una proprietà è cambiata.
La funzione ` calculateGeometry ` contiene tutti i complessi calcoli matematici necessari per determinare le forme e i vettori normali. Si tratta di un codice specifico per questo esempio, che non verrà spiegato in dettaglio. In generale: per ottenere un'ombreggiatura uniforme, è necessario calcolare il vettore normale per ciascun vertice. Matematicamente, il vettore normale può essere calcolato dalle derivate parziali della funzione che descrive il piano:

In questo esempio, semplifichiamo il tutto utilizzando un'onda cosinusoidale come forma di base, sapendo che la sua derivata è una funzione sinusoidale.
In pratica, i vettori normali possono spesso essere determinati tramite ragionamento geometrico. Per il morph target, utilizziamo il fatto che qualsiasi vettore dal centro di una sfera alla superficie sarà normale alla sfera in quel punto. Si noti che i vettori normali in QtQuick3D devono avere lunghezza unitaria, il che può essere ottenuto utilizzando QVector3D::normalized().
Parte QML
Definiamo un morph target che corrisponda a quello creato nella geometria personalizzata e applichiamo un'animazione al peso, in modo che si alterni tra le due forme:
MorphTarget {
id: morphtarget
attributes: MorphTarget.Position | MorphTarget.Normal | MorphTarget.Color
SequentialAnimation on weight {
PauseAnimation { duration: 1000 }
NumberAnimation { from: 0; to: 1; duration: 4000 }
PauseAnimation { duration: 1000 }
NumberAnimation { from: 1; to: 0; duration: 4000 }
loops: Animation.Infinite
}
}Infine, creiamo un modello utilizzando la nostra geometria personalizzata e vi applichiamo il morph target:
Model {
y: -1
geometry: MorphGeometry {}
morphTargets: [ morphtarget ]
materials: [ material ]
}© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.