| 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 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QQuick3DInstancingPrivate : public QQuick3DObjectPrivate |
| 26 | { |
| 27 | public: |
| 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 | |
| 37 | class 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 | |
| 50 | public: |
| 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 | |
| 83 | public 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 | |
| 91 | Q_SIGNALS: |
| 92 | void positionChanged(); |
| 93 | void scaleChanged(); |
| 94 | void eulerRotationChanged(); |
| 95 | void rotationChanged(); |
| 96 | void colorChanged(); |
| 97 | void customDataChanged(); |
| 98 | void changed(); |
| 99 | |
| 100 | protected: |
| 101 | QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *) override |
| 102 | { |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | private: |
| 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 | |
| 117 | class 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 | |
| 125 | Q_CLASSINFO("DefaultProperty" , "instances" ) |
| 126 | |
| 127 | public: |
| 128 | explicit QQuick3DInstanceList(QQuick3DObject *parent = nullptr); |
| 129 | ~QQuick3DInstanceList() override; |
| 130 | |
| 131 | QByteArray getInstanceBuffer(int *instanceCount) override; |
| 132 | QQmlListProperty<QQuick3DInstanceListEntry> instances(); |
| 133 | int instanceCount() const; |
| 134 | |
| 135 | Q_SIGNALS: |
| 136 | void instanceCountChanged(); |
| 137 | |
| 138 | private Q_SLOTS: |
| 139 | void handleInstanceChange(); |
| 140 | void onInstanceDestroyed(QObject *object); |
| 141 | |
| 142 | private: |
| 143 | void generateInstanceData(); |
| 144 | |
| 145 | static void qmlAppendInstanceListEntry(QQmlListProperty<QQuick3DInstanceListEntry> *list, QQuick3DInstanceListEntry *material); |
| 146 | static QQuick3DInstanceListEntry *qmlInstanceListEntryAt(QQmlListProperty<QQuick3DInstanceListEntry> *list, qsizetype index); |
| 147 | static qsizetype qmlInstanceListEntriesCount(QQmlListProperty<QQuick3DInstanceListEntry> *list); |
| 148 | static void qmlClearInstanceListEntries(QQmlListProperty<QQuick3DInstanceListEntry> *list); |
| 149 | |
| 150 | bool m_dirty = true; |
| 151 | QByteArray m_instanceData; |
| 152 | QList<QQuick3DInstanceListEntry *> m_instances; |
| 153 | }; |
| 154 | |
| 155 | class Q_QUICK3D_EXPORT QQuick3DFileInstancing : public QQuick3DInstancing |
| 156 | { |
| 157 | Q_OBJECT |
| 158 | QML_NAMED_ELEMENT(FileInstancing) |
| 159 | QML_ADDED_IN_VERSION(6, 2) |
| 160 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
| 161 | Q_PROPERTY(int instanceCount READ instanceCount NOTIFY instanceCountChanged) |
| 162 | |
| 163 | public: |
| 164 | explicit QQuick3DFileInstancing(QQuick3DObject *parent = nullptr); |
| 165 | ~QQuick3DFileInstancing() override; |
| 166 | |
| 167 | const QUrl &source() const; |
| 168 | void setSource(const QUrl &newSource); |
| 169 | |
| 170 | bool loadFromBinaryFile(const QString &filename); |
| 171 | bool loadFromXmlFile(const QString &filename); |
| 172 | int writeToBinaryFile(QIODevice *out); |
| 173 | |
| 174 | int instanceCount() const; |
| 175 | |
| 176 | Q_SIGNALS: |
| 177 | void instanceCountChanged(); |
| 178 | void sourceChanged(); |
| 179 | |
| 180 | protected: |
| 181 | QByteArray getInstanceBuffer(int *instanceCount) override; |
| 182 | |
| 183 | private: |
| 184 | bool loadFromFile(const QUrl &source); |
| 185 | |
| 186 | private: |
| 187 | int m_instanceCount = 0; |
| 188 | QByteArray m_instanceData; |
| 189 | QFile *m_dataFile = nullptr; |
| 190 | bool m_dirty = true; |
| 191 | QUrl m_source; |
| 192 | }; |
| 193 | |
| 194 | QT_END_NAMESPACE |
| 195 | |
| 196 | #endif |
| 197 | |