From a79e6b4a5b0dc1d49acea4a9630772a0145c5365 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 25 Jan 2023 18:23:55 +0900 Subject: [PATCH 1/6] ITextureExporter inherit IDisposable update ITextureExporter interface --- .../GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs | 4 ++-- .../GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs index e6bb6f0a0..bb132fcae 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using UnityEngine; namespace VRMShaders @@ -8,7 +8,7 @@ namespace VRMShaders /// /// glTF 拡張で Texture の用途を増やす必要がある場合は、この interface を継承して実装すればよい。 /// - public interface ITextureExporter + public interface ITextureExporter: IDisposable { /// /// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。 diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs index 3b4229dc6..d2f1da2df 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs @@ -8,7 +8,7 @@ namespace VRMShaders /// glTF にエクスポートする変換方式を蓄えて index を確定させる。 /// Exporter の最後で Export() でまとめて変換する。 /// - public sealed class TextureExporter : IDisposable, ITextureExporter + public sealed class TextureExporter : ITextureExporter { private readonly ITextureSerializer _textureSerializer; private readonly List _exportingList = new List(); From b1825e8968dd1086d87d566996c535fb2041c5b2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 25 Jan 2023 18:45:20 +0900 Subject: [PATCH 2/6] IDisposableResourceList Dispose a texture if `TextureConverter.CopyTexture` is used. --- .../Runtime/UniGLTF/IO/gltfExporter.cs | 2 ++ .../IO/Runtime/IDisposableResourceList.cs | 9 +++++ .../Runtime/IDisposableResourceList.cs.meta | 11 ++++++ .../Texture/Exporter/ITextureExporter.cs | 2 +- .../Texture/Exporter/TextureExportParam.cs | 4 +-- .../Texture/Exporter/TextureExporter.cs | 34 +++++++++++++++---- 6 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs create mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 2519a4233..8fcc316a4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -144,6 +144,8 @@ namespace UniGLTF { GameObject.Destroy(Copy); } + + _textureExporter.Dispose(); } #region Export diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs new file mode 100644 index 000000000..ce0942b72 --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs @@ -0,0 +1,9 @@ +using System; + +namespace VRMShaders +{ + public interface IDisposableResourceList : IDisposable + { + public void PushDisposable(UnityEngine.Object disposable); + } +} diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta new file mode 100644 index 000000000..9ea1d664e --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 199f81cd0c31d644ca19fb747ea89584 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs index bb132fcae..b0d300bfb 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs @@ -8,7 +8,7 @@ namespace VRMShaders /// /// glTF 拡張で Texture の用途を増やす必要がある場合は、この interface を継承して実装すればよい。 /// - public interface ITextureExporter: IDisposable + public interface ITextureExporter: IDisposableResourceList { /// /// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。 diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs index 447514a8e..df584c6a6 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs @@ -12,9 +12,9 @@ namespace VRMShaders public float OptionFactor { get; } public bool NeedsAlpha { get; set; } - public Func Creator { get; set; } + public Func<(Texture2D, bool)> Creator { get; set; } - public TextureExportParam(TextureExportTypes exportType, ColorSpace exportColorSpace, Texture primaryTexture, Texture secondaryTexture, float optionFactor, bool needsAlpha, Func creator) + public TextureExportParam(TextureExportTypes exportType, ColorSpace exportColorSpace, Texture primaryTexture, Texture secondaryTexture, float optionFactor, bool needsAlpha, Func<(Texture2D, bool)> creator) { ExportType = exportType; ExportColorSpace = exportColorSpace; diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs index d2f1da2df..b924bd696 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs @@ -12,6 +12,7 @@ namespace VRMShaders { private readonly ITextureSerializer _textureSerializer; private readonly List _exportingList = new List(); + private readonly List _disposables = new List(); public TextureExporter(ITextureSerializer textureSerializer) { @@ -20,6 +21,21 @@ namespace VRMShaders public void Dispose() { + foreach (var o in _disposables) + { + if (Application.isEditor) + { + GameObject.DestroyImmediate(o); + } + else + { + GameObject.Destroy(o); + } + } + } + public void PushDisposable(UnityEngine.Object disposable) + { + _disposables.Add(disposable); } /// @@ -31,7 +47,11 @@ namespace VRMShaders for (var idx = 0; idx < _exportingList.Count; ++idx) { var exporting = _exportingList[idx]; - var texture = exporting.Creator(); + var (texture, disposable) = exporting.Creator(); + if (disposable) + { + PushDisposable(texture); + } exportedTextures.Add((texture, exporting.ExportColorSpace)); } return exportedTextures; @@ -98,8 +118,8 @@ namespace VRMShaders { _textureSerializer.ModifyTextureAssetBeforeExporting(metallicSmoothTexture); _textureSerializer.ModifyTextureAssetBeforeExporting(occlusionTexture); - return OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, - occlusionTexture); + return (OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, + occlusionTexture), true); }); if (TryGetExistsParam(param, out var existsIdx)) { @@ -125,7 +145,7 @@ namespace VRMShaders false, () => { _textureSerializer.ModifyTextureAssetBeforeExporting(src); - return NormalConverter.Export(src); + return (NormalConverter.Export(src), true); }); if (TryGetExistsParam(param, out var existsIdx)) { @@ -142,10 +162,11 @@ namespace VRMShaders } } - private Texture2D ConvertTextureSimple(Texture src, bool needsAlpha, ColorSpace exportColorSpace) + private (Texture2D, bool) ConvertTextureSimple(Texture src, bool needsAlpha, ColorSpace exportColorSpace) { // get Texture2D var texture2D = src as Texture2D; + var disposable = false; if (_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace)) { // NOTE: 生のファイルとして出力可能な場合、何もせずそのまま Texture2D を渡す。 @@ -156,8 +177,9 @@ namespace VRMShaders { _textureSerializer.ModifyTextureAssetBeforeExporting(src); texture2D = TextureConverter.CopyTexture(src, exportColorSpace, needsAlpha, null); + disposable = true; } - return texture2D; + return (texture2D, disposable); } private bool TryGetExistsParam(TextureExportParam param, out int existsIdx) From 6d42ba01e3a42b5173be0159bc90f1f16ec6fedf Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 26 Jan 2023 15:18:07 +0900 Subject: [PATCH 3/6] remove IDisposableResourceList * UnityEngine.Object => UnityEngine.Texture2D --- .../GLTF/IO/Runtime/IDisposableResourceList.cs | 9 --------- .../GLTF/IO/Runtime/IDisposableResourceList.cs.meta | 11 ----------- .../IO/Runtime/Texture/Exporter/ITextureExporter.cs | 2 +- .../IO/Runtime/Texture/Exporter/TextureExporter.cs | 5 +++-- 4 files changed, 4 insertions(+), 23 deletions(-) delete mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs delete mode 100644 Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs deleted file mode 100644 index ce0942b72..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace VRMShaders -{ - public interface IDisposableResourceList : IDisposable - { - public void PushDisposable(UnityEngine.Object disposable); - } -} diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta deleted file mode 100644 index 9ea1d664e..000000000 --- a/Assets/VRMShaders/GLTF/IO/Runtime/IDisposableResourceList.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 199f81cd0c31d644ca19fb747ea89584 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs index b0d300bfb..bb132fcae 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureExporter.cs @@ -8,7 +8,7 @@ namespace VRMShaders /// /// glTF 拡張で Texture の用途を増やす必要がある場合は、この interface を継承して実装すればよい。 /// - public interface ITextureExporter: IDisposableResourceList + public interface ITextureExporter: IDisposable { /// /// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。 diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs index b924bd696..0196bffa8 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs @@ -12,7 +12,7 @@ namespace VRMShaders { private readonly ITextureSerializer _textureSerializer; private readonly List _exportingList = new List(); - private readonly List _disposables = new List(); + private readonly List _disposables = new List(); public TextureExporter(ITextureSerializer textureSerializer) { @@ -33,7 +33,8 @@ namespace VRMShaders } } } - public void PushDisposable(UnityEngine.Object disposable) + + public void PushDisposable(UnityEngine.Texture2D disposable) { _disposables.Add(disposable); } From 04f409aa1f6e6ab48b336bdb661ff9223e9e3bcc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 26 Jan 2023 15:24:25 +0900 Subject: [PATCH 4/6] (Texture2D Texture, bool IsDisposable) --- .../GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs index df584c6a6..d15f1a4a3 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs @@ -12,7 +12,7 @@ namespace VRMShaders public float OptionFactor { get; } public bool NeedsAlpha { get; set; } - public Func<(Texture2D, bool)> Creator { get; set; } + public Func<(Texture2D Texture, bool IsDisposable)> Creator { get; set; } public TextureExportParam(TextureExportTypes exportType, ColorSpace exportColorSpace, Texture primaryTexture, Texture secondaryTexture, float optionFactor, bool needsAlpha, Func<(Texture2D, bool)> creator) { From 7572ce5dedb04c193f8fcc0c00945d45ddbfa64a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 9 Feb 2023 19:01:29 +0900 Subject: [PATCH 5/6] Func<(Texture2D, bool IsDisposable)> --- .../GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs index d15f1a4a3..45aeac9a4 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExportParam.cs @@ -12,9 +12,11 @@ namespace VRMShaders public float OptionFactor { get; } public bool NeedsAlpha { get; set; } - public Func<(Texture2D Texture, bool IsDisposable)> Creator { get; set; } + public Func<(Texture2D, bool IsDisposable)> Creator { get; set; } - public TextureExportParam(TextureExportTypes exportType, ColorSpace exportColorSpace, Texture primaryTexture, Texture secondaryTexture, float optionFactor, bool needsAlpha, Func<(Texture2D, bool)> creator) + public TextureExportParam(TextureExportTypes exportType, ColorSpace exportColorSpace, + Texture primaryTexture, Texture secondaryTexture, float optionFactor, bool needsAlpha, + Func<(Texture2D, bool IsDisposable)> creator) { ExportType = exportType; ExportColorSpace = exportColorSpace; From 31870780116c9dae7e768681b74c1aa650737437 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 9 Feb 2023 19:01:50 +0900 Subject: [PATCH 6/6] private --- .../IO/Runtime/Texture/Exporter/TextureExporter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs index 0196bffa8..151f30242 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs @@ -34,7 +34,7 @@ namespace VRMShaders } } - public void PushDisposable(UnityEngine.Texture2D disposable) + private void PushDisposable(UnityEngine.Texture2D disposable) { _disposables.Add(disposable); } @@ -48,8 +48,8 @@ namespace VRMShaders for (var idx = 0; idx < _exportingList.Count; ++idx) { var exporting = _exportingList[idx]; - var (texture, disposable) = exporting.Creator(); - if (disposable) + var (texture, isDisposable) = exporting.Creator(); + if (isDisposable) { PushDisposable(texture); } @@ -163,11 +163,11 @@ namespace VRMShaders } } - private (Texture2D, bool) ConvertTextureSimple(Texture src, bool needsAlpha, ColorSpace exportColorSpace) + private (Texture2D, bool IsDisposable) ConvertTextureSimple(Texture src, bool needsAlpha, ColorSpace exportColorSpace) { // get Texture2D var texture2D = src as Texture2D; - var disposable = false; + var isDisposable = false; if (_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace)) { // NOTE: 生のファイルとして出力可能な場合、何もせずそのまま Texture2D を渡す。 @@ -178,9 +178,9 @@ namespace VRMShaders { _textureSerializer.ModifyTextureAssetBeforeExporting(src); texture2D = TextureConverter.CopyTexture(src, exportColorSpace, needsAlpha, null); - disposable = true; + isDisposable = true; } - return (texture2D, disposable); + return (texture2D, isDisposable); } private bool TryGetExistsParam(TextureExportParam param, out int existsIdx)