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..e59fc340b 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(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, TextureImporter importer)
{
switch (texDesc.TextureType)
{
case TextureImportTypes.NormalMap:
{
- ConfigureSize(external, importer);
- ConfigureNormalMap(external, importer);
+ ConfigureSize(importer);
+ ConfigureNormalMap(importer);
}
break;
case TextureImportTypes.StandardMap:
{
- ConfigureSize(external, importer);
- ConfigureLinear(external, importer);
+ ConfigureSize(importer);
+ ConfigureLinear(importer);
}
break;
case TextureImportTypes.sRGB:
{
- ConfigureSize(external, importer);
+ ConfigureSize(importer);
}
break;
case TextureImportTypes.Linear:
{
- ConfigureSize(external, importer);
- ConfigureLinear(external, importer);
+ ConfigureSize(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, importer);
+ importer.SaveAndReimport();
}
}
}