From 6fed2d0f2a725f167329acd7b9ba1aff05270329 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:00:04 +0900 Subject: [PATCH 1/6] =?UTF-8?q?ITextureSerializer=20=E3=81=AB=E3=80=81?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E4=BA=88=E5=AE=9A=E3=82=A2=E3=82=BB=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=81=AB=E5=AF=BE=E3=81=99=E3=82=8B=E4=BA=8B=E5=89=8D?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=AE=9A=E7=BE=A9=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Exporter/EditorTextureSerializer.cs | 17 ++++++++++--- .../Texture/Exporter/ITextureSerializer.cs | 7 ++++++ .../Exporter/RuntimeTextureSerializer.cs | 6 ++++- .../Texture/Exporter/TextureExporter.cs | 25 ++++++++++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs index f2ea7b9ff..ee0932873 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/Texture/Exporter/EditorTextureSerializer.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Reflection; using UnityEditor; using UnityEngine; @@ -8,7 +7,7 @@ namespace VRMShaders { public sealed class EditorTextureSerializer : ITextureSerializer { - private readonly RuntimeTextureSerializer m_runtimeSerializer = new RuntimeTextureSerializer(); + private readonly RuntimeTextureSerializer _runtimeSerializer = new RuntimeTextureSerializer(); /// /// Texture をオリジナルのテクスチャアセット(png/jpg)ファイルのバイト列そのまま出力してよいかどうか判断する。 @@ -56,7 +55,19 @@ namespace VRMShaders return (bytes, mime); } - return m_runtimeSerializer.ExportBytesWithMime(texture, exportColorSpace); + return _runtimeSerializer.ExportBytesWithMime(texture, exportColorSpace); + } + + /// + /// 出力に使用したいテクスチャに対して、Unity のエディタアセットとしての圧縮設定を OFF にする。 + /// + public void ModifyTextureAssetBeforeExporting(Texture texture) + { + if (EditorTextureUtility.TryGetAsEditorTexture2DAsset(texture, out var texture2D, out var assetImporter)) + { + assetImporter.textureCompression = TextureImporterCompression.Uncompressed; + assetImporter.SaveAndReimport(); + } } /// diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureSerializer.cs index 8da1bf82b..c14118b8c 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/ITextureSerializer.cs @@ -22,5 +22,12 @@ namespace VRMShaders /// 具体的には Texture2D をコピーする際に、コピー先の Texture2D の色空間を決定するために使用する。 /// (byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace exportColorSpace); + + /// + /// エクスポートに使用したい Texture に対して、事前準備を行う。 + /// + /// たとえば UnityEditor においては、Texture Asset の圧縮設定を OFF にしたりしたい。 + /// + void ModifyTextureAssetBeforeExporting(Texture texture); } } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/RuntimeTextureSerializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/RuntimeTextureSerializer.cs index 9bbd12e3f..c5894a8c4 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/RuntimeTextureSerializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/RuntimeTextureSerializer.cs @@ -1,6 +1,5 @@ using System; using UnityEngine; -using VRMShaders; namespace VRMShaders { @@ -39,6 +38,11 @@ namespace VRMShaders } } + public void ModifyTextureAssetBeforeExporting(Texture texture) + { + // NOTE: Do nothing. + } + private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace) { var needsAlpha = texture.format != TextureFormat.RGB24; diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs index 058bd8d14..3b4229dc6 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Exporter/TextureExporter.cs @@ -10,12 +10,12 @@ namespace VRMShaders /// public sealed class TextureExporter : IDisposable, ITextureExporter { - private readonly ITextureSerializer m_textureSerializer; + private readonly ITextureSerializer _textureSerializer; private readonly List _exportingList = new List(); public TextureExporter(ITextureSerializer textureSerializer) { - m_textureSerializer = textureSerializer; + _textureSerializer = textureSerializer; } public void Dispose() @@ -94,7 +94,13 @@ namespace VRMShaders var param = new TextureExportParam(TextureExportTypes.OcclusionMetallicRoughness, ColorSpace.Linear, metallicSmoothTexture, occlusionTexture, smoothness, false, - () => OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture)); + () => + { + _textureSerializer.ModifyTextureAssetBeforeExporting(metallicSmoothTexture); + _textureSerializer.ModifyTextureAssetBeforeExporting(occlusionTexture); + return OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, + occlusionTexture); + }); if (TryGetExistsParam(param, out var existsIdx)) { // Return cacehd @@ -116,7 +122,11 @@ namespace VRMShaders } var param = new TextureExportParam(TextureExportTypes.Normal, ColorSpace.Linear, src, default, default, - false, () => NormalConverter.Export(src)); + false, () => + { + _textureSerializer.ModifyTextureAssetBeforeExporting(src); + return NormalConverter.Export(src); + }); if (TryGetExistsParam(param, out var existsIdx)) { // Return cached; @@ -136,12 +146,15 @@ namespace VRMShaders { // get Texture2D var texture2D = src as Texture2D; - if (m_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace)) + if (_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace)) { - // do nothing + // NOTE: 生のファイルとして出力可能な場合、何もせずそのまま Texture2D を渡す。 + // ただし、この場合のみ圧縮設定をオフにしなかった場合、挙動としてバグっぽく見えるので、これもオフにする。 + _textureSerializer.ModifyTextureAssetBeforeExporting(src); } else { + _textureSerializer.ModifyTextureAssetBeforeExporting(src); texture2D = TextureConverter.CopyTexture(src, exportColorSpace, needsAlpha, null); } return texture2D; From e095052c8c9b08a0d8b0a88dc88c3ecc753d7689 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:23:47 +0900 Subject: [PATCH 2/6] comment --- .../Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs index 09ed4ad2b..603a94c2c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -17,10 +17,10 @@ namespace UniGLTF /// /// を更新し、textures の index を返す /// + /// もっとも根本の Exporter クラスのみが呼び出すべきである。 + /// 他の拡張機能などが呼び出すべきではない。 + /// /// - /// - /// - /// /// gltf texture index public static int PushGltfTexture(ExportingGltfData data, Texture2D texture, ColorSpace textureColorSpace, ITextureSerializer textureSerializer) { From 46c6d94e57b2768dd9425245fd6268a1be377298 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:24:07 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E6=98=94=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=81=8C=E5=8F=A4=E3=81=84=E5=91=BC=E3=81=B3=E5=87=BA?= =?UTF-8?q?=E3=81=97=E6=96=B9=E3=82=92=E3=81=97=E3=81=A6=E3=81=84=E3=81=9F?= =?UTF-8?q?=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM/Runtime/IO/VRMExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 741d31ad9..47178ed03 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -118,7 +118,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = GltfTextureExporter.PushGltfTexture(_data, meta.Thumbnail, ColorSpace.sRGB, textureSerializer); + VRM.meta.texture = TextureExporter.RegisterExportingAsSRgb(meta.Thumbnail, needsAlpha: true); } VRM.meta.licenseType = meta.LicenseType; From ad0b043c905531cfa8004cef5a47e2cc163ff81d Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:43:18 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E3=82=A2=E3=82=AF=E3=82=BB=E3=82=B9?= =?UTF-8?q?=E6=A8=A9=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 11552c84a..dcb9c439d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -46,8 +46,6 @@ namespace UniGLTF private set; } - public ITextureExporter TextureExporter => m_textureExporter; - protected virtual IMaterialExporter CreateMaterialExporter() { return new MaterialExporter(); @@ -65,7 +63,7 @@ namespace UniGLTF } } - TextureExporter m_textureExporter; + protected TextureExporter TextureExporter { get; private set; } GltfExportSettings m_settings; @@ -236,7 +234,7 @@ namespace UniGLTF #region Materials and Textures Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList(); - m_textureExporter = new TextureExporter(textureSerializer); + TextureExporter = new TextureExporter(textureSerializer); var materialExporter = CreateMaterialExporter(); _gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList(); From 04254d857406050bc4ca2d258782dd64815dadc9 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:43:47 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=BC=8F=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index dcb9c439d..8cc415d32 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -376,7 +376,7 @@ namespace UniGLTF ExportExtensions(textureSerializer); // Extension で Texture が増える場合があるので最後に呼ぶ - var exported = m_textureExporter.Export(); + var exported = TextureExporter.Export(); for (var exportedTextureIdx = 0; exportedTextureIdx < exported.Count; ++exportedTextureIdx) { var (unityTexture, colorSpace) = exported[exportedTextureIdx]; From 8b484ac86e08504fa1897bacf3ed5ea736dc4c72 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 4 Dec 2021 02:46:21 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 8cc415d32..4f8707b7b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -63,7 +63,8 @@ namespace UniGLTF } } - protected TextureExporter TextureExporter { get; private set; } + protected ITextureExporter TextureExporter => _textureExporter; + private TextureExporter _textureExporter; GltfExportSettings m_settings; @@ -234,7 +235,7 @@ namespace UniGLTF #region Materials and Textures Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList(); - TextureExporter = new TextureExporter(textureSerializer); + _textureExporter = new TextureExporter(textureSerializer); var materialExporter = CreateMaterialExporter(); _gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList(); @@ -376,7 +377,7 @@ namespace UniGLTF ExportExtensions(textureSerializer); // Extension で Texture が増える場合があるので最後に呼ぶ - var exported = TextureExporter.Export(); + var exported = _textureExporter.Export(); for (var exportedTextureIdx = 0; exportedTextureIdx < exported.Count; ++exportedTextureIdx) { var (unityTexture, colorSpace) = exported[exportedTextureIdx];