HelperScript.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public static class HelperScript
  6. {
  7. static Vector3[] playerJoints;
  8. public static Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  9. {
  10. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  11. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  12. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  13. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  14. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  15. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  16. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  17. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  18. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  19. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  20. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  21. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  22. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  23. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  24. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  25. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  26. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  27. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  28. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  29. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  30. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  31. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  32. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  33. { Kinect.JointType.Neck, Kinect.JointType.Head },
  34. };
  35. public static Vector3[] ConvertJointsDataToVector3Array(JointsData jd)
  36. {
  37. Vector3[] joints = new Vector3[jd.jointsPositionsX.Count];
  38. for (int i = 0; i < joints.Length; i++)
  39. {
  40. joints[i] = new Vector3(jd.jointsPositionsX[i], jd.jointsPositionsY[i], jd.jointsPositionsZ[i]);
  41. }
  42. return joints;
  43. }
  44. public static Vector3[] RescaleJoints(Vector3[] initJoints)
  45. {
  46. Vector3[] rescaledJoints = new Vector3[25];
  47. // Player's each bone length
  48. float lengthA = (playerJoints[(int)Kinect.JointType.SpineBase] - playerJoints[(int)Kinect.JointType.HipLeft]).magnitude;
  49. float lengthB = (playerJoints[(int)Kinect.JointType.HipLeft] - playerJoints[(int)Kinect.JointType.KneeLeft]).magnitude;
  50. float lengthC = (playerJoints[(int)Kinect.JointType.KneeLeft] - playerJoints[(int)Kinect.JointType.AnkleLeft]).magnitude;
  51. float lengthD = (playerJoints[(int)Kinect.JointType.AnkleLeft] - playerJoints[(int)Kinect.JointType.FootLeft]).magnitude;
  52. float lengthE = (playerJoints[(int)Kinect.JointType.SpineBase] - playerJoints[(int)Kinect.JointType.SpineMid]).magnitude;
  53. float lengthF = (playerJoints[(int)Kinect.JointType.SpineMid] - playerJoints[(int)Kinect.JointType.SpineShoulder]).magnitude;
  54. float lengthG = (playerJoints[(int)Kinect.JointType.SpineShoulder] - playerJoints[(int)Kinect.JointType.ShoulderLeft]).magnitude;
  55. float lengthH = (playerJoints[(int)Kinect.JointType.ShoulderLeft] - playerJoints[(int)Kinect.JointType.ElbowLeft]).magnitude;
  56. float lengthI = (playerJoints[(int)Kinect.JointType.ElbowLeft] - playerJoints[(int)Kinect.JointType.WristLeft]).magnitude;
  57. float lengthJ = (playerJoints[(int)Kinect.JointType.SpineShoulder] - playerJoints[(int)Kinect.JointType.Neck]).magnitude;
  58. rescaledJoints[(int)Kinect.JointType.SpineBase] = Vector3.zero;
  59. // Lower part
  60. rescaledJoints[(int)Kinect.JointType.HipLeft] = GetTargetJointWithDesiredLength(
  61. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipLeft], lengthA) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  62. rescaledJoints[(int)Kinect.JointType.HipRight] = GetTargetJointWithDesiredLength(
  63. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipRight], lengthA) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  64. rescaledJoints[(int)Kinect.JointType.KneeLeft] = GetTargetJointWithDesiredLength(
  65. initJoints[(int)Kinect.JointType.HipLeft], initJoints[(int)Kinect.JointType.KneeLeft], lengthB) + rescaledJoints[(int)Kinect.JointType.HipLeft];
  66. rescaledJoints[(int)Kinect.JointType.KneeRight] = GetTargetJointWithDesiredLength(
  67. initJoints[(int)Kinect.JointType.HipRight], initJoints[(int)Kinect.JointType.KneeRight], lengthB) + rescaledJoints[(int)Kinect.JointType.HipRight];
  68. rescaledJoints[(int)Kinect.JointType.AnkleLeft] = GetTargetJointWithDesiredLength(
  69. initJoints[(int)Kinect.JointType.KneeLeft], initJoints[(int)Kinect.JointType.AnkleLeft], lengthC) + rescaledJoints[(int)Kinect.JointType.KneeLeft];
  70. rescaledJoints[(int)Kinect.JointType.AnkleRight] = GetTargetJointWithDesiredLength(
  71. initJoints[(int)Kinect.JointType.KneeRight], initJoints[(int)Kinect.JointType.AnkleRight], lengthC) + rescaledJoints[(int)Kinect.JointType.KneeRight];
  72. rescaledJoints[(int)Kinect.JointType.FootLeft] = GetTargetJointWithDesiredLength(
  73. initJoints[(int)Kinect.JointType.AnkleLeft], initJoints[(int)Kinect.JointType.FootLeft], lengthD) + rescaledJoints[(int)Kinect.JointType.AnkleLeft] + new Vector3(0, 0.03f, 0);
  74. //initJoints[(int)Kinect.JointType.AnkleLeft], initJoints[(int)Kinect.JointType.FootLeft], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleLeft];
  75. rescaledJoints[(int)Kinect.JointType.FootRight] = GetTargetJointWithDesiredLength(
  76. initJoints[(int)Kinect.JointType.AnkleRight], initJoints[(int)Kinect.JointType.FootRight], lengthD) + rescaledJoints[(int)Kinect.JointType.AnkleRight] + new Vector3(0, 0.03f, 0);
  77. //initJoints[(int)Kinect.JointType.AnkleRight], initJoints[(int)Kinect.JointType.FootRight], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleRight];
  78. // Upper part
  79. rescaledJoints[(int)Kinect.JointType.SpineMid] = GetTargetJointWithDesiredLength(
  80. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.SpineMid], lengthE) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  81. rescaledJoints[(int)Kinect.JointType.SpineShoulder] = GetTargetJointWithDesiredLength(
  82. initJoints[(int)Kinect.JointType.SpineMid], initJoints[(int)Kinect.JointType.SpineShoulder], lengthF) + rescaledJoints[(int)Kinect.JointType.SpineMid];
  83. rescaledJoints[(int)Kinect.JointType.ShoulderLeft] = GetTargetJointWithDesiredLength(
  84. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderLeft], lengthG) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  85. rescaledJoints[(int)Kinect.JointType.ShoulderRight] = GetTargetJointWithDesiredLength(
  86. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderRight], lengthG) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  87. rescaledJoints[(int)Kinect.JointType.ElbowLeft] = GetTargetJointWithDesiredLength(
  88. initJoints[(int)Kinect.JointType.ShoulderLeft], initJoints[(int)Kinect.JointType.ElbowLeft], lengthH) + rescaledJoints[(int)Kinect.JointType.ShoulderLeft];
  89. rescaledJoints[(int)Kinect.JointType.ElbowRight] = GetTargetJointWithDesiredLength(
  90. initJoints[(int)Kinect.JointType.ShoulderRight], initJoints[(int)Kinect.JointType.ElbowRight], lengthH) + rescaledJoints[(int)Kinect.JointType.ShoulderRight];
  91. rescaledJoints[(int)Kinect.JointType.WristLeft] = GetTargetJointWithDesiredLength(
  92. initJoints[(int)Kinect.JointType.ElbowLeft], initJoints[(int)Kinect.JointType.WristLeft], lengthI) + rescaledJoints[(int)Kinect.JointType.ElbowLeft];
  93. rescaledJoints[(int)Kinect.JointType.WristRight] = GetTargetJointWithDesiredLength(
  94. initJoints[(int)Kinect.JointType.ElbowRight], initJoints[(int)Kinect.JointType.WristRight], lengthI) + rescaledJoints[(int)Kinect.JointType.ElbowRight];
  95. rescaledJoints[(int)Kinect.JointType.Neck] = GetTargetJointWithDesiredLength(
  96. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.Neck], lengthJ) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  97. return rescaledJoints;
  98. }
  99. private static Vector3 GetTargetJointWithDesiredLength(Vector3 source, Vector3 target, float desiredLength)
  100. {
  101. Vector3 sourceToTarget = target - source;
  102. float length = sourceToTarget.magnitude;
  103. return sourceToTarget * desiredLength / length;
  104. }
  105. public static void SetPlayerJoints(Vector3[] playerJoints)
  106. {
  107. HelperScript.playerJoints = playerJoints;
  108. }
  109. }