Skip to content

Instantly share code, notes, and snippets.

@ousttrue
Created December 29, 2015 05:27
Show Gist options
  • Save ousttrue/d25d6cf237e7ee384b9b to your computer and use it in GitHub Desktop.
Save ousttrue/d25d6cf237e7ee384b9b to your computer and use it in GitHub Desktop.
Unity5.3 retargetting
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