From d2fd56ef51e29ba9b83fefcc1d91830080bd4038 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 10 Feb 2021 21:59:52 +0900 Subject: [PATCH] =?UTF-8?q?TextureLoader.cs=20=E3=82=92=20class=20?= =?UTF-8?q?=E5=88=A5=E3=81=AB=E5=88=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MaterialFacotry/MaterialFactory.cs | 2 +- .../MaterialFacotry/TextureLoader.meta | 8 + .../TextureLoader/AssetTextureLoader.cs | 77 +++++++++ .../TextureLoader/AssetTextureLoader.cs.meta | 11 ++ .../TextureLoader/GltfTextureLoader.cs | 70 ++++++++ .../TextureLoader/GltfTextureLoader.cs.meta | 11 ++ .../TextureLoader/ITextureLoader.cs | 35 ++++ .../TextureLoader/ITextureLoader.cs.meta} | 0 .../UnityWebRequestTextureLoader.cs} | 160 ------------------ .../UnityWebRequestTextureLoader.cs.meta | 11 ++ 10 files changed, 224 insertions(+), 161 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs rename Assets/UniGLTF/Runtime/UniGLTF/{IO/TextureLoader.cs.meta => MaterialFacotry/TextureLoader/ITextureLoader.cs.meta} (100%) rename Assets/UniGLTF/Runtime/UniGLTF/{IO/TextureLoader.cs => MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs} (55%) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs index c6d6687c8..6a2d46291 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs @@ -76,7 +76,7 @@ namespace UniGLTF #if UNIGLTF_USE_WEBREQUEST_TEXTURELOADER return new UnityWebRequestTextureLoader(index); #else - return new TextureLoader(index); + return new GltfTextureLoader(index); #endif } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta new file mode 100644 index 000000000..84d4a147d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 54a405d31cce94643be8f76f805da8c8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs new file mode 100644 index 000000000..f1d1fe62d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs @@ -0,0 +1,77 @@ +using System.Collections; +using UnityEngine; + +namespace UniGLTF +{ +#if UNITY_EDITOR + public class AssetTextureLoader : ITextureLoader + { + public Texture2D Texture + { + private set; + get; + } + + UnityPath m_assetPath; + + public AssetTextureLoader(UnityPath assetPath, string _) + { + m_assetPath = assetPath; + } + + public void Dispose() + { + } + + public void ProcessOnAnyThread(glTF gltf, IStorage storage) + { + } + + public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler) + { + // + // texture from assets + // + m_assetPath.ImportAsset(); + var importer = m_assetPath.GetImporter(); + if (importer == null) + { + Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath); + } + importer.maxTextureSize = 8192; + importer.sRGBTexture = !isLinear; + + importer.SaveAndReimport(); + + Texture = m_assetPath.LoadAsset(); + + //Texture.name = m_textureName; + if (Texture == null) + { + Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath); + } + + else + { + var maxSize = Mathf.Max(Texture.width, Texture.height); + + importer.maxTextureSize + = maxSize > 4096 ? 8192 : + maxSize > 2048 ? 4096 : + maxSize > 1024 ? 2048 : + maxSize > 512 ? 1024 : + 512; + + importer.SaveAndReimport(); + } + + if (sampler != null) + { + TextureSamplerUtil.SetSampler(Texture, sampler); + } + + yield break; + } + } +#endif +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs.meta new file mode 100644 index 000000000..c32ca741a --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/AssetTextureLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 748452d292d79304da4c271f584ab985 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs new file mode 100644 index 000000000..b512d93ad --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections; +using UnityEngine; + +namespace UniGLTF +{ + public class GltfTextureLoader : ITextureLoader + { + int m_textureIndex; + public GltfTextureLoader(int textureIndex) + { + m_textureIndex = textureIndex; + } + + public Texture2D Texture + { + private set; + get; + } + + public void Dispose() + { + } + + static Byte[] ToArray(ArraySegment bytes) + { + if (bytes.Array == null) + { + return new byte[] { }; + } + else if (bytes.Offset == 0 && bytes.Count == bytes.Array.Length) + { + return bytes.Array; + } + else + { + Byte[] result = new byte[bytes.Count]; + Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length); + return result; + } + } + + 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) + { + // + // texture from image(png etc) bytes + // + Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear); + Texture.name = m_textureName; + if (m_imageBytes != null) + { + Texture.LoadImage(m_imageBytes); + } + if (sampler != null) + { + TextureSamplerUtil.SetSampler(Texture, sampler); + } + yield break; + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs.meta new file mode 100644 index 000000000..28ea0287a --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c541795ac7ae51d468dfff535e5333cb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs new file mode 100644 index 000000000..1a8152779 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections; +using System.IO; +using UnityEngine; +using UnityEngine.Networking; +#if UNITY_EDITOR +using UnityEditor; +#endif + + +namespace UniGLTF +{ + public interface ITextureLoader : IDisposable + { + Texture2D Texture { get; } + + /// + /// Call from any thread + /// + /// + /// + void ProcessOnAnyThread(glTF gltf, IStorage storage); + + /// + /// Call from unity main thread + /// + /// + /// + /// + IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler); + } + + + +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/ITextureLoader.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs similarity index 55% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.cs rename to Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs index ee4ecf663..75128cb3f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs @@ -3,169 +3,9 @@ using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; -#if UNITY_EDITOR -using UnityEditor; -#endif - namespace UniGLTF { - public interface ITextureLoader : IDisposable - { - Texture2D Texture { get; } - - /// - /// Call from any thread - /// - /// - /// - void ProcessOnAnyThread(glTF gltf, IStorage storage); - - /// - /// Call from unity main thread - /// - /// - /// - /// - IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler); - } - -#if UNITY_EDITOR - public class AssetTextureLoader : ITextureLoader - { - public Texture2D Texture - { - private set; - get; - } - - UnityPath m_assetPath; - - public AssetTextureLoader(UnityPath assetPath, string _) - { - m_assetPath = assetPath; - } - - public void Dispose() - { - } - - public void ProcessOnAnyThread(glTF gltf, IStorage storage) - { - } - - public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler) - { - // - // texture from assets - // - m_assetPath.ImportAsset(); - var importer = m_assetPath.GetImporter(); - if (importer == null) - { - Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath); - } - importer.maxTextureSize = 8192; - importer.sRGBTexture = !isLinear; - - importer.SaveAndReimport(); - - Texture = m_assetPath.LoadAsset(); - - //Texture.name = m_textureName; - if (Texture == null) - { - Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath); - } - - else - { - var maxSize = Mathf.Max(Texture.width, Texture.height); - - importer.maxTextureSize - = maxSize > 4096 ? 8192 : - maxSize > 2048 ? 4096 : - maxSize > 1024 ? 2048 : - maxSize > 512 ? 1024 : - 512; - - importer.SaveAndReimport(); - } - - if (sampler != null) - { - TextureSamplerUtil.SetSampler(Texture, sampler); - } - - yield break; - } - } -#endif - - public class TextureLoader : ITextureLoader - { - int m_textureIndex; - public TextureLoader(int textureIndex) - { - m_textureIndex = textureIndex; - } - - public Texture2D Texture - { - private set; - get; - } - - public void Dispose() - { - } - - static Byte[] ToArray(ArraySegment bytes) - { - if (bytes.Array == null) - { - return new byte[] { }; - } - else if (bytes.Offset == 0 && bytes.Count == bytes.Array.Length) - { - return bytes.Array; - } - else - { - Byte[] result = new byte[bytes.Count]; - Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length); - return result; - } - } - - 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) - { - // - // texture from image(png etc) bytes - // - Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear); - Texture.name = m_textureName; - if (m_imageBytes != null) - { - Texture.LoadImage(m_imageBytes); - } - if (sampler != null) - { - TextureSamplerUtil.SetSampler(Texture, sampler); - } - yield break; - } - } - public class UnityWebRequestTextureLoader : ITextureLoader { public Texture2D Texture diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs.meta new file mode 100644 index 000000000..d3369dfbc --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b91b7b42ddfd6284a9303a0901fccaa3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: