今天发现了官方demo
上午看了一个脚本,很长,其中的关系还不是太清楚,大概看了一小会,发现自己真的真的弱爆了,可能这种大几千行的代码确实是只需要会用就好了吧,没有特别的想法,更不会去创作修改,那个测量身高的真的想了很多办法(委屈脸)。
其中一个脚本是KinectPoseDetector,好好运行+研究。今天将它贴在这里研究:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class PoseDetectorScript : MonoBehaviour
{
//使用那个模型
public PoseModelHelper avatarModel;
//Model in pose that need to be reached by the user.")]
public PoseModelHelper poseModel;
//需要匹配的关节点列表
public List<KinectInterop.JointType> poseJoints = new List<KinectInterop.JointType>();
[Tooltip("Threshold, above which we consider the pose is matched.")]
public float matchThreshold = 0.7f;
//接收消息的GUI
public GUIText infoText;
// 匹配度 0-1
private float fMatchPercent = 0f;
// 姿势是否匹配
private bool bPoseMatched = false;
/// <summary>
/// Gets the pose match percent.
///得到匹配的百分比
/// </summary>
/// <returns>The match percent (value between 0 and 1).</returns>
public float GetMatchPercent()
{
return fMatchPercent;
}
/// <summary>
/// 检查目标姿势是否匹配
/// </summary>
/// <returns><c>true</c> if the target pose is matched; otherwise, <c>false</c>.</returns>
public bool IsPoseMatched()
{
return bPoseMatched;
}
void Update ()
{
KinectManager kinectManager = KinectManager.Instance;
AvatarController avatarCtrl = avatarModel ? avatarModel.gameObject.GetComponent<AvatarController>() : null;
if(kinectManager != null && kinectManager.IsInitialized() &&
avatarModel != null && avatarCtrl && kinectManager.IsUserTracked(avatarCtrl.playerId))
{
// get mirrored state
bool isMirrored = avatarCtrl.mirroredMovement;
// get the difference
string sDiffDetails = string.Empty;
fMatchPercent = 1f - GetPoseDifference(isMirrored, true, ref sDiffDetails);
bPoseMatched = (fMatchPercent >= matchThreshold);
string sPoseMessage = string.Format("Pose match: {0:F0}% {1}", fMatchPercent * 100f, (bPoseMatched ? "- Matched" : ""));
if(infoText != null)
{
infoText.text = sPoseMessage + "\n\n" + sDiffDetails;
}
}
else
{
// no user found
if(infoText != null)
{
infoText.text = "Try to match the pose on the left.";
}
}
}
// gets angle or percent difference in pose
public float GetPoseDifference(bool isMirrored, bool bPercentDiff, ref string sDiffDetails)
{
float fAngleDiff = 0f;
float fMaxDiff = 0f;
sDiffDetails = string.Empty;
KinectManager kinectManager = KinectManager.Instance;
if(!kinectManager || !avatarModel || !poseModel || poseJoints.Count == 0)
{
return 0f;
}
// copy model rotation
Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation;
poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation;
StringBuilder sbDetails = new StringBuilder();
sbDetails.Append("Joint differences:").AppendLine();
for(int i = 0; i < poseJoints.Count; i++)
{
KinectInterop.JointType joint = poseJoints[i];
KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint);
if(nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount)
{
Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored));
Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored));
Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored));
Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored));
if(avatarTransform1 != null && avatarTransform2 != null && poseTransform1 != null && poseTransform2 != null)
{
Vector3 vAvatarBone = (avatarTransform2.position - avatarTransform1.position).normalized;
Vector3 vPoseBone = (poseTransform2.position - poseTransform1.position).normalized;
float fDiff = Vector3.Angle(vPoseBone, vAvatarBone);
if(fDiff > 90f) fDiff = 90f;
fAngleDiff += fDiff;
fMaxDiff += 90f; // we assume the max diff could be 90 degrees
sbDetails.AppendFormat("{0} - {1:F0} deg.", joint, fDiff).AppendLine();
}
else
{
sbDetails.AppendFormat("{0} - n/a", joint).AppendLine();
}
}
}
poseModel.GetBoneTransform(0).rotation = poseSavedRotation;
// calculate percent diff
float fPercentDiff = 0f;
if(bPercentDiff && fMaxDiff > 0f)
{
fPercentDiff = fAngleDiff / fMaxDiff;
}
// details info
sbDetails.AppendLine();
sbDetails.AppendFormat("Sum-Diff: - {0:F0} deg out of {1:F0} deg", fAngleDiff, fMaxDiff).AppendLine();
sbDetails.AppendFormat("Percent-Diff: {0:F0}%", fPercentDiff * 100).AppendLine();
sDiffDetails = sbDetails.ToString();
return (bPercentDiff ? fPercentDiff : fAngleDiff);
}
}
然后我想搞明白到底怎么匹配(原理,原理原理!!!!)。好好学习,啊啊啊啊啊啊。
19.1.7