From c0719ecbf1f02ae968ba2ba33329a33a7f6be0c4 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 25 Feb 2021 16:57:13 +0900 Subject: [PATCH] FixTextureAndImageNameUnique --- .../ScriptedImporter/GltfScriptedImporter.cs | 19 ---- Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs | 7 -- .../UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs | 103 ++++++++++++------ .../Runtime/UniGLTF/IO/ImporterContext.cs | 20 ++-- .../IO/MaterialLoader/MaterialFactory.cs | 5 + .../IO/TextureLoader/GetTextureParam.cs | 33 +++--- .../IO/TextureLoader/GltfTextureLoader.cs | 3 +- 7 files changed, 108 insertions(+), 82 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs index a71aaf6eb..a6c63a846 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs @@ -82,20 +82,6 @@ namespace UniGLTF } } - // public void ExtractTextures() - // { - // // extract textures to files - // this.ExtractTextures(TextureDirName); - // // reimport - // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - // } - - // public void ExtractMaterials() - // { - // this.ExtractAssets(MaterialDirName, ".mat"); - // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - // } - public void ExtractMaterialsAndTextures() { this.ExtractTextures(TextureDirName, () => @@ -105,11 +91,6 @@ namespace UniGLTF }); } - // public Dictionary GetExternalUnityObjects() where T : UnityEngine.Object - // { - // return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value); - // } - public void SetExternalUnityObject(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object { this.AddRemap(sourceAssetIdentifier, obj); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs index ce7850ccc..73aef408a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs @@ -172,12 +172,5 @@ namespace UniGLTF && animations.SequenceEqual(other.animations) ; } - - public string ImageNameFromTextureIndex(int index) - { - var gltfTexture = textures[index]; - var glTFImage = images[gltfTexture.source]; - return glTFImage.name; - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs index 514055d59..1395221b7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs @@ -164,7 +164,7 @@ namespace UniGLTF RestoreOlderVersionValues(); FixMeshNameUnique(); - FixImageNameUnique(); + FixTextureAndImageNameUnique(); FixMaterialNameUnique(); FixNodeName(); @@ -219,49 +219,90 @@ namespace UniGLTF } } - void FixImageNameUnique() + /// + /// gltfTexture.name を Unity Asset 名として運用する。 + /// ユニークである必要がある。 + /// + void FixTextureAndImageNameUnique() { - var used = new HashSet(); - for (int i = 0; i < GLTF.images.Count; ++i) { - var image = GLTF.images[i]; - if (string.IsNullOrEmpty(image.name)) + // process images + var used = new HashSet(); + for (int i = 0; i < GLTF.images.Count; ++i) { - RenameImageFromTexture(i); - } - - if (string.IsNullOrEmpty(image.name)) - { - var newName = $"image_{i}"; - if (!used.Add(newName)) + var image = GLTF.images[i]; + if (string.IsNullOrEmpty(image.name)) { - newName = "image_" + Guid.NewGuid().ToString("N"); + RenameImageFromTexture(i); + } + + if (string.IsNullOrEmpty(image.name)) + { + var newName = $"image_{i}"; if (!used.Add(newName)) { - throw new Exception(); + newName = "image_" + Guid.NewGuid().ToString("N"); + if (!used.Add(newName)) + { + throw new Exception(); + } } + image.name = newName; + // Debug.LogWarning($"no name: => {image.name}"); } - image.name = newName; - // Debug.LogWarning($"no name: => {image.name}"); - } - else - { - var lower = image.name.ToLower(); - if (used.Contains(lower)) + else { - // rename - var uname = lower + "_" + Guid.NewGuid().ToString("N"); - Debug.LogWarning($"same name: {lower} => {uname}"); - image.name = uname; - lower = uname; + var lower = image.name.ToLower(); + if (used.Contains(lower)) + { + // rename + var uname = lower + "_" + Guid.NewGuid().ToString("N"); + Debug.LogWarning($"same name: {lower} => {uname}"); + image.name = uname; + lower = uname; + } + used.Add(lower); } - used.Add(lower); } + } - var ext = GetTextureExtension(i); - if (!string.IsNullOrEmpty(ext)) + { + // process textures + var used = new HashSet(); + for (int i = 0; i < GLTF.textures.Count; ++i) { - AppendImageExtension(image, ext); + var gltfTexture = GLTF.textures[i]; + if (string.IsNullOrEmpty(gltfTexture.name)) + { + // use image name + gltfTexture.name = GLTF.images[gltfTexture.source].name; + } + if (string.IsNullOrEmpty(gltfTexture.name)) + { + var newName = $"image_{i}"; + if (!used.Add(newName)) + { + newName = "image_" + Guid.NewGuid().ToString("N"); + if (!used.Add(newName)) + { + throw new Exception(); + } + } + gltfTexture.name = newName; + } + else + { + var lower = gltfTexture.name.ToLower(); + if (used.Contains(lower)) + { + // rename + var uname = lower + "_" + Guid.NewGuid().ToString("N"); + Debug.LogWarning($"same name: {lower} => {uname}"); + gltfTexture.name = uname; + lower = uname; + } + used.Add(lower); + } } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 3ad1db441..5c82ba2fe 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -98,16 +98,16 @@ namespace UniGLTF throw new UniGLTFNotSupportedException("draco is not supported"); } - using (MeasureTime("LoadTextures")) - { - for (int i = 0; i < GLTF.materials.Count; ++i) - { - foreach (var param in MaterialFactory.EnumerateGetTextureparam(i)) - { - await m_textureFactory.GetTextureAsync(GLTF, param); - } - } - } + // using (MeasureTime("LoadTextures")) + // { + // for (int i = 0; i < GLTF.materials.Count; ++i) + // { + // foreach (var param in MaterialFactory.EnumerateGetTextureparam(i)) + // { + // await m_textureFactory.GetTextureAsync(GLTF, param); + // } + // } + // } using (MeasureTime("LoadMaterials")) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs index 75f100b01..041122e9c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/MaterialFactory.cs @@ -96,6 +96,11 @@ namespace UniGLTF return m_materials[index].Asset; } + /// + /// テクスチャ生成 + /// + /// + /// public async Awaitable LoadMaterialsAsync(GetTextureAsyncFunc getTexture) { if (m_gltf.materials == null || m_gltf.materials.Count == 0) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs index 8c193c3dd..47fa55d56 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs @@ -1,4 +1,6 @@ -namespace UniGLTF +using System; + +namespace UniGLTF { public struct GetTextureParam { @@ -18,7 +20,12 @@ public GetTextureParam(string name, string textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5) { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException(); + } Name = name; + TextureType = textureType; MetallicFactor = metallicFactor; Index0 = (ushort)i0; @@ -29,10 +36,10 @@ Index5 = (ushort)i5; } - public static GetTextureParam Create(glTF gltf, int index) + public static GetTextureParam Create(glTF gltf, int textureIndex) { - var name = gltf.ImageNameFromTextureIndex(index); - return new GetTextureParam(name, default, default, index, default, default, default, default, default); + var name = gltf.textures[textureIndex].name; + return new GetTextureParam(name, default, default, textureIndex, default, default, default, default, default); } public static GetTextureParam Create(glTF gltf, int index, string prop) @@ -53,22 +60,22 @@ } } - public static GetTextureParam CreateNormal(glTF gltf, int index) + public static GetTextureParam CreateNormal(glTF gltf, int textureIndex) { - var name = gltf.ImageNameFromTextureIndex(index); - return new GetTextureParam(name, NORMAL_PROP, default, index, default, default, default, default, default); + var name = gltf.textures[textureIndex].name; + return new GetTextureParam(name, NORMAL_PROP, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateMetallic(glTF gltf, int index, float metallicFactor) + public static GetTextureParam CreateMetallic(glTF gltf, int textureIndex, float metallicFactor) { - var name = gltf.ImageNameFromTextureIndex(index); - return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, index, default, default, default, default, default); + var name = gltf.textures[textureIndex].name; + return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateOcclusion(glTF gltf, int index) + public static GetTextureParam CreateOcclusion(glTF gltf, int textureIndex) { - var name = gltf.ImageNameFromTextureIndex(index); - return new GetTextureParam(name, OCCLUSION_PROP, default, index, default, default, default, default, default); + var name = gltf.textures[textureIndex].name; + return new GetTextureParam(name, OCCLUSION_PROP, default, textureIndex, default, default, default, default, default); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs index 5ef1a32c1..5169e5f70 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GltfTextureLoader.cs @@ -26,7 +26,6 @@ namespace UniGLTF public static async Awaitable LoadTextureAsync(glTF gltf, IStorage storage, int textureIndex) { - string textureName = default; var imageBytes = await Awaitable.Run(() => { var imageIndex = gltf.textures[textureIndex].source; @@ -43,7 +42,7 @@ namespace UniGLTF var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear); - texture.name = textureName; + texture.name = gltf.textures[textureIndex].name; if (imageBytes != null) { texture.LoadImage(imageBytes);