From 693cc665c285846d8a1a80ac933c791c4befd9d0 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Feb 2021 13:44:30 +0900 Subject: [PATCH] Separate TextureFactory from MaterialFactory --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 17 +- .../MaterialFacotry/MaterialFactory.cs | 162 ++++-------------- .../MaterialFacotry/TextureLoader.meta | 2 +- .../{ => TextureLoader}/GetTextureParam.cs | 1 - .../GetTextureParam.cs.meta | 2 +- .../TextureLoader/TextureFactory.cs | 126 ++++++++++++++ .../TextureLoader/TextureFactory.cs.meta | 11 ++ .../Runtime/UniGLTF/TextureLoader.meta | 8 + Assets/VRM/Runtime/IO/VRMImporterContext.cs | 2 +- 9 files changed, 195 insertions(+), 136 deletions(-) rename Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/{ => TextureLoader}/GetTextureParam.cs (95%) rename Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/{ => TextureLoader}/GetTextureParam.cs.meta (83%) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/TextureLoader.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 12098ecdd..53fd5ae6d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -108,6 +108,9 @@ namespace UniGLTF MaterialFactory m_materialFactory; public MaterialFactory MaterialFactory => m_materialFactory; + TextureFactory m_textureFactory; + public TextureFactory TextureFactory => m_textureFactory; + public ImporterContext() { } @@ -499,7 +502,13 @@ namespace UniGLTF { // root task. do nothing }) - .ContinueWithCoroutine(Scheduler.MainThread, m_materialFactory.LoadMaterials) + .ContinueWithCoroutine(Scheduler.MainThread, () => + { + using (MeasureTime("LoadMaterials")) + { + return m_materialFactory.LoadMaterials(m_textureFactory.GetTextureAsync); + } + }) .OnExecute(Scheduler.ThreadPool, parent => { // UniGLTF does not support draco @@ -690,6 +699,10 @@ namespace UniGLTF protected virtual IEnumerable ObjectsForSubAsset() { + foreach (var x in TextureFactory.ObjectsForSubAsset()) + { + yield return x; + } foreach (var x in MaterialFactory.ObjectsForSubAsset()) { yield return x; @@ -885,7 +898,7 @@ namespace UniGLTF } // texture will load from assets - m_materialFactory.ImageBaseDir = prefabParentDir; + m_textureFactory.ImageBaseDir = prefabParentDir; } #endregion #endif diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs index a49b368e1..17b8b0426 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs @@ -4,9 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using UnityEngine; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace UniGLTF { @@ -20,8 +17,6 @@ namespace UniGLTF m_storage = storage; } - public UnityPath ImageBaseDir { get; set; } - public delegate Task CreateMaterialAsyncFunc(glTF glTF, int i, GetTextureAsyncFunc getTexture); CreateMaterialAsyncFunc m_createMaterialAsync; public CreateMaterialAsyncFunc CreateMaterialAsync @@ -40,85 +35,24 @@ namespace UniGLTF } } - List m_textuers = new List(); - Dictionary m_textureCache = new Dictionary(); - - /// - /// テクスチャーをロード、必要であれば変換して返す。 - /// 同じものはキャッシュを返す - /// - /// 変換の有無を判断する: METALLIC_GLOSS_PROP - /// METALLIC_GLOSS_PROPの追加パラメーター - /// gltf の texture index - /// - public async Task GetTextureAsync(GetTextureParam param) - { - if (m_textureCache.TryGetValue(param, out Texture2D texture)) - { - return texture; - } - - { - var defaultParam = GetTextureParam.Create(param.Index0.Value); - if (!m_textureCache.TryGetValue(defaultParam, out texture)) - { - texture = await LoadTextureAsync(param.Index0.Value); - m_textureCache.Add(defaultParam, texture); - } - } - - switch (param.TextureType) - { - case GetTextureParam.NORMAL_PROP: - { - if (Application.isPlaying) - { - var converted = new NormalConverter().GetImportTexture(texture); - m_textureCache.Add(param, converted); - return converted; - } - else - { -#if UNITY_EDITOR - var textureAssetPath = AssetDatabase.GetAssetPath(texture); - if (!string.IsNullOrEmpty(textureAssetPath)) - { - TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath); - } - else - { - Debug.LogWarningFormat("no asset for {0}", texture); - } -#endif - m_textureCache.Add(param, texture); - return texture; - } - } - - case GetTextureParam.METALLIC_GLOSS_PROP: - { - // Bake roughnessFactor values into a texture. - var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(texture); - m_textureCache.Add(param, converted); - return converted; - } - - case GetTextureParam.OCCLUSION_PROP: - { - var converted = new OcclusionConverter().GetImportTexture(texture); - m_textureCache.Add(param, converted); - return converted; - } - - default: - return texture; - } - - throw new NotImplementedException(); - } - List m_materials = new List(); public IReadOnlyList Materials => m_materials; + public void Dispose() + { + foreach (var x in ObjectsForSubAsset()) + { + UnityEngine.Object.DestroyImmediate(x, true); + } + } + + public IEnumerable ObjectsForSubAsset() + { + foreach (var x in m_materials) + { + yield return x; + } + } + public void AddMaterial(Material material) { var originalName = material.name; @@ -136,65 +70,33 @@ namespace UniGLTF return m_materials[index]; } - public virtual Task LoadTextureAsync(int index) + public IEnumerator LoadMaterials(GetTextureAsyncFunc getTexture) { -#if UNIGLTF_USE_WEBREQUEST_TEXTURELOADER - return UnityWebRequestTextureLoader.LoadTextureAsync(index); -#else - return GltfTextureLoader.LoadTextureAsync(m_gltf, m_storage, index); -#endif - } - - public IEnumerator LoadMaterials() - { - // using (MeasureTime("LoadMaterials")) + if (m_gltf.materials == null || m_gltf.materials.Count == 0) { - if (m_gltf.materials == null || m_gltf.materials.Count == 0) - { - var task = CreateMaterialAsync(m_gltf, 0, GetTextureAsync); + var task = CreateMaterialAsync(m_gltf, 0, getTexture); + foreach (var x in task.AsIEnumerator()) + { + yield return x; + } + + AddMaterial(task.Result); + } + else + { + for (int i = 0; i < m_gltf.materials.Count; ++i) + { + var task = CreateMaterialAsync(m_gltf, i, getTexture); foreach (var x in task.AsIEnumerator()) { - yield return x; + yield return null; } AddMaterial(task.Result); } - else - { - for (int i = 0; i < m_gltf.materials.Count; ++i) - { - var task = CreateMaterialAsync(m_gltf, i, GetTextureAsync); - foreach (var x in task.AsIEnumerator()) - { - yield return null; - } - - AddMaterial(task.Result); - } - } } yield return null; } - - public void Dispose() - { - foreach (var x in ObjectsForSubAsset()) - { - UnityEngine.Object.DestroyImmediate(x, true); - } - } - - public IEnumerable ObjectsForSubAsset() - { - foreach (var kv in m_textureCache) - { - yield return kv.Value; - } - foreach (var x in m_materials) - { - yield return x; - } - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta index 84d4a147d..1ef61d2aa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 54a405d31cce94643be8f76f805da8c8 +guid: a4bd2e8f388fb204186d743f337ddb83 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs similarity index 95% rename from Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs index c712f34f5..c2bce5fbc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs @@ -51,5 +51,4 @@ namespace UniGLTF } } - public delegate Task GetTextureAsyncFunc(GetTextureParam param); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs.meta similarity index 83% rename from Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs.meta index d5bf2ad96..cdeb33876 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/GetTextureParam.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GetTextureParam.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 8f1dc05e4970e724e9f50c0a5a67a25e +guid: 405597f56d6540347bbecb1203aba033 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs new file mode 100644 index 000000000..fc20ef08e --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEditor; +using UnityEngine; + +namespace UniGLTF +{ + public delegate Task GetTextureAsyncFunc(GetTextureParam param); + public class TextureFactory : IDisposable + + { + glTF m_gltf; + IStorage m_storage; + + public UnityPath ImageBaseDir { get; set; } + + public TextureFactory(glTF gltf, IStorage storage) + { + m_gltf = gltf; + m_storage = storage; + } + + List m_textuers = new List(); + public void Dispose() + { + foreach (var x in ObjectsForSubAsset()) + { + UnityEngine.Object.DestroyImmediate(x, true); + } + } + + public IEnumerable ObjectsForSubAsset() + { + foreach (var kv in m_textureCache) + { + yield return kv.Value; + } + } + + Dictionary m_textureCache = new Dictionary(); + + public virtual Task LoadTextureAsync(int index) + { +#if UNIGLTF_USE_WEBREQUEST_TEXTURELOADER + return UnityWebRequestTextureLoader.LoadTextureAsync(index); +#else + return GltfTextureLoader.LoadTextureAsync(m_gltf, m_storage, index); +#endif + } + + /// + /// テクスチャーをロード、必要であれば変換して返す。 + /// 同じものはキャッシュを返す + /// + /// 変換の有無を判断する: METALLIC_GLOSS_PROP + /// METALLIC_GLOSS_PROPの追加パラメーター + /// gltf の texture index + /// + public async Task GetTextureAsync(GetTextureParam param) + { + if (m_textureCache.TryGetValue(param, out Texture2D texture)) + { + return texture; + } + + { + var defaultParam = GetTextureParam.Create(param.Index0.Value); + if (!m_textureCache.TryGetValue(defaultParam, out texture)) + { + texture = await LoadTextureAsync(param.Index0.Value); + m_textureCache.Add(defaultParam, texture); + } + } + + switch (param.TextureType) + { + case GetTextureParam.NORMAL_PROP: + { + if (Application.isPlaying) + { + var converted = new NormalConverter().GetImportTexture(texture); + m_textureCache.Add(param, converted); + return converted; + } + else + { +#if UNITY_EDITOR + var textureAssetPath = AssetDatabase.GetAssetPath(texture); + if (!string.IsNullOrEmpty(textureAssetPath)) + { + TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath); + } + else + { + Debug.LogWarningFormat("no asset for {0}", texture); + } +#endif + m_textureCache.Add(param, texture); + return texture; + } + } + + case GetTextureParam.METALLIC_GLOSS_PROP: + { + // Bake roughnessFactor values into a texture. + var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(texture); + m_textureCache.Add(param, converted); + return converted; + } + + case GetTextureParam.OCCLUSION_PROP: + { + var converted = new OcclusionConverter().GetImportTexture(texture); + m_textureCache.Add(param, converted); + return converted; + } + + default: + return texture; + } + + throw new NotImplementedException(); + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs.meta new file mode 100644 index 000000000..58a2c5064 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/TextureFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d6069f2bfbfc0c4c9276e8a5f8b0789 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/TextureLoader.meta b/Assets/UniGLTF/Runtime/UniGLTF/TextureLoader.meta new file mode 100644 index 000000000..84d4a147d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/TextureLoader.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 54a405d31cce94643be8f76f805da8c8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index bd73d1959..c15c9995c 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -310,7 +310,7 @@ namespace VRM meta.ContactInformation = gltfMeta.contactInformation; meta.Reference = gltfMeta.reference; meta.Title = gltfMeta.title; - meta.Thumbnail = await MaterialFactory.GetTextureAsync(GetTextureParam.Create(gltfMeta.texture)); + meta.Thumbnail = await TextureFactory.GetTextureAsync(GetTextureParam.Create(gltfMeta.texture)); meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; meta.SexualUssage = gltfMeta.sexualUssage;