mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-13 22:09:53 -05:00
次の修正で使う。いったん削除
This commit is contained in:
parent
2e29b7da65
commit
dbe33c3c1e
|
|
@ -68,136 +68,5 @@ namespace UniVRM10.VRM10Viewer
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region experimental vrm-animation deserializer
|
||||
//
|
||||
// vrm-animation の簡易実装
|
||||
//
|
||||
// https://github.com/vrm-c/vrm-animation
|
||||
//
|
||||
static float ForceMeterScale(Dictionary<HumanBodyBones, Transform> map)
|
||||
{
|
||||
var positionMap = map.ToDictionary(kv => kv.Key, kv => kv.Value.position);
|
||||
var hipsHeight = positionMap[HumanBodyBones.Hips].y;
|
||||
|
||||
float scaling = 1.0f;
|
||||
if (hipsHeight > 80)
|
||||
{
|
||||
// cm スケールであると見做す
|
||||
scaling = 0.01f;
|
||||
}
|
||||
return scaling;
|
||||
}
|
||||
|
||||
static bool TryGet(JsonNode obj, string key, out JsonNode found)
|
||||
{
|
||||
foreach (var kv in obj.ObjectItems())
|
||||
{
|
||||
if (kv.Key.GetString() == key)
|
||||
{
|
||||
found = kv.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
found = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
static (HumanBodyBones, int) ToTuple(KeyValuePair<JsonNode, JsonNode> kv)
|
||||
{
|
||||
if (TryGet(kv.Value, "node", out var value))
|
||||
{
|
||||
var name = kv.Key.GetString();
|
||||
switch (name)
|
||||
{
|
||||
case "rightThumbMetacarpal":
|
||||
return (HumanBodyBones.RightThumbProximal, value.GetInt32());
|
||||
case "leftThumbMetacarpal":
|
||||
return (HumanBodyBones.LeftThumbProximal, value.GetInt32());
|
||||
case "rightThumbProximal":
|
||||
return (HumanBodyBones.RightThumbIntermediate, value.GetInt32());
|
||||
case "leftThumbProximal":
|
||||
return (HumanBodyBones.LeftThumbIntermediate, value.GetInt32());
|
||||
default:
|
||||
return ((HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), name, true), value.GetInt32());
|
||||
}
|
||||
}
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
static Dictionary<HumanBodyBones, Transform> GetHumanMap(GltfData data, IReadOnlyList<Transform> nodes)
|
||||
{
|
||||
var humanMap = new Dictionary<HumanBodyBones, Transform>();
|
||||
if (data.GLTF.extensions is UniGLTF.glTFExtensionImport extensions)
|
||||
{
|
||||
foreach (var kv in extensions.ObjectItems())
|
||||
{
|
||||
if (kv.Key.GetString() == "VRMC_vrm_animation")
|
||||
{
|
||||
var animation = kv.Value;
|
||||
if (TryGet(animation, "humanoid", out var animation_humanoid))
|
||||
{
|
||||
if (TryGet(animation_humanoid, "humanBones", out var bones))
|
||||
{
|
||||
foreach (var kkvv in bones.ObjectItems())
|
||||
{
|
||||
var (bone, index) = ToTuple(kkvv);
|
||||
// Debug.Log($"{bone} => {index}");
|
||||
humanMap.Add(bone, nodes[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return humanMap;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static async Task<VRM10Motion> LoadVrmAnimationFromPathAsync(string path)
|
||||
{
|
||||
//
|
||||
// GetHumanoid Mapping
|
||||
//
|
||||
using (GltfData data = new AutoGltfFileParser(path).Parse())
|
||||
using (var loader = new UniGLTF.ImporterContext(data))
|
||||
{
|
||||
loader.InvertAxis = Axes.X;
|
||||
// loader.PositionScaling = 0.01f;
|
||||
var instance = await loader.LoadAsync(new ImmediateCaller());
|
||||
var humanMap = GetHumanMap(data, loader.Nodes);
|
||||
if (humanMap.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("fail to load VRMC_vrm_animation");
|
||||
}
|
||||
|
||||
var scaling = ForceMeterScale(humanMap);
|
||||
// instance.transform.localScale = new Vector3(scaling, scaling, scaling);
|
||||
var description = AvatarDescription.Create(humanMap);
|
||||
|
||||
//
|
||||
// avatar
|
||||
//
|
||||
var avatar = description.CreateAvatar(instance.Root.transform);
|
||||
avatar.name = "Avatar";
|
||||
// AvatarDescription = description;
|
||||
var animator = instance.gameObject.AddComponent<Animator>();
|
||||
animator.avatar = avatar;
|
||||
|
||||
// create SkinnedMesh for bone visualize
|
||||
var renderer = SkeletonMeshUtility.CreateRenderer(animator);
|
||||
var material = new Material(Shader.Find("Standard"));
|
||||
renderer.sharedMaterial = material;
|
||||
var mesh = renderer.sharedMesh;
|
||||
mesh.name = "box-man";
|
||||
|
||||
var humanoid = instance.gameObject.AddComponent<Humanoid>();
|
||||
humanoid.AssignBonesFromAnimator();
|
||||
var motion = new VRM10Motion(instance);
|
||||
var poseProvider = new InitRotationPoseProvider(instance.transform, humanoid);
|
||||
motion.ControlRig = (poseProvider, poseProvider);
|
||||
return motion;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user