From c26ea50e6a514f0bb2b47ff04967434793104d73 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Feb 2021 17:17:13 +0900 Subject: [PATCH] remove ITextureLoader.ProcessOnAnyThread --- .../IEnumeratorExtensions.cs | 3 +- .../Runtime/UniGLTF/IO/ImporterContext.cs | 3 +- .../UniGLTF/Runtime/UniGLTF/IO/TextureItem.cs | 24 ++--- .../MaterialFacotry/MaterialFactory.cs | 16 +--- .../TextureLoader/AssetTextureLoader.cs | 6 +- .../TextureLoader/GltfTextureLoader.cs | 26 ++++-- .../TextureLoader/ITextureLoader.cs | 14 +-- .../UnityWebRequestTextureLoader.cs | 91 +++---------------- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 3 +- 9 files changed, 45 insertions(+), 141 deletions(-) diff --git a/Assets/UniGLTF/Runtime/DepthFirstScheduler/IEnumeratorExtensions.cs b/Assets/UniGLTF/Runtime/DepthFirstScheduler/IEnumeratorExtensions.cs index 30bd9f66d..cb89c4c9b 100644 --- a/Assets/UniGLTF/Runtime/DepthFirstScheduler/IEnumeratorExtensions.cs +++ b/Assets/UniGLTF/Runtime/DepthFirstScheduler/IEnumeratorExtensions.cs @@ -3,8 +3,7 @@ using System.Collections; using System.Collections.Generic; -namespace - DepthFirstScheduler +namespace DepthFirstScheduler { public static class IEnumeratorExtensions { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 647601e35..3445e005c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -503,8 +503,7 @@ namespace UniGLTF { m_materialFactory.Prepare(GLTF); }) - .ContinueWithCoroutine(Scheduler.ThreadPool, () => m_materialFactory.TexturesProcessOnAnyThread(GLTF, Storage)) - .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.TexturesProcessOnMainThread(GLTF)) + .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.TexturesProcessOnMainThread(GLTF, Storage)) .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.LoadMaterials(GLTF)) .OnExecute(Scheduler.ThreadPool, parent => { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureItem.cs index e96402d3c..6a0477461 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureItem.cs @@ -124,31 +124,19 @@ namespace UniGLTF #region Process ITextureLoader m_textureLoader; - public void Process(glTF gltf, IStorage storage) - { - ProcessOnAnyThread(gltf, storage); - ProcessOnMainThreadCoroutine(gltf).CoroutineToEnd(); - } + // public void Process(glTF gltf, IStorage storage) + // { + // ProcessOnMainThreadCoroutine(gltf, storage).CoroutineToEnd(); + // } - public IEnumerator ProcessCoroutine(glTF gltf, IStorage storage) - { - ProcessOnAnyThread(gltf, storage); - yield return ProcessOnMainThreadCoroutine(gltf); - } - - public void ProcessOnAnyThread(glTF gltf, IStorage storage) - { - m_textureLoader.ProcessOnAnyThread(gltf, storage); - } - - public IEnumerator ProcessOnMainThreadCoroutine(glTF gltf) + public IEnumerator ProcessOnMainThreadCoroutine(glTF gltf, IStorage storage) { using (m_textureLoader) { var textureType = TextureIO.GetglTFTextureType(gltf, m_textureIndex); var colorSpace = TextureIO.GetColorSpace(textureType); var isLinear = colorSpace == RenderTextureReadWrite.Linear; - yield return m_textureLoader.ProcessOnMainThread(isLinear, gltf.GetSamplerFromTextureIndex(m_textureIndex)); + yield return m_textureLoader.ProcessOnMainThread(gltf, storage, isLinear, gltf.GetSamplerFromTextureIndex(m_textureIndex)); } } #endregion diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs index 659f620e4..14901128f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs @@ -136,25 +136,13 @@ namespace UniGLTF } } - public IEnumerator TexturesProcessOnAnyThread(glTF gltf, IStorage storage) - { - // using (MeasureTime("TexturesProcessOnAnyThread")) - { - foreach (var x in GetTextures()) - { - x.ProcessOnAnyThread(gltf, storage); - yield return null; - } - } - } - - public IEnumerator TexturesProcessOnMainThread(glTF gltf) + public IEnumerator TexturesProcessOnMainThread(glTF gltf, IStorage storage) { // using (MeasureTime("TexturesProcessOnMainThread")) { foreach (var x in GetTextures()) { - yield return x.ProcessOnMainThreadCoroutine(gltf); + yield return x.ProcessOnMainThreadCoroutine(gltf, storage); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs index f1d1fe62d..9c92a7a02 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs @@ -23,11 +23,7 @@ namespace UniGLTF { } - public void ProcessOnAnyThread(glTF gltf, IStorage storage) - { - } - - public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler) + public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler) { // // texture from assets diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs index b512d93ad..17028c1cc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Threading.Tasks; using UnityEngine; namespace UniGLTF @@ -40,25 +41,30 @@ namespace UniGLTF } } - Byte[] m_imageBytes; string m_textureName; - public void ProcessOnAnyThread(glTF gltf, IStorage storage) - { - var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); - var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName); - m_imageBytes = ToArray(segments); - } - public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler) + public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler) { + Byte[] imageBytes = default; + var task = Task.Run(() => + { + var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); + var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName); + var m_imageBytes = ToArray(segments); + }); + while (!task.IsCompleted) + { + yield break; + } + // // texture from image(png etc) bytes // Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear); Texture.name = m_textureName; - if (m_imageBytes != null) + if (imageBytes != null) { - Texture.LoadImage(m_imageBytes); + Texture.LoadImage(imageBytes); } if (sampler != null) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs index 1a8152779..a0381ab87 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs @@ -1,8 +1,6 @@ using System; using System.Collections; -using System.IO; using UnityEngine; -using UnityEngine.Networking; #if UNITY_EDITOR using UnityEditor; #endif @@ -14,22 +12,12 @@ namespace UniGLTF { Texture2D Texture { get; } - /// - /// Call from any thread - /// - /// - /// - void ProcessOnAnyThread(glTF gltf, IStorage storage); - /// /// Call from unity main thread /// /// /// /// - IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler); + IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler); } - - - } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs index 75128cb3f..775f71375 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.IO; +using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; @@ -31,84 +32,11 @@ namespace UniGLTF } } - ArraySegment m_segments; string m_textureName; - public void ProcessOnAnyThread(glTF gltf, IStorage storage) + public void ProcessOnAnyThread() { - var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); - m_segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName); } -#if false - HttpHost m_http; - class HttpHost : IDisposable - { - TcpListener m_listener; - Socket m_connection; - - public HttpHost(int port) - { - m_listener = new TcpListener(IPAddress.Loopback, port); - m_listener.Start(); - m_listener.BeginAcceptSocket(OnAccepted, m_listener); - } - - void OnAccepted(IAsyncResult ar) - { - var l = ar.AsyncState as TcpListener; - if (l == null) return; - m_connection = l.EndAcceptSocket(ar); - // 次の接続受付はしない - - BeginRead(m_connection, new byte[8192]); - } - - void BeginRead(Socket c, byte[] buffer) - { - AsyncCallback callback = ar => - { - var s = ar.AsyncState as Socket; - if (s == null) return; - var size = s.EndReceive(ar); - if (size > 0) - { - OnRead(buffer, size); - } - BeginRead(s, buffer); - }; - m_connection.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, callback, m_connection); - } - - List m_buffer = new List(); - void OnRead(byte[] buffer, int len) - { - m_buffer.AddRange(buffer.Take(len)); - } - - public string Url - { - get - { - - } - } - - public void Dispose() - { - if (m_connection != null) - { - m_connection.Dispose(); - m_connection = null; - } - if(m_listener != null) - { - m_listener.Stop(); - m_listener = null; - } - } - } -#endif - class Deleter : IDisposable { string m_path; @@ -125,13 +53,24 @@ namespace UniGLTF } } - public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler) + public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler) { + ArraySegment bytes = default; + var task = Task.Run(() => + { + var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); + bytes = gltf.GetImageBytes(storage, imageIndex, out m_textureName); + }); + while (!task.IsCompleted) + { + yield return null; + } + // tmp file var tmp = Path.GetTempFileName(); using (var f = new FileStream(tmp, FileMode.Create)) { - f.Write(m_segments.Array, m_segments.Offset, m_segments.Count); + f.Write(bytes.Array, bytes.Offset, bytes.Count); } using (var d = new Deleter(tmp)) diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 50d6ff0d2..fc0c19780 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -6,6 +6,7 @@ using UnityEngine; using System.IO; using System.Collections; using UniJSON; +using DepthFirstScheduler; namespace VRM { @@ -317,7 +318,7 @@ namespace VRM if (gltfMeta.texture >= 0 && gltfMeta.texture < GLTF.textures.Count) { var t = new TextureItem(gltfMeta.texture, MaterialFactory.CreateTextureLoader(gltfMeta.texture)); - t.Process(GLTF, Storage); + t.ProcessOnMainThreadCoroutine(GLTF, Storage).CoroutineToEnd(); meta.Thumbnail = t.Texture; } }