1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QSSGMODEL_H
5#define QSSGMODEL_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/private/qquick3dnode_p.h>
19#include <QtQuick3D/private/qquick3dmaterial_p.h>
20#include <QtQuick3D/private/qquick3dgeometry_p.h>
21#include <QtQuick3D/private/qquick3dinstancing_p.h>
22#include <QtQuick3D/private/qquick3dskeleton_p.h>
23#include <QtQuick3D/private/qquick3dskin_p.h>
24#include <QtQuick3D/private/qquick3dbakedlightmap_p.h>
25#include <QtQuick3D/private/qquick3dmorphtarget_p.h>
26#include <QtQuick3DUtils/private/qssgbounds3_p.h>
27
28#include <QtQml/QQmlListProperty>
29
30#include <QtCore/QVector>
31#include <QtCore/QList>
32#include <QtCore/QUrl>
33
34#include <QtGui/QVector4D>
35
36QT_BEGIN_NAMESPACE
37
38class Q_QUICK3D_EXPORT QQuick3DBounds3
39{
40 Q_GADGET
41 Q_PROPERTY(QVector3D minimum READ minimum CONSTANT)
42 Q_PROPERTY(QVector3D maximum READ maximum CONSTANT)
43 QML_VALUE_TYPE(bounds)
44
45public:
46 QVector3D minimum() const
47 {
48 return bounds.minimum;
49 }
50
51 QVector3D maximum() const
52 {
53 return bounds.maximum;
54 }
55
56 // For compatibility we start with zero values.
57 QSSGBounds3 bounds { QVector3D{}, QVector3D{} };
58};
59
60class Q_QUICK3D_EXPORT QQuick3DModel : public QQuick3DNode
61{
62 Q_OBJECT
63 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
64 Q_PROPERTY(bool castsShadows READ castsShadows WRITE setCastsShadows NOTIFY castsShadowsChanged)
65 Q_PROPERTY(bool receivesShadows READ receivesShadows WRITE setReceivesShadows NOTIFY receivesShadowsChanged)
66 Q_PROPERTY(QQmlListProperty<QQuick3DMaterial> materials READ materials)
67 Q_PROPERTY(QQmlListProperty<QQuick3DMorphTarget> morphTargets READ morphTargets NOTIFY morphTargetsChanged)
68 Q_PROPERTY(bool pickable READ pickable WRITE setPickable NOTIFY pickableChanged)
69 Q_PROPERTY(QQuick3DGeometry *geometry READ geometry WRITE setGeometry NOTIFY geometryChanged)
70 Q_PROPERTY(QQuick3DInstancing *instancing READ instancing WRITE setInstancing NOTIFY instancingChanged)
71 Q_PROPERTY(QQuick3DNode *instanceRoot READ instanceRoot WRITE setInstanceRoot NOTIFY instanceRootChanged)
72 Q_PROPERTY(QQuick3DSkeleton *skeleton READ skeleton WRITE setSkeleton NOTIFY skeletonChanged)
73 Q_PROPERTY(QQuick3DSkin *skin READ skin WRITE setSkin NOTIFY skinChanged REVISION(6, 4))
74 Q_PROPERTY(QList<QMatrix4x4> inverseBindPoses READ inverseBindPoses WRITE setInverseBindPoses NOTIFY inverseBindPosesChanged)
75 Q_PROPERTY(QQuick3DBounds3 bounds READ bounds NOTIFY boundsChanged)
76 Q_PROPERTY(float depthBias READ depthBias WRITE setDepthBias NOTIFY depthBiasChanged)
77 Q_PROPERTY(bool receivesReflections READ receivesReflections WRITE setReceivesReflections NOTIFY receivesReflectionsChanged REVISION(6, 3))
78 Q_PROPERTY(bool castsReflections READ castsReflections WRITE setCastsReflections NOTIFY castsReflectionsChanged REVISION(6, 4))
79 Q_PROPERTY(bool usedInBakedLighting READ isUsedInBakedLighting WRITE setUsedInBakedLighting NOTIFY usedInBakedLightingChanged REVISION(6, 4))
80 Q_PROPERTY(int lightmapBaseResolution READ lightmapBaseResolution WRITE setLightmapBaseResolution NOTIFY lightmapBaseResolutionChanged REVISION(6, 4))
81 Q_PROPERTY(QQuick3DBakedLightmap *bakedLightmap READ bakedLightmap WRITE setBakedLightmap NOTIFY bakedLightmapChanged REVISION(6, 4))
82 Q_PROPERTY(float instancingLodMin READ instancingLodMin WRITE setInstancingLodMin NOTIFY instancingLodMinChanged REVISION(6, 5))
83 Q_PROPERTY(float instancingLodMax READ instancingLodMax WRITE setInstancingLodMax NOTIFY instancingLodMaxChanged REVISION(6, 5))
84 Q_PROPERTY(float levelOfDetailBias READ levelOfDetailBias WRITE setLevelOfDetailBias NOTIFY levelOfDetailBiasChanged REVISION(6, 5))
85
86 QML_NAMED_ELEMENT(Model)
87
88public:
89 explicit QQuick3DModel(QQuick3DNode *parent = nullptr);
90 ~QQuick3DModel() override;
91
92 QUrl source() const;
93 bool castsShadows() const;
94 bool receivesShadows() const;
95 bool pickable() const;
96 QQuick3DGeometry *geometry() const;
97 QQuick3DSkeleton *skeleton() const;
98 QList<QMatrix4x4> inverseBindPoses() const;
99 QQuick3DBounds3 bounds() const;
100 float depthBias() const;
101
102 QQmlListProperty<QQuick3DMaterial> materials();
103 QQmlListProperty<QQuick3DMorphTarget> morphTargets();
104
105 QQuick3DInstancing *instancing() const;
106 QQuick3DNode *instanceRoot() const;
107
108 Q_REVISION(6, 3) bool receivesReflections() const;
109 Q_REVISION(6, 4) bool castsReflections() const;
110 Q_REVISION(6, 4) QQuick3DSkin *skin() const;
111
112 static QString translateMeshSource(const QUrl &source, QObject *contextObject);
113
114 Q_REVISION(6, 4) bool isUsedInBakedLighting() const;
115 Q_REVISION(6, 4) int lightmapBaseResolution() const;
116 Q_REVISION(6, 4) QQuick3DBakedLightmap *bakedLightmap() const;
117
118 Q_REVISION(6, 5) float instancingLodMin() const;
119 Q_REVISION(6, 5) float instancingLodMax() const;
120 Q_REVISION(6, 5) float levelOfDetailBias() const;
121
122public Q_SLOTS:
123 void setSource(const QUrl &source);
124 void setCastsShadows(bool castsShadows);
125 void setReceivesShadows(bool receivesShadows);
126 void setPickable(bool pickable);
127 void setGeometry(QQuick3DGeometry *geometry);
128 void setSkeleton(QQuick3DSkeleton *skeleton);
129 void setInverseBindPoses(const QList<QMatrix4x4> &poses);
130 void setBounds(const QVector3D &min, const QVector3D &max);
131 void setInstancing(QQuick3DInstancing *instancing);
132 void setInstanceRoot(QQuick3DNode *instanceRoot);
133 void setDepthBias(float bias);
134 Q_REVISION(6, 3) void setReceivesReflections(bool receivesReflections);
135 Q_REVISION(6, 4) void setCastsReflections(bool castsReflections);
136 Q_REVISION(6, 4) void setSkin(QQuick3DSkin *skin);
137 Q_REVISION(6, 4) void setUsedInBakedLighting(bool enable);
138 Q_REVISION(6, 4) void setLightmapBaseResolution(int resolution);
139 Q_REVISION(6, 4) void setBakedLightmap(QQuick3DBakedLightmap *bakedLightmap);
140
141 Q_REVISION(6, 5) void setInstancingLodMin(float minDistance);
142 Q_REVISION(6, 5) void setInstancingLodMax(float maxDistance);
143 Q_REVISION(6, 5) void setLevelOfDetailBias(float newLevelOfDetailBias);
144
145Q_SIGNALS:
146 void sourceChanged();
147 void castsShadowsChanged();
148 void receivesShadowsChanged();
149 void pickableChanged();
150 void geometryChanged();
151 void skeletonChanged();
152 void inverseBindPosesChanged();
153 void boundsChanged();
154 void instancingChanged();
155 void instanceRootChanged();
156 void morphTargetsChanged();
157 void depthBiasChanged();
158 Q_REVISION(6, 3) void receivesReflectionsChanged();
159 Q_REVISION(6, 4) void castsReflectionsChanged();
160 Q_REVISION(6, 4) void skinChanged();
161 Q_REVISION(6, 4) void usedInBakedLightingChanged();
162 Q_REVISION(6, 4) void lightmapBaseResolutionChanged();
163 Q_REVISION(6, 4) void bakedLightmapChanged();
164
165 Q_REVISION(6, 5) void instancingLodMinChanged();
166 Q_REVISION(6, 5) void instancingLodMaxChanged();
167 Q_REVISION(6, 5) void levelOfDetailBiasChanged();
168
169protected:
170 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
171 void markAllDirty() override;
172 void itemChange(ItemChange, const ItemChangeData &) override;
173
174private Q_SLOTS:
175 void onMaterialDestroyed(QObject *object);
176 void onMorphTargetDestroyed(QObject *object);
177
178private:
179 enum QSSGModelDirtyType {
180 SourceDirty = 0x00000001,
181 MaterialsDirty = 0x00000002,
182 ShadowsDirty = 0x00000004,
183 PickingDirty = 0x00000008,
184 GeometryDirty = 0x00000010,
185 SkeletonDirty = 0x00000020,
186 PoseDirty = 0x00000040,
187 InstancesDirty = 0x00000080,
188 MorphTargetsDirty = 0x00000100,
189 PropertyDirty = 0x00000200,
190 ReflectionDirty = 0x00000400,
191 SkinDirty = 0x00000800,
192 LodDirty = 0x00001000,
193 InstanceRootDirty = 0x00002000,
194 };
195
196 QUrl m_source;
197
198 quint32 m_dirtyAttributes = 0xffffffff; // all dirty by default
199 void markDirty(QSSGModelDirtyType type);
200 void updateSceneManager(QQuick3DSceneManager *sceneManager);
201
202 static void qmlAppendMaterial(QQmlListProperty<QQuick3DMaterial> *list, QQuick3DMaterial *material);
203 static QQuick3DMaterial *qmlMaterialAt(QQmlListProperty<QQuick3DMaterial> *list, qsizetype index);
204 static qsizetype qmlMaterialsCount(QQmlListProperty<QQuick3DMaterial> *list);
205 static void qmlClearMaterials(QQmlListProperty<QQuick3DMaterial> *list);
206
207 struct Material {
208 QQuick3DMaterial *material;
209 bool refed;
210 };
211 QVector<Material> m_materials;
212
213 static void qmlAppendMorphTarget(QQmlListProperty<QQuick3DMorphTarget> *list, QQuick3DMorphTarget *morphTarget);
214 static QQuick3DMorphTarget *qmlMorphTargetAt(QQmlListProperty<QQuick3DMorphTarget> *list, qsizetype index);
215 static qsizetype qmlMorphTargetsCount(QQmlListProperty<QQuick3DMorphTarget> *list);
216 static void qmlClearMorphTargets(QQmlListProperty<QQuick3DMorphTarget> *list);
217 QVector<QQuick3DMorphTarget *> m_morphTargets;
218 size_t m_numMorphAttribs = 0;
219 QQuick3DGeometry *m_geometry = nullptr;
220 QQuick3DBounds3 m_bounds;
221 QQuick3DSkeleton *m_skeleton = nullptr;
222 QList<QMatrix4x4> m_inverseBindPoses;
223 QQuick3DInstancing *m_instancing = nullptr;
224 QQuick3DNode *m_instanceRoot = nullptr;
225 QMetaObject::Connection m_geometryConnection;
226 QMetaObject::Connection m_instancingConnection;
227 float m_depthBias = 0.0f;
228 bool m_castsShadows = true;
229 bool m_receivesShadows = true;
230 bool m_pickable = false;
231 bool m_receivesReflections = false;
232 bool m_castsReflections = true;
233 bool m_usedInBakedLighting = false;
234 int m_lightmapBaseResolution = 1024;
235 QQuick3DBakedLightmap *m_bakedLightmap = nullptr;
236 QMetaObject::Connection m_bakedLightmapSignalConnection;
237 QQuick3DSkin *m_skin = nullptr;
238 float m_instancingLodMin = -1;
239 float m_instancingLodMax = -1;
240 float m_levelOfDetailBias = 1.0f;
241};
242
243QT_END_NAMESPACE
244
245Q_DECLARE_METATYPE(QQuick3DBounds3)
246
247#endif // QSSGMODEL_H
248

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