1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef Q_QUICK3D_INSTANCING_P_H
5#define Q_QUICK3D_INSTANCING_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick3D/qquick3dinstancing.h>
19#include <QtQuick3D/private/qquick3dobject_p.h>
20
21#include <QtGui/qvector3d.h>
22
23QT_BEGIN_NAMESPACE
24
25class QQuick3DInstancingPrivate : public QQuick3DObjectPrivate
26{
27public:
28 QQuick3DInstancingPrivate();
29 int m_instanceCountOverride = -1;
30 int m_instanceCount = 0;
31 bool m_hasTransparency = false;
32 bool m_instanceDataChanged = true;
33 bool m_instanceCountOverrideChanged = false;
34 bool m_depthSortingEnabled = false;
35};
36
37class Q_QUICK3D_EXPORT QQuick3DInstanceListEntry : public QQuick3DObject
38{
39 Q_OBJECT
40
41 QML_ADDED_IN_VERSION(6, 2)
42 Q_PROPERTY(QVector3D position READ position WRITE setPosition NOTIFY positionChanged)
43 Q_PROPERTY(QVector3D scale READ scale WRITE setScale NOTIFY scaleChanged)
44 Q_PROPERTY(QVector3D eulerRotation READ eulerRotation WRITE setEulerRotation NOTIFY eulerRotationChanged)
45 Q_PROPERTY(QQuaternion rotation READ rotation WRITE setRotation NOTIFY rotationChanged)
46 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
47 Q_PROPERTY(QVector4D customData READ customData WRITE setCustomData NOTIFY customDataChanged)
48 QML_NAMED_ELEMENT(InstanceListEntry)
49
50public:
51 explicit QQuick3DInstanceListEntry(QQuick3DObject *parent = nullptr);
52 ~QQuick3DInstanceListEntry() override {}
53
54 QVector3D position() const
55 {
56 return m_position;
57 }
58 QVector3D scale() const
59 {
60 return m_scale;
61 }
62
63 QVector3D eulerRotation() const
64 {
65 return m_eulerRotation;
66 }
67
68 QQuaternion rotation() const
69 {
70 return m_rotation;
71 }
72
73 QColor color() const
74 {
75 return m_color;
76 }
77
78 QVector4D customData() const
79 {
80 return m_customData;
81 }
82
83public Q_SLOTS:
84 void setPosition(QVector3D position);
85 void setScale(QVector3D scale);
86 void setEulerRotation(QVector3D eulerRotation);
87 void setRotation(QQuaternion rotation);
88 void setColor(QColor color);
89 void setCustomData(QVector4D customData);
90
91Q_SIGNALS:
92 void positionChanged();
93 void scaleChanged();
94 void eulerRotationChanged();
95 void rotationChanged();
96 void colorChanged();
97 void customDataChanged();
98 void changed();
99
100protected:
101 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *) override
102 {
103 return nullptr;
104 }
105
106private:
107 QVector3D m_position;
108 QVector3D m_scale = {1, 1, 1};
109 QVector3D m_eulerRotation;
110 QQuaternion m_rotation;
111 QColor m_color = Qt::white;
112 QVector4D m_customData;
113 bool m_useEulerRotation = true;
114 friend class QQuick3DInstanceList;
115};
116
117class Q_QUICK3D_EXPORT QQuick3DInstanceList : public QQuick3DInstancing
118{
119 Q_OBJECT
120 Q_PROPERTY(QQmlListProperty<QQuick3DInstanceListEntry> instances READ instances)
121 Q_PROPERTY(int instanceCount READ instanceCount NOTIFY instanceCountChanged)
122 QML_NAMED_ELEMENT(InstanceList)
123 QML_ADDED_IN_VERSION(6, 2)
124
125public:
126 explicit QQuick3DInstanceList(QQuick3DObject *parent = nullptr);
127 ~QQuick3DInstanceList() override;
128
129 QByteArray getInstanceBuffer(int *instanceCount) override;
130 QQmlListProperty<QQuick3DInstanceListEntry> instances();
131 int instanceCount() const;
132
133Q_SIGNALS:
134 void instanceCountChanged();
135
136private Q_SLOTS:
137 void handleInstanceChange();
138 void onInstanceDestroyed(QObject *object);
139
140private:
141 void generateInstanceData();
142
143 static void qmlAppendInstanceListEntry(QQmlListProperty<QQuick3DInstanceListEntry> *list, QQuick3DInstanceListEntry *material);
144 static QQuick3DInstanceListEntry *qmlInstanceListEntryAt(QQmlListProperty<QQuick3DInstanceListEntry> *list, qsizetype index);
145 static qsizetype qmlInstanceListEntriesCount(QQmlListProperty<QQuick3DInstanceListEntry> *list);
146 static void qmlClearInstanceListEntries(QQmlListProperty<QQuick3DInstanceListEntry> *list);
147
148 bool m_dirty = true;
149 QByteArray m_instanceData;
150 QList<QQuick3DInstanceListEntry *> m_instances;
151};
152
153class Q_QUICK3D_EXPORT QQuick3DFileInstancing : public QQuick3DInstancing
154{
155 Q_OBJECT
156 QML_NAMED_ELEMENT(FileInstancing)
157 QML_ADDED_IN_VERSION(6, 2)
158 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
159 Q_PROPERTY(int instanceCount READ instanceCount NOTIFY instanceCountChanged)
160
161public:
162 explicit QQuick3DFileInstancing(QQuick3DObject *parent = nullptr);
163 ~QQuick3DFileInstancing() override;
164
165 const QUrl &source() const;
166 void setSource(const QUrl &newSource);
167
168 bool loadFromBinaryFile(const QString &filename);
169 bool loadFromXmlFile(const QString &filename);
170 int writeToBinaryFile(QIODevice *out);
171
172 int instanceCount() const;
173
174Q_SIGNALS:
175 void instanceCountChanged();
176 void sourceChanged();
177
178protected:
179 QByteArray getInstanceBuffer(int *instanceCount) override;
180
181private:
182 bool loadFromFile(const QUrl &source);
183
184private:
185 int m_instanceCount = 0;
186 QByteArray m_instanceData;
187 QFile *m_dataFile = nullptr;
188 bool m_dirty = true;
189 QUrl m_source;
190};
191
192QT_END_NAMESPACE
193
194#endif
195

source code of qtquick3d/src/quick3d/qquick3dinstancing_p.h