BodySourceView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Kinect = Windows.Kinect;
  4. using Valve.VR;
  5. public class BodySourceView : MonoBehaviour
  6. {
  7. public Transform[] joints;
  8. public GameObject BodySourceManager;
  9. public Transform cameraRig;
  10. public PlayerReplay playerReplay;
  11. public ModeController modeController;
  12. // TODO: Implementing color feedback
  13. public GameObject body;
  14. public bool wristLeftLate;
  15. public bool wristRightLate;
  16. private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
  17. private BodySourceManager _BodyManager;
  18. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  19. {
  20. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  21. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  22. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  23. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  24. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  25. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  26. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  27. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  28. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  29. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  30. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  31. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  32. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  33. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  34. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  35. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  36. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  37. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  38. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  39. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  40. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  41. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  42. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  43. { Kinect.JointType.Neck, Kinect.JointType.Head },
  44. };
  45. void Update()
  46. {
  47. if (BodySourceManager == null)
  48. {
  49. return;
  50. }
  51. _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
  52. if (_BodyManager == null)
  53. {
  54. return;
  55. }
  56. Kinect.Body[] data = _BodyManager.GetData();
  57. if (data == null)
  58. {
  59. return;
  60. }
  61. List<ulong> trackedIds = new List<ulong>();
  62. foreach (var body in data)
  63. {
  64. if (body == null)
  65. {
  66. continue;
  67. }
  68. if (body.IsTracked)
  69. {
  70. trackedIds.Add(body.TrackingId);
  71. }
  72. }
  73. List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
  74. // First delete untracked bodies
  75. foreach (ulong trackingId in knownIds)
  76. {
  77. if (!trackedIds.Contains(trackingId))
  78. {
  79. Destroy(_Bodies[trackingId]);
  80. _Bodies.Remove(trackingId);
  81. }
  82. }
  83. foreach (var body in data)
  84. {
  85. if (body == null)
  86. {
  87. continue;
  88. }
  89. if (body.IsTracked)
  90. {
  91. if (!_Bodies.ContainsKey(body.TrackingId))
  92. {
  93. _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
  94. }
  95. RefreshBodyObject(body, _Bodies[body.TrackingId]);
  96. }
  97. }
  98. }
  99. private GameObject CreateBodyObject(ulong id)
  100. {
  101. body = new GameObject("Body:" + id);
  102. body.transform.parent = gameObject.transform;
  103. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  104. {
  105. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  106. // Give WristLeft and WristRight rigidbody
  107. if (jt == Kinect.JointType.WristLeft)
  108. {
  109. jointObj.tag = "WristLeft";
  110. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  111. rb.useGravity = false;
  112. rb.isKinematic = true;
  113. }
  114. else if (jt == Kinect.JointType.WristRight)
  115. {
  116. jointObj.tag = "WristRight";
  117. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  118. rb.useGravity = false;
  119. rb.isKinematic = true;
  120. }
  121. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  122. //lr.SetVertexCount(2);
  123. lr.positionCount = 2;
  124. //lr.SetWidth(0.05f, 0.05f);
  125. lr.startWidth = 0.05f;
  126. lr.endWidth = 0.05f;
  127. Material whiteDiffuseMat = new Material(Shader.Find("Sprites/Default"));
  128. whiteDiffuseMat.color = new Color(1, 1, 1, 0.5f);
  129. lr.material = whiteDiffuseMat;
  130. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  131. jointObj.name = jt.ToString();
  132. jointObj.transform.parent = body.transform;
  133. }
  134. return body;
  135. }
  136. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  137. {
  138. Vector3[] initJoints = new Vector3[25];
  139. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  140. {
  141. Kinect.Joint sourceJoint = body.Joints[jt];
  142. initJoints[(int)jt] = GetVector3FromJoint(sourceJoint);
  143. }
  144. Vector3[] rescaledJoints = RescaleJoints(initJoints);
  145. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  146. {
  147. Kinect.Joint sourceJoint = body.Joints[jt];
  148. Kinect.Joint? targetJoint = null;
  149. if (_BoneMap.ContainsKey(jt))
  150. {
  151. targetJoint = body.Joints[_BoneMap[jt]];
  152. }
  153. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  154. //jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  155. jointObj.localPosition = rescaledJoints[(int)jt];
  156. if (jt == Kinect.JointType.Head)
  157. {
  158. // Make head joint invisible
  159. jointObj.GetComponent<Renderer>().enabled = false;
  160. }
  161. if (jt == Kinect.JointType.Neck)
  162. {
  163. // Make camera follow the neck
  164. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective)
  165. {
  166. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y, jointObj.localPosition.z - 0.03f);
  167. jointObj.GetComponent<Renderer>().enabled = false;
  168. }
  169. else
  170. {
  171. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y + 0.1f, jointObj.localPosition.z + 0.5f);
  172. }
  173. }
  174. // Make these joints invisible
  175. if (jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  176. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  177. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  178. {
  179. jointObj.GetComponent<Renderer>().enabled = false;
  180. }
  181. // WristLeft and WristRight do something if not following the motions
  182. //if ((jt == Kinect.JointType.WristLeft && wristLeftLate) || (jt == Kinect.JointType.WristRight && wristRightLate))
  183. //{
  184. // if (modeController.feedback == ModeController.Feedback.ColorFeedback)
  185. // {
  186. // jointObj.GetComponent<Renderer>().material.color = new Color(1, 0, 0);
  187. // jointObj.GetComponent<LineRenderer>().startColor = new Color(1, 0, 0);
  188. // }
  189. // else if (modeController.feedback == ModeController.Feedback.HapticFeedback)
  190. // {
  191. // // TODO: Add haptic feedback need to test
  192. // if (jt == Kinect.JointType.WristLeft)
  193. // {
  194. // Debug.Log("WristLeft vibrate");
  195. // SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  196. // }
  197. // else if (jt == Kinect.JointType.WristRight)
  198. // {
  199. // Debug.Log("WristRight vibrate");
  200. // SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  201. // }
  202. // }
  203. //}
  204. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  205. if (targetJoint.HasValue && jt != Kinect.JointType.Neck && jt != Kinect.JointType.ThumbLeft && jt != Kinect.JointType.ThumbRight
  206. && jt != Kinect.JointType.HandLeft && jt != Kinect.JointType.HandRight
  207. && jt != Kinect.JointType.HandTipLeft && jt != Kinect.JointType.HandTipRight) // Do not make LineRenderer for these joints
  208. {
  209. lr.SetPosition(0, jointObj.localPosition);
  210. //lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  211. lr.SetPosition(1, rescaledJoints[(int)_BoneMap[jt]]);
  212. // Coloring the line renderer
  213. //lr.SetColors(GetColorForState(sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  214. //lr.startColor = GetColorForState(sourceJoint.TrackingState);
  215. //lr.endColor = GetColorForState(targetJoint.Value.TrackingState);
  216. }
  217. else
  218. {
  219. lr.enabled = false;
  220. }
  221. // Record joints to PlayerReplay.cs
  222. playerReplay.joints[(int)jt] = jointObj;
  223. }
  224. // Joint orientations
  225. //Kinect.Vector4[] orientation = new Kinect.Vector4[22];
  226. //orientation[0] = body.JointOrientations[Kinect.JointType.Head].Orientation;
  227. //orientation[1] = body.JointOrientations[Kinect.JointType.Neck].Orientation;
  228. //orientation[2] = body.JointOrientations[Kinect.JointType.SpineMid].Orientation;
  229. //orientation[3] = body.JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  230. //orientation[4] = body.JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  231. //orientation[5] = body.JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  232. //orientation[6] = body.JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  233. //orientation[7] = body.JointOrientations[Kinect.JointType.WristLeft].Orientation;
  234. //orientation[8] = body.JointOrientations[Kinect.JointType.WristRight].Orientation;
  235. //orientation[9] = body.JointOrientations[Kinect.JointType.HipLeft].Orientation;
  236. //orientation[10] = body.JointOrientations[Kinect.JointType.HipRight].Orientation;
  237. //orientation[11] = body.JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  238. //orientation[12] = body.JointOrientations[Kinect.JointType.KneeRight].Orientation;
  239. //orientation[13] = body.JointOrientations[Kinect.JointType.SpineBase].Orientation;
  240. //orientation[14] = body.JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  241. //orientation[15] = body.JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  242. //orientation[16] = body.JointOrientations[Kinect.JointType.FootLeft].Orientation;
  243. //orientation[17] = body.JointOrientations[Kinect.JointType.FootRight].Orientation;
  244. //orientation[18] = body.JointOrientations[Kinect.JointType.HandLeft].Orientation;
  245. //orientation[19] = body.JointOrientations[Kinect.JointType.HandRight].Orientation;
  246. //orientation[20] = body.JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  247. //orientation[21] = body.JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  248. //for (int j = 0; j < 22; j++)
  249. //{
  250. // //joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  251. //}
  252. }
  253. private static Color GetColorForState(Kinect.TrackingState state)
  254. {
  255. switch (state)
  256. {
  257. case Kinect.TrackingState.Tracked:
  258. return Color.green;
  259. case Kinect.TrackingState.Inferred:
  260. return Color.red;
  261. default:
  262. return Color.black;
  263. }
  264. }
  265. private Vector3 GetVector3FromJoint(Kinect.Joint joint)
  266. {
  267. //return new Vector3(joint.Position.X * -10, joint.Position.Y * 10, joint.Position.Z * 10);
  268. return new Vector3(joint.Position.X * -1, joint.Position.Y, joint.Position.Z);
  269. }
  270. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  271. {
  272. Quaternion orientationUnity;
  273. orientationUnity.x = -orientation.X;
  274. orientationUnity.y = orientation.Y;
  275. orientationUnity.z = orientation.Z;
  276. orientationUnity.w = orientation.W;
  277. return orientationUnity;
  278. }
  279. private Vector3[] RescaleJoints(Vector3[] initJoints)
  280. {
  281. Vector3[] rescaledJoints = new Vector3[25];
  282. rescaledJoints[(int)Kinect.JointType.SpineBase] = Vector3.zero;
  283. // Lower part
  284. rescaledJoints[(int)Kinect.JointType.HipLeft] = GetTargetJointWithDesiredLength(
  285. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipLeft], 0.08f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  286. rescaledJoints[(int)Kinect.JointType.HipRight] = GetTargetJointWithDesiredLength(
  287. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipRight], 0.08f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  288. rescaledJoints[(int)Kinect.JointType.KneeLeft] = GetTargetJointWithDesiredLength(
  289. initJoints[(int)Kinect.JointType.HipLeft], initJoints[(int)Kinect.JointType.KneeLeft], 0.25f) + rescaledJoints[(int)Kinect.JointType.HipLeft];
  290. rescaledJoints[(int)Kinect.JointType.KneeRight] = GetTargetJointWithDesiredLength(
  291. initJoints[(int)Kinect.JointType.HipRight], initJoints[(int)Kinect.JointType.KneeRight], 0.25f) + rescaledJoints[(int)Kinect.JointType.HipRight];
  292. rescaledJoints[(int)Kinect.JointType.AnkleLeft] = GetTargetJointWithDesiredLength(
  293. initJoints[(int)Kinect.JointType.KneeLeft], initJoints[(int)Kinect.JointType.AnkleLeft], 0.3f) + rescaledJoints[(int)Kinect.JointType.KneeLeft];
  294. rescaledJoints[(int)Kinect.JointType.AnkleRight] = GetTargetJointWithDesiredLength(
  295. initJoints[(int)Kinect.JointType.KneeRight], initJoints[(int)Kinect.JointType.AnkleRight], 0.3f) + rescaledJoints[(int)Kinect.JointType.KneeRight];
  296. rescaledJoints[(int)Kinect.JointType.FootLeft] = GetTargetJointWithDesiredLength(
  297. initJoints[(int)Kinect.JointType.AnkleLeft], initJoints[(int)Kinect.JointType.FootLeft], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleLeft];
  298. rescaledJoints[(int)Kinect.JointType.FootRight] = GetTargetJointWithDesiredLength(
  299. initJoints[(int)Kinect.JointType.AnkleRight], initJoints[(int)Kinect.JointType.FootRight], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleRight];
  300. // Upper part
  301. rescaledJoints[(int)Kinect.JointType.SpineMid] = GetTargetJointWithDesiredLength(
  302. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.SpineMid], 0.2f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  303. rescaledJoints[(int)Kinect.JointType.SpineShoulder] = GetTargetJointWithDesiredLength(
  304. initJoints[(int)Kinect.JointType.SpineMid], initJoints[(int)Kinect.JointType.SpineShoulder], 0.2f) + rescaledJoints[(int)Kinect.JointType.SpineMid];
  305. rescaledJoints[(int)Kinect.JointType.ShoulderLeft] = GetTargetJointWithDesiredLength(
  306. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderLeft], 0.1f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  307. rescaledJoints[(int)Kinect.JointType.ShoulderRight] = GetTargetJointWithDesiredLength(
  308. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderRight], 0.1f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  309. rescaledJoints[(int)Kinect.JointType.ElbowLeft] = GetTargetJointWithDesiredLength(
  310. initJoints[(int)Kinect.JointType.ShoulderLeft], initJoints[(int)Kinect.JointType.ElbowLeft], 0.2f) + rescaledJoints[(int)Kinect.JointType.ShoulderLeft];
  311. rescaledJoints[(int)Kinect.JointType.ElbowRight] = GetTargetJointWithDesiredLength(
  312. initJoints[(int)Kinect.JointType.ShoulderRight], initJoints[(int)Kinect.JointType.ElbowRight], 0.2f) + rescaledJoints[(int)Kinect.JointType.ShoulderRight];
  313. rescaledJoints[(int)Kinect.JointType.WristLeft] = GetTargetJointWithDesiredLength(
  314. initJoints[(int)Kinect.JointType.ElbowLeft], initJoints[(int)Kinect.JointType.WristLeft], 0.2f) + rescaledJoints[(int)Kinect.JointType.ElbowLeft];
  315. rescaledJoints[(int)Kinect.JointType.WristRight] = GetTargetJointWithDesiredLength(
  316. initJoints[(int)Kinect.JointType.ElbowRight], initJoints[(int)Kinect.JointType.WristRight], 0.2f) + rescaledJoints[(int)Kinect.JointType.ElbowRight];
  317. rescaledJoints[(int)Kinect.JointType.Neck] = GetTargetJointWithDesiredLength(
  318. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.Neck], 0.07f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  319. return rescaledJoints;
  320. }
  321. private Vector3 GetTargetJointWithDesiredLength(Vector3 source, Vector3 target, float desiredLength)
  322. {
  323. Vector3 sourceToTarget = target - source;
  324. float length = sourceToTarget.magnitude;
  325. return sourceToTarget * desiredLength / length;
  326. }
  327. }