diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs index 28bd4f201..2071151a1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureLoader.cs @@ -6,50 +6,5 @@ namespace UniGLTF { public static class GltfTextureLoader { - 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; - } - } - - public static async Task LoadTextureAsync(IAwaitCaller awaitCaller, glTF gltf, IStorage storage, int textureIndex) - { - var imageBytes = await awaitCaller.Run(() => - { - var imageIndex = gltf.textures[textureIndex].source; - var segments = gltf.GetImageBytes(storage, imageIndex); - return ToArray(segments); - }); - - // - // texture from image(png etc) bytes - // - var colorSpace = gltf.GetColorSpace(textureIndex); - var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); - texture.name = gltf.textures[textureIndex].name; - if (imageBytes != null) - { - texture.LoadImage(imageBytes); - } - - var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); - if (sampler != null) - { - TextureSamplerUtil.SetSampler(texture, sampler); - } - return texture; - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index 8676488fd..8cd8391b0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -133,15 +133,59 @@ namespace UniGLTF public IEnumerable Textures => m_textureCache.Values; + 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; + } + } + 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)) + if (m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo)) { - var texture = await GltfTextureLoader.LoadTextureAsync(awaitCaller, m_gltf, m_storage, textureIndex); - cacheInfo = new TextureLoadInfo(texture, used, false); - m_textureCache.Add(name, cacheInfo); + return cacheInfo; } + + // not found. load new + var imageBytes = await awaitCaller.Run(() => + { + var imageIndex = gltf.textures[textureIndex].source; + var segments = gltf.GetImageBytes(m_storage, imageIndex); + return ToArray(segments); + }); + + // + // texture from image(png etc) bytes + // + var colorSpace = gltf.GetColorSpace(textureIndex); + var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); + texture.name = gltf.textures[textureIndex].name; + if (imageBytes != null) + { + texture.LoadImage(imageBytes); + } + + var sampler = gltf.GetSamplerFromTextureIndex(textureIndex); + if (sampler != null) + { + TextureSamplerUtil.SetSampler(texture, sampler); + } + + cacheInfo = new TextureLoadInfo(texture, used, false); + m_textureCache.Add(name, cacheInfo); return cacheInfo; }