/// <summary> /// Rename every material from the obj and mtl to make them unique after the merge /// </summary> /// <param name="objData"></param> /// <param name="mtlData"></param> /// <param name="indexMaterial"></param> private static void SetUniqueMaterialsNames(ObjData objData, MtlData mtlData, IntWrapper indexMaterial) { // Dictionary that associate every material to a list of groups Dictionary <string, List <int> > dictMaterialGroups = ObjUtils.GetDictMaterialGroups(objData); if (mtlData == null) // Need to rename the Materials in obj only { foreach (KeyValuePair <string, List <int> > keyValue in dictMaterialGroups) { SetUniqueMaterialsName(objData, keyValue.Value, indexMaterial); indexMaterial.Value++; } } else // Need to rename the Materials in both obj and mtl { // Dictionary that associate every material of the mtl file to their index Dictionary <string, int> dictMaterialIndex = MtlUtils.GetDictMaterialIndex(mtlData); foreach (KeyValuePair <string, List <int> > keyValue in dictMaterialGroups) { SetUniqueMaterialsName(objData, keyValue.Value, indexMaterial); if (dictMaterialIndex.TryGetValue(keyValue.Key, out int index)) // We get the index of the material { MaterialMtl materialMtl = mtlData.MaterialsList[index]; if (materialMtl.NewMtl != null) { materialMtl.NewMtl = GenericUtils.MergeIndexStr(indexMaterial.Value, materialMtl.NewMtl); } } indexMaterial.Value++; } } }
/// <summary> /// Delete every unused material the obj file /// </summary> /// <param name="objData">Data parsed from the obj file</param> /// <param name="mtlData">Data parsed from the mtl file</param> public static bool DeleteUnusedMaterials(ObjData objData, MtlData mtlData) { if (mtlData == null) { return(false); } // Dictionary that associate every material to a list of groups Dictionary <string, List <int> > dictMaterialGroups = ObjUtils.GetDictMaterialGroups(objData); // Dictionary that associate every material of the mtl file to their index Dictionary <string, int> dictMaterialIndex = MtlUtils.GetDictMaterialIndex(mtlData); List <MaterialMtl> newMaterialsList = new List <MaterialMtl>(); foreach (KeyValuePair <string, List <int> > keyValue in dictMaterialGroups) { if (dictMaterialIndex.TryGetValue(keyValue.Key, out int index)) // We get the index of the material { MaterialMtl materialMtl = mtlData.MaterialsList[index]; newMaterialsList.Add(materialMtl); } } // Every material that wasnt used in a group was not added to the list mtlData.MaterialsList = newMaterialsList; return(true); }