From 8aa450d3ac4ddba9093293bece04fd79a5820066 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 23 Mar 2021 18:43:12 +0900 Subject: [PATCH] remove UnityWebRequestTextureLoader --- .../ScriptedImporter/ScriptedImporterImpl.cs | 3 +- .../Runtime/UniGLTF/IO/ImporterContext.cs | 16 +-- .../UniGLTF/IO/TextureIO/TextureFactory.cs | 18 +-- .../TextureIO/UnityWebRequestTextureLoader.cs | 119 ------------------ .../UnityWebRequestTextureLoader.cs.meta | 11 -- Assets/VRM/Editor/Format/VRMImporterMenu.cs | 2 +- .../Editor/Format/vrmAssetPostprocessor.cs | 2 +- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 3 +- 8 files changed, 14 insertions(+), 160 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 02f785dd3..1129e72c9 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -39,7 +39,7 @@ namespace UniGLTF var externalTextures = EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent).ToArray(); - using (var loaded = new ImporterContext(parser, null, externalObjectMap.Concat(externalTextures))) + using (var loaded = new ImporterContext(parser, externalObjectMap.Concat(externalTextures))) { // settings TextureImporters foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser.GLTF)) @@ -96,7 +96,6 @@ namespace UniGLTF if (exclude != null && exclude.Any(kv => kv.Item2.name == asset.name)) { // exclude. skip - var a = 0; } else{ if(used.Add(asset)){ diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index e815eb303..5f14f4c27 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -41,24 +41,10 @@ namespace UniGLTF IAwaitCaller m_awaitCaller; public ImporterContext(GltfParser parser, - LoadTextureAsyncFunc loadTextureAsync = null, IEnumerable<(string, UnityEngine.Object)> externalObjectMap = null) { m_parser = parser; - if (loadTextureAsync == null) - { -#if UNIGLTF_USE_WEBREQUEST_TEXTURELOADER - loadTextureAsync = (awaitCaller, index, used) => UnityWebRequestTextureLoader.LoadTextureAsync(index); -#else - loadTextureAsync = async (awaitCaller, index, used) => - { - var texture = await GltfTextureLoader.LoadTextureAsync(awaitCaller, GLTF, Storage, index); - return new TextureLoadInfo(texture, used, false); - }; -#endif - } - - m_textureFactory = new TextureFactory(loadTextureAsync, externalObjectMap); + m_textureFactory = new TextureFactory(GLTF, Storage, externalObjectMap); m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index bbae20e97..8676488fd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -42,10 +42,12 @@ namespace UniGLTF } } - public delegate Task LoadTextureAsyncFunc(IAwaitCaller awaitCaller, int index, bool used); public delegate Task GetTextureAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, GetTextureParam param); public class TextureFactory : IDisposable { + glTF m_gltf; + IStorage m_storage; + public readonly Dictionary ExternalMap; public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) @@ -72,12 +74,11 @@ namespace UniGLTF return false; } - public UnityPath ImageBaseDir { get; set; } - - public TextureFactory(LoadTextureAsyncFunc loadTextureAsync, - IEnumerable<(string, UnityEngine.Object)> externalMap) + public TextureFactory(glTF gltf, IStorage storage, IEnumerable<(string, UnityEngine.Object)> externalMap) { - LoadTextureAsync = loadTextureAsync; + m_gltf = gltf; + m_storage = storage; + if (externalMap != null) { ExternalMap = externalMap @@ -132,14 +133,13 @@ namespace UniGLTF public IEnumerable Textures => m_textureCache.Values; - public LoadTextureAsyncFunc LoadTextureAsync; - async Task GetOrCreateBaseTexture(IAwaitCaller awaitCaller, glTF gltf, int textureIndex, bool used) { var name = gltf.textures[textureIndex].name; if (!m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo)) { - cacheInfo = await LoadTextureAsync(awaitCaller, textureIndex, used); + var texture = await GltfTextureLoader.LoadTextureAsync(awaitCaller, m_gltf, m_storage, textureIndex); + cacheInfo = new TextureLoadInfo(texture, used, false); m_textureCache.Add(name, cacheInfo); } return cacheInfo; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs deleted file mode 100644 index 329bc5cbc..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System; -using System.Collections; -using System.IO; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.Networking; - -namespace UniGLTF -{ - public class UnityWebRequestTextureLoader - { - public Texture2D Texture - { - private set; - get; - } - - int m_textureIndex; - - public UnityWebRequestTextureLoader(int textureIndex) - { - m_textureIndex = textureIndex; - } - - UnityWebRequest m_uwr; - public void Dispose() - { - if (m_uwr != null) - { - m_uwr.Dispose(); - m_uwr = null; - } - } - - string m_textureName = default; - public void ProcessOnAnyThread() - { - } - - class Deleter : IDisposable - { - string m_path; - public Deleter(string path) - { - m_path = path; - } - public void Dispose() - { - if (File.Exists(m_path)) - { - File.Delete(m_path); - } - } - } - - public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler) - { - var gltfTexture = gltf.textures[m_textureIndex]; - var bytes = gltf.GetImageBytes(storage, gltfTexture.source); - - // tmp file - var tmp = Path.GetTempFileName(); - using (var f = new FileStream(tmp, FileMode.Create)) - { - f.Write(bytes.Array, bytes.Offset, bytes.Count); - } - - using (var d = new Deleter(tmp)) - { - var url = "file:///" + tmp.Replace("\\", "/"); - Debug.LogFormat("UnityWebRequest: {0}", url); -#if UNITY_2017_1_OR_NEWER - using (var m_uwr = UnityWebRequestTexture.GetTexture(url, true)) - { - yield return m_uwr.SendWebRequest(); - - if (m_uwr.isNetworkError || m_uwr.isHttpError) - { - Debug.LogWarning(m_uwr.error); - } - else - { - // Get downloaded asset bundle - Texture = ((DownloadHandlerTexture)m_uwr.downloadHandler).texture; - Texture.name = m_textureName; - } - } -#elif UNITY_5 - using (var m_uwr = new WWW(url)) - { - yield return m_uwr; - - // wait for request - while (!m_uwr.isDone) - { - yield return null; - } - - if (!string.IsNullOrEmpty(m_uwr.error)) - { - Debug.Log(m_uwr.error); - yield break; - } - - // Get downloaded asset bundle - Texture = m_uwr.textureNonReadable; - Texture.name = m_textureName; - } -#else -#error Unsupported Unity version -#endif - } - if (sampler != null) - { - TextureSamplerUtil.SetSampler(Texture, sampler); - } - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta deleted file mode 100644 index d3369dfbc..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/UnityWebRequestTextureLoader.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: b91b7b42ddfd6284a9303a0901fccaa3 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 37f6259c5..727cdc289 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -80,7 +80,7 @@ namespace VRM return (texture.name, texture); }).ToArray(); - using (var context = new VRMImporterContext(parser, null, map)) + using (var context = new VRMImporterContext(parser, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 261232e42..4c5ac52d0 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -57,7 +57,7 @@ namespace VRM return (texture.name, texture: texture as UnityEngine.Object); }).ToArray(); - using (var context = new VRMImporterContext(parser, null, map)) + using (var context = new VRMImporterContext(parser, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser.GLTF)) diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 3f6c7d63d..f081bc205 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -19,8 +19,7 @@ namespace VRM public VRM.glTF_VRM_extensions VRM { get; private set; } public VRMImporterContext(GltfParser parser, - UniGLTF.LoadTextureAsyncFunc asyncTextureLoader = null, - IEnumerable<(string, UnityEngine.Object)> externalObjectMap = null) : base(parser, asyncTextureLoader, externalObjectMap) + IEnumerable<(string, UnityEngine.Object)> externalObjectMap = null) : base(parser, externalObjectMap) { // parse VRM part if (glTF_VRM_extensions.TryDeserialize(GLTF.extensions, out glTF_VRM_extensions vrm))