Skip to content

Commit

Permalink
Perform material processing after lod merging to fix share material c…
Browse files Browse the repository at this point in the history
…omparison (#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
  • Loading branch information
marpe authored and najadojo committed Oct 1, 2018
1 parent cbc4a2d commit a55c8a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
83 changes: 47 additions & 36 deletions WindowsMRAssetConverter/WindowsMRAssetConverter.cpp
Expand Up @@ -62,14 +62,45 @@ class GLBStreamWriter : public Microsoft::glTF::IStreamWriter
std::shared_ptr<std::ofstream> m_stream;
};

Document ProcessTextures(
size_t maxTextureSize,
TexturePacking packing,
bool retainOriginalImages,
const std::wstring& tempDirectory,
const Document& document,
const std::shared_ptr<GLTFStreamReader>& streamReader)
{
Document resultDocument(document);

std::string tempDirectoryA(tempDirectory.begin(), tempDirectory.end());

std::wcout << L"Specular Glossiness conversion..." << std::endl;

// 0. Specular Glossiness conversion
resultDocument = GLTFSpecularGlossinessUtils::ConvertMaterials(streamReader, resultDocument, tempDirectoryA);

std::wcout << L"Removing redundant textures and images..." << std::endl;

// 1. Remove redundant textures and images
resultDocument = GLTFTextureUtils::RemoveRedundantTexturesAndImages(resultDocument);

std::wcout << L"Packing textures..." << std::endl;

// 2. Texture Packing
resultDocument = GLTFTexturePackingUtils::PackAllMaterialsForWindowsMR(streamReader, resultDocument, packing, tempDirectoryA);

std::wcout << L"Compressing textures - this can take a few minutes..." << std::endl;

// 3. Texture Compression
resultDocument = GLTFTextureCompressionUtils::CompressAllTexturesForWindowsMR(streamReader, resultDocument, tempDirectoryA, maxTextureSize, retainOriginalImages);

return resultDocument;
}

Document LoadAndConvertDocumentForWindowsMR(
std::wstring& inputFilePath,
AssetType inputAssetType,
const std::wstring& tempDirectory,
size_t maxTextureSize,
TexturePacking packing,
bool processTextures,
bool retainOriginalImages,
bool meshCompression)
{
// Load the document
Expand Down Expand Up @@ -101,29 +132,6 @@ Document LoadAndConvertDocumentForWindowsMR(

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

if (processTextures)
{
std::wcout << L"Specular Glossiness conversion..." << std::endl;

// 0. Specular Glossiness conversion
document = GLTFSpecularGlossinessUtils::ConvertMaterials(streamReader, document, tempDirectoryA);

std::wcout << L"Removing redundant textures and images..." << std::endl;

// 1. Remove redundant textures and images
document = GLTFTextureUtils::RemoveRedundantTexturesAndImages(document);

std::wcout << L"Packing textures..." << std::endl;

// 2. Texture Packing
document = GLTFTexturePackingUtils::PackAllMaterialsForWindowsMR(streamReader, document, packing, tempDirectoryA);

std::wcout << L"Compressing textures - this can take a few minutes..." << std::endl;

// 3. Texture Compression
document = GLTFTextureCompressionUtils::CompressAllTexturesForWindowsMR(streamReader, document, tempDirectoryA, maxTextureSize, retainOriginalImages);
}

if (meshCompression)
{
std::wcout << L"Compressing meshes - this can take a few minutes..." << std::endl;
Expand Down Expand Up @@ -207,12 +215,11 @@ int wmain(int argc, wchar_t *argv[])
std::wcout << L"\nThis will generate an asset compatible with " << compatibleVersionsText << L"\n" << std::endl;

// Load document, and perform steps:
// 1. Texture Packing
// 2. Texture Compression
auto document = LoadAndConvertDocumentForWindowsMR(inputFilePath, inputAssetType, tempDirectory, maxTextureSize, packing, true /* processTextures */, !replaceTextures, meshCompression);
// 1. Mesh Compression
auto document = LoadAndConvertDocumentForWindowsMR(inputFilePath, inputAssetType, tempDirectory, meshCompression);

// 3. LOD Merging
if (lodFilePaths.size() > 0)
// 2. LOD Merging
if (!lodFilePaths.empty())
{
std::wcout << L"Merging LODs..." << std::endl;

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

lodDocuments.push_back(LoadAndConvertDocumentForWindowsMR(lod, AssetTypeUtils::AssetTypeFromFilePath(lod), subFolder, maxTextureSize, packing, !shareMaterials, !replaceTextures, meshCompression));
lodDocuments.push_back(LoadAndConvertDocumentForWindowsMR(lod, AssetTypeUtils::AssetTypeFromFilePath(lod), subFolder, meshCompression));

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

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

// 4. Make sure there's a default scene
// 3. Texture Packing
// 4. Texture Compression
auto streamReader = std::make_shared<GLTFStreamReader>(FileSystem::GetBasePath(inputFilePath));
document = ProcessTextures(maxTextureSize, packing, !replaceTextures, tempDirectory, document, streamReader);

// 5. Make sure there's a default scene
if (!document.HasDefaultScene())
{
document.defaultSceneId = document.scenes.Elements()[0].id;
}

// 5. GLB Export
// 6. GLB Export
std::wcout << L"Exporting as GLB..." << std::endl;

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

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

std::wcout << L"Done!" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions glTF-Toolkit/src/GLTFLODUtils.cpp
Expand Up @@ -361,12 +361,12 @@ namespace
// lower quality LODs can have fewer images and textures than the highest LOD,
// so we need to find the correct material index for the same material from the highest LOD

auto localMaterial = lod.materials.Get(primitive.materialId);
const Material& localMaterial = lod.materials.Get(primitive.materialId);

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

0 comments on commit a55c8a2

Please sign in to comment.