// adds current pose of poseModel to the saved poses list private void AddCurrentPoseToSaved(float fCurrentTime, bool isMirrored) { KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null || poseModel == null || poseJoints == null) { return; } PoseModelData pose = new PoseModelData(); pose.fTime = fCurrentTime; pose.avBoneDirs = new Vector3[poseJoints.Count]; // save model rotation Quaternion poseModelRotation = poseModel.transform.rotation; if (avatarController) { ulong avatarUserId = avatarController.playerId; bool isAvatarMirrored = avatarController.mirroredMovement; Quaternion userRotation = kinectManager.GetUserOrientation(avatarUserId, !isAvatarMirrored); poseModel.transform.rotation = initialPoseRotation * userRotation; } int jointCount = kinectManager.GetJointCount(); 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 < jointCount) { Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (poseTransform1 != null && poseTransform2 != null) { pose.avBoneDirs[i] = (poseTransform2.position - poseTransform1.position).normalized; } } } // add pose to the list alSavedPoses.Add(pose); // restore model rotation poseModel.transform.rotation = poseModelRotation; }
// gets the difference between the avatar pose and the list of saved poses private void GetPoseDifference(bool isMirrored) { // by-default values bPoseMatched = false; fMatchPercent = 0f; fMatchPoseTime = 0f; KinectManager kinectManager = KinectManager.Instance; if (poseJoints == null || poseAvatar.avBoneDirs == null) { return; } // check the difference with saved poses, starting from the last one for (int p = alSavedPoses.Count - 1; p >= 0; p--) { float fAngleDiff = 0f; float fMaxDiff = 0f; PoseModelData poseModel = alSavedPoses[p]; for (int i = 0; i < poseJoints.Count; i++) { Vector3 vPoseBone = poseModel.avBoneDirs[i]; Vector3 vAvatarBone = poseAvatar.avBoneDirs[i]; float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; fMaxDiff += 90f; // we assume the max diff could be 90 degrees } float fPoseMatch = fMaxDiff > 0f ? (1f - fAngleDiff / fMaxDiff) : 0f; if (fPoseMatch > fMatchPercent) { fMatchPercent = fPoseMatch; fMatchPoseTime = poseModel.fTime; bPoseMatched = (fMatchPercent >= matchThreshold); } } }
// adds current pose of poseModel to the saved poses list private void AddCurrentPoseToSaved(float fCurrentTime, bool isMirrored) { KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null || poseModel == null || poseJoints == null) { return; } PoseModelData pose = new PoseModelData(); pose.fTime = fCurrentTime; pose.avBoneDirs = new Vector3[poseJoints.Count]; // save model rotation Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation; poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; 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 poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (poseTransform1 != null && poseTransform2 != null) { pose.avBoneDirs[i] = (poseTransform2.position - poseTransform1.position).normalized; } } } // add pose to the list alSavedPoses.Add(pose); // restore model rotation poseModel.GetBoneTransform(0).rotation = poseSavedRotation; }
// gets the difference between the avatar pose and the list of saved poses private void GetPoseDifference(bool isMirrored) { // by-default values bPoseMatched = false; fMatchPercent = 0f; fMatchPoseTime = 0f; KinectManager kinectManager = KinectManager.Instance; if (poseJoints == null || poseAvatar.avBoneDirs == null) { return; } if (sbDebug != null) { sbDebug.Clear(); sbDebug.AppendLine(); } // check the difference with saved poses, starting from the last one for (int p = alSavedPoses.Count - 1; p >= 0; p--) { float fAngleDiff = 0f; float fMaxDiff = 0f; PoseModelData poseModel = alSavedPoses[p]; for (int i = 0; i < poseJoints.Count; i++) { Vector3 vPoseBone = poseModel.avBoneDirs[i]; Vector3 vAvatarBone = poseAvatar.avBoneDirs[i]; if (vPoseBone == Vector3.zero || vAvatarBone == Vector3.zero) { continue; } float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; fMaxDiff += 90f; // we assume the max diff could be 90 degrees if (sbDebug != null) { sbDebug.AppendFormat("SP: {0}, {1} - angle: {2:F0}, match: {3:F0}%", p, poseJoints[i], fDiff, (1f - fDiff / 90f) * 100f); sbDebug.AppendLine(); } } float fPoseMatch = fMaxDiff > 0f ? (1f - fAngleDiff / fMaxDiff) : 0f; if (fPoseMatch > fMatchPercent) { fMatchPercent = fPoseMatch; fMatchPoseTime = poseModel.fTime; bPoseMatched = (fMatchPercent >= matchThreshold); } } }