diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs new file mode 100644 index 000000000..881f74b0a --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs @@ -0,0 +1,25 @@ +using System.IO; +using System.Threading.Tasks; +using UnityEngine; +using VRMShaders; + +namespace UniGLTF +{ + public static class GltfUtility + { + public static async Task LoadAsync(string path, IAwaitCaller awaitCaller = null, IMaterialDescriptorGenerator materialGenerator = null) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException(path); + } + + Debug.LogFormat("{0}", path); + using (GltfData data = new AutoGltfFileParser(path).Parse()) + using (var loader = new UniGLTF.ImporterContext(data, materialGenerator: materialGenerator)) + { + return await loader.LoadAsync(awaitCaller); + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs.meta new file mode 100644 index 000000000..2adf5c700 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 317decd1053397d4b9bdb9fb183bba4a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/Runtime/IO/VrmUtility.cs b/Assets/VRM/Runtime/IO/VrmUtility.cs new file mode 100644 index 000000000..0a5ec9979 --- /dev/null +++ b/Assets/VRM/Runtime/IO/VrmUtility.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using UniGLTF; +using UnityEngine; +using VRMShaders; + +namespace VRM +{ + public static class VrmUtility + { + public delegate IMaterialDescriptorGenerator MaterialGeneratorCallback(VRM.glTF_VRM_extensions vrm); + public delegate void MetaCallback(VRMMetaObject meta); + public static async Task LoadAsync(string path, + IAwaitCaller awaitCaller = null, + MaterialGeneratorCallback materialGeneratorCallback = null, + MetaCallback metaCallback = null + ) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException(path); + } + + using (GltfData data = new AutoGltfFileParser(path).Parse()) + { + try + { + var vrm = new VRMData(data); + using (var loader = new VRMImporterContext(vrm, materialGenerator: materialGeneratorCallback(vrm.VrmExtension))) + { + if (metaCallback != null) + { + var meta = await loader.ReadMetaAsync(new ImmediateCaller(), true); + metaCallback(meta); + } + return await loader.LoadAsync(awaitCaller); + } + } + catch (NotVrm0Exception) + { + // retry + Debug.LogWarning("file extension is vrm. but not vrm ?"); + using (var loader = new UniGLTF.ImporterContext(data)) + { + return await loader.LoadAsync(awaitCaller); + } + } + } + } + } +} diff --git a/Assets/VRM/Runtime/IO/VrmUtility.cs.meta b/Assets/VRM/Runtime/IO/VrmUtility.cs.meta new file mode 100644 index 000000000..f2c9cf8df --- /dev/null +++ b/Assets/VRM/Runtime/IO/VrmUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 431e57aed523ae240a0c88b0657677f8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM_Samples/SimpleViewer/ViewerUI.cs b/Assets/VRM_Samples/SimpleViewer/ViewerUI.cs index d60fa64a6..fb924cd22 100644 --- a/Assets/VRM_Samples/SimpleViewer/ViewerUI.cs +++ b/Assets/VRM_Samples/SimpleViewer/ViewerUI.cs @@ -103,10 +103,8 @@ namespace VRM.SimpleViewer m_textDistributionOther.text = ""; } - public async Task UpdateMetaAsync(VRMImporterContext context) + public void UpdateMeta(VRMMetaObject meta) { - var meta = await context.ReadMetaAsync(new ImmediateCaller(), true); - m_textModelTitle.text = meta.Title; m_textModelVersion.text = meta.Version; m_textModelAuthor.text = meta.Author; @@ -321,7 +319,7 @@ namespace VRM.SimpleViewer string[] cmds = System.Environment.GetCommandLineArgs(); if (cmds.Length > 1) { - LoadModel(cmds[1]); + LoadModelAsync(cmds[1]); } m_texts.Start(); @@ -366,10 +364,10 @@ namespace VRM.SimpleViewer return; } - LoadModel(path); + LoadModelAsync(path); } - void LoadModel(string path) + async void LoadModelAsync(string path) { var ext = Path.GetExtension(path).ToLower(); switch (ext) @@ -377,12 +375,21 @@ namespace VRM.SimpleViewer case ".gltf": case ".glb": case ".zip": - LoadModelAsync(path, false); - break; + { + var instance = await GltfUtility.LoadAsync(path, + GetIAwaitCaller(m_useAsync.isOn), + GetGltfMaterialGenerator(m_useUrpMaterial.isOn)); + break; + } case ".vrm": - LoadModelAsync(path, true); - break; + { + VrmUtility.MaterialGeneratorCallback materialCallback = (VRM.glTF_VRM_extensions vrm) => GetVrmMaterialGenerator(m_useUrpMaterial.isOn, vrm); + VrmUtility.MetaCallback metaCallback = m_texts.UpdateMeta; + var instance = await VrmUtility.LoadAsync(path, GetIAwaitCaller(m_useAsync.isOn), materialCallback, metaCallback); + SetModel(instance); + break; + } case ".bvh": LoadMotion(path, File.ReadAllText(path)); @@ -431,59 +438,6 @@ namespace VRM.SimpleViewer } } - async void LoadModelAsync(string path, bool isVrm) - { - if (!File.Exists(path)) - { - return; - } - - Debug.LogFormat("{0}", path); - - GltfData data; - try - { - data = new AutoGltfFileParser(path).Parse(); - } - catch (Exception ex) - { - Debug.LogWarningFormat("parse error: {0}", ex); - return; - } - - if (isVrm) - { - try - { - var vrm = new VRMData(data); - using (var loader = new VRMImporterContext(vrm, materialGenerator: GetVrmMaterialGenerator(m_useUrpMaterial.isOn, vrm.VrmExtension))) - { - await m_texts.UpdateMetaAsync(loader); - var instance = await loader.LoadAsync(GetIAwaitCaller(m_useAsync.isOn)); - SetModel(instance); - } - } - catch (NotVrm0Exception) - { - // retry - Debug.LogWarning("file extension is vrm. but not vrm ?"); - using (var loader = new UniGLTF.ImporterContext(data, materialGenerator: GetGltfMaterialGenerator(m_useUrpMaterial.isOn))) - { - var instance = await loader.LoadAsync(GetIAwaitCaller(m_useAsync.isOn)); - SetModel(instance); - } - } - } - else - { - using (var loader = new UniGLTF.ImporterContext(data, materialGenerator: GetGltfMaterialGenerator(m_useUrpMaterial.isOn))) - { - var instance = await loader.LoadAsync(GetIAwaitCaller(m_useAsync.isOn)); - SetModel(instance); - } - } - } - void SetModel(RuntimeGltfInstance instance) { // cleanup @@ -495,7 +449,7 @@ namespace VRM.SimpleViewer if (m_useFastSpringBone.isOn) { - FastSpringBoneReplacer.ReplaceAsync(instance.Root); + var _ = FastSpringBoneReplacer.ReplaceAsync(instance.Root); } instance.EnableUpdateWhenOffscreen();