1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dpickresult_p.h"
5#include "qquick3dmodel_p.h"
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmlvaluetype pickResult
11 \inqmlmodule QtQuick3D
12 \brief Contains the results of a pick.
13
14 Created as a return object to View3D::pick.
15*/
16
17QQuick3DPickResult::QQuick3DPickResult()
18 : m_objectHit(nullptr)
19 , m_distance(0.0f)
20 , m_instanceIndex(-1)
21 , m_itemHit(nullptr)
22 , m_hitType(QQuick3DPickResultEnums::HitType::Null)
23{
24
25}
26
27QQuick3DPickResult::QQuick3DPickResult(QQuick3DModel *hitObject,
28 float distanceFromCamera,
29 const QVector2D &uvPosition,
30 const QVector3D &scenePosition,
31 const QVector3D &position,
32 const QVector3D &normal,
33 int instanceIndex)
34 : m_objectHit(hitObject)
35 , m_distance(distanceFromCamera)
36 , m_uvPosition(uvPosition)
37 , m_scenePosition(scenePosition)
38 , m_position(position)
39 , m_normal(normal)
40 , m_instanceIndex(instanceIndex)
41 , m_itemHit(nullptr)
42 , m_hitType(QQuick3DPickResultEnums::HitType::Model)
43{
44}
45
46// NB: we are intentionally storing the sceneNormal in the "m_normal" member variable
47// as 2D Items always have the same face normal, but we can't calculate the scene normal
48// on demand either. This logic should be handled in the respective getters.
49QQuick3DPickResult::QQuick3DPickResult(QQuickItem *itemHit,
50 float distanceFromCamera,
51 const QVector2D &uvPosition,
52 const QVector3D &scenePosition,
53 const QVector3D &position,
54 const QVector3D &sceneNormal)
55 : m_objectHit(nullptr)
56 , m_distance(distanceFromCamera)
57 , m_uvPosition(uvPosition)
58 , m_scenePosition(scenePosition)
59 , m_position(position)
60 , m_normal(sceneNormal)
61 , m_instanceIndex(-1)
62 , m_itemHit(itemHit)
63 , m_hitType(QQuick3DPickResultEnums::HitType::Item)
64{
65
66}
67
68/*!
69 \qmlproperty Model pickResult::objectHit
70 \readonly
71
72 This property holds the model object hit by the pick. This value will be null if
73 \l{pickResult::hitType} {hitType} is not \c pickResult.Model.
74
75 \sa itemHit
76*/
77QQuick3DModel *QQuick3DPickResult::objectHit() const
78{
79 return m_objectHit;
80}
81
82/*!
83 \qmlproperty float pickResult::distance
84 \readonly
85
86 This property holds the distance between the pick origin and the hit position
87 i.e. the length of the ray. In the case of using viewport coordinates for
88 picking the pick origin will be the active camera's position.
89*/
90float QQuick3DPickResult::distance() const
91{
92 return m_distance;
93}
94
95/*!
96 \qmlproperty vector2d pickResult::uvPosition
97 \readonly
98
99 This property holds the UV position of the hit. The UV position is calculated as
100 the normalized local x and y coordinates of the hit point relative to the bounding volume.
101 Useful for further picking against an offscreen-rendered object.
102
103 When \l{pickResult::}{hitType} is \c pickResult.Item this value will represent the position
104 of the hit in the coordinate space of \l{pickResult::}{itemHit}.
105*/
106QVector2D QQuick3DPickResult::uvPosition() const
107{
108 return m_uvPosition;
109}
110
111/*!
112 \qmlproperty vector3d pickResult::scenePosition
113 \readonly
114
115 This property holds the scene position of the hit.
116*/
117QVector3D QQuick3DPickResult::scenePosition() const
118{
119 return m_scenePosition;
120}
121
122/*!
123 \qmlproperty vector3d pickResult::position
124 \readonly
125
126 This property holds the scene position of the hit in local coordinate
127 space.
128*/
129QVector3D QQuick3DPickResult::position() const
130{
131 return m_position;
132}
133
134/*!
135 \qmlproperty vector3d pickResult::normal
136 \readonly
137
138 This property holds the normal of the face that was hit in local coordinate
139 space.
140
141 \note for 2D Items this will always be (0, 0, 1).
142*/
143QVector3D QQuick3DPickResult::normal() const
144{
145 if (m_itemHit)
146 return QVector3D(0, 0, 1);
147
148 return m_normal;
149}
150
151
152/*!
153 \qmlproperty vector3d pickResult::sceneNormal
154 \readonly
155
156 This property holds the normal of the face that was hit in scene coordinate
157 space.
158*/
159QVector3D QQuick3DPickResult::sceneNormal() const
160{
161 if (m_objectHit)
162 return m_objectHit->mapDirectionToScene(localDirection: m_normal);
163
164 return m_normal;
165}
166
167
168/*!
169 \qmlproperty int pickResult::instanceIndex
170 \readonly
171 \since 6.5
172
173 This property holds the index in the instance table for the case
174 where the pick hit an instance of an instanced model.
175*/
176int QQuick3DPickResult::instanceIndex() const
177{
178 return m_instanceIndex;
179}
180
181/*!
182 \qmlproperty Item pickResult::itemHit
183 \readonly
184 \since 6.8
185
186 This property holds the Qt Quick Item hit by the pick. This value will be null if
187 \l{pickResult::}{hitType} is not \c pickResult.Item.
188
189 \sa objectHit
190*/
191
192QQuickItem *QQuick3DPickResult::itemHit() const
193{
194 return m_itemHit;
195}
196
197/*!
198 \qmlproperty enumeration pickResult::hitType
199 \readonly
200 \since 6.8
201
202 This property holds the hit type of the pick result.
203
204 \value PickResult.Null The pick did not hit anything.
205 \value PickResult.Model The pick hit a Model.
206 \value PickResult.Item The pick hit a QQuickItem.
207*/
208
209QQuick3DPickResultEnums::HitType QQuick3DPickResult::hitType() const
210{
211 return m_hitType;
212}
213
214QT_END_NAMESPACE
215

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtquick3d/src/quick3d/qquick3dpickresult.cpp