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 \qmltype 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{
21
22}
23
24QQuick3DPickResult::QQuick3DPickResult(QQuick3DModel *hitObject,
25 float distanceFromCamera,
26 const QVector2D &uvPosition,
27 const QVector3D &scenePosition,
28 const QVector3D &position,
29 const QVector3D &normal,
30 int instanceIndex)
31 : m_objectHit(hitObject)
32 , m_distance(distanceFromCamera)
33 , m_uvPosition(uvPosition)
34 , m_scenePosition(scenePosition)
35 , m_position(position)
36 , m_normal(normal)
37 , m_instanceIndex(instanceIndex)
38{
39}
40
41/*!
42 \qmlproperty Model PickResult::objectHit
43 \readonly
44
45 This property holds the model object hit by the pick.
46*/
47QQuick3DModel *QQuick3DPickResult::objectHit() const
48{
49 return m_objectHit;
50}
51
52/*!
53 \qmlproperty float PickResult::distance
54 \readonly
55
56 This property holds the distance between the pick origin and the hit position
57 i.e. the length of the ray. In the case of using viewport coordinates for
58 picking the pick origin will be the active camera's position.
59*/
60float QQuick3DPickResult::distance() const
61{
62 return m_distance;
63}
64
65/*!
66 \qmlproperty vector2d PickResult::uvPosition
67 \readonly
68
69 This property holds the UV position of the hit. The UV position is calculated as
70 the normalized local x and y coordinates of the hit point relative to the bounding volume.
71 Useful for further picking against an offscreen-rendered object.
72*/
73QVector2D QQuick3DPickResult::uvPosition() const
74{
75 return m_uvPosition;
76}
77
78/*!
79 \qmlproperty vector3d PickResult::scenePosition
80 \readonly
81
82 This property holds the scene position of the hit.
83*/
84QVector3D QQuick3DPickResult::scenePosition() const
85{
86 return m_scenePosition;
87}
88
89/*!
90 \qmlproperty vector3d PickResult::position
91 \readonly
92
93 This property holds the scene position of the hit in local coordinate
94 space.
95*/
96QVector3D QQuick3DPickResult::position() const
97{
98 return m_position;
99}
100
101/*!
102 \qmlproperty vector3d PickResult::normal
103 \readonly
104
105 This property holds the normal of the face that was hit in local coordinate
106 space.
107*/
108QVector3D QQuick3DPickResult::normal() const
109{
110 return m_normal;
111}
112
113
114/*!
115 \qmlproperty vector3d PickResult::sceneNormal
116 \readonly
117
118 This property holds the normal of the face that was hit in scene coordinate
119 space.
120*/
121QVector3D QQuick3DPickResult::sceneNormal() const
122{
123 if (!m_objectHit)
124 return QVector3D();
125
126 return m_objectHit->mapDirectionToScene(localDirection: m_normal);
127}
128
129
130/*!
131 \qmlproperty int PickResult::instanceIndex
132 \readonly
133 \since 6.5
134
135 This property holds the index in the instance table for the case
136 where the pick hit an instance of an instanced model.
137*/
138int QQuick3DPickResult::instanceIndex() const
139{
140 return m_instanceIndex;
141}
142
143QT_END_NAMESPACE
144

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