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
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "lightmapfile.h"
#include <QFile>
#include <QTextStream>
#include <QStandardPaths>
#include <QDir>
#include <QBuffer>
#include <QQmlEngine>
#include <QtQuick3DRuntimeRender/private/qssglightmapio_p.h>
#include "lightmapviewerhelpers.h"
LightmapFile::LightmapFile(QObject *parent) : QObject { parent } { }
QVariantList LightmapFile::dataList() const
{
return m_dataList;
}
void LightmapFile::loadData()
{
QSharedPointer<QSSGLightmapLoader> loader = QSSGLightmapLoader::open(m_source.toLocalFile());
auto keys = loader ? loader->getKeys() : QList<std::pair<QString, QSSGLightmapIODataTag>>();
m_dataList.clear();
m_dataList.reserve(keys.size());
QVariantMap sceneMetadata;
std::unordered_map<QString, QStringList> keysReferencingMeshes;
// First pass: find scene metadata and populate keysReferencingMesh
for (const auto &[key, tag] : std::as_const(keys)) {
if (tag == QSSGLightmapIODataTag::SceneMetadata) {
sceneMetadata = loader->readMap(key, tag);
continue;
} else if (tag != QSSGLightmapIODataTag::Mesh) {
continue;
}
keysReferencingMeshes[key] = keysReferencingMesh(key);
}
QSet<QString> meshRowAddedForGroup;
// Second pass: populate datalist
for (const auto &[key, tag] : std::as_const(keys)) {
switch (tag) {
case QSSGLightmapIODataTag::Mask:
case QSSGLightmapIODataTag::Texture_Direct:
case QSSGLightmapIODataTag::Texture_Indirect:
case QSSGLightmapIODataTag::Texture_Final: {
const QString tagString = LightmapViewerHelpers::lightmapTagToString(tag);
QString meshKeyForThisImage;
for (auto it = keysReferencingMeshes.cbegin(); it != keysReferencingMeshes.cend(); ++it) {
if (it->second.contains(key)) {
meshKeyForThisImage = it->first;
break;
}
}
// Meshes
if (!meshRowAddedForGroup.contains(key)) {
QVariantMap meshEntry;
meshEntry["kind"] = "mesh";
meshEntry["key"] = meshKeyForThisImage;
meshEntry["display"] = meshKeyForThisImage;
meshEntry["owner"] = key;
m_dataList.push_back(meshEntry);
meshRowAddedForGroup.insert(key);
}
// Images
QVariantMap imgEntry;
imgEntry["kind"] = "image";
imgEntry["key"] = key;
imgEntry["tag"] = tagString;
imgEntry["owner"] = key;
imgEntry["display"] = tagString.split('_').last();
m_dataList.push_back(imgEntry);
} break;
default:
break;
}
}
emit dataListChanged();
const auto processed = LightmapViewerHelpers::processSceneMetadata(sceneMetadata);
m_qtVersion = processed.qtVersion;
m_bakeStart = processed.bakeStartTime;
m_bakeDuration = processed.bakeDuration;
m_options = processed.options;
}
QUrl LightmapFile::source() const
{
return m_source;
}
void LightmapFile::setSource(const QUrl &newSource)
{
if (m_source == newSource)
return;
m_source = newSource;
emit sourceChanged();
}
QVariantList LightmapFile::metadataFor(const QVariant &selectedEntry)
{
const QString key = selectedEntry.toMap()["owner"].toString();
if (key.isEmpty())
return {};
auto loader = QSSGLightmapLoader::open(m_source.toLocalFile());
if (!loader)
return {};
const auto metadata = loader->readMap(key, QSSGLightmapIODataTag::Metadata);
if (metadata.isEmpty())
return {};
return LightmapViewerHelpers::processMetadata(key, metadata);
}
QStringList LightmapFile::keysReferencingMesh(const QString &meshKey) const
{
QStringList out;
auto loader = QSSGLightmapLoader::open(m_source.toLocalFile());
if (!loader)
return out;
const auto keys = loader->getKeys();
for (const auto &kp : keys) {
if (kp.second != QSSGLightmapIODataTag::Metadata)
continue;
const QVariantMap md = loader->readMap(kp.first, QSSGLightmapIODataTag::Metadata);
if (md.value(QStringLiteral("mesh_key")).toString() == meshKey)
out << kp.first;
}
out.removeDuplicates();
return out;
}
QQuick3DTextureData *LightmapFile::textureDataFor(const QString &key, quint32 tag)
{
if (key.isEmpty())
return nullptr;
auto loader = QSSGLightmapLoader::open(m_source.toLocalFile());
if (!loader)
return nullptr;
const QVariantMap md = loader->readMap(key, QSSGLightmapIODataTag::Metadata);
const int w = md.value(QStringLiteral("width")).toInt();
const int h = md.value(QStringLiteral("height")).toInt();
if (w <= 0 || h <= 0)
return nullptr;
const QSSGLightmapIODataTag actualTag = static_cast<QSSGLightmapIODataTag>(tag);
QByteArray src;
if (actualTag == QSSGLightmapIODataTag::Mask) {
QByteArray src = loader->readU32Image(key, actualTag);
if (src.size() != w * h * int(sizeof(quint32)))
return nullptr;
LightmapViewerHelpers::maskToBBGRColor(src, false);
QByteArray dst;
dst.resize(src.size());
const int stride = w * 4;
const uchar* s = reinterpret_cast<const uchar*>(src.constData());
uchar* d = reinterpret_cast<uchar*>(dst.data());
for (int y = 0; y < h; ++y) {
const int srcRow = y;
const int dstRow = (h - 1) - y;
memcpy(d + dstRow * stride, s + srcRow * stride, size_t(stride));
}
auto *tex = new QQuick3DTextureData;
QQmlEngine::setObjectOwnership(tex, QQmlEngine::CppOwnership);
tex->setSize(QSize(w, h));
tex->setFormat(QQuick3DTextureData::RGBA8);
tex->setHasTransparency(false);
tex->setTextureData(dst);
return tex;
}
if (actualTag == QSSGLightmapIODataTag::Texture_Final
|| actualTag == QSSGLightmapIODataTag::Texture_Direct
|| actualTag == QSSGLightmapIODataTag::Texture_Indirect) {
src = loader->readF32Image(key, actualTag);
if (src.size() != w * h * int(4 * sizeof(float)))
return nullptr;
QByteArray dst;
dst.resize(src.size());
const float *srcF = reinterpret_cast<const float *>(src.constData());
float *dstF = reinterpret_cast<float *>(dst.data());
const int strideFloats = w * 4;
for (int y = 0; y < h; ++y) {
const int srcRow = y;
const int dstRow = (h - 1) - y;
memcpy(dstF + dstRow * strideFloats,
srcF + srcRow * strideFloats,
size_t(strideFloats) * sizeof(float));
}
auto *tex = new QQuick3DTextureData;
QQmlEngine::setObjectOwnership(tex, QQmlEngine::CppOwnership);
tex->setSize(QSize(w, h));
tex->setFormat(QQuick3DTextureData::RGBA32F);
tex->setHasTransparency(false);
tex->setTextureData(dst);
return tex;
}
return nullptr;
}
QVariantList LightmapFile::texturesAvailableFor(const QString &key) const
{
if (key.isEmpty())
return {};
auto loader = QSSGLightmapLoader::open(m_source.toLocalFile());
if (!loader)
return {};
QVariantList out;
const auto keys = loader->getKeys();
for (const auto &kp : keys) {
if (kp.first != key) continue;
const QSSGLightmapIODataTag tag = kp.second;
if (tag== QSSGLightmapIODataTag::Mask || tag== QSSGLightmapIODataTag::Texture_Direct
|| tag == QSSGLightmapIODataTag::Texture_Indirect || tag == QSSGLightmapIODataTag::Texture_Final) {
out << QVariantMap { {"name", LightmapViewerHelpers::lightmapTagToString(tag).replace("Texture_", "")},
{"value", static_cast<quint32>(tag)}
};
}
}
return out;
}
|