-
Notifications
You must be signed in to change notification settings - Fork 40
/
GLTFMeshCompressionUtils.cpp
291 lines (258 loc) · 11.8 KB
/
GLTFMeshCompressionUtils.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "AccessorUtils.h"
#include "GLTFMeshCompressionUtils.h"
#include "GLTFSDK/MeshPrimitiveUtils.h"
#include "GLTFSDK/ExtensionsKHR.h"
#include "GLTFSDK/BufferBuilder.h"
#pragma warning(push)
#pragma warning(disable: 4018 4081 4244 4267 4389)
#include "draco/compression/encode.h"
#include "draco/core/cycle_timer.h"
#include "draco/io/mesh_io.h"
#include "draco/io/point_cloud_io.h"
#pragma warning(pop)
// Usings for glTF
using namespace Microsoft::glTF;
using namespace Microsoft::glTF::Toolkit;
std::wstring PathConcat(const std::wstring& part1, const std::wstring& part2)
{
wchar_t uriAbsoluteRaw[MAX_PATH];
// Note: PathCchCombine will return the last argument if it's an absolute path
if (FAILED(::PathCchCombine(uriAbsoluteRaw, ARRAYSIZE(uriAbsoluteRaw), part1.c_str(), part2.c_str())))
{
auto msg = L"Could not combine the path names: " + part1 + L" and " + part2;
throw std::invalid_argument(std::string(msg.begin(), msg.end()));
}
return uriAbsoluteRaw;
}
std::string PathConcat(const std::string& part1, const std::string& part2)
{
std::wstring part1W = std::wstring(part1.begin(), part1.end());
std::wstring part2W = std::wstring(part2.begin(), part2.end());
auto pathW = PathConcat(part1W, part2W);
return std::string(pathW.begin(), pathW.end());
}
class FilepathStreamWriter : public IStreamWriter
{
public:
FilepathStreamWriter(std::string uriBase) : m_uriBase(uriBase) {}
virtual ~FilepathStreamWriter() override {}
virtual std::shared_ptr<std::ostream> GetOutputStream(const std::string& filename) const override
{
return std::make_shared<std::ofstream>(PathConcat(m_uriBase, filename), std::ios::binary);
}
private:
const std::string m_uriBase;
};
draco::GeometryAttribute::Type GetTypeFromAttributeName(const std::string& name)
{
if (name == ACCESSOR_POSITION)
{
return draco::GeometryAttribute::Type::POSITION;
}
if (name == ACCESSOR_NORMAL)
{
return draco::GeometryAttribute::Type::NORMAL;
}
if (name == ACCESSOR_TEXCOORD_0)
{
return draco::GeometryAttribute::Type::TEX_COORD;
}
if (name == ACCESSOR_TEXCOORD_1)
{
return draco::GeometryAttribute::Type::TEX_COORD;
}
if (name == ACCESSOR_COLOR_0)
{
return draco::GeometryAttribute::Type::COLOR;
}
if (name == ACCESSOR_JOINTS_0)
{
return draco::GeometryAttribute::Type::GENERIC;
}
if (name == ACCESSOR_WEIGHTS_0)
{
return draco::GeometryAttribute::Type::GENERIC;
}
if (name == ACCESSOR_TANGENT)
{
return draco::GeometryAttribute::Type::GENERIC;
}
return draco::GeometryAttribute::Type::GENERIC;
}
draco::DataType GetDataType(const Accessor& accessor)
{
switch (accessor.componentType)
{
case COMPONENT_BYTE: return draco::DataType::DT_INT8;
case COMPONENT_UNSIGNED_BYTE: return draco::DataType::DT_UINT8;
case COMPONENT_SHORT: return draco::DataType::DT_INT16;
case COMPONENT_UNSIGNED_SHORT: return draco::DataType::DT_UINT16;
case COMPONENT_UNSIGNED_INT: return draco::DataType::DT_UINT32;
case COMPONENT_FLOAT: return draco::DataType::DT_FLOAT32;
}
return draco::DataType::DT_INVALID;
}
template<typename T>
int InitializePointAttribute(draco::Mesh& dracoMesh, const std::string& attributeName, const Document& doc, GLTFResourceReader& reader, Accessor& accessor)
{
auto stride = sizeof(T) * Accessor::GetTypeCount(accessor.type);
auto numComponents = Accessor::GetTypeCount(accessor.type);
draco::PointAttribute pointAttr;
pointAttr.Init(GetTypeFromAttributeName(attributeName), nullptr, numComponents, GetDataType(accessor), accessor.normalized, stride, 0);
int attId = dracoMesh.AddAttribute(pointAttr, true, static_cast<unsigned int>(accessor.count));
auto attrActual = dracoMesh.attribute(attId);
std::vector<T> values = reader.ReadBinaryData<T>(doc, accessor);
if ((accessor.min.empty() || accessor.max.empty()) && !values.empty())
{
auto minmax = AccessorUtils::CalculateMinMax(accessor, values);
accessor.min = minmax.first;
accessor.max = minmax.second;
}
for (draco::PointIndex i(0); i < static_cast<uint32_t>(accessor.count); ++i)
{
attrActual->SetAttributeValue(attrActual->mapped_index(i), &values[i.value() * numComponents]);
}
if (dracoMesh.num_points() == 0)
{
dracoMesh.set_num_points(static_cast<unsigned int>(accessor.count));
}
else if (dracoMesh.num_points() != accessor.count)
{
throw GLTFException("Inconsistent points count.");
}
return attId;
}
void SetEncoderOptions(draco::Encoder& encoder, const CompressionOptions& options)
{
encoder.SetAttributeQuantization(draco::GeometryAttribute::POSITION, options.PositionQuantizationBits);
encoder.SetAttributeQuantization(draco::GeometryAttribute::TEX_COORD, options.TexCoordQuantizationBits);
encoder.SetAttributeQuantization(draco::GeometryAttribute::NORMAL, options.NormalQuantizationBits);
encoder.SetAttributeQuantization(draco::GeometryAttribute::COLOR, options.ColorQuantizationBits);
encoder.SetAttributeQuantization(draco::GeometryAttribute::GENERIC, options.GenericQuantizationBits);
encoder.SetSpeedOptions(options.Speed, options.Speed);
encoder.SetTrackEncodedProperties(true);
}
Document GLTFMeshCompressionUtils::CompressMesh(
std::shared_ptr<IStreamReader> streamReader,
const Document & doc,
CompressionOptions options,
const Mesh & mesh,
BufferBuilder* builder,
std::unordered_set<std::string>& bufferViewsToRemove)
{
GLTFResourceReader reader(streamReader);
Document resultDocument(doc);
draco::Encoder encoder;
SetEncoderOptions(encoder, options);
Mesh resultMesh(mesh);
resultMesh.primitives.clear();
for (const auto& primitive : mesh.primitives)
{
if (primitive.HasExtension<KHR::MeshPrimitives::DracoMeshCompression>())
{
resultMesh.primitives.emplace_back(primitive);
continue;
}
auto dracoExtension = std::make_unique<KHR::MeshPrimitives::DracoMeshCompression>();
draco::Mesh dracoMesh;
auto indices = MeshPrimitiveUtils::GetIndices32(doc, reader, primitive);
size_t numFaces = indices.size() / 3;
dracoMesh.SetNumFaces(numFaces);
for (uint32_t i = 0; i < numFaces; i++)
{
draco::Mesh::Face face;
face[0] = indices[(i * 3) + 0];
face[1] = indices[(i * 3) + 1];
face[2] = indices[(i * 3) + 2];
dracoMesh.SetFace(draco::FaceIndex(i), face);
}
Accessor indiciesAccessor(doc.accessors[primitive.indicesAccessorId]);
bufferViewsToRemove.emplace(indiciesAccessor.bufferViewId);
indiciesAccessor.bufferViewId = "";
indiciesAccessor.byteOffset = 0;
resultDocument.accessors.Replace(indiciesAccessor);
for (const auto& attribute : primitive.attributes)
{
const auto& accessor = doc.accessors[attribute.second];
Accessor attributeAccessor(accessor);
int attId;
switch (accessor.componentType)
{
case COMPONENT_BYTE: attId = InitializePointAttribute<int8_t>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
case COMPONENT_UNSIGNED_BYTE: attId = InitializePointAttribute<uint8_t>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
case COMPONENT_SHORT: attId = InitializePointAttribute<int16_t>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
case COMPONENT_UNSIGNED_SHORT: attId = InitializePointAttribute<uint16_t>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
case COMPONENT_UNSIGNED_INT: attId = InitializePointAttribute<uint32_t>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
case COMPONENT_FLOAT: attId = InitializePointAttribute<float>(dracoMesh, attribute.first, doc, reader, attributeAccessor); break;
default: throw GLTFException("Unknown component type.");
}
bufferViewsToRemove.emplace(accessor.bufferViewId);
attributeAccessor.bufferViewId = "";
attributeAccessor.byteOffset = 0;
resultDocument.accessors.Replace(attributeAccessor);
dracoExtension->attributes.emplace(attribute.first, dracoMesh.attribute(attId)->unique_id());
}
if (primitive.targets.size() > 0)
{
// Set sequential encoding to preserve order of vertices.
encoder.SetEncodingMethod(draco::MESH_SEQUENTIAL_ENCODING);
}
dracoMesh.DeduplicateAttributeValues();
dracoMesh.DeduplicatePointIds();
draco::EncoderBuffer buffer;
const draco::Status status = encoder.EncodeMeshToBuffer(dracoMesh, &buffer);
if (!status.ok()) {
throw GLTFException(std::string("Failed to encode the mesh: ") + status.error_msg());
}
// We must update the original accessors to the encoding out values.
Accessor encodedIndexAccessor(resultDocument.accessors[primitive.indicesAccessorId]);
encodedIndexAccessor.count = encoder.num_encoded_faces() * 3;
resultDocument.accessors.Replace(encodedIndexAccessor);
for (const auto& dracoAttribute : dracoExtension->attributes)
{
auto accessorId = primitive.attributes.at(dracoAttribute.first);
Accessor encodedAccessor(resultDocument.accessors[accessorId]);
encodedAccessor.count = encoder.num_encoded_points();
resultDocument.accessors.Replace(encodedAccessor);
}
// Finally put the encoded data in place.
auto bufferView = builder->AddBufferView(buffer.data(), buffer.size());
dracoExtension->bufferViewId = bufferView.id;
MeshPrimitive resultPrim(primitive);
resultPrim.SetExtension(std::move(dracoExtension));
resultMesh.primitives.emplace_back(resultPrim);
}
resultDocument.meshes.Replace(resultMesh);
return resultDocument;
}
Document GLTFMeshCompressionUtils::CompressMeshes(std::shared_ptr<IStreamReader> streamReader, const Document & doc, CompressionOptions options, const std::string& outputDirectory)
{
Document resultDocument(doc);
auto writerStream = std::make_shared<FilepathStreamWriter>(outputDirectory);
auto writer = std::make_unique<GLTFResourceWriter>(writerStream);
writer->SetUriPrefix(PathConcat(outputDirectory, "MeshCompression"));
std::unique_ptr<BufferBuilder> builder = std::make_unique<BufferBuilder>(std::move(writer),
[&doc](const BufferBuilder& builder) { return std::to_string(doc.buffers.Size() + builder.GetBufferCount()); },
[&doc](const BufferBuilder& builder) { return std::to_string(doc.bufferViews.Size() + builder.GetBufferViewCount()); },
[&doc](const BufferBuilder& builder) { return std::to_string(doc.accessors.Size() + builder.GetAccessorCount()); });
auto buffer = builder->AddBuffer();
std::unordered_set<std::string> bufferViewsToRemove;
for (const auto& mesh : doc.meshes.Elements())
{
resultDocument = CompressMesh(streamReader, resultDocument, options, mesh, builder.get(), bufferViewsToRemove);
}
for (const auto& bufferViewId : bufferViewsToRemove)
{
if (resultDocument.bufferViews.Has(bufferViewId))
{
resultDocument.bufferViews.Remove(bufferViewId);
}
}
builder->Output(resultDocument);
resultDocument.extensionsUsed.emplace(KHR::MeshPrimitives::DRACOMESHCOMPRESSION_NAME);
resultDocument.extensionsRequired.emplace(KHR::MeshPrimitives::DRACOMESHCOMPRESSION_NAME);
return resultDocument;
}