From e1c897b22010b24c73d9da06bfdc6830c8c49cf8 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 29 Jun 2021 22:23:03 +0900 Subject: [PATCH] Add unique suffix to same name assets. --- .../ScriptedImporter/RemapEditorMaterial.cs | 2 +- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 10 +- .../UniGLTF/IO/Parser/GlbLowLevelParser.cs | 92 +++++++------------ .../Runtime/UniGLTF/IO/gltfExporter.cs | 32 +++++++ Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 3 + 5 files changed, 73 insertions(+), 66 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs index f31edc23a9..6750de1d74 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/RemapEditorMaterial.cs @@ -90,7 +90,7 @@ void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureD .ToDictionary(kv => kv.Item1, kv => kv.Item2) ; - var assetPath = UnityPath.FromFullpath(data.TargetPath); + var assetPath = UnityPath.FromFullpath(self.assetPath); var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures( data, diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 4502ba2242..67534b39b9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -14,28 +14,28 @@ public sealed class GltfData /// Maybe empty if source file was on memory. /// public string TargetPath { get; } - + /// /// JSON source /// public string Json { get; } - + /// /// GLTF parsed from JSON /// public glTF GLTF { get; } - + /// /// Chunk Data. /// Maybe empty if source file was not glb format. /// public IReadOnlyList Chunks { get; } - + /// /// URI access /// public IStorage Storage { get; } - + /// /// Migration Flags used by ImporterContext /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs index 1a8c7e3e6c..28591e9527 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs @@ -12,9 +12,10 @@ namespace UniGLTF /// public sealed class GlbLowLevelParser { + public static readonly string UniqueFixResourceSuffix = "__UNIGLTF__DUPLICATED__"; private readonly string _path; private readonly byte[] _binary; - + public GlbLowLevelParser(string path, byte[] specifiedBinary) { _path = path; @@ -44,7 +45,7 @@ public GltfData Parse() throw; } } - + public static List ParseGlbChunks(byte[] data) { var chunks = glbImporter.ParseGlbChunks(data); @@ -97,7 +98,7 @@ public static GltfData ParseGltf(string path, string json, IReadOnlyList(); @@ -171,9 +172,9 @@ public static string PrepareUri(string uri) private static void FixTextureNameUnique(glTF GLTF) { var used = new HashSet(); - for (int i = 0; i < GLTF.textures.Count; ++i) + for (var textureIdx = 0; textureIdx < GLTF.textures.Count; ++textureIdx) { - var gltfTexture = GLTF.textures[i]; + var gltfTexture = GLTF.textures[textureIdx]; var gltfImage = GLTF.images[gltfTexture.source]; if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) { @@ -187,61 +188,26 @@ private static void FixTextureNameUnique(glTF GLTF) } if (string.IsNullOrEmpty(gltfTexture.name)) { - // no name - var newName = $"texture_{i}"; - if (!used.Add(newName)) - { - newName = "texture_" + Guid.NewGuid().ToString("N"); - if (!used.Add(newName)) - { - throw new Exception(); - } - } - gltfTexture.name = newName; - } - else - { - var lower = gltfTexture.name.ToLower(); - if (!used.Add(lower)) - { - // rename - var uname = lower + "_" + Guid.NewGuid().ToString("N"); - // Debug.LogWarning($"texture.name: {lower} => {uname}"); - gltfTexture.name = uname; - if (!used.Add(uname)) - { - throw new Exception(); - } - } + gltfTexture.name = $"texture_{textureIdx}"; } + + gltfTexture.name = FixNameUnique(used, gltfTexture.name); } } private static void FixMaterialNameUnique(glTF GLTF) { var used = new HashSet(); - for (int i = 0; i < GLTF.materials.Count; ++i) + for (var materialIdx = 0; materialIdx < GLTF.materials.Count; ++materialIdx) { - var material = GLTF.materials[i]; - var originalName = material.name; - int j = 2; + var material = GLTF.materials[materialIdx]; if (string.IsNullOrEmpty(material.name)) { - material.name = $"material_{i}"; + material.name = $"material_{materialIdx}"; } - 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++); - } + material.name = FixNameUnique(used, material.name); } } @@ -266,25 +232,13 @@ private static void FixAnimationNameUnique(glTF GLTF) for (int i = 0; i < GLTF.animations.Count; ++i) { var animation = GLTF.animations[i]; - var originalName = animation.name; - int j = 2; if (string.IsNullOrEmpty(animation.name)) { animation.name = $"animation_{i}"; } - while (true) - { - if (used.Add(animation.name)) - { -#if VRM_DEVELOP - // Debug.Log($"Material: {material.name}"); -#endif - break; - } - animation.name = string.Format("{0}({1})", originalName, j++); - } + animation.name = FixNameUnique(used, animation.name); } } @@ -319,5 +273,23 @@ public static void AppendImageExtension(glTFImage texture, string extension) texture.name = texture.name + extension; } } + + private static string FixNameUnique(HashSet used, string originalName) + { + if (used.Add(originalName)) + { + return originalName; + } + + var duplicatedIdx = 2; + while (true) + { + var newName = $"{originalName}{UniqueFixResourceSuffix}{duplicatedIdx++}"; + if (used.Add(newName)) + { + return newName; + } + } + } } } \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 62977359bf..7044ea4a9b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using UnityEngine; using VRMShaders; @@ -372,6 +373,37 @@ public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerial var (unityTexture, colorSpace) = exported[exportedTextureIdx]; glTF.PushGltfTexture(bufferIndex, unityTexture, colorSpace, textureSerializer); } + + FixName(glTF); + } + + /// + /// GlbLowPevelParser.FixNameUnique で付与した Suffix を remove + /// + public static void FixName(glTF gltf) + { + var regex = new Regex($@"{GlbLowLevelParser.UniqueFixResourceSuffix}\d+$"); + foreach (var gltfImages in gltf.images) + { + if (regex.IsMatch(gltfImages.name)) + { + gltfImages.name = regex.Replace(gltfImages.name, string.Empty); + } + } + foreach (var gltfMaterial in gltf.materials) + { + if (regex.IsMatch(gltfMaterial.name)) + { + gltfMaterial.name = regex.Replace(gltfMaterial.name, string.Empty); + } + } + foreach (var gltfAnimation in gltf.animations) + { + if (regex.IsMatch(gltfAnimation.name)) + { + gltfAnimation.name = regex.Replace(gltfAnimation.name, string.Empty); + } + } } #endregion } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 55a4d49cd1..5f6e673452 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -203,6 +203,9 @@ public void Export(GameObject root, Model model, ModelExporter converter, Export { UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrmSpringBone); } + + // Fix Duplicated name + gltfExporter.FixName(Storage.Gltf); } ///