-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathCesiumGltfTextures.cpp
More file actions
261 lines (225 loc) · 8.86 KB
/
Copy pathCesiumGltfTextures.cpp
File metadata and controls
261 lines (225 loc) · 8.86 KB
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
// Copyright 2020-2024 CesiumGS, Inc. and Contributors
#include "CesiumGltfTextures.h"
#include "CesiumRuntime.h"
#include "CesiumTextureResource.h"
#include "CesiumTextureUtility.h"
#include "ExtensionImageAssetUnreal.h"
THIRD_PARTY_INCLUDES_START
#include <CesiumGltf/AccessorView.h>
#include <CesiumGltf/Model.h>
#include <CesiumGltf/VertexAttributeSemantics.h>
#include <CesiumGltfReader/GltfReader.h>
THIRD_PARTY_INCLUDES_END
using namespace CesiumAsync;
namespace {
// Determines if a glTF primitive is usable for our purposes.
bool isValidPrimitive(
const CesiumGltf::Model& gltf,
const CesiumGltf::MeshPrimitive& primitive);
// Determines if an Accessor's componentType is valid for an index buffer.
bool isSupportedIndexComponentType(int32_t componentType);
// Determines if the given Primitive mode is one that we support.
bool isSupportedPrimitiveMode(int32_t primitiveMode);
// Determines if the given texture uses mipmaps.
bool doesTextureUseMipmaps(
const CesiumGltf::Model& gltf,
const CesiumGltf::Texture& texture);
// Creates a single texture in the load thread.
SharedFuture<void> createTextureInLoadThread(
const AsyncSystem& asyncSystem,
CesiumGltf::Model& gltf,
CesiumGltf::TextureInfo& textureInfo,
bool sRGB,
const std::vector<bool>& imageNeedsMipmaps);
} // namespace
/*static*/ CesiumAsync::Future<void> CesiumGltfTextures::createInWorkerThread(
const CesiumAsync::AsyncSystem& asyncSystem,
CesiumGltf::Model& model) {
// This array is parallel to model.images and indicates whether each image
// requires mipmaps. An image requires mipmaps if any of its textures have a
// sampler that will use them.
std::vector<bool> imageNeedsMipmaps(model.images.size(), false);
for (const CesiumGltf::Texture& texture : model.textures) {
int32_t imageIndex = texture.source;
if (imageIndex < 0 || imageIndex >= model.images.size()) {
continue;
}
if (!imageNeedsMipmaps[imageIndex]) {
imageNeedsMipmaps[imageIndex] = doesTextureUseMipmaps(model, texture);
}
}
std::vector<SharedFuture<void>> futures;
model.forEachPrimitiveInScene(
-1,
[&imageNeedsMipmaps, &asyncSystem, &futures](
CesiumGltf::Model& gltf,
CesiumGltf::Node& node,
CesiumGltf::Mesh& mesh,
CesiumGltf::MeshPrimitive& primitive,
const glm::dmat4& transform) {
if (!isValidPrimitive(gltf, primitive)) {
return;
}
CesiumGltf::Material* pMaterial =
CesiumGltf::Model::getSafe(&gltf.materials, primitive.material);
if (!pMaterial) {
// A primitive using the default material will not have any textures.
return;
}
if (pMaterial->pbrMetallicRoughness) {
if (pMaterial->pbrMetallicRoughness->baseColorTexture) {
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
*pMaterial->pbrMetallicRoughness->baseColorTexture,
true,
imageNeedsMipmaps));
}
if (pMaterial->pbrMetallicRoughness->metallicRoughnessTexture) {
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
*pMaterial->pbrMetallicRoughness->metallicRoughnessTexture,
false,
imageNeedsMipmaps));
}
}
if (pMaterial->emissiveTexture)
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
*pMaterial->emissiveTexture,
true,
imageNeedsMipmaps));
if (pMaterial->normalTexture)
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
*pMaterial->normalTexture,
false,
imageNeedsMipmaps));
if (pMaterial->occlusionTexture)
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
*pMaterial->occlusionTexture,
false,
imageNeedsMipmaps));
// Initialize water mask if needed.
auto onlyWaterIt = primitive.extras.find("OnlyWater");
auto onlyLandIt = primitive.extras.find("OnlyLand");
if (onlyWaterIt != primitive.extras.end() &&
onlyWaterIt->second.isBool() &&
onlyLandIt != primitive.extras.end() &&
onlyLandIt->second.isBool()) {
bool onlyWater = onlyWaterIt->second.getBoolOrDefault(false);
bool onlyLand = onlyLandIt->second.getBoolOrDefault(true);
if (!onlyWater && !onlyLand) {
// We have to use the water mask
auto waterMaskTextureIdIt = primitive.extras.find("WaterMaskTex");
if (waterMaskTextureIdIt != primitive.extras.end() &&
waterMaskTextureIdIt->second.isInt64()) {
int32_t waterMaskTextureId = static_cast<int32_t>(
waterMaskTextureIdIt->second.getInt64OrDefault(-1));
CesiumGltf::TextureInfo waterMaskInfo;
waterMaskInfo.index = waterMaskTextureId;
if (waterMaskTextureId >= 0 &&
waterMaskTextureId < gltf.textures.size()) {
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
waterMaskInfo,
false,
imageNeedsMipmaps));
}
}
}
}
});
return asyncSystem.all(std::move(futures));
}
namespace {
bool isSupportedIndexComponentType(int32_t componentType) {
return componentType == CesiumGltf::Accessor::ComponentType::UNSIGNED_BYTE ||
componentType == CesiumGltf::Accessor::ComponentType::UNSIGNED_SHORT ||
componentType == CesiumGltf::Accessor::ComponentType::UNSIGNED_INT;
}
bool isSupportedPrimitiveMode(int32_t primitiveMode) {
return primitiveMode == CesiumGltf::MeshPrimitive::Mode::TRIANGLES ||
primitiveMode == CesiumGltf::MeshPrimitive::Mode::TRIANGLE_STRIP ||
primitiveMode == CesiumGltf::MeshPrimitive::Mode::POINTS;
}
// Determines if a glTF primitive is usable for our purposes.
bool isValidPrimitive(
const CesiumGltf::Model& gltf,
const CesiumGltf::MeshPrimitive& primitive) {
if (!isSupportedPrimitiveMode(primitive.mode)) {
// This primitive's mode is not supported.
return false;
}
auto positionAccessorIt =
primitive.attributes.find(CesiumGltf::VertexAttributeSemantics::POSITION);
if (positionAccessorIt == primitive.attributes.end()) {
// This primitive doesn't have a POSITION semantic, so it's not valid.
return false;
}
CesiumGltf::AccessorView<FVector3f> positionView(
gltf,
positionAccessorIt->second);
if (positionView.status() != CesiumGltf::AccessorViewStatus::Valid) {
// This primitive's POSITION accessor is invalid, so the primitive is not
// valid.
return false;
}
const CesiumGltf::Accessor* pIndexAccessor =
CesiumGltf::Model::getSafe(&gltf.accessors, primitive.indices);
if (pIndexAccessor &&
!isSupportedIndexComponentType(pIndexAccessor->componentType)) {
// This primitive's indices are not a supported type, so the primitive is
// not valid.
return false;
}
return true;
}
bool doesTextureUseMipmaps(
const CesiumGltf::Model& gltf,
const CesiumGltf::Texture& texture) {
const CesiumGltf::Sampler& sampler =
CesiumGltf::Model::getSafe(gltf.samplers, texture.sampler);
switch (sampler.minFilter.value_or(
CesiumGltf::Sampler::MinFilter::LINEAR_MIPMAP_LINEAR)) {
case CesiumGltf::Sampler::MinFilter::LINEAR_MIPMAP_LINEAR:
case CesiumGltf::Sampler::MinFilter::LINEAR_MIPMAP_NEAREST:
case CesiumGltf::Sampler::MinFilter::NEAREST_MIPMAP_LINEAR:
case CesiumGltf::Sampler::MinFilter::NEAREST_MIPMAP_NEAREST:
return true;
default: // LINEAR and NEAREST
return false;
}
}
SharedFuture<void> createTextureInLoadThread(
const AsyncSystem& asyncSystem,
CesiumGltf::Model& gltf,
CesiumGltf::TextureInfo& textureInfo,
bool sRGB,
const std::vector<bool>& imageNeedsMipmaps) {
CesiumGltf::Texture* pTexture =
CesiumGltf::Model::getSafe(&gltf.textures, textureInfo.index);
if (pTexture == nullptr)
return asyncSystem.createResolvedFuture().share();
CesiumGltf::Image* pImage =
CesiumGltf::Model::getSafe(&gltf.images, pTexture->source);
if (pImage == nullptr || pImage->pAsset == nullptr)
return asyncSystem.createResolvedFuture().share();
check(pTexture->source >= 0 && pTexture->source < imageNeedsMipmaps.size());
bool needsMips = imageNeedsMipmaps[pTexture->source];
const ExtensionImageAssetUnreal& extension =
ExtensionImageAssetUnreal::getOrCreate(
asyncSystem,
*pImage->pAsset,
sRGB,
needsMips,
std::nullopt);
return extension.getFuture();
}
} // namespace