BodySourceView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Kinect = Windows.Kinect;
  4. public class BodySourceView : MonoBehaviour
  5. {
  6. public Transform[] joints;
  7. public Material BoneMaterial;
  8. public GameObject BodySourceManager;
  9. public Transform cameraRig;
  10. public PlayerReplay playerReplay;
  11. public ModeController modeController;
  12. public bool wristLeftLate;
  13. public bool wristRightLate;
  14. private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
  15. private BodySourceManager _BodyManager;
  16. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  17. {
  18. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  19. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  20. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  21. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  22. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  23. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  24. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  25. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  26. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  27. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  28. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  29. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  30. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  31. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  32. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  33. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  34. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  35. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  36. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  37. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  38. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  39. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  40. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  41. { Kinect.JointType.Neck, Kinect.JointType.Head },
  42. };
  43. void Update()
  44. {
  45. if (BodySourceManager == null)
  46. {
  47. return;
  48. }
  49. _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
  50. if (_BodyManager == null)
  51. {
  52. return;
  53. }
  54. Kinect.Body[] data = _BodyManager.GetData();
  55. if (data == null)
  56. {
  57. return;
  58. }
  59. List<ulong> trackedIds = new List<ulong>();
  60. foreach (var body in data)
  61. {
  62. if (body == null)
  63. {
  64. continue;
  65. }
  66. if (body.IsTracked)
  67. {
  68. trackedIds.Add(body.TrackingId);
  69. }
  70. }
  71. List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
  72. // First delete untracked bodies
  73. foreach (ulong trackingId in knownIds)
  74. {
  75. if (!trackedIds.Contains(trackingId))
  76. {
  77. Destroy(_Bodies[trackingId]);
  78. _Bodies.Remove(trackingId);
  79. }
  80. }
  81. foreach (var body in data)
  82. {
  83. if (body == null)
  84. {
  85. continue;
  86. }
  87. if (body.IsTracked)
  88. {
  89. if (!_Bodies.ContainsKey(body.TrackingId))
  90. {
  91. _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
  92. }
  93. RefreshBodyObject(body, _Bodies[body.TrackingId]);
  94. }
  95. }
  96. }
  97. private GameObject CreateBodyObject(ulong id)
  98. {
  99. GameObject body = new GameObject("Body:" + id);
  100. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  101. {
  102. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  103. // TODO: need to test
  104. if (jt == Kinect.JointType.WristLeft)
  105. {
  106. jointObj.tag = "WristLeft";
  107. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  108. rb.useGravity = false;
  109. } else if (jt == Kinect.JointType.WristRight)
  110. {
  111. jointObj.tag = "WristRight";
  112. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  113. rb.useGravity = false;
  114. }
  115. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  116. //lr.SetVertexCount(2);
  117. lr.positionCount = 2;
  118. lr.material = BoneMaterial;
  119. //lr.SetWidth(0.05f, 0.05f);
  120. lr.startWidth = 0.3f;
  121. lr.endWidth = 0.3f;
  122. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  123. jointObj.name = jt.ToString();
  124. jointObj.transform.parent = body.transform;
  125. }
  126. return body;
  127. }
  128. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  129. {
  130. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  131. {
  132. Kinect.Joint sourceJoint = body.Joints[jt];
  133. Kinect.Joint? targetJoint = null;
  134. if (_BoneMap.ContainsKey(jt))
  135. {
  136. targetJoint = body.Joints[_BoneMap[jt]];
  137. }
  138. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  139. jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  140. if (jt == Kinect.JointType.Head)
  141. {
  142. // Make head joint invisible
  143. jointObj.GetComponent<Renderer>().enabled = false;
  144. // Make camera follow the head
  145. // TODO: need to test
  146. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective)
  147. {
  148. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y, jointObj.localPosition.z);
  149. } else
  150. {
  151. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y - 3, jointObj.localPosition.z + 2);
  152. }
  153. }
  154. // TODO: need to test
  155. if ((jt == Kinect.JointType.WristLeft && wristLeftLate) || (jt == Kinect.JointType.WristRight && wristRightLate))
  156. {
  157. jointObj.GetComponent<Renderer>().material.color = new Color(1, 0, 0);
  158. }
  159. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  160. if (targetJoint.HasValue && jt != Kinect.JointType.Neck) // Do not make LineRenderer from neck to head
  161. {
  162. lr.SetPosition(0, jointObj.localPosition);
  163. lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  164. // Coloring the line renderer
  165. //lr.SetColors(GetColorForState(sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  166. //lr.startColor = GetColorForState(sourceJoint.TrackingState);
  167. //lr.endColor = GetColorForState(targetJoint.Value.TrackingState);
  168. }
  169. else
  170. {
  171. lr.enabled = false;
  172. }
  173. // Record joints to PlayerReplay.cs
  174. playerReplay.joints[(int)jt] = jointObj;
  175. }
  176. // Joint orientations
  177. //Kinect.Vector4[] orientation = new Kinect.Vector4[22];
  178. //orientation[0] = body.JointOrientations[Kinect.JointType.Head].Orientation;
  179. //orientation[1] = body.JointOrientations[Kinect.JointType.Neck].Orientation;
  180. //orientation[2] = body.JointOrientations[Kinect.JointType.SpineMid].Orientation;
  181. //orientation[3] = body.JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  182. //orientation[4] = body.JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  183. //orientation[5] = body.JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  184. //orientation[6] = body.JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  185. //orientation[7] = body.JointOrientations[Kinect.JointType.WristLeft].Orientation;
  186. //orientation[8] = body.JointOrientations[Kinect.JointType.WristRight].Orientation;
  187. //orientation[9] = body.JointOrientations[Kinect.JointType.HipLeft].Orientation;
  188. //orientation[10] = body.JointOrientations[Kinect.JointType.HipRight].Orientation;
  189. //orientation[11] = body.JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  190. //orientation[12] = body.JointOrientations[Kinect.JointType.KneeRight].Orientation;
  191. //orientation[13] = body.JointOrientations[Kinect.JointType.SpineBase].Orientation;
  192. //orientation[14] = body.JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  193. //orientation[15] = body.JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  194. //orientation[16] = body.JointOrientations[Kinect.JointType.FootLeft].Orientation;
  195. //orientation[17] = body.JointOrientations[Kinect.JointType.FootRight].Orientation;
  196. //orientation[18] = body.JointOrientations[Kinect.JointType.HandLeft].Orientation;
  197. //orientation[19] = body.JointOrientations[Kinect.JointType.HandRight].Orientation;
  198. //orientation[20] = body.JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  199. //orientation[21] = body.JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  200. //for (int j = 0; j < 22; j++)
  201. //{
  202. // //joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  203. //}
  204. }
  205. private static Color GetColorForState(Kinect.TrackingState state)
  206. {
  207. switch (state)
  208. {
  209. case Kinect.TrackingState.Tracked:
  210. return Color.green;
  211. case Kinect.TrackingState.Inferred:
  212. return Color.red;
  213. default:
  214. return Color.black;
  215. }
  216. }
  217. private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
  218. {
  219. return new Vector3(joint.Position.X * -10, joint.Position.Y * 10 + 13.3f, joint.Position.Z * 10);
  220. }
  221. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  222. {
  223. Quaternion orientationUnity;
  224. orientationUnity.x = -orientation.X;
  225. orientationUnity.y = orientation.Y;
  226. orientationUnity.z = orientation.Z;
  227. orientationUnity.w = orientation.W;
  228. return orientationUnity;
  229. }
  230. }