UniVRM10.VRM10Viewer.VrmAnimation to UniVRM10.Vrm10PoseLoader

This commit is contained in:
ousttrue 2023-08-17 14:58:50 +09:00
parent 8408d3cbb0
commit 7cab5161fa
3 changed files with 17 additions and 52 deletions

View File

@ -6,59 +6,26 @@ using UniJSON;
using UnityEngine;
using VRMShaders;
namespace UniVRM10.VRM10Viewer
namespace UniVRM10
{
public class VrmAnimation : IVrm10Animation
public static class Vrm10PoseLoader
{
private readonly Vrm10AnimationInstance m_instance;
(INormalizedPoseProvider, ITPoseProvider) IVrm10Animation.ControlRig => m_instance.ControlRig;
IReadOnlyDictionary<ExpressionKey, Func<float>> IVrm10Animation.ExpressionMap => m_instance.ExpressionMap;
public VrmAnimation(Vrm10AnimationInstance instance,
public static void LoadHumanPose(Vrm10AnimationInstance instance,
Vector3 hips = default, Dictionary<HumanBodyBones, Quaternion> map = null)
{
m_instance = instance;
if (instance.GetComponent<Animation>() is Animation animation)
// experimental: set pose
var animator = instance.GetComponent<Animator>();
animator.GetBoneTransform(HumanBodyBones.Hips).localPosition = hips;
foreach (var kv in map)
{
if (animation != null)
var t = animator.GetBoneTransform(kv.Key);
if (t != null)
{
animation.Play();
}
else
{
// experimental: set pose
var animator = instance.GetComponent<Animator>();
animator.GetBoneTransform(HumanBodyBones.Hips).localPosition = hips;
foreach (var kv in map)
{
var t = animator.GetBoneTransform(kv.Key);
if (t != null)
{
t.localRotation = kv.Value;
}
}
t.localRotation = kv.Value;
}
}
}
public void ShowBoxMan(bool enable)
{
if (m_instance.BoxMan != null)
{
m_instance.BoxMan.enabled = enable;
}
}
public void Dispose()
{
if (m_instance.BoxMan != null)
{
GameObject.Destroy(m_instance.BoxMan.gameObject);
}
}
static Vector3 ToVec3(JsonNode j)
{
return new Vector3(-j[0].GetSingle(), j[1].GetSingle(), j[2].GetSingle());
@ -146,7 +113,7 @@ namespace UniVRM10.VRM10Viewer
return (root, map);
}
public static async Task<VrmAnimation> LoadVrmAnimationPose(string text)
public static async Task<Vrm10AnimationInstance> LoadVrmAnimationPose(string text)
{
using GltfData data = GlbLowLevelParser.ParseGltf(
"tmp.vrma",
@ -156,7 +123,8 @@ namespace UniVRM10.VRM10Viewer
new MigrationFlags()
);
using var loader = new VrmAnimationImporter(data);
var instance = await loader.LoadAsync(new ImmediateCaller());
var gltfInstance = await loader.LoadAsync(new ImmediateCaller());
var instance = gltfInstance.GetComponent<Vrm10AnimationInstance>();
if (data.GLTF.extensions is UniGLTF.glTFExtensionImport extensions)
{
@ -171,7 +139,7 @@ namespace UniVRM10.VRM10Viewer
if (pose.TryGet("humanoid", out var humanoid))
{
var (root, map) = GetPose(humanoid);
return new VrmAnimation(instance.GetComponent<Vrm10AnimationInstance>(), root, map);
LoadHumanPose(instance, root, map);
}
}
}
@ -179,7 +147,7 @@ namespace UniVRM10.VRM10Viewer
}
}
throw new System.Exception("no pose");
return instance;
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f0180f844d447bc45889d6c72a2d4ec4
guid: 9e5c411775bed594e9698d805f06e710
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UniGLTF;
using UnityEngine;
using UnityEngine.UI;
using VRMShaders;
using UniJSON;
namespace UniVRM10.VRM10Viewer
{
@ -454,7 +451,7 @@ namespace UniVRM10.VRM10Viewer
try
{
Motion = await VrmAnimation.LoadVrmAnimationPose(text);
Motion = await Vrm10PoseLoader.LoadVrmAnimationPose(text);
}
catch (UniJSON.ParserException)
{