From 2e11704708242785c47aa78823f81b8a3cd41d64 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 00:09:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Editor=20Import=20=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=80=812048=20=E3=82=92=E8=B6=85=E3=81=88=E3=82=8B=20Texture?= =?UTF-8?q?=20=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E3=81=99=E3=82=8B=20MaxSize=20=E6=8C=87=E5=AE=9A=E3=81=8C?= =?UTF-8?q?=E3=81=86=E3=81=BE=E3=81=8F=E3=81=84=E3=81=A3=E3=81=A6=E3=81=AA?= =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=9F=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IO/Editor/Texture/EditorTextureUtility.cs | 50 ++++++++++ .../Texture/EditorTextureUtility.cs.meta | 3 + .../Exporter/EditorTextureSerializer.cs | 43 ++------- .../Importer/TextureImporterConfigurator.cs | 95 +++++-------------- 4 files changed, 86 insertions(+), 105 deletions(-) create mode 100644 Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs create mode 100644 Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs.meta diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs b/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs new file mode 100644 index 000000000..45d088afd --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs @@ -0,0 +1,50 @@ +using System; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace VRMShaders +{ + internal static class EditorTextureUtility + { + public static bool TryGetAsEditorTexture2DAsset(Texture texture, out Texture2D texture2D, out TextureImporter assetImporter) + { + texture2D = texture as Texture2D; + if (texture2D != null) + { + var path = AssetDatabase.GetAssetPath(texture2D); + if (!string.IsNullOrEmpty(path)) + { + assetImporter = AssetImporter.GetAtPath(path) as TextureImporter; + if (assetImporter != null) + { + return true; + } + } + } + + texture2D = null; + assetImporter = null; + return false; + } + + public static bool TryGetOriginalTexturePixelSize(TextureImporter textureImporter, out Vector2Int size) + { + // private メソッド TextureImporter.GetWidthAndHeight を無理やり呼ぶ + var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); + if (textureImporter != null && getSizeMethod != null) + { + var args = new object[2] { 0, 0 }; + getSizeMethod.Invoke(textureImporter, args); + var originalWidth = (int)args[0]; + var originalHeight = (int)args[1]; + + size = new Vector2Int(originalWidth, originalHeight); + return true; + } + + size = default; + return false; + } + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs.meta b/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs.meta new file mode 100644 index 000000000..79ab25f71 --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/EditorTextureUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 20719e2944224be681a607b6db951720 +timeCreated: 1638542710 \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs index 3def724cc..f2ea7b9ff 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs @@ -24,13 +24,13 @@ namespace VRMShaders public bool CanExportAsEditorAssetFile(Texture texture, ColorSpace exportColorSpace) { // Exists as UnityEditor Texture2D Assets ? - if (!TryGetAsEditorTexture2DAsset(texture, out var texture2D, out var textureImporter)) return false; + if (!EditorTextureUtility.TryGetAsEditorTexture2DAsset(texture, out var texture2D, out var textureImporter)) return false; // Maintain original width/height ? - if (!IsTextureSizeMaintained(texture2D, textureImporter)) return false; + if (!IsTextureSizeMaintained(textureImporter)) return false; // Equals color space ? - if (!IsFileColorSpaceSameWithExportColorSpace(texture2D, textureImporter, exportColorSpace)) return false; + if (!IsFileColorSpaceSameWithExportColorSpace(textureImporter, exportColorSpace)) return false; // Each Texture Importer Type Validation switch (textureImporter.textureType) @@ -94,44 +94,17 @@ namespace VRMShaders return false; } - private bool TryGetAsEditorTexture2DAsset(Texture texture, out Texture2D texture2D, out TextureImporter assetImporter) - { - texture2D = texture as Texture2D; - if (texture2D != null) - { - var path = AssetDatabase.GetAssetPath(texture2D); - if (!string.IsNullOrEmpty(path)) - { - assetImporter = AssetImporter.GetAtPath(path) as TextureImporter; - if (assetImporter != null) - { - return true; - } - } - } - - texture2D = null; - assetImporter = null; - return false; - } - /// /// Texture2D の画像サイズが、オリジナルの画像サイズを維持しているかどうか /// /// TextureImporter の MaxTextureSize 設定によっては、Texture2D の画像サイズはオリジナルも小さくなりうる。 /// - private bool IsTextureSizeMaintained(Texture2D texture, TextureImporter textureImporter) + private bool IsTextureSizeMaintained(TextureImporter textureImporter) { - // private メソッド TextureImporter.GetWidthAndHeight を無理やり呼ぶ - var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); - if (textureImporter != null && getSizeMethod != null) + if (EditorTextureUtility.TryGetOriginalTexturePixelSize(textureImporter, out var originalSize)) { - var args = new object[2] { 0, 0 }; - getSizeMethod.Invoke(textureImporter, args); - var originalWidth = (int)args[0]; - var originalHeight = (int)args[1]; - var originalSize = Mathf.Max(originalWidth, originalHeight); - if (textureImporter.maxTextureSize >= originalSize) + var originalMaxSize = Mathf.Max(originalSize.x, originalSize.y); + if (textureImporter.maxTextureSize >= originalMaxSize) { return true; } @@ -140,7 +113,7 @@ namespace VRMShaders return false; } - private bool IsFileColorSpaceSameWithExportColorSpace(Texture2D texture, TextureImporter textureImporter, ColorSpace colorSpace) + private bool IsFileColorSpaceSameWithExportColorSpace(TextureImporter textureImporter, ColorSpace colorSpace) { switch (colorSpace) { diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs index c6266f698..601e529d9 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs @@ -8,31 +8,29 @@ namespace VRMShaders { public static class TextureImporterConfigurator { - public static void ConfigureSize(Texture texture, TextureImporter textureImporter) + private static void ConfigureSize(Texture2D texture, TextureImporter textureImporter) { - var maxSize = Mathf.Max(texture.width, texture.height); - textureImporter.maxTextureSize - = maxSize > 4096 ? 8192 : - maxSize > 2048 ? 4096 : - maxSize > 1024 ? 2048 : - maxSize > 512 ? 1024 : + if (!EditorTextureUtility.TryGetOriginalTexturePixelSize(textureImporter, out var originalSize)) return; + + var originalMaxSize = Mathf.Max(originalSize.x, originalSize.y); + textureImporter.maxTextureSize = originalMaxSize > 4096 ? 8192 : + originalMaxSize > 2048 ? 4096 : + originalMaxSize > 1024 ? 2048 : + originalMaxSize > 512 ? 1024 : 512; - textureImporter.SaveAndReimport(); } - public static void ConfigureNormalMap(Texture texture, TextureImporter textureImporter) + private static void ConfigureNormalMap(TextureImporter textureImporter) { textureImporter.textureType = TextureImporterType.NormalMap; - textureImporter.SaveAndReimport(); } - public static void ConfigureLinear(Texture texture, TextureImporter textureImporter) + private static void ConfigureLinear(TextureImporter textureImporter) { textureImporter.sRGBTexture = false; - textureImporter.SaveAndReimport(); } - public static void ConfigureSampler(TextureDescriptor texDesc, TextureImporter textureImporter) + private static void ConfigureSampler(TextureDescriptor texDesc, TextureImporter textureImporter) { textureImporter.mipmapEnabled = texDesc.Sampler.EnableMipMap; textureImporter.filterMode = texDesc.Sampler.FilterMode; @@ -40,72 +38,34 @@ namespace VRMShaders textureImporter.wrapModeV = texDesc.Sampler.WrapModesV; } - class ImporterGetter : IDisposable - { - public TextureImporter Importer; - - ImporterGetter(TextureImporter importer) - { - Importer = importer; - } - - public void Dispose() - { - Importer.SaveAndReimport(); - } - - public static bool TryGetImporter(Texture 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; - } - } - - static void Configure(TextureDescriptor texDesc, Texture external, TextureImporter importer) + private static void Configure(TextureDescriptor texDesc, Texture2D texture, TextureImporter importer) { switch (texDesc.TextureType) { case TextureImportTypes.NormalMap: { - ConfigureSize(external, importer); - ConfigureNormalMap(external, importer); + ConfigureSize(texture, importer); + ConfigureNormalMap(importer); } break; case TextureImportTypes.StandardMap: { - ConfigureSize(external, importer); - ConfigureLinear(external, importer); + ConfigureSize(texture, importer); + ConfigureLinear(importer); } break; case TextureImportTypes.sRGB: { - ConfigureSize(external, importer); + ConfigureSize(texture, importer); } break; case TextureImportTypes.Linear: { - ConfigureSize(external, importer); - ConfigureLinear(external, importer); + ConfigureSize(texture, importer); + ConfigureLinear(importer); } break; @@ -116,18 +76,13 @@ namespace VRMShaders ConfigureSampler(texDesc, importer); } - public static void Configure(TextureDescriptor texDesc, IReadOnlyDictionary ExternalMap) + public static void Configure(TextureDescriptor texDesc, IReadOnlyDictionary externalMap) { - if (ExternalMap.TryGetValue(texDesc.SubAssetKey, out Texture external)) - { - if (ImporterGetter.TryGetImporter(external, out ImporterGetter getter)) - { - using (getter) - { - Configure(texDesc, external, getter.Importer); - } - } - } + if (!externalMap.TryGetValue(texDesc.SubAssetKey, out var externalTexture)) return; + if (!EditorTextureUtility.TryGetAsEditorTexture2DAsset(externalTexture, out var texture2D, out var importer)) return; + + Configure(texDesc, texture2D, importer); + importer.SaveAndReimport(); } } } From c51cf3a7beda766a1141b2cbda56694a0951ea66 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 00:13:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A6=81=E3=82=89=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=BC=95=E6=95=B0=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Importer/TextureImporterConfigurator.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs index 601e529d9..e59fc340b 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Importer/TextureImporterConfigurator.cs @@ -8,7 +8,7 @@ namespace VRMShaders { public static class TextureImporterConfigurator { - private static void ConfigureSize(Texture2D texture, TextureImporter textureImporter) + private static void ConfigureSize(TextureImporter textureImporter) { if (!EditorTextureUtility.TryGetOriginalTexturePixelSize(textureImporter, out var originalSize)) return; @@ -38,33 +38,33 @@ namespace VRMShaders textureImporter.wrapModeV = texDesc.Sampler.WrapModesV; } - private static void Configure(TextureDescriptor texDesc, Texture2D texture, TextureImporter importer) + private static void Configure(TextureDescriptor texDesc, TextureImporter importer) { switch (texDesc.TextureType) { case TextureImportTypes.NormalMap: { - ConfigureSize(texture, importer); + ConfigureSize(importer); ConfigureNormalMap(importer); } break; case TextureImportTypes.StandardMap: { - ConfigureSize(texture, importer); + ConfigureSize(importer); ConfigureLinear(importer); } break; case TextureImportTypes.sRGB: { - ConfigureSize(texture, importer); + ConfigureSize(importer); } break; case TextureImportTypes.Linear: { - ConfigureSize(texture, importer); + ConfigureSize(importer); ConfigureLinear(importer); } break; @@ -81,7 +81,7 @@ namespace VRMShaders if (!externalMap.TryGetValue(texDesc.SubAssetKey, out var externalTexture)) return; if (!EditorTextureUtility.TryGetAsEditorTexture2DAsset(externalTexture, out var texture2D, out var importer)) return; - Configure(texDesc, texture2D, importer); + Configure(texDesc, importer); importer.SaveAndReimport(); } }