LoopAwaitable

This commit is contained in:
ousttrue
2021-02-19 16:53:38 +09:00
parent 6914df7de0
commit 758e90bcab
6 changed files with 49 additions and 17 deletions

View File

@@ -5,6 +5,16 @@ using System.Threading.Tasks;
namespace UniGLTF.AltTask
{
/// <summary>
/// Importer 向けに Task を wrap した。
///
/// * EditorやUnitTestなどで、Unity の MainLoop を進行させずに 非同期を進行させることが目的
///
/// Global変数 SynchronizationContext.Current を一時的に変更したいのだが、
/// システムに予期せぬ副作用を与える可能性があるのでこれを回避。
/// UniGLTF.AltTask.TaskQueue.Current に同じ機能を与えて、タスクのPost先を制御することにした。
/// </summary>
public interface IAwaiter : INotifyCompletion
{
bool IsCompleted { get; }
@@ -86,18 +96,12 @@ namespace UniGLTF.AltTask
}, null);
}
public static Awaitable Delay()
{
var task = Task.FromResult<object>(default);
return new Awaitable(task);
}
public static Awaitable<T> Run<T>(Func<T> action)
{
return new Awaitable<T>(Task.Run(action));
}
public static Awaitable Run<T>(Action action)
public static Awaitable Run(Action action)
{
return new Awaitable(Task.Run(action));
}

View File

@@ -0,0 +1,17 @@
using System;
namespace UniGLTF.AltTask
{
public static class LoopAwaitable
{
// TODO
// loop スレッド使わないようにしたい
public static Awaitable Create()
{
return Awaitable.Run(() =>
{
System.Threading.Thread.Sleep(10);
});
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db36517c11c455e4b94d9cbb8c1cc3ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -391,7 +391,7 @@ namespace UniGLTF
Nodes.Add(NodeImporter.ImportNode(GLTF.nodes[i], i).transform);
}
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("BuildHierarchy"))
{
@@ -416,7 +416,7 @@ namespace UniGLTF
t.SetParent(Root.transform, false);
}
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("AnimationImporter"))
{
@@ -434,7 +434,7 @@ namespace UniGLTF
protected virtual async Awaitable OnLoadModel()
{
Root.name = "GLTF";
await Awaitable.Delay();
await LoopAwaitable.Create();
}
async Awaitable<MeshWithMaterials> BuildMeshAsync(MeshImporter.MeshContext x, int i)

View File

@@ -660,9 +660,9 @@ namespace UniGLTF
if (recalculateTangents)
{
await Awaitable.Delay();
await LoopAwaitable.Create();
mesh.RecalculateTangents();
await Awaitable.Delay();
await LoopAwaitable.Create();
}
// 先にすべてのマテリアルを作成済みなのでテクスチャーは生成済み。Resultを使ってよい
@@ -672,7 +672,7 @@ namespace UniGLTF
Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray()
};
await Awaitable.Delay();
await LoopAwaitable.Create();
if (meshContext.BlendShapes.Count > 0)
{
var emptyVertices = new Vector3[mesh.vertexCount];

View File

@@ -61,26 +61,26 @@ namespace VRM
{
await LoadMetaAsync();
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("VRM LoadHumanoid"))
{
LoadHumanoid();
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("VRM LoadBlendShapeMaster"))
{
LoadBlendShapeMaster();
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("VRM LoadSecondary"))
{
VRMSpringUtility.LoadSecondary(Root.transform, Nodes,
VRM.secondaryAnimation);
}
await Awaitable.Delay();
await LoopAwaitable.Create();
using (MeasureTime("VRM LoadFirstPerson"))
{