diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs index 56f723a10..1ac48c0a9 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureExporter.cs @@ -1,9 +1,20 @@ -using UnityEngine; +using System.Collections.Generic; +using UnityEngine; namespace VRMShaders { + /// + /// Texture を用途別に変換の要不要を判断して gltf.textures の index に対応させる機能。 + /// + /// glTF 拡張で Texture の用途を増やす必要がある場合は、この interface を継承して実装すればよい。 + /// public interface ITextureExporter { + /// + /// Export する Texture2D のリスト。これが gltf.textures になる + /// + IReadOnlyList<(Texture2D, UniGLTF.ColorSpace)> Exported { get; } + int ExportSRGB(Texture src); int ExportLinear(Texture src); int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs deleted file mode 100644 index 28fa1c197..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace VRMShaders -{ - public enum TextureExportTypes - { - // 無変換 - None, - // Unity Standard様式 から glTF PBR様式への変換 - OcclusionMetallicRoughness, - // Assetを使うときはそのバイト列を無変換で、それ以外は DXT5nm 形式からのデコードを行う - Normal, - } -} \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs.meta deleted file mode 100644 index a2316ad95..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExportTypes.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: c2182c41d0d7469baec383d0069938e6 -timeCreated: 1621508069 \ No newline at end of file diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs index 91c91f597..06547f4b1 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs @@ -13,7 +13,11 @@ namespace VRMShaders /// public class TextureExporter : IDisposable, ITextureExporter { - private ITextureSerializer m_textureSerializer; + private readonly ITextureSerializer m_textureSerializer; + private readonly Dictionary m_exportMap = new Dictionary(); + private readonly List<(Texture2D, ColorSpace)> m_exported = new List<(Texture2D, ColorSpace)>(); + + public IReadOnlyList<(Texture2D, ColorSpace)> Exported => m_exported; public TextureExporter(ITextureSerializer textureSerializer) { @@ -25,12 +29,22 @@ namespace VRMShaders // TODO: export 用にコピー・変換したテクスチャーをここで解放したい } - struct ExportKey + enum ExportTypes + { + // 無変換 + None, + // Unity Standard様式 から glTF PBR様式への変換 + OcclusionMetallicRoughness, + // Assetを使うときはそのバイト列を無変換で、それ以外は DXT5nm 形式からのデコードを行う + Normal, + } + + readonly struct ExportKey { public readonly Texture Src; - public readonly TextureExportTypes TextureType; + public readonly ExportTypes TextureType; - public ExportKey(Texture src, TextureExportTypes type) + public ExportKey(Texture src, ExportTypes type) { if (src == null) { @@ -40,29 +54,6 @@ namespace VRMShaders TextureType = type; } } - Dictionary m_exportMap = new Dictionary(); - - /// - /// Export する Texture2D のリスト。これが gltf.textures になる - /// - /// - /// - public readonly List<(Texture2D, ColorSpace)> Exported = new List<(Texture2D, ColorSpace)>(); - - /// - /// Texture の export index を得る - /// - /// - /// - /// - public int GetTextureIndex(Texture src, TextureExportTypes textureType) - { - if (src == null) - { - return -1; - } - return m_exportMap[new ExportKey(src, textureType)]; - } /// /// sRGBなテクスチャーを処理し、index を確定させる @@ -77,13 +68,13 @@ namespace VRMShaders } // cache - if (m_exportMap.TryGetValue(new ExportKey(src, TextureExportTypes.None), out var index)) + if (m_exportMap.TryGetValue(new ExportKey(src, ExportTypes.None), out var index)) { return index; } // get Texture2D - index = Exported.Count; + index = m_exported.Count; var texture2D = src as Texture2D; if (m_textureSerializer.CanExportAsEditorAssetFile(texture2D)) { @@ -93,8 +84,8 @@ namespace VRMShaders { texture2D = TextureConverter.CopyTexture(src, TextureImportTypes.sRGB, null); } - Exported.Add((texture2D, ColorSpace.sRGB)); - m_exportMap.Add(new ExportKey(src, TextureExportTypes.None), index); + m_exported.Add((texture2D, ColorSpace.sRGB)); + m_exportMap.Add(new ExportKey(src, ExportTypes.None), index); return index; } @@ -111,7 +102,7 @@ namespace VRMShaders return -1; } - var exportKey = new ExportKey(src, TextureExportTypes.None); + var exportKey = new ExportKey(src, ExportTypes.None); // search cache if (m_exportMap.TryGetValue(exportKey, out var index)) @@ -119,7 +110,7 @@ namespace VRMShaders return index; } - index = Exported.Count; + index = m_exported.Count; var texture2d = src as Texture2D; if (m_textureSerializer.CanExportAsEditorAssetFile(texture2d)) { @@ -129,7 +120,7 @@ namespace VRMShaders { texture2d = TextureConverter.CopyTexture(src, TextureImportTypes.Linear, null); } - Exported.Add((texture2d, ColorSpace.Linear)); + m_exported.Add((texture2d, ColorSpace.Linear)); m_exportMap.Add(exportKey, index); return index; @@ -150,11 +141,11 @@ namespace VRMShaders } // cache - if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, TextureExportTypes.OcclusionMetallicRoughness), out var index)) + if (metallicSmoothTexture != null && m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, ExportTypes.OcclusionMetallicRoughness), out var index)) { return index; } - if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, TextureExportTypes.OcclusionMetallicRoughness), out index)) + if (occlusionTexture != null && m_exportMap.TryGetValue(new ExportKey(occlusionTexture, ExportTypes.OcclusionMetallicRoughness), out index)) { return index; } @@ -162,17 +153,17 @@ namespace VRMShaders // // Unity と glTF で互換性が無いので必ず変換が必用 // - index = Exported.Count; + index = m_exported.Count; var texture2D = OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture); - Exported.Add((texture2D, ColorSpace.Linear)); + m_exported.Add((texture2D, ColorSpace.Linear)); if (metallicSmoothTexture != null) { - m_exportMap.Add(new ExportKey(metallicSmoothTexture, TextureExportTypes.OcclusionMetallicRoughness), index); + m_exportMap.Add(new ExportKey(metallicSmoothTexture, ExportTypes.OcclusionMetallicRoughness), index); } if (occlusionTexture != null && occlusionTexture != metallicSmoothTexture) { - m_exportMap.Add(new ExportKey(occlusionTexture, TextureExportTypes.OcclusionMetallicRoughness), index); + m_exportMap.Add(new ExportKey(occlusionTexture, ExportTypes.OcclusionMetallicRoughness), index); } return index; @@ -191,13 +182,13 @@ namespace VRMShaders } // cache - if (m_exportMap.TryGetValue(new ExportKey(src, TextureExportTypes.Normal), out var index)) + if (m_exportMap.TryGetValue(new ExportKey(src, ExportTypes.Normal), out var index)) { return index; } // get Texture2D - index = Exported.Count; + index = m_exported.Count; var texture2D = src as Texture2D; if (m_textureSerializer.CanExportAsEditorAssetFile(texture2D)) { @@ -209,8 +200,8 @@ namespace VRMShaders texture2D = NormalConverter.Export(src); } - Exported.Add((texture2D, ColorSpace.Linear)); - m_exportMap.Add(new ExportKey(src, TextureExportTypes.Normal), index); + m_exported.Add((texture2D, ColorSpace.Linear)); + m_exportMap.Add(new ExportKey(src, ExportTypes.Normal), index); return index; }