Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dviewport_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
6#ifndef QSSGVIEW3D_H
7#define QSSGVIEW3D_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtQuick/QQuickItem>
21#include <QtCore/qurl.h>
22
23#include <QtQuick3D/qtquick3dglobal.h>
24#include <QtQuick3D/private/qquick3dpickresult_p.h>
25#if QT_CONFIG(quick_shadereffect)
26#include <QtQuick/private/qquickshadereffectsource_p.h>
27#endif
28
29#include <QtQuick3DRuntimeRender/private/qssgrenderpickresult_p.h>
30
34
35QT_BEGIN_NAMESPACE
36
37class QSSGView3DPrivate;
38class QQuick3DCamera;
39class QQuick3DSceneEnvironment;
40class QQuick3DNode;
43class QQuick3DRenderStats;
44class QQuick3DSceneManager;
45
47class QQuick3DSGRenderNode;
49
50class Q_QUICK3D_EXPORT QQuick3DViewport : public QQuickItem
51{
52 Q_OBJECT
53 Q_PROPERTY(QQmlListProperty<QObject> data READ data DESIGNABLE false FINAL)
54 Q_PROPERTY(QQuick3DCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged FINAL)
55 Q_PROPERTY(QQuick3DSceneEnvironment *environment READ environment WRITE setEnvironment NOTIFY environmentChanged FINAL)
56 Q_PROPERTY(QQuick3DNode *scene READ scene NOTIFY sceneChanged)
57 Q_PROPERTY(QQuick3DNode *importScene READ importScene WRITE setImportScene NOTIFY importSceneChanged FINAL)
58 Q_PROPERTY(RenderMode renderMode READ renderMode WRITE setRenderMode NOTIFY renderModeChanged FINAL)
59#if QT_CONFIG(quick_shadereffect)
60 Q_PROPERTY(QQuickShaderEffectSource::Format renderFormat READ renderFormat WRITE setRenderFormat NOTIFY renderFormatChanged FINAL REVISION(6, 4))
61#endif
62 Q_PROPERTY(QQuick3DRenderStats *renderStats READ renderStats CONSTANT)
63 Q_PROPERTY(QQmlListProperty<QQuick3DObject> extensions READ extensions FINAL REVISION(6, 6))
64 Q_PROPERTY(int explicitTextureWidth READ explicitTextureWidth WRITE setExplicitTextureWidth NOTIFY explicitTextureWidthChanged FINAL REVISION(6, 7))
65 Q_PROPERTY(int explicitTextureHeight READ explicitTextureHeight WRITE setExplicitTextureHeight NOTIFY explicitTextureHeightChanged FINAL REVISION(6, 7))
66 Q_PROPERTY(QSize effectiveTextureSize READ effectiveTextureSize NOTIFY effectiveTextureSizeChanged FINAL REVISION(6, 7))
67 Q_PROPERTY(RenderOverrides renderOverrides READ renderOverrides WRITE setRenderOverrides NOTIFY renderOverridesChanged FINAL REVISION(6, 11))
68 Q_CLASSINFO("DefaultProperty", "data")
69
70 QML_NAMED_ELEMENT(View3D)
71
72public:
73 enum RenderMode {
74 Offscreen,
75 Underlay,
76 Overlay,
77 Inline
78 };
79 Q_ENUM(RenderMode)
80
81 enum class RenderOverride {
82 None = 0x0,
83 DisableInternalPasses = 0x1,
84 };
85 Q_ENUM(RenderOverride)
86 Q_DECLARE_FLAGS(RenderOverrides, RenderOverride)
87 Q_FLAG(RenderOverrides)
88
89 explicit QQuick3DViewport(QQuickItem *parent = nullptr);
90 ~QQuick3DViewport() override;
91
92 QQmlListProperty<QObject> data();
93
94 QQuick3DCamera *camera() const;
95 QQuick3DSceneEnvironment *environment() const;
96 QQuick3DNode *scene() const;
97 QQuick3DNode *importScene() const;
98 RenderMode renderMode() const;
99#if QT_CONFIG(quick_shadereffect)
100 Q_REVISION(6, 4) QQuickShaderEffectSource::Format renderFormat() const;
101#endif
102 QQuick3DRenderStats *renderStats() const;
103
104 QQuick3DSceneRenderer *createRenderer() const;
105
106 bool isTextureProvider() const override;
107 QSGTextureProvider *textureProvider() const override;
108 void releaseResources() override;
109
110 Q_INVOKABLE QVector3D mapFrom3DScene(const QVector3D &scenePos) const;
111 Q_INVOKABLE QVector3D mapTo3DScene(const QVector3D &viewPos) const;
112
113 Q_INVOKABLE QQuick3DPickResult pick(float x, float y) const;
114 Q_REVISION(6, 8) Q_INVOKABLE QQuick3DPickResult pick(float x, float y, QQuick3DModel *model) const;
115 Q_REVISION(6, 8) Q_INVOKABLE QList<QQuick3DPickResult> pickSubset(float x, float y, const QJSValue &models) const;
116 Q_REVISION(6, 2) Q_INVOKABLE QList<QQuick3DPickResult> pickAll(float x, float y) const;
117 Q_REVISION(6, 2) Q_INVOKABLE QQuick3DPickResult rayPick(const QVector3D &origin, const QVector3D &direction) const;
118 Q_REVISION(6, 2) Q_INVOKABLE QList<QQuick3DPickResult> rayPickAll(const QVector3D &origin, const QVector3D &direction) const;
119
120 Q_REVISION(6, 11) Q_INVOKABLE QQuick3DPickResult rayPick(const QVector3D &origin, const QVector3D &direction, QQuick3DModel *model) const;
121 Q_REVISION(6, 11) Q_INVOKABLE QQuick3DPickResult closestPointPick(const QVector3D &origin, float radius, QQuick3DModel *model = nullptr) const;
122 Q_REVISION(6, 11) Q_INVOKABLE QList<QQuick3DObject *> pickInRect(const QPointF &start, const QPointF &end) const;
123
124 void processPointerEventFromRay(const QVector3D &origin, const QVector3D &direction, QPointerEvent *event) const;
125 bool singlePointPick(QSinglePointEvent *event, const QVector3D &origin, const QVector3D &direction);
126
127 Q_REVISION(6, 8) Q_INVOKABLE void setTouchpoint(QQuickItem *target, const QPointF &position, int pointId, bool active);
128
129 QQuick3DLightmapBaker *maybeLightmapBaker();
130 QQuick3DLightmapBaker *lightmapBaker();
131
132 Q_INVOKABLE void bakeLightmap();
133 Q_REVISION(6, 10) Q_INVOKABLE void denoiseLightmap();
134
135 QQmlListProperty<QQuick3DObject> extensions();
136
137 Q_REVISION(6, 7) int explicitTextureWidth() const;
138 Q_REVISION(6, 7) int explicitTextureHeight() const;
139 Q_REVISION(6, 7) QSize effectiveTextureSize() const;
140
141 // Private helpers
142 [[nodiscard]] bool extensionListDirty() const { return m_extensionListDirty; }
143 [[nodiscard]] const QList<QQuick3DObject *> &extensionList() const { return m_extensions; }
144 void clearExtensionListDirty() { m_extensionListDirty = false; }
145
146 Q_REVISION(6, 7) Q_INVOKABLE void rebuildExtensionList();
147
148 enum class PrivateInstanceType : quint8 { XrViewInstance = 1 };
149 explicit QQuick3DViewport(PrivateInstanceType type, QQuickItem *parent = nullptr);
150 [[nodiscard]] bool isXrViewInstance() const { return m_isXrViewInstance; }
151
152 static void updateCameraForLayer(const QQuick3DViewport &view3D, QSSGRenderLayer &layerNode);
153
154 RenderOverrides renderOverrides() const;
155 void setRenderOverrides(RenderOverrides newRenderOverrides);
156
157protected:
158 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
159 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
160 void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override;
161
162 bool event(QEvent *) override;
163 void componentComplete() override;
164
165public Q_SLOTS:
166 void setCamera(QQuick3DCamera *camera);
167 void setEnvironment(QQuick3DSceneEnvironment * environment);
168 void setImportScene(QQuick3DNode *inScene);
169 void setRenderMode(QQuick3DViewport::RenderMode renderMode);
170#if QT_CONFIG(quick_shadereffect)
171 Q_REVISION(6, 4) void setRenderFormat(QQuickShaderEffectSource::Format format);
172#endif
173 Q_REVISION(6, 7) void setExplicitTextureWidth(int width);
174 Q_REVISION(6, 7) void setExplicitTextureHeight(int height);
175 void cleanupDirectRenderer();
176
177 // Setting this true enables picking for all the models, regardless of
178 // the models pickable property.
179 void setGlobalPickingEnabled(bool isEnabled);
180
181private Q_SLOTS:
182 void invalidateSceneGraph();
183 void updateInputProcessing();
184 void onReleaseCachedResources();
185
186Q_SIGNALS:
187 void cameraChanged();
188 void environmentChanged();
189 void sceneChanged();
190 void importSceneChanged();
191 void renderModeChanged();
192 Q_REVISION(6, 4) void renderFormatChanged();
193 Q_REVISION(6, 7) void explicitTextureWidthChanged();
194 Q_REVISION(6, 7) void explicitTextureHeightChanged();
195 Q_REVISION(6, 7) void effectiveTextureSizeChanged();
196
197 void renderOverridesChanged();
198
199private:
200 void setMultiViewCameras(QQuick3DCamera **firstCamera, int count);
201 template <size_t N>
202 void setMultiViewCameras(QQuick3DCamera *(&cameras)[N])
203 {
204 static_assert(N > 1, "Use setCamera for single view");
205 setMultiViewCameras(cameras, N);
206 }
207
208 friend class QQuick3DExtensionListHelper;
209 friend class QQuick3DXrManager;
210 friend class QQuick3DXrManagerPrivate;
211 friend class QQuick3DRenderLayerHelpers;
212
213 Q_DISABLE_COPY(QQuick3DViewport)
214 struct SubsceneInfo {
215 QQuick3DObject* obj = nullptr;
216 QVarLengthArray<QPointF, 16> eventPointScenePositions;
217 };
218 QQuick3DSceneRenderer *getRenderer() const;
219 void updateDynamicTextures();
220 QSGNode *setupOffscreenRenderer(QSGNode *node);
221 QSGNode *setupInlineRenderer(QSGNode *node);
222 void setupDirectRenderer(RenderMode mode);
223 bool checkIsVisible() const;
224 bool internalPick(QPointerEvent *event, const QVector3D &origin = QVector3D(), const QVector3D &direction = QVector3D()) const;
225 QPair<QQuickItem *, QPointF> getItemAndPosition(const QSSGRenderPickResult &pickResult) const;
226 QVarLengthArray<QSSGRenderPickResult, 20> getPickResults(QQuick3DSceneRenderer *renderer, const QVector3D &origin, const QVector3D &direction) const;
227 QVarLengthArray<QSSGRenderPickResult, 20> getPickResults(QQuick3DSceneRenderer *renderer, const QEventPoint &eventPoint) const;
228 bool forwardEventToSubscenes(QPointerEvent *event,
229 bool useRayPicking,
230 QQuick3DSceneRenderer *renderer,
231 const QFlatMap<QQuickItem *, SubsceneInfo> &visitedSubscenes) const;
232
233 void processPickedObject(const QSSGRenderPickResult &pickResult,
234 int pointIndex,
235 QPointerEvent *event,
236 QFlatMap<QQuickItem *, SubsceneInfo> &vistedSubscenes) const;
237 QQuickItem *getSubSceneRootItem(QQuick3DMaterial *material) const;
238 QQuick3DPickResult getNearestPickResult(const QVarLengthArray<QSSGRenderPickResult, 20> &pickResults) const;
239 QQuick3DPickResult processPickResult(const QSSGRenderPickResult &pickResult) const;
240 QQuick3DObject *findFrontendNode(const QSSGRenderGraphObject *backendObject) const;
241 QQuick3DSceneManager *findChildSceneManager(QQuick3DObject *inObject, QQuick3DSceneManager *manager = nullptr);
242 void updateSceneManagerForImportScene();
243 QQuick3DCamera *m_camera = nullptr;
244 QVarLengthArray<QQuick3DCamera *, 2> m_multiViewCameras;
245 QQuick3DSceneEnvironment *m_environment = nullptr;
246 mutable QPointer<QQuick3DSceneEnvironment> m_builtInEnvironment;
247 QQuick3DSceneRootNode *m_sceneRoot = nullptr;
248 QQuick3DNode *m_importScene = nullptr;
249 mutable SGFramebufferObjectNode *m_node = nullptr;
250 mutable QQuick3DSGRenderNode *m_renderNode = nullptr;
251 mutable QQuick3DSGDirectRenderer *m_directRenderer = nullptr;
252 bool m_renderModeDirty = false;
253 RenderMode m_renderMode = Offscreen;
254#if QT_CONFIG(quick_shadereffect)
255 QQuickShaderEffectSource::Format m_renderFormat = QQuickShaderEffectSource::RGBA8;
256#endif
257 int m_explicitTextureWidth = 0;
258 int m_explicitTextureHeight = 0;
259 QSize m_effectiveTextureSize;
260 float m_widthMultiplier = 1.0f;
261 float m_heightMultiplier = 1.0f;
262 QQuick3DRenderStats *m_renderStats = nullptr;
263 bool m_enableInputProcessing = false;
264 QQuick3DLightmapBaker *m_lightmapBaker = nullptr;
265 QList<QQuick3DObject *> m_extensions;
266 bool m_extensionListDirty = false;
267 bool m_isXrViewInstance = false;
268
269 struct TouchState {
270 QQuickItem *target = nullptr;
271 QPointF position;
272 bool isPressed = false;
273 };
274 QPointingDevice *m_syntheticTouchDevice = nullptr;
275 QVarLengthArray<TouchState, 2> m_touchState{2};
276
277 QPointer<QQuickItem> m_prevMouseItem = nullptr;
278 QPointF m_prevMousePos;
279 RenderOverrides m_renderOverrides;
280
281 Q_QUICK3D_PROFILE_ID
282};
283
284QT_END_NAMESPACE
285
286#endif // QSSGVIEW3D_H
Combined button and popup list for selecting options.
static QByteArray prepareCustomShader(QSSGRenderCustomMaterial *customMaterial, const QSSGShaderCustomMaterialAdapter::StringPairList &uniforms, const QByteArray &snippet, QSSGShaderCache::ShaderType shaderType, QSSGCustomShaderMetaData &meta, bool multiViewCompatible)
static QT_BEGIN_NAMESPACE QRhiGraphicsPipeline::BlendFactor toRhiBlendFactor(QQuick3DCustomMaterial::BlendMode mode)
\qmlproperty url CustomMaterial::vertexShader
static void setCustomMaterialFlagsFromShader(QSSGRenderCustomMaterial *material, const QSSGCustomShaderMetaData &meta)