Add await support

This commit is contained in:
TORISOUP 2018-04-19 23:26:31 +09:00
parent d77470d244
commit 353da5574c
2 changed files with 120 additions and 26 deletions

View File

@ -75,12 +75,12 @@ namespace VRM
public static CreateMaterialFunc GetMaterialFunc(List<glTF_VRM_Material> materials)
{
var CreateDefault= gltfImporter.CreateMaterialFuncFromShader(Shader.Find("VRM/UnlitTexture"));
var CreateDefault = gltfImporter.CreateMaterialFuncFromShader(Shader.Find("VRM/UnlitTexture"));
var CreateZWrite = gltfImporter.CreateMaterialFuncFromShader(Shader.Find("VRM/UnlitTransparentZWrite"));
CreateMaterialFunc fallback = (ctx, i) =>
{
var vrm = ctx.GLTF as glTF_VRM;
if(vrm!=null && vrm.materials[i].name.ToLower().Contains("zwrite"))
if (vrm != null && vrm.materials[i].name.ToLower().Contains("zwrite"))
{
// 一応。不要かも
Debug.Log("fallback to VRM/UnlitTransparentZWrite");
@ -326,7 +326,7 @@ namespace VRM
asset.MaterialValues = group.materialValues.Select(x =>
{
var value = new Vector4();
for(int i=0; i<x.targetValue.Length; ++i)
for (int i = 0; i < x.targetValue.Length; ++i)
{
switch (i)
{
@ -341,7 +341,7 @@ namespace VRM
MaterialName = x.materialName,
ValueName = x.propertyName,
TargetValue = value,
BaseValue = context.Materials.First(y => y.name==x.materialName).GetColor(x.propertyName),
BaseValue = context.Materials.First(y => y.name == x.materialName).GetColor(x.propertyName),
};
})
.ToArray();
@ -523,6 +523,36 @@ namespace VRM
yield return null;
}
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(string path)
{
var context = new VRMImporterContext(path);
context.ParseVrm(File.ReadAllBytes(path));
return LoadVrmAsyncInternal(context);
}
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(Byte[] bytes)
{
var context = new VRMImporterContext();
context.ParseVrm(bytes);
return LoadVrmAsync(context);
}
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(VRMImporterContext ctx)
{
return LoadVrmAsyncInternal(ctx);
}
#endif
public static void LoadVrmAsync(string path, Action<GameObject> onLoaded)
{
var context = new VRMImporterContext(path);
@ -538,10 +568,16 @@ namespace VRM
}
public static void LoadVrmAsync(VRMImporterContext ctx, Action<GameObject> onLoaded)
{
LoadVrmAsyncInternal(ctx)
.Subscribe(Scheduler.MainThread, onLoaded, Debug.LogError);
}
private static Schedulable<GameObject> LoadVrmAsyncInternal(VRMImporterContext ctx)
{
var schedulable = Schedulable.Create();
schedulable
return schedulable
.AddTask(Scheduler.ThreadPool, () =>
{
ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path);
@ -584,28 +620,24 @@ namespace VRM
.ContinueWithCoroutine(Scheduler.MainThread, () => LoadNodes(ctx))
.ContinueWithCoroutine(Scheduler.MainThread, () => BuildHierarchy(ctx))
.ContinueWith(Scheduler.MainThread, _ => VRMImporter.OnLoadModel(ctx))
.Subscribe(Scheduler.MainThread,
_ =>
{
/*
Debug.LogFormat("task end: {0}/{1}/{2}/{3}",
ctx.Textures.Count,
ctx.Materials.Count,
ctx.Meshes.Count,
ctx.Nodes.Count
);
*/
ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path);
.ContinueWith(Scheduler.MainThread,
_ =>
{
/*
Debug.LogFormat("task end: {0}/{1}/{2}/{3}",
ctx.Textures.Count,
ctx.Materials.Count,
ctx.Meshes.Count,
ctx.Nodes.Count
);
*/
ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path);
// 非表示のメッシュを表示する
ctx.ShowMeshes();
// 非表示のメッシュを表示する
ctx.ShowMeshes();
onLoaded(ctx.Root);
}, ex =>
{
Debug.LogError(ex);
})
;
return ctx.Root;
});
}
#endregion
}

View File

@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
using System.Threading;
using System.Runtime.CompilerServices;
#endif
namespace UniTask
{
@ -182,6 +185,13 @@ namespace UniTask
TaskChain.Schedule(schedulable.GetRoot(), onError);
}
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
public static SchedulableAwaiter<T> GetAwaiter<T>(this Schedulable<T> schedulable)
{
return new SchedulableAwaiter<T>(schedulable, true);
}
#endif
/*
public static ISchedulable<Unit> Sequencial<T>(IEnumerable<ISchedulable<T>> schedulables, Action<T> mergePred)
{
@ -223,5 +233,57 @@ namespace UniTask
}, scheduler);
}
*/
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
public class SchedulableAwaiter<T> : INotifyCompletion
{
private readonly Schedulable<T> target = null;
private T result;
private readonly bool continueOnCapturedContext;
public T GetResult()
{
return result;
}
public bool IsCompleted { get; private set; }
public SchedulableAwaiter(Schedulable<T> target)
{
this.target = target;
continueOnCapturedContext = true;
}
public SchedulableAwaiter(Schedulable<T> target, bool continueOnCapturedContext)
{
this.target = target;
this.continueOnCapturedContext = continueOnCapturedContext;
}
public void OnCompleted(Action continuation)
{
var context = SynchronizationContext.Current;
if (continueOnCapturedContext && context != null)
{
target.Subscribe(target.Parent.Schedulder, r =>
{
result = r;
IsCompleted = true;
context.Post(_ => continuation(), null);
}, ex => { throw ex; });
}
else
{
target.Subscribe(target.Parent.Schedulder, r =>
{
result = r;
IsCompleted = true;
continuation();
}, ex => { throw ex; });
}
}
}
#endif
}
}