1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef RANDOMINSTANCING_P_H
16#define RANDOMINSTANCING_P_H
17
18#include <QtQuick3D/QQuick3DInstancing>
19#include <private/qglobal_p.h>
20
21QT_BEGIN_NAMESPACE
22
23class QQuick3DInstanceRange : public QQuick3DObject
24{
25 Q_OBJECT
26
27 QML_ADDED_IN_VERSION(6, 2)
28 Q_PROPERTY(QVariant from READ from WRITE setFrom NOTIFY fromChanged)
29 Q_PROPERTY(QVariant to READ to WRITE setTo NOTIFY toChanged)
30 Q_PROPERTY(bool proportional READ proportional WRITE setProportional NOTIFY proportionalChanged)
31 QML_NAMED_ELEMENT(InstanceRange)
32
33public:
34 explicit QQuick3DInstanceRange(QQuick3DObject *parent = nullptr);
35 ~QQuick3DInstanceRange() override {}
36 QVariant from() const
37 {
38 return m_from;
39 }
40 QVariant to() const
41 {
42 return m_to;
43 }
44
45 bool proportional() const
46 {
47 return m_proportional;
48 }
49
50public Q_SLOTS:
51 void setFrom(QVariant from);
52 void setTo(QVariant to);
53 void setProportional(bool proportional);
54
55Q_SIGNALS:
56 void fromChanged();
57 void toChanged();
58 void proportionalChanged();
59 void changed();
60
61protected:
62 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *) override
63 {
64 return nullptr;
65 }
66private:
67 QVariant m_from;
68 QVariant m_to;
69 bool m_proportional = false;
70};
71
72class QRandomGenerator;
73class QQuick3DRandomInstancing : public QQuick3DInstancing
74{
75 Q_OBJECT
76
77 Q_PROPERTY(int instanceCount READ instanceCount WRITE setInstanceCount NOTIFY instanceCountChanged)
78 Q_PROPERTY(QQuick3DInstanceRange *position READ position WRITE setPosition NOTIFY positionChanged)
79 Q_PROPERTY(QQuick3DInstanceRange *scale READ scale WRITE setScale NOTIFY scaleChanged)
80 Q_PROPERTY(QQuick3DInstanceRange *rotation READ rotation WRITE setRotation NOTIFY rotationChanged)
81 Q_PROPERTY(QQuick3DInstanceRange *color READ color WRITE setColor NOTIFY colorChanged)
82 Q_PROPERTY(ColorModel colorModel READ colorModel WRITE setColorModel NOTIFY colorModelChanged)
83 Q_PROPERTY(QQuick3DInstanceRange *customData READ customData WRITE setCustomData NOTIFY customDataChanged)
84
85 Q_PROPERTY(int randomSeed READ randomSeed WRITE setRandomSeed NOTIFY randomSeedChanged)
86
87 QML_NAMED_ELEMENT(RandomInstancing)
88 QML_ADDED_IN_VERSION(6, 2)
89
90public:
91 enum class ColorModel { RGB, HSV, HSL };
92 Q_ENUM(ColorModel)
93
94 explicit QQuick3DRandomInstancing(QQuick3DObject *parent = nullptr);
95 ~QQuick3DRandomInstancing();
96
97 QByteArray getInstanceBuffer(int *instanceCount) override;
98
99 int instanceCount() const
100 {
101 return m_randomCount;
102 }
103
104 int randomSeed() const
105 {
106 return m_randomSeed;
107 }
108
109 QQuick3DInstanceRange *position() const
110 {
111 return m_position;
112 }
113
114 QQuick3DInstanceRange* scale() const
115 {
116 return m_scale;
117 }
118
119 QQuick3DInstanceRange* rotation() const
120 {
121 return m_rotation;
122 }
123
124 QQuick3DInstanceRange* color() const
125 {
126 return m_color;
127 }
128
129 QQuick3DInstanceRange* customData() const
130 {
131 return m_customData;
132 }
133
134 ColorModel colorModel() const
135 {
136 return m_colorModel;
137 }
138
139public Q_SLOTS:
140 void setInstanceCount(int instanceCount);
141 void setRandomSeed(int randomSeed);
142
143 void setPosition(QQuick3DInstanceRange *position);
144 void setScale(QQuick3DInstanceRange *scale);
145 void setRotation(QQuick3DInstanceRange *rotation);
146 void setColor(QQuick3DInstanceRange *color);
147 void setCustomData(QQuick3DInstanceRange *customData);
148 void setColorModel(ColorModel colorModel);
149
150Q_SIGNALS:
151 void instanceCountChanged();
152 void randomSeedChanged();
153 void positionChanged();
154 void scaleChanged();
155 void rotationChanged();
156 void colorChanged();
157 void customDataChanged();
158 void colorModelChanged();
159
160private Q_SLOTS:
161 void handleChange();
162
163private:
164 void generateInstanceTable();
165 int m_randomCount = 0;
166 int m_randomSeed = -1;
167 bool m_dirty = true;
168 QByteArray m_instanceData;
169 QQuick3DInstanceRange *m_position = nullptr;
170 QQuick3DInstanceRange *m_scale = nullptr;
171 QQuick3DInstanceRange *m_rotation = nullptr;
172 QQuick3DInstanceRange *m_color = nullptr;
173 QQuick3DInstanceRange *m_customData = nullptr;
174 ColorModel m_colorModel = ColorModel::RGB;
175};
176QT_END_NAMESPACE
177#endif // RANDOMINSTANCING_P_H
178

source code of qtquick3d/src/helpers/randominstancing_p.h