1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef Q_QUICK3D_INSTANCING_H
5#define Q_QUICK3D_INSTANCING_H
6
7#include <QtQuick3D/qquick3dobject.h>
8#include <QtGui/QVector4D>
9#include <QtGui/QQuaternion>
10#include <QtGui/QColor>
11
12QT_BEGIN_NAMESPACE
13
14class QQuick3DInstancingPrivate;
15
16class Q_QUICK3D_EXPORT QQuick3DInstancing : public QQuick3DObject
17{
18 Q_OBJECT
19 Q_DECLARE_PRIVATE(QQuick3DInstancing)
20
21 QML_NAMED_ELEMENT(Instancing)
22 QML_UNCREATABLE("Instancing is Abstract")
23 QML_ADDED_IN_VERSION(6, 2)
24 Q_PROPERTY(int instanceCountOverride READ instanceCountOverride WRITE setInstanceCountOverride NOTIFY instanceCountOverrideChanged)
25 Q_PROPERTY(bool hasTransparency READ hasTransparency WRITE setHasTransparency NOTIFY hasTransparencyChanged)
26 Q_PROPERTY(bool depthSortingEnabled READ depthSortingEnabled WRITE setDepthSortingEnabled NOTIFY depthSortingEnabledChanged)
27
28public:
29 struct Q_QUICK3D_EXPORT InstanceTableEntry {
30 QVector4D row0;
31 QVector4D row1;
32 QVector4D row2;
33 QVector4D color;
34 QVector4D instanceData;
35
36 QVector3D getPosition() const;
37 QVector3D getScale() const;
38 QQuaternion getRotation() const;
39 QColor getColor() const;
40 };
41
42 explicit QQuick3DInstancing(QQuick3DObject *parent = nullptr);
43 ~QQuick3DInstancing() override;
44
45 QByteArray instanceBuffer(int *instanceCount);
46 int instanceCountOverride() const;
47 bool hasTransparency() const;
48 bool depthSortingEnabled() const;
49
50 Q_REVISION(6, 3) Q_INVOKABLE QVector3D instancePosition(int index);
51 Q_REVISION(6, 3) Q_INVOKABLE QVector3D instanceScale(int index);
52 Q_REVISION(6, 3) Q_INVOKABLE QQuaternion instanceRotation(int index);
53 Q_REVISION(6, 3) Q_INVOKABLE QColor instanceColor(int index);
54 Q_REVISION(6, 3) Q_INVOKABLE QVector4D instanceCustomData(int index);
55
56public Q_SLOTS:
57 void setInstanceCountOverride(int instanceCountOverride);
58 void setHasTransparency(bool hasTransparency);
59 void setDepthSortingEnabled(bool enabled);
60
61Q_SIGNALS:
62 void instanceTableChanged();
63 void instanceNodeDirty();
64 void instanceCountOverrideChanged();
65 void hasTransparencyChanged();
66 void depthSortingEnabledChanged();
67
68protected:
69 virtual QByteArray getInstanceBuffer(int *instanceCount) = 0;
70 void markDirty();
71 static InstanceTableEntry calculateTableEntry(const QVector3D &position,
72 const QVector3D &scale, const QVector3D &eulerRotation,
73 const QColor &color, const QVector4D &customData = {});
74 static InstanceTableEntry calculateTableEntryFromQuaternion(const QVector3D &position,
75 const QVector3D &scale, const QQuaternion &rotation,
76 const QColor &color, const QVector4D &customData = {});
77 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
78
79private:
80 const InstanceTableEntry *getInstanceEntry(int index);
81};
82
83QT_END_NAMESPACE
84
85#endif // Q_QUICK3D_INSTANCING_H
86

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