Created
December 29, 2015 05:27
-
-
Save ousttrue/d25d6cf237e7ee384b9b to your computer and use it in GitHub Desktop.
Unity5.3 retargetting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public class Retargetting : MonoBehaviour { | |
[SerializeField] | |
Retargetting Source; | |
Animator m_animator; | |
HumanPoseHandler m_handler; | |
HumanPose m_pose; | |
int m_poseFrame; | |
HumanPose GetPose() | |
{ | |
if(m_poseFrame!=Time.frameCount) | |
{ | |
m_handler.GetHumanPose(ref m_pose); | |
m_poseFrame = Time.frameCount; | |
} | |
return m_pose; | |
} | |
void Awake() | |
{ | |
m_animator = GetComponent<Animator>(); | |
m_handler = new HumanPoseHandler(m_animator.avatar, transform); | |
} | |
/// <summary> | |
/// AnimatorにControllerをセットしてbase layerのIKフラグをonにすること | |
/// </summary> | |
void OnAnimatorIK() | |
{ | |
if (Source) | |
{ | |
// | |
// Transformに転写してからAnimatorコピーしなおす | |
// | |
var pose = Source.GetPose(); | |
// ここでposeを加工できるんじゃないか(自ポーズと混ぜるとか) | |
m_handler.SetHumanPose(ref pose); | |
// apply positions | |
m_animator.bodyPosition = pose.bodyPosition + transform.position; | |
// apply rotations | |
foreach (HumanBodyBones bone in Enum.GetValues(typeof(HumanBodyBones))) | |
{ | |
var t = m_animator.GetBoneTransform(bone); | |
if (t != null) | |
{ | |
m_animator.SetBoneLocalRotation(bone, t.localRotation); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment