From 1b1715841301781ae68d4911cc0b526bd7270076 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 12 Apr 2021 18:05:21 +0900 Subject: [PATCH] =?UTF-8?q?GetBytesWithMime=20=E3=82=92=20VRMShaders=20?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * UniGLTF.GltfTextureExporter.GetBytesWithMime を VRMShaders.AssetTextureUtil.GetTextureBytesWithMime に移動 * テスト NotReadable, Compressed を VRMShaders に移動 * テスト用リソース Resources/4x4.png と 4x4compressed.DDS を VRMShaders に移動 --- .../Editor/UniGLTF/GltfExportWindow.cs | 2 +- .../IO/TextureIO/GltfTextureExporter.cs | 64 +------------ .../Runtime/UniGLTF/IO/gltfExporter.cs | 8 +- Assets/UniGLTF/Tests/Resources.meta | 8 -- Assets/UniGLTF/Tests/Resources/4x4.png.meta | 88 ------------------ Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/Resources.meta | 8 -- .../UniGLTF/Tests/UniGLTF/Resources/4x4.png | Bin 560 -> 0 bytes Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 21 +---- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 6 +- .../Editor/Tests/VRMImportExportTests.cs | 2 +- .../VRM.Samples/Scripts/VRMRuntimeExporter.cs | 3 +- Assets/VRM.Samples/VRM.Samples.asmdef | 5 +- Assets/VRM/Editor/Format/VRMEditorExporter.cs | 2 +- Assets/VRM/Runtime/IO/VRMExporter.cs | 8 +- Assets/VRM/Tests/VRMLoadTests.cs | 2 +- Assets/VRM10/Editor/Vrm10ExportDialog.cs | 2 +- Assets/VRM10/Runtime/IO/ModelExtensions.cs | 8 +- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 4 +- .../VRM10/Runtime/Scenes/ExportDebugUtil.cs | 35 ------- Assets/VRM10/Runtime/Scenes/Sample.cs | 3 +- Assets/VRM10/Tests/ApiSampleTests.cs | 3 +- Assets/VRM10/Tests/VRM10.Tests.asmdef | 3 +- .../GLTF/IO/Editor/AssetTextureUtil.cs | 49 ++++++++++ .../Editor/VRMShaders.GLTF.IO.Editor.asmdef | 9 +- .../GLTF/IO/Runtime/TextureExporter.cs | 32 +++++++ .../GLTF/IO/Tests}/4x4.png | Bin .../GLTF/IO/Tests}/4x4.png.meta | 0 .../GLTF/IO/Tests}/4x4compressed.DDS | Bin .../GLTF/IO/Tests}/4x4compressed.DDS.meta | 0 .../GLTF/IO/Tests/TextureBytesTests.cs | 29 ++++++ .../GLTF/IO/Tests/TextureBytesTests.cs.meta} | 2 +- 32 files changed, 154 insertions(+), 254 deletions(-) delete mode 100644 Assets/UniGLTF/Tests/Resources.meta delete mode 100644 Assets/UniGLTF/Tests/Resources/4x4.png.meta delete mode 100644 Assets/UniGLTF/Tests/UniGLTF/Resources.meta delete mode 100644 Assets/UniGLTF/Tests/UniGLTF/Resources/4x4.png delete mode 100644 Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs rename Assets/{UniGLTF/Tests/Resources => VRMShaders/GLTF/IO/Tests}/4x4.png (100%) rename Assets/{UniGLTF/Tests/UniGLTF/Resources => VRMShaders/GLTF/IO/Tests}/4x4.png.meta (100%) rename Assets/{UniGLTF/Tests/UniGLTF/Resources => VRMShaders/GLTF/IO/Tests}/4x4compressed.DDS (100%) rename Assets/{UniGLTF/Tests/UniGLTF/Resources => VRMShaders/GLTF/IO/Tests}/4x4compressed.DDS.meta (100%) create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs rename Assets/{VRM10/Runtime/Scenes/ExportDebugUtil.cs.meta => VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs.meta} (83%) diff --git a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs index 4842878dc5..308c0bc359 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/GltfExportWindow.cs @@ -36,7 +36,7 @@ private static void Export(GameObject go, string path, MeshExportSettings settin using (var exporter = new gltfExporter(gltf, inverseAxis)) { exporter.Prepare(go); - exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset ); + exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs index 7ef729c714..cd7df68bec 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureExporter.cs @@ -6,64 +6,6 @@ namespace UniGLTF { public static class GltfTextureExporter { - - /// - /// 画像のバイト列を得る - /// - /// - /// - /// - public static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) - { -#if UNITY_EDITOR - var path = UnityPath.FromAsset(texture); - if (path.IsUnderAssetsFolder) - { - if (path.Extension == ".png") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/png" - ); - } - if (path.Extension == ".jpg") - { - return - ( - System.IO.File.ReadAllBytes(path.FullPath), - "image/jpeg" - ); - } - } -#endif - - try - { - var png = texture.EncodeToPNG(); - if (png != null) - { - return (png, "image/png"); - } - } - catch (Exception ex) - { - // fail to EncodeToPng - // System.ArgumentException: not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. - - Debug.LogWarning(ex); - } - - { - // try copy and EncodeToPng - var copy = TextureConverter.CopyTexture(texture, TextureImportTypes.sRGB, null); - var png = copy.EncodeToPNG(); - UnityEngine.Object.DestroyImmediate(copy); - - return (png, "image/png"); - } - } - /// /// gltf に texture を足す /// @@ -79,9 +21,9 @@ public static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) /// /// /// gltf texture index - public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture) + public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture, Func getTextureBytes) { - var bytesWithMime = GetBytesWithMime(texture); + var bytesWithMime = getTextureBytes(texture); // add view var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); @@ -93,7 +35,7 @@ public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D tex { name = TextureImportName.RemoveSuffix(texture.name), bufferView = viewIndex, - mimeType = bytesWithMime.mine, + mimeType = bytesWithMime.mime, }); // add sampler diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 3f54a66c51..aacd117d4d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -168,12 +168,12 @@ static glTFNode ExportNode(Transform x, List nodes, List re return node; } - public virtual void ExportExtensions() + public virtual void ExportExtensions(Func getTextureBytes) { } - public virtual void Export(MeshExportSettings meshExportSettings, Func useAsset) + public virtual void Export(MeshExportSettings meshExportSettings, Func useAsset, Func getTextureBytes) { var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); var bufferIndex = glTF.AddBuffer(bytesBuffer); @@ -306,13 +306,13 @@ public virtual void Export(MeshExportSettings meshExportSettings, FuncEX>4Tx04R}tkv&MmP!xqvQ%glE4t5YJn4vmZ5EXHhDi*;)X)CnqVDi#GXws0R zxHt-~1qXi?s}3&Cx;nTDg5VE`o12rOimr&?r8EJaJd5vJ?WAmIg+22P%HxPXY@^ZVDJ{`TXW~uKF8?;kfm8I-v9@P zz*vd0*FE0d+da2`d)o8+0XEZek5q5Bk^lez32;bRa{vG?BLDy{BLR4&KXw2B00(qQ zO+^Rg10Mwj5@xRntpET38FWQhbVF}#ZDnqB07G(RVRU6=Aa`kWXdp*PO;A^X4i^9b y02xU{K~xCWV_-l6jP>>Pa1IlS%>V!Y;T!;1j0As$a4%f|0000("4x4"); - Assert.False(readonlyTexture.isReadable); - var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture); - Assert.NotNull(bytes); - } - - [Test] - public void Compressed() - { - var readonlyTexture = Resources.Load("4x4compressed"); - Assert.False(readonlyTexture.isReadable); - var (bytes, mime) = GltfTextureExporter.GetBytesWithMime(readonlyTexture); - Assert.NotNull(bytes); - } } } diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index e4d38cde1d..844d26fed5 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -106,7 +106,7 @@ public void UniGLTFSimpleSceneTest() using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); // remove empty buffer gltf.buffers.Clear(); @@ -298,7 +298,7 @@ public void GlTFToJsonTest() using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(CreateSimpleScene()); - exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); + exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); } var expected = gltf.ToJson().ParseAsJson(); @@ -534,7 +534,7 @@ public void SameMeshButDifferentMaterialExport() using (var exporter = new gltfExporter(gltf)) { exporter.Prepare(go); - exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset ); + exporter.Export(UniGLTF.MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); json = gltf.ToJson(); } diff --git a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs index e3e25f9a15..acdf73ec78 100644 --- a/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs +++ b/Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs @@ -170,7 +170,7 @@ public void ImportExportTest() */ importedJson.RemoveValue(Utf8String.From("/bufferViews/*/byteStride")); - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset ); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, context.Root, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); // TODO: Check contents in JSON /*var exportJson = */ diff --git a/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs b/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs index 23d5ad165c..f0056b16f5 100644 --- a/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs +++ b/Assets/VRM.Samples/Scripts/VRMRuntimeExporter.cs @@ -3,6 +3,7 @@ using UnityEngine; using UnityEngine.UI; using VRM; +using VRMShaders; namespace VRM.Samples { @@ -95,7 +96,7 @@ void OnExportClicked() return; } - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, _ => false); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, m_model, (Texture _) => false, TextureExporter.GetTextureBytesWithMime); var bytes = vrm.ToGlbBytes(); File.WriteAllBytes(path, bytes); Debug.LogFormat("export to {0}", path); diff --git a/Assets/VRM.Samples/VRM.Samples.asmdef b/Assets/VRM.Samples/VRM.Samples.asmdef index 7019a6e74f..c9bb54fbe9 100644 --- a/Assets/VRM.Samples/VRM.Samples.asmdef +++ b/Assets/VRM.Samples/VRM.Samples.asmdef @@ -6,13 +6,12 @@ "UniGLTF", "VRMShaders.GLTF.IO.Runtime" ], + "optionalUnityReferences": [], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [], - "versionDefines": [], - "noEngineReferences": false + "defineConstraints": [] } \ No newline at end of file diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 14756193c0..2fce5b9f01 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -225,7 +225,7 @@ static void Export(string path, GameObject exportRoot, VRMMetaObject meta, using (var exporter = new VRMExporter(gltf)) { exporter.Prepare(target); - exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset ); + exporter.Export(settings.MeshExportSettings, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); } var bytes = gltf.ToGlbBytes(); File.WriteAllBytes(path, bytes); diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index a1a5ce7485..ed567d485f 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -14,13 +14,13 @@ protected override IMaterialExporter CreateMaterialExporter() return new VRMMaterialExporter(); } - public static glTF Export(MeshExportSettings configuration, GameObject go, Func useAsset) + public static glTF Export(MeshExportSettings configuration, GameObject go, Func useAsset, Func getTextureBytes) { var gltf = new glTF(); using (var exporter = new VRMExporter(gltf)) { exporter.Prepare(go); - exporter.Export(configuration, useAsset); + exporter.Export(configuration, useAsset, getTextureBytes); } return gltf; } @@ -32,7 +32,7 @@ public VRMExporter(glTF gltf) : base(gltf, Axises.Z) gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName); } - public override void ExportExtensions() + public override void ExportExtensions(Func getTextureBytes) { // avatar var animator = Copy.GetComponent(); @@ -110,7 +110,7 @@ public override void ExportExtensions() VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail); + VRM.meta.texture = glTF.PushGltfTexture(glTF.buffers.Count - 1, meta.Thumbnail, getTextureBytes); } VRM.meta.licenseType = meta.LicenseType; diff --git a/Assets/VRM/Tests/VRMLoadTests.cs b/Assets/VRM/Tests/VRMLoadTests.cs index dbec0820a8..3399073962 100644 --- a/Assets/VRM/Tests/VRMLoadTests.cs +++ b/Assets/VRM/Tests/VRMLoadTests.cs @@ -106,7 +106,7 @@ public void VrmTestModelsTests() try { // export - var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.IsTextureEditorAsset ); + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime); // re import if (vrm != null) diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs index 90f8768b3e..9cc0b8bcb1 100644 --- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs +++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs @@ -320,7 +320,7 @@ void OnExportClicked(GameObject root) // export vrm-1.0 var exporter = new UniVRM10.Vrm10Exporter(AssetTextureUtil.IsTextureEditorAsset); var option = new VrmLib.ExportArgs(); - exporter.Export(root, model, converter, option, Meta ? Meta : m_tmpMeta); + exporter.Export(root, model, converter, option, AssetTextureUtil.GetTextureBytesWithMime, Meta ? Meta : m_tmpMeta); var exportedBytes = exporter.Storage.ToBytes(); diff --git a/Assets/VRM10/Runtime/IO/ModelExtensions.cs b/Assets/VRM10/Runtime/IO/ModelExtensions.cs index 3f8a292048..b88227440e 100644 --- a/Assets/VRM10/Runtime/IO/ModelExtensions.cs +++ b/Assets/VRM10/Runtime/IO/ModelExtensions.cs @@ -1,10 +1,12 @@ -using VrmLib; +using System; +using UnityEngine; + namespace UniVRM10 { public static class ModelExtensions { - public static byte[] ToGlb(this VrmLib.Model model) + public static byte[] ToGlb(this VrmLib.Model model, Func getTextureBytes) { // export vrm-1.0 var exporter10 = new Vrm10Exporter(_ => false); @@ -12,7 +14,7 @@ public static byte[] ToGlb(this VrmLib.Model model) { // vrm = false }; - exporter10.Export(null, model, null, option); + exporter10.Export(null, model, null, option, getTextureBytes); var glb10 = UniGLTF.Glb.Parse(exporter10.Storage.ToBytes()); return glb10.ToBytes(); } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 82f6de8562..89f22b7f28 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -128,7 +128,7 @@ static float[] ReverseX(Vector3 v) return new float[] { -v.x, v.y, v.z }; } - public void Export(GameObject root, Model model, RuntimeVrmConverter converter, ExportArgs option, VRM10MetaObject metaObject = null) + public void Export(GameObject root, Model model, RuntimeVrmConverter converter, ExportArgs option, Func getTextureBytes, VRM10MetaObject metaObject = null) { ExportAsset(model); @@ -182,7 +182,7 @@ public void Export(GameObject root, Model model, RuntimeVrmConverter converter, for (int i = 0; i < m_textureExporter.Exported.Count; ++i) { var unityTexture = m_textureExporter.Exported[i]; - Storage.Gltf.PushGltfTexture(0, unityTexture); + Storage.Gltf.PushGltfTexture(0, unityTexture, getTextureBytes); } if (thumbnailTextureIndex.HasValue) diff --git a/Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs b/Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs deleted file mode 100644 index 0b29ff9bc2..0000000000 --- a/Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs +++ /dev/null @@ -1,35 +0,0 @@ -using UniVRM10; -using VrmLib; - -public static class ExportDebugUtil -{ - public static void SaveJson(VrmLib.Model model, string path) - { - using (var stream = new System.IO.StreamWriter(path)) - { - stream.Write(GetJsonString(model)); - } - } - - public static void SaveVrm(VrmLib.Model model, string path) - { - using (var stream = new System.IO.StreamWriter(path)) - { - stream.Write(model.ToGlb()); - } - } - - public static string GetJsonString(VrmLib.Model model) - { - // export vrm-1.0 - var exporter10 = new Vrm10Exporter(_ => false); - var option = new VrmLib.ExportArgs - { - // vrm = false - }; - exporter10.Export(null, model, null, option); - var glbBytes10 = exporter10.Storage.ToBytes(); - var glb10 = UniGLTF.Glb.Parse(glbBytes10); - return System.Text.Encoding.UTF8.GetString(glb10.Json.Bytes.Array, glb10.Json.Bytes.Offset, glb10.Json.Bytes.Count); - } -} diff --git a/Assets/VRM10/Runtime/Scenes/Sample.cs b/Assets/VRM10/Runtime/Scenes/Sample.cs index 9bc304bb6f..927aed4b1d 100644 --- a/Assets/VRM10/Runtime/Scenes/Sample.cs +++ b/Assets/VRM10/Runtime/Scenes/Sample.cs @@ -4,6 +4,7 @@ using VrmLib; using UniVRM10; using UniGLTF; +using VRMShaders; public class Sample : MonoBehaviour { @@ -34,7 +35,7 @@ void OnEnable() var model = exporter.ToModelFrom10(vrm0x); // 右手系に変換 model.ConvertCoordinate(VrmLib.Coordinates.Vrm1); - var exportedBytes = model.ToGlb(); + var exportedBytes = model.ToGlb(TextureExporter.GetTextureBytesWithMime); // Import 1.0 var vrm10 = Import(exportedBytes, src); diff --git a/Assets/VRM10/Tests/ApiSampleTests.cs b/Assets/VRM10/Tests/ApiSampleTests.cs index df721491be..ed8dba284d 100644 --- a/Assets/VRM10/Tests/ApiSampleTests.cs +++ b/Assets/VRM10/Tests/ApiSampleTests.cs @@ -4,6 +4,7 @@ using UniGLTF; using UnityEngine; using UnityEngine.TestTools; +using VRMShaders; namespace UniVRM10.Test { @@ -45,7 +46,7 @@ byte[] ToVrm10(VrmLib.Model model) { // 右手系に変換 VrmLib.ModelExtensionsForCoordinates.ConvertCoordinate(model, VrmLib.Coordinates.Vrm1); - var bytes = UniVRM10.ModelExtensions.ToGlb(model); + var bytes = UniVRM10.ModelExtensions.ToGlb(model, AssetTextureUtil.GetTextureBytesWithMime); return bytes; } diff --git a/Assets/VRM10/Tests/VRM10.Tests.asmdef b/Assets/VRM10/Tests/VRM10.Tests.asmdef index edbcc5fca7..06f9033eee 100644 --- a/Assets/VRM10/Tests/VRM10.Tests.asmdef +++ b/Assets/VRM10/Tests/VRM10.Tests.asmdef @@ -3,7 +3,8 @@ "references": [ "VrmLib", "VRM10", - "UniGLTF" + "UniGLTF", + "VRMShaders.GLTF.IO.Editor" ], "optionalUnityReferences": [ "TestAssemblies" diff --git a/Assets/VRMShaders/GLTF/IO/Editor/AssetTextureUtil.cs b/Assets/VRMShaders/GLTF/IO/Editor/AssetTextureUtil.cs index a4423e84fe..c69ae48ede 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/AssetTextureUtil.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/AssetTextureUtil.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Reflection; using UnityEditor; using UnityEngine; @@ -67,5 +68,53 @@ public static bool IsTextureEditorAsset(Texture texture) // not Texture2D or not exists Texture2D asset. EncodeToPng return false; } + + /// + /// Assetから画像のバイト列を得る + /// + /// + /// + /// + public static bool TryGetBytesWithMime(Texture2D texture, out byte[] bytes, out string mime) + { + var path = AssetDatabase.GetAssetOrScenePath(texture); + if (string.IsNullOrEmpty(path)) + { + bytes = default; + mime = default; + return false; + } + + var ext = Path.GetExtension(path).ToLower(); + + switch (ext) + { + case ".png": + bytes = System.IO.File.ReadAllBytes(path); + mime = "image/png"; + return true; + + case ".jpg": + bytes = System.IO.File.ReadAllBytes(path); + mime = "image/jpeg"; + return true; + } + + // dds ? astc ? tga ? + + bytes = default; + mime = default; + return false; + } + + public static (byte[], string) GetTextureBytesWithMime(Texture2D texture) + { + if (TryGetBytesWithMime(texture, out byte[] bytes, out string mime)) + { + return (bytes, mime); + } + + return TextureExporter.GetTextureBytesWithMime(texture); + } } } diff --git a/Assets/VRMShaders/GLTF/IO/Editor/VRMShaders.GLTF.IO.Editor.asmdef b/Assets/VRMShaders/GLTF/IO/Editor/VRMShaders.GLTF.IO.Editor.asmdef index 175af88d24..52326176ae 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/VRMShaders.GLTF.IO.Editor.asmdef +++ b/Assets/VRMShaders/GLTF/IO/Editor/VRMShaders.GLTF.IO.Editor.asmdef @@ -1,6 +1,9 @@ { "name": "VRMShaders.GLTF.IO.Editor", - "references": [], + "references": [ + "VRMShaders.GLTF.IO.Runtime" + ], + "optionalUnityReferences": [], "includePlatforms": [ "Editor" ], @@ -9,7 +12,5 @@ "overrideReferences": false, "precompiledReferences": [], "autoReferenced": false, - "defineConstraints": [], - "versionDefines": [], - "noEngineReferences": false + "defineConstraints": [] } \ 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 43f8e7d278..a8f089f4a4 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureExporter.cs @@ -196,5 +196,37 @@ public int ExportNormal(Texture src) return index; } + + /// + /// 画像のバイト列を得る + /// + /// + /// + /// + public static (byte[] bytes, string mime) GetTextureBytesWithMime(Texture2D texture) + { + try + { + var png = texture.EncodeToPNG(); + if (png != null) + { + return (png, "image/png"); + } + } + catch (Exception ex) + { + // fail to EncodeToPng + // System.ArgumentException: not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. + Debug.LogWarning(ex); + } + + { + // try copy and EncodeToPng + var copy = TextureConverter.CopyTexture(texture, TextureImportTypes.sRGB, null); + var png = copy.EncodeToPNG(); + UnityEngine.Object.DestroyImmediate(copy); + return (png, "image/png"); + } + } } } diff --git a/Assets/UniGLTF/Tests/Resources/4x4.png b/Assets/VRMShaders/GLTF/IO/Tests/4x4.png similarity index 100% rename from Assets/UniGLTF/Tests/Resources/4x4.png rename to Assets/VRMShaders/GLTF/IO/Tests/4x4.png diff --git a/Assets/UniGLTF/Tests/UniGLTF/Resources/4x4.png.meta b/Assets/VRMShaders/GLTF/IO/Tests/4x4.png.meta similarity index 100% rename from Assets/UniGLTF/Tests/UniGLTF/Resources/4x4.png.meta rename to Assets/VRMShaders/GLTF/IO/Tests/4x4.png.meta diff --git a/Assets/UniGLTF/Tests/UniGLTF/Resources/4x4compressed.DDS b/Assets/VRMShaders/GLTF/IO/Tests/4x4compressed.DDS similarity index 100% rename from Assets/UniGLTF/Tests/UniGLTF/Resources/4x4compressed.DDS rename to Assets/VRMShaders/GLTF/IO/Tests/4x4compressed.DDS diff --git a/Assets/UniGLTF/Tests/UniGLTF/Resources/4x4compressed.DDS.meta b/Assets/VRMShaders/GLTF/IO/Tests/4x4compressed.DDS.meta similarity index 100% rename from Assets/UniGLTF/Tests/UniGLTF/Resources/4x4compressed.DDS.meta rename to Assets/VRMShaders/GLTF/IO/Tests/4x4compressed.DDS.meta diff --git a/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs new file mode 100644 index 0000000000..7f14db73d0 --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs @@ -0,0 +1,29 @@ +using NUnit.Framework; +using UnityEditor; +using UnityEngine; + +namespace VRMShaders +{ + public class TextureBytesTests + { + static string AssetPath = "Assets/VRMShaders/GLTF/IO/Tests"; + + [Test] + public void NotReadable() + { + var readonlyTexture = AssetDatabase.LoadAssetAtPath($"{AssetPath}/4x4.png"); + Assert.False(readonlyTexture.isReadable); + var (bytes, mime) = AssetTextureUtil.GetTextureBytesWithMime(readonlyTexture); + Assert.NotNull(bytes); + } + + [Test] + public void Compressed() + { + var readonlyTexture = AssetDatabase.LoadAssetAtPath($"{AssetPath}/4x4compressed.dds"); + Assert.False(readonlyTexture.isReadable); + var (bytes, mime) = AssetTextureUtil.GetTextureBytesWithMime(readonlyTexture); + Assert.NotNull(bytes); + } + } +} diff --git a/Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs.meta b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs.meta similarity index 83% rename from Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs.meta rename to Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs.meta index 35a36d750e..be8f4b5944 100644 --- a/Assets/VRM10/Runtime/Scenes/ExportDebugUtil.cs.meta +++ b/Assets/VRMShaders/GLTF/IO/Tests/TextureBytesTests.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: cee5c63e6473ed848a935b446d60cccf +guid: 2c9ac46bc2fc4d947bab6555f14d1d9f MonoImporter: externalObjects: {} serializedVersion: 2