diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index f63b36422..890e12085 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -220,29 +220,33 @@ namespace UniGLTF await awaitCaller.NextFrame(); } - public async Task LoadTexturesAsync() + public async Task LoadTexturesAsync(IAwaitCaller awaitCaller = null) { + awaitCaller = awaitCaller ?? new ImmediateCaller(); + var textures = TextureDescriptorGenerator.Get().GetEnumerable(); foreach (var param in textures) { - var tex = await TextureFactory.GetTextureAsync(param); + var tex = await TextureFactory.GetTextureAsync(param, awaitCaller); } } - public async Task LoadMaterialsAsync() + public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller = null) { + awaitCaller = awaitCaller ?? new ImmediateCaller(); + if (Parser.GLTF.materials == null || Parser.GLTF.materials.Count == 0) { // no material. work around. var param = MaterialDescriptorGenerator.Get(Parser, 0); - var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync); + var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller); } else { for (int i = 0; i < Parser.GLTF.materials.Count; ++i) { var param = MaterialDescriptorGenerator.Get(Parser, i); - var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync); + var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs index 180a91d84..d13203f9d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using VRMShaders; namespace UniGLTF { @@ -11,7 +12,7 @@ namespace UniGLTF public static RuntimeGltfInstance Load(this ImporterContext self) { var meassureTime = new ImporterContextSpeedLog(); - var task = self.LoadAsync(default(ImmediateCaller), meassureTime.MeasureTime); + var task = self.LoadAsync(new ImmediateCaller(), meassureTime.MeasureTime); if (!task.IsCompleted) { throw new Exception(); diff --git a/Assets/VRM.Samples/Scripts/ViewerUI.cs b/Assets/VRM.Samples/Scripts/ViewerUI.cs index 5a7eb185a..0e2d0f4f6 100644 --- a/Assets/VRM.Samples/Scripts/ViewerUI.cs +++ b/Assets/VRM.Samples/Scripts/ViewerUI.cs @@ -6,6 +6,7 @@ using UniGLTF; using UniHumanoid; using UnityEngine; using UnityEngine.UI; +using VRMShaders; namespace VRM.Samples @@ -89,7 +90,7 @@ namespace VRM.Samples public async Task UpdateMetaAsync(VRMImporterContext context) { - var meta = await context.ReadMetaAsync(default(TaskCaller), true); + var meta = await context.ReadMetaAsync(new TaskCaller(), true); m_textModelTitle.text = meta.Title; m_textModelVersion.text = meta.Version; diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index eebf178e4..d8b0fba46 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -285,10 +285,7 @@ namespace VRM public async Task ReadMetaAsync(IAwaitCaller awaitCaller = null, bool createThumbnail = false) { - if (awaitCaller == null) - { - awaitCaller = default(ImmediateCaller); - } + awaitCaller = awaitCaller ?? new ImmediateCaller(); var meta = ScriptableObject.CreateInstance(); meta.name = "Meta"; @@ -303,7 +300,7 @@ namespace VRM if (gltfMeta.texture >= 0) { var (key, param) = GltfTextureImporter.CreateSRGB(Parser, gltfMeta.texture, Vector2.zero, Vector2.one); - meta.Thumbnail = await TextureFactory.GetTextureAsync(param) as Texture2D; + meta.Thumbnail = await TextureFactory.GetTextureAsync(param, awaitCaller) as Texture2D; } meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; diff --git a/Assets/VRM/Runtime/IO/VRMImporterContextExtensions.cs b/Assets/VRM/Runtime/IO/VRMImporterContextExtensions.cs index 79a3d909e..130d9fa0a 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContextExtensions.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContextExtensions.cs @@ -1,10 +1,12 @@ -namespace VRM +using VRMShaders; + +namespace VRM { public static class VRMImporterContextExtensions { public static VRMMetaObject ReadMeta(this VRMImporterContext context, bool createThumbnail = false) { - var task = context.ReadMetaAsync(default(UniGLTF.ImmediateCaller), createThumbnail); + var task = context.ReadMetaAsync(new ImmediateCaller(), createThumbnail); task.Wait(); return task.Result; } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index 38654335d..050fbdbda 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -299,7 +299,7 @@ namespace UniVRM10 } if (Vrm10TextureDescriptorGenerator.TryGetMetaThumbnailTextureImportParam(Parser, vrmExtension, out (SubAssetKey, VRMShaders.TextureDescriptor Param) kv)) { - var texture = await TextureFactory.GetTextureAsync(kv.Param); + var texture = await TextureFactory.GetTextureAsync(kv.Param, awaitCaller); if (texture is Texture2D tex2D) { meta.Thumbnail = tex2D; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/IAwaitCaller.cs b/Assets/VRMShaders/GLTF/IO/Runtime/IAwaitCaller.cs similarity index 93% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/IAwaitCaller.cs rename to Assets/VRMShaders/GLTF/IO/Runtime/IAwaitCaller.cs index 68258e8a6..57b919879 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/IAwaitCaller.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/IAwaitCaller.cs @@ -1,8 +1,7 @@ using System; -using System.Threading; using System.Threading.Tasks; -namespace UniGLTF +namespace VRMShaders { /// /// ImporterContext の 非同期実行 LoadAsync を補助する。 @@ -37,7 +36,7 @@ namespace UniGLTF /// /// 同期実行 /// - public struct ImmediateCaller : IAwaitCaller + public sealed class ImmediateCaller : IAwaitCaller { public Task NextFrame() { @@ -59,7 +58,7 @@ namespace UniGLTF /// /// 非同期実行 /// - public class TaskCaller : IAwaitCaller + public sealed class TaskCaller : IAwaitCaller { public Task NextFrame() { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/IAwaitCaller.cs.meta b/Assets/VRMShaders/GLTF/IO/Runtime/IAwaitCaller.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/IAwaitCaller.cs.meta rename to Assets/VRMShaders/GLTF/IO/Runtime/IAwaitCaller.cs.meta diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs index 7551898b9..916171cbf 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/ITextureDeserializer.cs @@ -16,6 +16,6 @@ namespace VRMShaders /// Texture2D の mipmap が生成されるべきか否か /// Texture2D の色空間 /// - Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace); + Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace, IAwaitCaller awaitCaller); } } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs index eb1ceb870..a2ede5335 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs @@ -6,7 +6,7 @@ using UnityEngine; namespace VRMShaders { - public delegate Task GetTextureAsyncFunc(TextureDescriptor texDesc); + public delegate Task GetTextureAsyncFunc(TextureDescriptor texDesc, IAwaitCaller awaitCaller); public class MaterialFactory : IResponsibilityForDestroyObjects { @@ -100,7 +100,7 @@ namespace VRMShaders return m_materials[index].Asset; } - public async Task LoadAsync(MaterialDescriptor matDesc, GetTextureAsyncFunc getTexture) + public async Task LoadAsync(MaterialDescriptor matDesc, GetTextureAsyncFunc getTexture, IAwaitCaller awaitCaller) { if (m_externalMap.TryGetValue(matDesc.SubAssetKey, out Material material)) { @@ -110,7 +110,7 @@ namespace VRMShaders if (getTexture == null) { - getTexture = (_) => Task.FromResult(null); + getTexture = (x, y) => Task.FromResult(null); } var shaderName = matDesc.ShaderName; @@ -135,7 +135,7 @@ namespace VRMShaders foreach (var kv in matDesc.TextureSlots) { - var texture = await getTexture(kv.Value); + var texture = await getTexture(kv.Value, awaitCaller); if (texture != null) { material.SetTexture(kv.Key, texture); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs index 6246efabf..8bc66729a 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs @@ -56,7 +56,7 @@ namespace VRMShaders /// テクスチャ生成情報を基に、テクスチャ生成を行う。 /// SubAssetKey が同じ場合はキャッシュを返す。 /// - public async Task GetTextureAsync(TextureDescriptor texDesc) + public async Task GetTextureAsync(TextureDescriptor texDesc, IAwaitCaller awaitCaller) { var subAssetKey = texDesc.SubAssetKey; @@ -79,7 +79,7 @@ namespace VRMShaders // > contrary to Unity’s usual convention of using Y as “up” // https://docs.unity3d.com/2018.4/Documentation/Manual/StandardShaderMaterialParameterNormalMap.html var data0 = await texDesc.Index0(); - var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear, awaitCaller); rawTexture.name = subAssetKey.Name; rawTexture.SetSampler(texDesc.Sampler); _textureCache.Add(subAssetKey, rawTexture); @@ -94,12 +94,12 @@ namespace VRMShaders if (texDesc.Index0 != null) { var data0 = await texDesc.Index0(); - metallicRoughnessTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + metallicRoughnessTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear, awaitCaller); } if (texDesc.Index1 != null) { var data1 = await texDesc.Index1(); - occlusionTexture = await TextureDeserializer.LoadTextureAsync(data1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + occlusionTexture = await TextureDeserializer.LoadTextureAsync(data1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear, awaitCaller); } var combinedTexture = OcclusionMetallicRoughnessConverter.Import(metallicRoughnessTexture, @@ -115,7 +115,7 @@ namespace VRMShaders case TextureImportTypes.sRGB: { var data0 = await texDesc.Index0(); - var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB); + var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB, awaitCaller); rawTexture.name = subAssetKey.Name; rawTexture.SetSampler(texDesc.Sampler); _textureCache.Add(subAssetKey, rawTexture); @@ -124,7 +124,7 @@ namespace VRMShaders case TextureImportTypes.Linear: { var data0 = await texDesc.Index0(); - var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear); + var rawTexture = await TextureDeserializer.LoadTextureAsync(data0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear, awaitCaller); rawTexture.name = subAssetKey.Name; rawTexture.SetSampler(texDesc.Sampler); _textureCache.Add(subAssetKey, rawTexture); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs index ad84c9bc5..d57ad37bc 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/UnityTextureDeserializer.cs @@ -9,12 +9,13 @@ namespace VRMShaders /// public sealed class UnityTextureDeserializer : ITextureDeserializer { - public async Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace) + public async Task LoadTextureAsync(byte[] imageData, bool useMipmap, ColorSpace colorSpace, IAwaitCaller awaitCaller) { var texture = new Texture2D(2, 2, TextureFormat.ARGB32, useMipmap, colorSpace == ColorSpace.Linear); if (imageData != null) { texture.LoadImage(imageData); + await awaitCaller.NextFrame(); } return texture;