1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3druntimeloader_p.h"
#include <QtQuick3DAssetUtils/private/qssgscenedesc_p.h>
#include <QtQuick3DAssetUtils/private/qssgqmlutilities_p.h>
#include <QtQuick3DAssetUtils/private/qssgrtutilities_p.h>
#include <QtQuick3DAssetImport/private/qssgassetimportmanager_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderbuffermanager_p.h>
#if QT_CONFIG(mimetype)
#include <QtCore/qmimedatabase.h>
#endif
/*!
\qmltype RuntimeLoader
\inherits Node
\inqmlmodule QtQuick3D.AssetUtils
\since 6.2
\brief Imports a 3D asset at runtime.
The RuntimeLoader type provides a way to load a 3D asset directly from source at runtime,
without converting it to QtQuick3D's internal format first.
RuntimeLoader supports .obj and glTF version 2.0 files in both in text (.gltf) and binary
(.glb) formats.
*/
/*!
\qmlproperty url RuntimeLoader::source
This property holds the location of the source file containing the 3D asset.
Changing this property will unload the current asset and attempt to load an asset from
the given URL.
The success or failure of the load operation is indicated by \l status.
*/
/*!
\qmlproperty enumeration RuntimeLoader::status
This property holds the status of the latest load operation.
\value RuntimeLoader.Empty
No URL was specified.
\value RuntimeLoader.Success
The load operation was successful.
\value RuntimeLoader.Error
The load operation failed. A human-readable error message is provided by \l errorString.
\readonly
*/
/*!
\qmlproperty string RuntimeLoader::errorString
This property holds a human-readable string indicating the status of the latest load operation.
\readonly
*/
/*!
\qmlproperty Bounds RuntimeLoader::bounds
This property describes the extents of the bounding volume around the imported model.
\note The value may not be available before the first render
\readonly
*/
/*!
\qmlproperty Instancing RuntimeLoader::instancing
If this property is set, the imported model will not be rendered normally. Instead, a number of
instances will be rendered, as defined by the instance table.
See the \l{Instanced Rendering} overview documentation for more information.
*/
QT_BEGIN_NAMESPACE
QQuick3DRuntimeLoader::QQuick3DRuntimeLoader(QQuick3DNode *parent)
: QQuick3DNode(parent)
{
}
QUrl QQuick3DRuntimeLoader::source() const
{
return m_source;
}
void QQuick3DRuntimeLoader::setSource(const QUrl &newSource)
{
if (m_source == newSource)
return;
const QQmlContext *context = qmlContext(this);
auto resolvedUrl = (context ? context->resolvedUrl(newSource) : newSource);
if (m_source == resolvedUrl)
return;
m_source = resolvedUrl;
emit sourceChanged();
if (isComponentComplete())
loadSource();
}
void QQuick3DRuntimeLoader::componentComplete()
{
QQuick3DNode::componentComplete();
loadSource();
}
QStringList QQuick3DRuntimeLoader::supportedExtensions()
{
static QStringList extensions;
if (!extensions.isEmpty())
return extensions;
static const QStringList supportedExtensions = { QLatin1StringView("obj"),
QLatin1StringView("gltf"),
QLatin1StringView("glb")};
QSSGAssetImportManager importManager;
const auto types = importManager.getImporterPluginInfos();
for (const auto &t : types) {
for (const QString &extension : t.inputExtensions) {
if (supportedExtensions.contains(extension))
extensions << extension;
}
}
return extensions;
}
#if QT_CONFIG(mimetype)
QList<QMimeType> QQuick3DRuntimeLoader::supportedMimeTypes()
{
static QList<QMimeType> mimeTypes;
if (!mimeTypes.isEmpty())
return mimeTypes;
const QStringList &extensions = supportedExtensions();
QMimeDatabase db;
for (const auto &ext : extensions) {
// TODO: Change to db.mimeTypesForExtension(ext), once it is implemented (QTBUG-118566)
const QString fileName = QLatin1StringView("test.") + ext;
mimeTypes << db.mimeTypesForFileName(fileName);
}
return mimeTypes;
}
#endif
static void boxBoundsRecursive(const QQuick3DNode *baseNode, const QQuick3DNode *node, QQuick3DBounds3 &accBounds)
{
if (!node)
return;
if (auto *model = qobject_cast<const QQuick3DModel *>(node)) {
auto b = model->bounds();
for (const QVector3D point : b.bounds.toQSSGBoxPoints()) {
auto p = model->mapPositionToNode(const_cast<QQuick3DNode *>(baseNode), point);
if (Q_UNLIKELY(accBounds.bounds.isEmpty()))
accBounds.bounds = { p, p };
else
accBounds.bounds.include(p);
}
}
for (auto *child : node->childItems())
boxBoundsRecursive(baseNode, qobject_cast<const QQuick3DNode *>(child), accBounds);
}
template<typename Func>
static void applyToModels(QQuick3DObject *obj, Func &&lambda)
{
if (!obj)
return;
for (auto *child : obj->childItems()) {
if (auto *model = qobject_cast<QQuick3DModel *>(child))
lambda(model);
applyToModels(child, lambda);
}
}
void QQuick3DRuntimeLoader::loadSource()
{
delete m_root;
m_root.clear();
QSSGBufferManager::unregisterMeshData(m_assetId);
m_status = Status::Empty;
m_errorString = QStringLiteral("No file selected");
if (!m_source.isValid()) {
emit statusChanged();
emit errorStringChanged();
return;
}
QSSGAssetImportManager importManager;
QSSGSceneDesc::Scene scene;
QString error(QStringLiteral("Unknown error"));
auto result = importManager.importFile(m_source, scene, &error);
switch (result) {
case QSSGAssetImportManager::ImportState::Success:
m_errorString = QStringLiteral("Success!");
m_status = Status::Success;
break;
case QSSGAssetImportManager::ImportState::IoError:
m_errorString = QStringLiteral("IO Error: ") + error;
m_status = Status::Error;
break;
case QSSGAssetImportManager::ImportState::Unsupported:
m_errorString = QStringLiteral("Unsupported: ") + error;
m_status = Status::Error;
break;
}
if (m_status == Status::Success) {
// We create a dummy root node here, as it will be the parent to the first-level nodes
// and resources. If we use 'this' those first-level nodes/resources won't be deleted
// when a new scene is loaded.
m_root = new QQuick3DNode(this);
m_imported = QSSGRuntimeUtils::createScene(*m_root, scene);
m_assetId = scene.id;
m_boundsDirty = true;
m_instancingChanged = m_instancing != nullptr;
updateModels();
// Cleanup scene before deleting.
scene.cleanup();
} else {
m_source.clear();
emit sourceChanged();
}
emit statusChanged();
emit errorStringChanged();
}
void QQuick3DRuntimeLoader::updateModels()
{
if (m_instancingChanged) {
applyToModels(m_imported, [this](QQuick3DModel *model) {
model->setInstancing(m_instancing);
model->setInstanceRoot(m_imported);
});
m_instancingChanged = false;
}
}
QQuick3DRuntimeLoader::Status QQuick3DRuntimeLoader::status() const
{
return m_status;
}
QString QQuick3DRuntimeLoader::errorString() const
{
return m_errorString;
}
QSSGRenderGraphObject *QQuick3DRuntimeLoader::updateSpatialNode(QSSGRenderGraphObject *node)
{
auto *result = QQuick3DNode::updateSpatialNode(node);
if (m_boundsDirty)
QMetaObject::invokeMethod(this, &QQuick3DRuntimeLoader::boundsChanged, Qt::QueuedConnection);
return result;
}
void QQuick3DRuntimeLoader::calculateBounds()
{
if (!m_imported || !m_boundsDirty)
return;
m_bounds.bounds.setEmpty();
boxBoundsRecursive(m_imported, m_imported, m_bounds);
m_boundsDirty = false;
}
const QQuick3DBounds3 &QQuick3DRuntimeLoader::bounds() const
{
if (m_boundsDirty) {
auto *that = const_cast<QQuick3DRuntimeLoader *>(this);
that->calculateBounds();
return that->m_bounds;
}
return m_bounds;
}
QQuick3DInstancing *QQuick3DRuntimeLoader::instancing() const
{
return m_instancing;
}
void QQuick3DRuntimeLoader::setInstancing(QQuick3DInstancing *newInstancing)
{
if (m_instancing == newInstancing)
return;
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DRuntimeLoader::setInstancing,
newInstancing, m_instancing);
m_instancing = newInstancing;
m_instancingChanged = true;
updateModels();
emit instancingChanged();
}
QT_END_NAMESPACE
|