From 28d931aa6f8621a0f430885167aa30ee28a37b3c Mon Sep 17 00:00:00 2001 From: hiroj Date: Fri, 22 Jan 2021 21:25:01 +0900 Subject: [PATCH] add ITextureExporter --- .../Runtime/UniGLTF/IO/ITextureExporter.cs | 16 ++++ .../UniGLTF/IO/ITextureExporter.cs.meta | 11 +++ .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 76 +++++++------------ .../Runtime/UniGLTF/IO/gltfExporter.cs | 28 ++++++- Assets/VRM/Runtime/IO/VRMExporter.cs | 4 +- 5 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs new file mode 100644 index 0000000000..1346247ba3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +#if UNITY_EDITOR +#endif + + +namespace UniGLTF +{ + public interface ITextureExporter + { + (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType); + IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m); + int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType); + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta new file mode 100644 index 0000000000..d6e509685e --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9968c71baa1b1c04c94b0d3191cf513a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs index 99515c8f87..ff01045bfb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs @@ -10,7 +10,8 @@ namespace UniGLTF { - public static class TextureIO + + public class TextureIO : ITextureExporter { public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType) { @@ -80,24 +81,12 @@ public static void MarkTextureAssetAsNormalMap(string assetPath) } #endif - public struct TextureExportItem - { - public Texture Texture; - public glTFTextureTypes TextureType; - - public TextureExportItem(Texture texture, glTFTextureTypes textureType) - { - Texture = texture; - TextureType = textureType; - } - } - - public static IEnumerable GetTextures(Material m) + public virtual IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m) { var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); if (props == null) { - yield return new TextureExportItem(m.mainTexture, glTFTextureTypes.BaseColor); + yield return (m.mainTexture, glTFTextureTypes.BaseColor); } foreach (var prop in props.Properties) @@ -105,19 +94,12 @@ public static IEnumerable GetTextures(Material m) if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv) { - yield return new TextureExportItem(m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key)); + yield return (m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key)); } } } - - struct BytesWithMime - { - public Byte[] Bytes; - public string Mime; - } - - static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType) + public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType) { #if UNITY_EDITOR var path = UnityPath.FromAsset(texture); @@ -138,46 +120,46 @@ static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureT // Resized exporting if MaxSize setting value is smaller than original image size. if (originalSize > requiredMaxSize) { - return new BytesWithMime - { - Bytes = TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(), - Mime = "image/png", - }; + return + ( + TextureItem.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(), + "image/png" + ); } } if (path.Extension == ".png") { - return new BytesWithMime - { - Bytes = System.IO.File.ReadAllBytes(path.FullPath), - Mime = "image/png", - }; + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/png" + ); } if (path.Extension == ".jpg") { - return new BytesWithMime - { - Bytes = System.IO.File.ReadAllBytes(path.FullPath), - Mime = "image/jpeg", - }; + return + ( + System.IO.File.ReadAllBytes(path.FullPath), + "image/jpeg" + ); } } #endif - return new BytesWithMime - { - Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), - Mime = "image/png", - }; + return + ( + TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), + "image/png" + ); } - public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType) + public virtual int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType) { var bytesWithMime = GetBytesWithMime(texture, textureType); ; // add view - var view = gltf.buffers[bufferIndex].Append(bytesWithMime.Bytes, glBufferTarget.NONE); + var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); var viewIndex = gltf.AddBufferView(view); // add image @@ -186,7 +168,7 @@ public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glT { name = texture.name, bufferView = viewIndex, - mimeType = bytesWithMime.Mime, + mimeType = bytesWithMime.mine, }); // add sampler diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 8ae3762d80..7c5f1097ba 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -141,6 +141,28 @@ protected virtual IMaterialExporter CreateMaterialExporter() return new MaterialExporter(); } + private ITextureExporter _textureExporter; + public ITextureExporter TextureExporter + { + get + { + if (_textureExporter != null) + { + return _textureExporter; + } + else + { + _textureExporter = new TextureIO(); + return _textureExporter; + } + } + set + { + _textureExporter = value; + } + } + + /// /// このエクスポーターがサポートするExtension /// @@ -237,9 +259,9 @@ public virtual void Export(MeshExportSettings meshExportSettings) #region Materials and Textures Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList(); - var unityTextures = Materials.SelectMany(x => TextureIO.GetTextures(x)).Where(x => x.Texture != null).Distinct().ToList(); + var unityTextures = Materials.SelectMany(x => TextureExporter.GetTextures(x)).Where(x => x.texture != null).Distinct().ToList(); - TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture)); + TextureManager = new TextureExportManager(unityTextures.Select(x => x.texture)); var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); @@ -247,7 +269,7 @@ public virtual void Export(MeshExportSettings meshExportSettings) for (int i = 0; i < unityTextures.Count; ++i) { var unityTexture = unityTextures[i]; - TextureIO.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType); + TextureExporter.ExportTexture(glTF, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.textureType); } #endregion diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 390cfe73d9..3181c3ca73 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -113,7 +113,7 @@ public override void Export(MeshExportSettings configuration) VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); } VRM.meta.licenseType = meta.LicenseType; @@ -138,7 +138,7 @@ public override void Export(MeshExportSettings configuration) VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); + VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown); } // ussage permission