From 02b7ffd5e2040724cd7fc86680bd454f4f1bdc2e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 16 Mar 2021 16:07:21 +0900 Subject: [PATCH] fix material rename --- .../UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs | 13 +++++++++-- .../Editor/Format/VRMEditorImporterContext.cs | 22 +++++++++---------- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 9 ++++---- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs index 92b5445cf..eeacff864 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs @@ -261,7 +261,7 @@ namespace UniGLTF { // rename var uname = lower + "_" + Guid.NewGuid().ToString("N"); - Debug.LogWarning($"same name: {lower} => {uname}"); + Debug.LogWarning($"texture.name: {lower} => {uname}"); gltfTexture.name = uname; if (!used.Add(uname)) { @@ -274,12 +274,21 @@ namespace UniGLTF public void FixMaterialNameUnique() { + var used = new HashSet(); foreach (var material in GLTF.materials) { var originalName = material.name; int j = 2; - while (GLTF.materials.Any(x => x != material && x.name == material.name)) + + while (true) { + if (used.Add(material.name)) + { +#if VRM_DEVELOP + Debug.Log($"Material: {material.name}"); +#endif + break; + } material.name = string.Format("{0}({1})", originalName, j++); } } diff --git a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs index 350052dca..3b0ebcf60 100644 --- a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs +++ b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs @@ -178,17 +178,17 @@ namespace VRM return true; } -// if (assetPath.IsFileExists) -// { -// if (AvoidOverwriteAndLoad(assetPath, o)) -// { -// #if VRM_DEVELOP -// Debug.Log($"AvoidOverwriteAndLoad: {assetPath}"); -// #endif -// // 上書きせずに既存のアセットからロードして置き換えた -// return true; -// } -// } + // if (assetPath.IsFileExists) + // { + // if (AvoidOverwriteAndLoad(assetPath, o)) + // { + // #if VRM_DEVELOP + // Debug.Log($"AvoidOverwriteAndLoad: {assetPath}"); + // #endif + // // 上書きせずに既存のアセットからロードして置き換えた + // return true; + // } + // } // アセットとして書き込む assetPath.Parent.EnsureFolder(); diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index 042bbd7e2..28cacb2e9 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -37,9 +37,9 @@ namespace VRM } else { -// #if VRM_DEVELOP -// Debug.LogWarningFormat("unknown shader {0}.", shaderName); -// #endif + // #if VRM_DEVELOP + // Debug.LogWarningFormat("unknown shader {0}.", shaderName); + // #endif } return await MaterialFactory.DefaultCreateMaterialAsync(awaitCaller, gltf, m_index, getTexture); } @@ -48,7 +48,8 @@ namespace VRM // restore VRM material // var material = new Material(shader); - material.name = item.name; + // use material.name, because material name may renamed in GltfParser. + material.name = gltf.materials[m_index].name; material.renderQueue = item.renderQueue; foreach (var kv in item.floatProperties)