UniVRM/Assets/UniGLTF/Runtime/UniHumanoid/MuscleDebug.cs
ousttrue 29545202d3 UniHumanoid のフォルダ構成を整理し、不要になった asmdef を削除した。Resource/test_motion.txt の場所が変更になるのに注意。
* Assets/UniHumanoid/Scripts => Assets/UniGLTF/Runtime/Humanoid
* Assets/UniHumanoid/Editor => Assets/UniGLTF/Editor/Humanoid
* Assets/UniHumanoid/Editor/Tests => Assets/UniGLTF/Tests/Humanoid
* => docs/unihumanoid/index.md
2021-12-06 16:22:25 +09:00

106 lines
2.5 KiB
C#

using System;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniHumanoid
{
public class MuscleDebug : MonoBehaviour
{
Avatar GetAvatar()
{
var animator = GetComponent<Animator>();
if (animator != null && animator.avatar != null)
{
return animator.avatar;
}
var transfer = GetComponent<HumanPoseTransfer>();
if (transfer != null && transfer.Avatar != null)
{
return transfer.Avatar;
}
return null;
}
HumanPoseHandler m_handler;
public HumanPose m_pose;
[Serializable]
public struct Muscle
{
public int Index;
public string Name;
public float Value;
}
public Vector3 BodyPosition;
public Muscle[] Muscles;
private void OnEnable()
{
var avatar = GetAvatar();
if (avatar == null)
{
enabled = false;
return;
}
m_handler = new HumanPoseHandler(avatar, transform);
Muscles = HumanTrait.MuscleName.Select((x, i) =>
{
return new Muscle
{
Index = i,
Name = x,
};
})
.ToArray()
;
}
private void OnDisable()
{
}
private void Update()
{
m_handler.GetHumanPose(ref m_pose);
BodyPosition = m_pose.bodyPosition;
for (int i = 0; i < m_pose.muscles.Length; ++i)
{
Muscles[i].Value = m_pose.muscles[i];
}
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(MuscleDebug.Muscle))]
public class MuscleDrawer : PropertyDrawer
{
public override void OnGUI(Rect position,
SerializedProperty property, GUIContent label)
{
var nameProp = property.FindPropertyRelative("Name");
var valueProp = property.FindPropertyRelative("Value");
/*
var label = string.Format("{0}: {1}",
nameProp.stringValue,
valueProp.floatValue
);
*/
EditorGUI.LabelField(position, nameProp.stringValue, string.Format("{0:0.00}", valueProp.floatValue));
}
}
#endif
}