From a8dcec026393eec130a3ba679f0f484346a04013 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 17:02:46 +0900 Subject: [PATCH] static class TextureIO --- .../Runtime/UniGLTF/IO/ITextureExporter.cs | 16 ---- .../UniGLTF/IO/ITextureExporter.cs.meta | 11 --- .../OcclusionMetallicRoughnessConverter.cs | 6 +- .../TextureConverter/TextureExportManager.cs | 44 ++++++++- .../UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs | 95 +------------------ .../Runtime/UniGLTF/IO/gltfExporter.cs | 28 ++---- Assets/VRM/Runtime/IO/VRMExporter.cs | 5 +- 7 files changed, 59 insertions(+), 146 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs delete 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 deleted file mode 100644 index 1346247ba..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index d6e509685..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ITextureExporter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -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/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index 068cb105a..44b89f433 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -100,7 +100,11 @@ namespace UniGLTF } else { - throw new NotImplementedException(); + var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null); + copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray()); + copyTexture.Apply(); + copyTexture.name = metallicSmoothTexture.name; + return copyTexture; } } else if (metallicSmoothTexture) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs index d321b6e00..6a53c45fa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; using UnityEngine; - +using System.Reflection; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace UniGLTF { @@ -28,6 +31,34 @@ namespace UniGLTF Dictionary m_exportMap = new Dictionary(); List m_exported = new List(); + public IReadOnlyList Exported => m_exported; + + static bool CopyIfMaxTextureSizeIsSmaller(Texture src/*, glTFTextureTypes textureType, out Texture2D dst*/) + { +#if UNITY_EDITOR + var textureImporter = AssetImporter.GetAtPath(UnityPath.FromAsset(src).Value) as TextureImporter; + var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); + if (textureImporter != null && getSizeMethod != null) + { + var args = new object[2] { 0, 0 }; + getSizeMethod.Invoke(textureImporter, args); + var originalWidth = (int)args[0]; + var originalHeight = (int)args[1]; + var originalSize = Mathf.Max(originalWidth, originalHeight); + if (textureImporter.maxTextureSize < originalSize) + { + // export resized texture. + // this has textureImporter.maxTextureSize + // dst = TextureConverter.CopyTexture(src, textureType, null); + return true; + } + } +#endif + + // dst = default; + return false; + } + /// /// Texture の export index を得る /// @@ -59,7 +90,7 @@ namespace UniGLTF // get Texture2D index = m_exported.Count; - if (src is Texture2D texture2D) + if (src is Texture2D texture2D && !CopyIfMaxTextureSizeIsSmaller(src)) { // do nothing } @@ -105,7 +136,10 @@ namespace UniGLTF m_exported.Add(texture2D); m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); - m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + if (occlusionTexture != metallicSmoothTexture && occlusionTexture != null) + { + m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index); + } return index; } @@ -117,6 +151,10 @@ namespace UniGLTF texture2D = src as Texture2D; if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src))) { + if (CopyIfMaxTextureSizeIsSmaller(src)) + { + return false; + } return true; } #endif diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs index 84518f300..13b122e17 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO.cs @@ -1,17 +1,12 @@ using System; -using System.Collections.Generic; using System.Linq; using UnityEngine; -#if UNITY_EDITOR -using System.Reflection; -using UnityEditor; -#endif namespace UniGLTF { - public class TextureIO : ITextureExporter + public class TextureIO { public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType) { @@ -39,24 +34,6 @@ namespace UniGLTF } } - public static glTFTextureTypes GetglTFTextureType(string shaderName, string propName) - { - switch (propName) - { - case "_MetallicGlossMap": - case "_OcclusionMap": - return glTFTextureTypes.OcclusionMetallicRoughness; - case "_BumpMap": - return glTFTextureTypes.Normal; - case "_Color": - case "_EmissionMap": - return glTFTextureTypes.SRGB; - default: - // Debug.LogWarning($"unknown texture property: {propName} as sRGB"); - return glTFTextureTypes.SRGB; - } - } - public static bool TryGetglTFTextureType(glTF glTf, int textureIndex, out glTFTextureTypes textureType) { foreach (var material in glTf.materials) @@ -74,74 +51,12 @@ namespace UniGLTF return false; } -#if UNITY_EDITOR - public static void MarkTextureAssetAsNormalMap(string assetPath) - { - if (string.IsNullOrEmpty(assetPath)) - { - return; - } - - var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; - if (null == textureImporter) - { - return; - } - - //Debug.LogFormat("[MarkTextureAssetAsNormalMap] {0}", assetPath); - textureImporter.textureType = TextureImporterType.NormalMap; - textureImporter.SaveAndReimport(); - } -#endif - - public virtual IEnumerable<(Texture texture, glTFTextureTypes textureType)> GetTextures(Material m) - { - var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); - if (props == null) - { - // unknown shader - yield return (m.mainTexture, glTFTextureTypes.SRGB); - } - - foreach (var prop in props.Properties) - { - - if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv) - { - yield return (m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key)); - } - } - } - - public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType) + static (Byte[] bytes, string mine) GetBytesWithMime(Texture2D texture) { #if UNITY_EDITOR var path = UnityPath.FromAsset(texture); if (path.IsUnderAssetsFolder) { - var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter; - var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); - if (textureImporter != null && getSizeMethod != null) - { - var args = new object[2] { 0, 0 }; - getSizeMethod.Invoke(textureImporter, args); - var originalWidth = (int)args[0]; - var originalHeight = (int)args[1]; - - var originalSize = Mathf.Max(originalWidth, originalHeight); - var requiredMaxSize = textureImporter.maxTextureSize; - - // Resized exporting if MaxSize setting value is smaller than original image size. - if (originalSize > requiredMaxSize) - { - return - ( - TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), - "image/png" - ); - } - } - if (path.Extension == ".png") { return @@ -163,14 +78,14 @@ namespace UniGLTF return ( - TextureConverter.CopyTexture(texture, textureType, null).EncodeToPNG(), + texture.EncodeToPNG(), "image/png" ); } - public int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType) + static public int ExportTexture(glTF gltf, int bufferIndex, Texture2D texture) { - var bytesWithMime = GetBytesWithMime(texture, textureType); ; + var bytesWithMime = GetBytesWithMime(texture); // add view var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index abd51a5cb..7cb16bf6a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -54,28 +54,6 @@ namespace UniGLTF 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 /// @@ -206,6 +184,12 @@ namespace UniGLTF var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); + + for (int i = 0; i < TextureManager.Exported.Count; ++i) + { + var unityTexture = TextureManager.Exported[i]; + TextureIO.ExportTexture(glTF, bufferIndex, unityTexture); + } #endregion #region Meshes diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index fdbb0507b..e7f8237b5 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using UniGLTF; using UniJSON; @@ -113,7 +112,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); + VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } VRM.meta.licenseType = meta.LicenseType; @@ -138,7 +137,7 @@ namespace VRM VRM.meta.title = meta.Title; if (meta.Thumbnail != null) { - VRM.meta.texture = TextureExporter.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.SRGB); + VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail); } // ussage permission