Skip to content

Commit a55c8a2

Browse files
marpenajadojo
authored andcommitted
Perform material processing after lod merging to fix share material comparison (#45)
* Don't bother comparing spec gloss members since top level materials are converted to metal rough * Revert changes to material comparison in LOD merging * Perform material processing after LOD merge
1 parent cbc4a2d commit a55c8a2

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

WindowsMRAssetConverter/WindowsMRAssetConverter.cpp

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,45 @@ class GLBStreamWriter : public Microsoft::glTF::IStreamWriter
6262
std::shared_ptr<std::ofstream> m_stream;
6363
};
6464

65+
Document ProcessTextures(
66+
size_t maxTextureSize,
67+
TexturePacking packing,
68+
bool retainOriginalImages,
69+
const std::wstring& tempDirectory,
70+
const Document& document,
71+
const std::shared_ptr<GLTFStreamReader>& streamReader)
72+
{
73+
Document resultDocument(document);
74+
75+
std::string tempDirectoryA(tempDirectory.begin(), tempDirectory.end());
76+
77+
std::wcout << L"Specular Glossiness conversion..." << std::endl;
78+
79+
// 0. Specular Glossiness conversion
80+
resultDocument = GLTFSpecularGlossinessUtils::ConvertMaterials(streamReader, resultDocument, tempDirectoryA);
81+
82+
std::wcout << L"Removing redundant textures and images..." << std::endl;
83+
84+
// 1. Remove redundant textures and images
85+
resultDocument = GLTFTextureUtils::RemoveRedundantTexturesAndImages(resultDocument);
86+
87+
std::wcout << L"Packing textures..." << std::endl;
88+
89+
// 2. Texture Packing
90+
resultDocument = GLTFTexturePackingUtils::PackAllMaterialsForWindowsMR(streamReader, resultDocument, packing, tempDirectoryA);
91+
92+
std::wcout << L"Compressing textures - this can take a few minutes..." << std::endl;
93+
94+
// 3. Texture Compression
95+
resultDocument = GLTFTextureCompressionUtils::CompressAllTexturesForWindowsMR(streamReader, resultDocument, tempDirectoryA, maxTextureSize, retainOriginalImages);
96+
97+
return resultDocument;
98+
}
99+
65100
Document LoadAndConvertDocumentForWindowsMR(
66101
std::wstring& inputFilePath,
67102
AssetType inputAssetType,
68103
const std::wstring& tempDirectory,
69-
size_t maxTextureSize,
70-
TexturePacking packing,
71-
bool processTextures,
72-
bool retainOriginalImages,
73104
bool meshCompression)
74105
{
75106
// Load the document
@@ -101,29 +132,6 @@ Document LoadAndConvertDocumentForWindowsMR(
101132

102133
auto streamReader = std::make_shared<GLTFStreamReader>(FileSystem::GetBasePath(inputFilePath));
103134

104-
if (processTextures)
105-
{
106-
std::wcout << L"Specular Glossiness conversion..." << std::endl;
107-
108-
// 0. Specular Glossiness conversion
109-
document = GLTFSpecularGlossinessUtils::ConvertMaterials(streamReader, document, tempDirectoryA);
110-
111-
std::wcout << L"Removing redundant textures and images..." << std::endl;
112-
113-
// 1. Remove redundant textures and images
114-
document = GLTFTextureUtils::RemoveRedundantTexturesAndImages(document);
115-
116-
std::wcout << L"Packing textures..." << std::endl;
117-
118-
// 2. Texture Packing
119-
document = GLTFTexturePackingUtils::PackAllMaterialsForWindowsMR(streamReader, document, packing, tempDirectoryA);
120-
121-
std::wcout << L"Compressing textures - this can take a few minutes..." << std::endl;
122-
123-
// 3. Texture Compression
124-
document = GLTFTextureCompressionUtils::CompressAllTexturesForWindowsMR(streamReader, document, tempDirectoryA, maxTextureSize, retainOriginalImages);
125-
}
126-
127135
if (meshCompression)
128136
{
129137
std::wcout << L"Compressing meshes - this can take a few minutes..." << std::endl;
@@ -207,12 +215,11 @@ int wmain(int argc, wchar_t *argv[])
207215
std::wcout << L"\nThis will generate an asset compatible with " << compatibleVersionsText << L"\n" << std::endl;
208216

209217
// Load document, and perform steps:
210-
// 1. Texture Packing
211-
// 2. Texture Compression
212-
auto document = LoadAndConvertDocumentForWindowsMR(inputFilePath, inputAssetType, tempDirectory, maxTextureSize, packing, true /* processTextures */, !replaceTextures, meshCompression);
218+
// 1. Mesh Compression
219+
auto document = LoadAndConvertDocumentForWindowsMR(inputFilePath, inputAssetType, tempDirectory, meshCompression);
213220

214-
// 3. LOD Merging
215-
if (lodFilePaths.size() > 0)
221+
// 2. LOD Merging
222+
if (!lodFilePaths.empty())
216223
{
217224
std::wcout << L"Merging LODs..." << std::endl;
218225

@@ -226,21 +233,26 @@ int wmain(int argc, wchar_t *argv[])
226233
auto lod = lodFilePaths[i];
227234
auto subFolder = FileSystem::CreateSubFolder(tempDirectory, L"lod" + std::to_wstring(i + 1));
228235

229-
lodDocuments.push_back(LoadAndConvertDocumentForWindowsMR(lod, AssetTypeUtils::AssetTypeFromFilePath(lod), subFolder, maxTextureSize, packing, !shareMaterials, !replaceTextures, meshCompression));
236+
lodDocuments.push_back(LoadAndConvertDocumentForWindowsMR(lod, AssetTypeUtils::AssetTypeFromFilePath(lod), subFolder, meshCompression));
230237

231238
lodDocumentRelativePaths.push_back(FileSystem::GetRelativePathWithTrailingSeparator(FileSystem::GetBasePath(inputFilePath), FileSystem::GetBasePath(lod)));
232239
}
233240

234241
document = GLTFLODUtils::MergeDocumentsAsLODs(lodDocuments, screenCoveragePercentages, lodDocumentRelativePaths, shareMaterials);
235242
}
236243

237-
// 4. Make sure there's a default scene
244+
// 3. Texture Packing
245+
// 4. Texture Compression
246+
auto streamReader = std::make_shared<GLTFStreamReader>(FileSystem::GetBasePath(inputFilePath));
247+
document = ProcessTextures(maxTextureSize, packing, !replaceTextures, tempDirectory, document, streamReader);
248+
249+
// 5. Make sure there's a default scene
238250
if (!document.HasDefaultScene())
239251
{
240252
document.defaultSceneId = document.scenes.Elements()[0].id;
241253
}
242254

243-
// 5. GLB Export
255+
// 6. GLB Export
244256
std::wcout << L"Exporting as GLB..." << std::endl;
245257

246258
// The Windows MR Fall Creators update has restrictions on the supported
@@ -267,7 +279,6 @@ int wmain(int argc, wchar_t *argv[])
267279
return accessor.componentType;
268280
};
269281

270-
auto streamReader = std::make_shared<GLTFStreamReader>(FileSystem::GetBasePath(inputFilePath));
271282
SerializeBinary(document, streamReader, std::make_shared<GLBStreamWriter>(outFilePath), accessorConversion);
272283

273284
std::wcout << L"Done!" << std::endl;

glTF-Toolkit/src/GLTFLODUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ namespace
361361
// lower quality LODs can have fewer images and textures than the highest LOD,
362362
// so we need to find the correct material index for the same material from the highest LOD
363363

364-
auto localMaterial = lod.materials.Get(primitive.materialId);
364+
const Material& localMaterial = lod.materials.Get(primitive.materialId);
365365

366366
// find merged material index for the given material index in this LOD
367367
auto iter = std::find_if(gltfLod.materials.Elements().begin(),
368368
gltfLod.materials.Elements().end(),
369-
[localMaterial](auto globalMaterial) {
369+
[localMaterial](const Material& globalMaterial) {
370370
// check that the materials are the same, noting that the texture and material ids will differ
371371
return localMaterial.name == globalMaterial.name &&
372372
localMaterial.alphaMode == globalMaterial.alphaMode &&

0 commit comments

Comments
 (0)