Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
instancerepeater.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7#include <math.h>
8#include <QMatrix4x4>
9
10QT_BEGIN_NAMESPACE
11
12/*!
13 \qmltype InstanceModel
14 \inherits Object3D
15 \inqmlmodule QtQuick3D.Helpers
16 \since 6.4
17 \brief Defines a data model based on an instance table.
18
19 The InstanceModel QML type is a data model that provides access to the elements of an \l Instancing table.
20
21 The following roles are available:
22 \table
23 \header
24 \li Role name
25 \li Description
26 \row
27 \li \c modelPosition
28 \li The position of the instance as a \l vector3d
29 \row
30 \li \c modelRotation
31 \li The rotation of the instance as a \l quaternion
32 \row
33 \li \c modelScale
34 \li The scale of the instance as a \l vector3d
35 \row
36 \li \c modelColor
37 \li The \l color of the instance
38 \row
39 \li \c modelData
40 \li The custom data of the instance as a \l vector4d
41 \endtable
42
43 \sa InstanceRepeater
44*/
45
46/*!
47 \qmlproperty Instancing InstanceModel::instancingTable
48
49 This property specifies the underlying instance table of the model.
50*/
51
52InstanceModel::InstanceModel(QObject *parent)
53 : QAbstractListModel(parent)
54{
55}
56
57QVariant InstanceModel::data(const QModelIndex &index, int role) const
58{
59 if (!index.isValid())
60 return QVariant();
61 ensureTable();
62
63 int idx = index.row();
64
65 if (idx >= m_count) {
66 qWarning("InstanceModel: index out of range");
67 return QVariant();
68 }
69
70 auto *instanceData = reinterpret_cast<const QQuick3DInstancing::InstanceTableEntry*>(m_instanceData.data()) + idx;
71
72 switch (role) {
73 case ColorRole:
74 return instanceData->getColor();
75 case PositionRole:
76 return instanceData->getPosition();
77 case RotationRole:
78 return instanceData->getRotation();
79 case ScaleRole:
80 return instanceData->getScale();
81 case CustomDataRole:
82 return instanceData->instanceData;
83 }
84 return QVariant();
85}
86
87int InstanceModel::rowCount(const QModelIndex &) const
88{
89 ensureTable();
90 return m_count;
91}
92
93void InstanceModel::setInstancing(QQuick3DInstancing *instancing)
94{
95 if (m_instancing == instancing)
96 return;
97 QObject::disconnect(m_tableConnection);
98 m_instancing = instancing;
99 if (instancing)
100 m_tableConnection = QObject::connect(instancing, &QQuick3DInstancing::instanceTableChanged, this, &InstanceModel::reset);
101 emit instancingChanged();
102}
103
104const QQuick3DInstancing::InstanceTableEntry *InstanceModel::instanceData(int index) const
105{
106 if (index >= m_count)
107 return nullptr;
108 return reinterpret_cast<const QQuick3DInstancing::InstanceTableEntry*>(m_instanceData.constData()) + index;
109}
110
111void InstanceModel::ensureTable() const
112{
113 auto *that = const_cast<InstanceModel*>(this);
114 if (Q_UNLIKELY(!m_instancing)) {
115 that->m_instanceData.clear();
116 that->m_count = 0;
117 return;
118 }
119 that->m_instanceData = m_instancing->instanceBuffer(&that->m_count);
120 // ShaderInstancing has a count but no table
121 const int tableCount = int(m_instanceData.size() / sizeof(QQuick3DInstancing::InstanceTableEntry));
122 that->m_count = qMin(m_count, tableCount);
123}
124
125void InstanceModel::reset()
126{
127 beginResetModel();
128 m_instanceData.clear();
129 endResetModel();
130}
131
132/*!
133 \qmltype InstanceRepeater
134 \inherits Repeater3D
135 \inqmlmodule QtQuick3D.Helpers
136 \since 6.4
137 \brief Instantiates components based on an instance table.
138
139 The InstanceRepeater type is used to create a number of objects based on an
140 \l Instancing table. It is a \l Repeater3D subtype that takes an Instancing table instead
141 of a data model, and automatically applies \c position, \c scale, and \c rotation.
142
143 One use case is to implement \l {View3D::pick}{picking} by creating invisible dummy objects
144 that match the rendered instances. To improve performance, the dummy objects can be created with a
145 simpler geometry than the instanced models.
146
147 For example:
148 \qml
149 InstanceRepeater {
150 instancingTable: myInstanceTable
151 Model {
152 source: "#Cube"
153 pickable: true
154 property int instanceIndex: index // expose the index, so we can identify the instance
155 opacity: 0
156 }
157 }
158 \endqml
159
160 \sa InstanceModel
161*/
162
163/*!
164 \qmlproperty Instancing InstanceRepeater::instancingTable
165
166 This property specifies the instance table used by the repeater.
167*/
168
169InstanceRepeater::InstanceRepeater(QQuick3DNode *parent)
170 : QQuick3DRepeater(parent)
171{
172}
173
174QQuick3DInstancing *InstanceRepeater::instancing() const
175{
176 return m_model ? m_model->instancing() : nullptr;
177}
178
179void InstanceRepeater::setInstancing(QQuick3DInstancing *instancing)
180{
181 if (m_model && m_model->instancing() == instancing)
182 return;
183 if (!m_model)
184 m_model = new InstanceModel(this);
185 m_model->setInstancing(instancing);
186 setModel(QVariant::fromValue(m_model));
187 emit instancingChanged();
188}
189
190void InstanceRepeater::initDelegate(int index, QQuick3DNode *node)
191{
192 Q_ASSERT(m_model);
193 auto *entry = m_model->instanceData(index);
194 Q_ASSERT(entry);
195 node->setPosition(entry->getPosition());
196 node->setScale(entry->getScale());
197 node->setRotation(entry->getRotation());
198}
199
200QT_END_NAMESPACE