1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <Qt3DQuickRender/private/quick3draycaster_p.h> |
41 | #include <Qt3DQuickRender/private/quick3draycaster_p_p.h> |
42 | #include <Qt3DCore/private/qscene_p.h> |
43 | |
44 | #include <Qt3DCore/QEntity> |
45 | |
46 | #include <QQmlEngine> |
47 | #include <QJSValue> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | namespace Qt3DRender { |
52 | namespace Render { |
53 | namespace Quick { |
54 | |
55 | void Quick3DRayCasterPrivate::dispatchHits(const QAbstractRayCaster::Hits &hits) |
56 | { |
57 | m_hits = hits; |
58 | updateHitEntites(hits&: m_hits, scene: m_scene); |
59 | |
60 | Q_Q(Quick3DRayCaster); |
61 | if (!m_engine) |
62 | m_engine = qmlEngine(q->parent()); |
63 | |
64 | m_jsHits = convertHits(hits: m_hits, engine: m_engine); |
65 | |
66 | bool v = q->blockNotifications(block: true); |
67 | emit q->hitsChanged(hits: m_jsHits); |
68 | q->blockNotifications(block: v); |
69 | } |
70 | |
71 | QJSValue Quick3DRayCasterPrivate::convertHits(const QAbstractRayCaster::Hits &hits, QQmlEngine *engine) |
72 | { |
73 | auto jsHits = engine->newArray(length: hits.length()); |
74 | for (int i=0; i<hits.size(); i++) { |
75 | QJSValue v = engine->newObject(); |
76 | v.setProperty(name: QLatin1String("type" ), value: hits[i].type()); |
77 | v.setProperty(name: QLatin1String("entity" ), value: engine->newQObject(object: hits[i].entity())); |
78 | v.setProperty(name: QLatin1String("distance" ), value: hits[i].distance()); |
79 | { |
80 | QJSValue p = engine->newObject(); |
81 | p.setProperty(name: QLatin1String("x" ), value: hits[i].localIntersection().x()); |
82 | p.setProperty(name: QLatin1String("y" ), value: hits[i].localIntersection().y()); |
83 | p.setProperty(name: QLatin1String("z" ), value: hits[i].localIntersection().z()); |
84 | v.setProperty(name: QLatin1String("localIntersection" ), value: p); |
85 | } |
86 | { |
87 | QJSValue p = engine->newObject(); |
88 | p.setProperty(name: QLatin1String("x" ), value: hits[i].worldIntersection().x()); |
89 | p.setProperty(name: QLatin1String("y" ), value: hits[i].worldIntersection().y()); |
90 | p.setProperty(name: QLatin1String("z" ), value: hits[i].worldIntersection().z()); |
91 | v.setProperty(name: QLatin1String("worldIntersection" ), value: p); |
92 | } |
93 | |
94 | switch (hits[i].type()) { |
95 | case Qt3DRender::QRayCasterHit::TriangleHit: |
96 | v.setProperty(name: QLatin1String("primitiveIndex" ), value: hits[i].primitiveIndex()); |
97 | v.setProperty(name: QLatin1String("vertex1Index" ), value: hits[i].vertex1Index()); |
98 | v.setProperty(name: QLatin1String("vertex2Index" ), value: hits[i].vertex2Index()); |
99 | v.setProperty(name: QLatin1String("vertex3Index" ), value: hits[i].vertex3Index()); |
100 | break; |
101 | case Qt3DRender::QRayCasterHit::LineHit: |
102 | v.setProperty(name: QLatin1String("primitiveIndex" ), value: hits[i].primitiveIndex()); |
103 | v.setProperty(name: QLatin1String("vertex1Index" ), value: hits[i].vertex1Index()); |
104 | v.setProperty(name: QLatin1String("vertex2Index" ), value: hits[i].vertex2Index()); |
105 | break; |
106 | case Qt3DRender::QRayCasterHit::PointHit: |
107 | v.setProperty(name: QLatin1String("primitiveIndex" ), value: hits[i].primitiveIndex()); |
108 | break; |
109 | default: break; |
110 | } |
111 | |
112 | jsHits.setProperty(arrayIndex: i, value: v); |
113 | } |
114 | |
115 | return jsHits; |
116 | } |
117 | |
118 | void Quick3DRayCasterPrivate::appendLayer(QQmlListProperty<QLayer> *list, QLayer *layer) |
119 | { |
120 | QAbstractRayCaster *filter = qobject_cast<QAbstractRayCaster *>(object: list->object); |
121 | if (filter) |
122 | filter->addLayer(layer); |
123 | } |
124 | |
125 | QLayer *Quick3DRayCasterPrivate::layerAt(QQmlListProperty<QLayer> *list, int index) |
126 | { |
127 | QAbstractRayCaster *filter = qobject_cast<QAbstractRayCaster *>(object: list->object); |
128 | if (filter) |
129 | return filter->layers().at(i: index); |
130 | return nullptr; |
131 | } |
132 | |
133 | int Quick3DRayCasterPrivate::layerCount(QQmlListProperty<QLayer> *list) |
134 | { |
135 | QAbstractRayCaster *filter = qobject_cast<QAbstractRayCaster *>(object: list->object); |
136 | if (filter) |
137 | return filter->layers().count(); |
138 | return 0; |
139 | } |
140 | |
141 | void Quick3DRayCasterPrivate::clearLayers(QQmlListProperty<QLayer> *list) |
142 | { |
143 | QAbstractRayCaster *filter = qobject_cast<QAbstractRayCaster *>(object: list->object); |
144 | if (filter) { |
145 | const auto layers = filter->layers(); |
146 | for (QLayer *layer : layers) |
147 | filter->removeLayer(layer); |
148 | } |
149 | } |
150 | |
151 | Quick3DRayCaster::Quick3DRayCaster(QObject *parent) |
152 | : QRayCaster(*new Quick3DRayCasterPrivate(), qobject_cast<Qt3DCore::QNode *>(object: parent)) |
153 | { |
154 | } |
155 | |
156 | QJSValue Quick3DRayCaster::hits() const |
157 | { |
158 | Q_D(const Quick3DRayCaster); |
159 | return d->m_jsHits; |
160 | } |
161 | |
162 | QQmlListProperty<Qt3DRender::QLayer> Qt3DRender::Render::Quick::Quick3DRayCaster::qmlLayers() |
163 | { |
164 | return QQmlListProperty<QLayer>(this, 0, |
165 | &Quick3DRayCasterPrivate::appendLayer, |
166 | &Quick3DRayCasterPrivate::layerCount, |
167 | &Quick3DRayCasterPrivate::layerAt, |
168 | &Quick3DRayCasterPrivate::clearLayers); |
169 | } |
170 | |
171 | } // namespace Quick |
172 | } // namespace Render |
173 | } // namespace Qt3DRender |
174 | |
175 | QT_END_NAMESPACE |
176 | |