VRM10Viewer can cancel loading.

This commit is contained in:
Masataka SUMI
2022-02-09 23:14:56 +09:00
parent 9e06e955c3
commit 09ef2b93fc

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using UniGLTF;
using UniHumanoid;
using UnityEngine;
@@ -46,6 +47,8 @@ namespace UniVRM10.VRM10Viewer
[SerializeField]
TextAsset m_motion;
private CancellationTokenSource _cancellationTokenSource;
[Serializable]
class TextFields
{
@@ -328,6 +331,11 @@ namespace UniVRM10.VRM10Viewer
m_texts.Start();
}
private void OnDestroy()
{
_cancellationTokenSource?.Dispose();
}
private void LoadMotion(string source)
{
var context = new UniHumanoid.BvhImporterContext();
@@ -350,6 +358,14 @@ namespace UniVRM10.VRM10Viewer
if (Root != null) Root.SetActive(!Root.activeSelf);
}
if (Input.GetKeyDown(KeyCode.Escape))
{
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
}
}
m_ui.UpdateToggle(() => m_loaded?.EnableBvh(m_src), () => m_loaded?.EnableTPose(m_pose));
}
@@ -414,23 +430,53 @@ namespace UniVRM10.VRM10Viewer
return;
}
Debug.LogFormat("{0}", path);
var vrm10Instance = await Vrm10.LoadPathAsync(path,
canLoadVrm0X: true,
normalizeTransform: m_useNormalization.isOn,
showMeshes: false,
awaitCaller: new RuntimeOnlyAwaitCaller(),
materialGenerator: GetVrmMaterialDescriptorGenerator(m_useUrpMaterial.isOn),
vrmMetaInformationCallback: m_texts.UpdateMeta);
if (vrm10Instance != null)
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
try
{
SetModel(vrm10Instance.GetComponent<RuntimeGltfInstance>());
Debug.LogFormat("{0}", path);
var vrm10Instance = await Vrm10.LoadPathAsync(path,
canLoadVrm0X: true,
normalizeTransform: m_useNormalization.isOn,
showMeshes: false,
awaitCaller: new RuntimeOnlyAwaitCaller(),
materialGenerator: GetVrmMaterialDescriptorGenerator(m_useUrpMaterial.isOn),
vrmMetaInformationCallback: m_texts.UpdateMeta,
ct: _cancellationTokenSource.Token);
if (vrm10Instance != null)
{
if (_cancellationTokenSource.Token.IsCancellationRequested)
{
UnityObjectDestoyer.DestroyRuntimeOrEditor(vrm10Instance.gameObject);
return;
}
SetModel(vrm10Instance.GetComponent<RuntimeGltfInstance>());
}
else
{
// NOTE: load as glTF model if failed to load as VRM 1.0.
// TODO: Hand over CancellationToken
var gltfModel = await GltfUtility.LoadAsync(path, awaitCaller: new RuntimeOnlyAwaitCaller());
if (gltfModel == null)
{
throw new Exception("Failed to load the file as glTF format.");
}
if (_cancellationTokenSource.Token.IsCancellationRequested)
{
gltfModel.Dispose();
return;
}
SetModel(gltfModel);
}
}
else
catch (Exception ex)
{
// fallback to gltf
var instance = await GltfUtility.LoadAsync(path, awaitCaller: new RuntimeOnlyAwaitCaller());
SetModel(instance);
Debug.LogError($"Failed to Load: {path}");
Debug.LogException(ex);
}
}