From 78a3b02189c16c3a72bc57212ec1f2e77e59936d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 24 May 2021 15:33:21 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=B1=E9=80=9A=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=92=E3=81=8F=E3=81=8F=E3=82=8A=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IO/Editor/TextureImporterConfigurator.cs | 136 ++++++++++-------- 1 file changed, 76 insertions(+), 60 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Editor/TextureImporterConfigurator.cs b/Assets/VRMShaders/GLTF/IO/Editor/TextureImporterConfigurator.cs index b832730ca..113c7ddac 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/TextureImporterConfigurator.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/TextureImporterConfigurator.cs @@ -8,106 +8,122 @@ namespace VRMShaders { public static class TextureImporterConfigurator { - public static void ConfigureSize(Texture2D texture) + public static void ConfigureSize(Texture2D texture, TextureImporter textureImporter) { - var path = AssetDatabase.GetAssetPath(texture); - if (AssetImporter.GetAtPath(path) is TextureImporter textureImporter) - { - var maxSize = Mathf.Max(texture.width, texture.height); - textureImporter.maxTextureSize - = maxSize > 4096 ? 8192 : - maxSize > 2048 ? 4096 : - maxSize > 1024 ? 2048 : - maxSize > 512 ? 1024 : - 512; - textureImporter.SaveAndReimport(); - } - else - { - throw new System.IO.FileNotFoundException($"{path}"); - } + var maxSize = Mathf.Max(texture.width, texture.height); + textureImporter.maxTextureSize + = maxSize > 4096 ? 8192 : + maxSize > 2048 ? 4096 : + maxSize > 1024 ? 2048 : + maxSize > 512 ? 1024 : + 512; + textureImporter.SaveAndReimport(); } - public static void ConfigureNormalMap(Texture2D texture) + public static void ConfigureNormalMap(Texture2D texture, TextureImporter textureImporter) { - var path = AssetDatabase.GetAssetPath(texture); - if (AssetImporter.GetAtPath(path) is TextureImporter textureImporter) - { #if VRM_DEVELOP - Debug.Log($"{path} => normalmap"); + Debug.Log($"{texture} => normalmap"); #endif - textureImporter.textureType = TextureImporterType.NormalMap; - textureImporter.SaveAndReimport(); - } - else - { - throw new System.IO.FileNotFoundException($"{path}"); - } + textureImporter.textureType = TextureImporterType.NormalMap; + textureImporter.SaveAndReimport(); } - public static void ConfigureLinear(Texture2D texture) + public static void ConfigureLinear(Texture2D texture, TextureImporter textureImporter) { - var path = AssetDatabase.GetAssetPath(texture); - if (AssetImporter.GetAtPath(path) is TextureImporter textureImporter) - { #if VRM_DEVELOP - Debug.Log($"{path} => linear"); + Debug.Log($"{texture} => linear"); #endif - textureImporter.sRGBTexture = false; - textureImporter.SaveAndReimport(); - } - else + textureImporter.sRGBTexture = false; + textureImporter.SaveAndReimport(); + } + + class ImporterGetter : IDisposable + { + public TextureImporter Importer; + + ImporterGetter(TextureImporter importer) { - throw new System.IO.FileNotFoundException($"{path}"); + Importer = importer; + } + + public void Dispose() + { + Importer.SaveAndReimport(); + } + + public static bool TryGetImporter(Texture2D texture, out ImporterGetter getter) + { + var path = AssetDatabase.GetAssetPath(texture); + if (String.IsNullOrEmpty(path)) + { + Debug.LogWarning($"{path} is not asset"); + } + else + { + if (AssetImporter.GetAtPath(path) is TextureImporter importer) + { + getter = new ImporterGetter(importer); + return true; + } + else + { + Debug.LogWarning($"{path}: fail to get TextureImporter"); + } + } + getter = default; + return false; } } - public static void Configure(TextureImportParam textureInfo, IDictionary ExternalMap) + static void Configure(TextureImportParam textureInfo, Texture2D external, TextureImporter importer) { switch (textureInfo.TextureType) { case TextureImportTypes.NormalMap: { - if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external)) - { - ConfigureSize(external); - ConfigureNormalMap(external); - } + ConfigureSize(external, importer); + ConfigureNormalMap(external, importer); } break; case TextureImportTypes.StandardMap: { - if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external)) - { - ConfigureSize(external); - ConfigureLinear(external); - } + ConfigureSize(external, importer); + ConfigureLinear(external, importer); } break; case TextureImportTypes.sRGB: { - if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external)) - { - ConfigureSize(external); - } + ConfigureSize(external, importer); } break; case TextureImportTypes.Linear: { - if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external)) - { - ConfigureSize(external); - ConfigureLinear(external); - } + ConfigureSize(external, importer); + ConfigureLinear(external, importer); } break; - + default: throw new NotImplementedException(); } } + + public static void Configure(TextureImportParam textureInfo, IDictionary ExternalMap) + { + if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external)) + { + if (ImporterGetter.TryGetImporter(external, out ImporterGetter getter)) + { + using (getter) + { + Configure(textureInfo, external, getter.Importer); + } + } + } + } } }