BodySourceView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. 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. body.transform.parent = gameObject.transform;
  101. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  102. {
  103. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  104. // Give WristLeft and WristRight rigidbody
  105. if (jt == Kinect.JointType.WristLeft)
  106. {
  107. jointObj.tag = "WristLeft";
  108. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  109. rb.useGravity = false;
  110. rb.isKinematic = true;
  111. }
  112. else if (jt == Kinect.JointType.WristRight)
  113. {
  114. jointObj.tag = "WristRight";
  115. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  116. rb.useGravity = false;
  117. rb.isKinematic = true;
  118. }
  119. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  120. //lr.SetVertexCount(2);
  121. lr.positionCount = 2;
  122. //lr.SetWidth(0.05f, 0.05f);
  123. lr.startWidth = 0.05f;
  124. lr.endWidth = 0.05f;
  125. Material whiteDiffuseMat = new Material(Shader.Find("Sprites/Default"));
  126. whiteDiffuseMat.color = new Color(1, 1, 1, 0.5f);
  127. lr.material = whiteDiffuseMat;
  128. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  129. jointObj.name = jt.ToString();
  130. jointObj.transform.parent = body.transform;
  131. }
  132. return body;
  133. }
  134. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  135. {
  136. Vector3[] initJoints = new Vector3[25];
  137. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  138. {
  139. Kinect.Joint sourceJoint = body.Joints[jt];
  140. initJoints[(int)jt] = GetVector3FromJoint(sourceJoint);
  141. }
  142. Vector3[] rescaledJoints = RescaleJoints(initJoints);
  143. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  144. {
  145. Kinect.Joint sourceJoint = body.Joints[jt];
  146. Kinect.Joint? targetJoint = null;
  147. if (_BoneMap.ContainsKey(jt))
  148. {
  149. targetJoint = body.Joints[_BoneMap[jt]];
  150. }
  151. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  152. //jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  153. jointObj.localPosition = rescaledJoints[(int)jt];
  154. if (jt == Kinect.JointType.Head)
  155. {
  156. // Make head joint invisible
  157. jointObj.GetComponent<Renderer>().enabled = false;
  158. }
  159. if (jt == Kinect.JointType.Neck)
  160. {
  161. // Make camera follow the neck
  162. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective)
  163. {
  164. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y, jointObj.localPosition.z - 0.03f);
  165. jointObj.GetComponent<Renderer>().enabled = false;
  166. }
  167. else
  168. {
  169. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y + 0.1f, jointObj.localPosition.z + 0.5f);
  170. }
  171. }
  172. // Make these joints invisible
  173. if (jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  174. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  175. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  176. {
  177. jointObj.GetComponent<Renderer>().enabled = false;
  178. }
  179. // WristLeft and WristRight do something if not following the motions
  180. if ((jt == Kinect.JointType.WristLeft && wristLeftLate) || (jt == Kinect.JointType.WristRight && wristRightLate))
  181. {
  182. if (modeController.feedback == ModeController.Feedback.ColorFeedback)
  183. {
  184. jointObj.GetComponent<Renderer>().material.color = new Color(1, 0, 0);
  185. jointObj.GetComponent<LineRenderer>().startColor = new Color(1, 0, 0);
  186. }
  187. else if (modeController.feedback == ModeController.Feedback.HapticFeedback)
  188. {
  189. // TODO: Add haptic feedback need to test
  190. if (jt == Kinect.JointType.WristLeft)
  191. {
  192. Debug.Log("WristLeft vibrate");
  193. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  194. }
  195. else if (jt == Kinect.JointType.WristRight)
  196. {
  197. Debug.Log("WristRight vibrate");
  198. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  199. }
  200. }
  201. }
  202. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  203. if (targetJoint.HasValue && jt != Kinect.JointType.Neck && jt != Kinect.JointType.ThumbLeft && jt != Kinect.JointType.ThumbRight
  204. && jt != Kinect.JointType.HandLeft && jt != Kinect.JointType.HandRight
  205. && jt != Kinect.JointType.HandTipLeft && jt != Kinect.JointType.HandTipRight) // Do not make LineRenderer for these joints
  206. {
  207. lr.SetPosition(0, jointObj.localPosition);
  208. //lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  209. lr.SetPosition(1, rescaledJoints[(int)_BoneMap[jt]]);
  210. // Coloring the line renderer
  211. //lr.SetColors(GetColorForState(sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  212. //lr.startColor = GetColorForState(sourceJoint.TrackingState);
  213. //lr.endColor = GetColorForState(targetJoint.Value.TrackingState);
  214. }
  215. else
  216. {
  217. lr.enabled = false;
  218. }
  219. // Record joints to PlayerReplay.cs
  220. playerReplay.joints[(int)jt] = jointObj;
  221. }
  222. // Joint orientations
  223. //Kinect.Vector4[] orientation = new Kinect.Vector4[22];
  224. //orientation[0] = body.JointOrientations[Kinect.JointType.Head].Orientation;
  225. //orientation[1] = body.JointOrientations[Kinect.JointType.Neck].Orientation;
  226. //orientation[2] = body.JointOrientations[Kinect.JointType.SpineMid].Orientation;
  227. //orientation[3] = body.JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  228. //orientation[4] = body.JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  229. //orientation[5] = body.JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  230. //orientation[6] = body.JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  231. //orientation[7] = body.JointOrientations[Kinect.JointType.WristLeft].Orientation;
  232. //orientation[8] = body.JointOrientations[Kinect.JointType.WristRight].Orientation;
  233. //orientation[9] = body.JointOrientations[Kinect.JointType.HipLeft].Orientation;
  234. //orientation[10] = body.JointOrientations[Kinect.JointType.HipRight].Orientation;
  235. //orientation[11] = body.JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  236. //orientation[12] = body.JointOrientations[Kinect.JointType.KneeRight].Orientation;
  237. //orientation[13] = body.JointOrientations[Kinect.JointType.SpineBase].Orientation;
  238. //orientation[14] = body.JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  239. //orientation[15] = body.JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  240. //orientation[16] = body.JointOrientations[Kinect.JointType.FootLeft].Orientation;
  241. //orientation[17] = body.JointOrientations[Kinect.JointType.FootRight].Orientation;
  242. //orientation[18] = body.JointOrientations[Kinect.JointType.HandLeft].Orientation;
  243. //orientation[19] = body.JointOrientations[Kinect.JointType.HandRight].Orientation;
  244. //orientation[20] = body.JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  245. //orientation[21] = body.JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  246. //for (int j = 0; j < 22; j++)
  247. //{
  248. // //joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  249. //}
  250. }
  251. private static Color GetColorForState(Kinect.TrackingState state)
  252. {
  253. switch (state)
  254. {
  255. case Kinect.TrackingState.Tracked:
  256. return Color.green;
  257. case Kinect.TrackingState.Inferred:
  258. return Color.red;
  259. default:
  260. return Color.black;
  261. }
  262. }
  263. private Vector3 GetVector3FromJoint(Kinect.Joint joint)
  264. {
  265. //return new Vector3(joint.Position.X * -10, joint.Position.Y * 10, joint.Position.Z * 10);
  266. return new Vector3(joint.Position.X * -1, joint.Position.Y, joint.Position.Z);
  267. }
  268. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  269. {
  270. Quaternion orientationUnity;
  271. orientationUnity.x = -orientation.X;
  272. orientationUnity.y = orientation.Y;
  273. orientationUnity.z = orientation.Z;
  274. orientationUnity.w = orientation.W;
  275. return orientationUnity;
  276. }
  277. private Vector3[] RescaleJoints(Vector3[] initJoints)
  278. {
  279. Vector3[] rescaledJoints = new Vector3[25];
  280. rescaledJoints[(int)Kinect.JointType.SpineBase] = Vector3.zero;
  281. // Lower part
  282. rescaledJoints[(int)Kinect.JointType.HipLeft] = GetTargetJointWithDesiredLength(
  283. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipLeft], 0.08f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  284. rescaledJoints[(int)Kinect.JointType.HipRight] = GetTargetJointWithDesiredLength(
  285. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.HipRight], 0.08f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  286. rescaledJoints[(int)Kinect.JointType.KneeLeft] = GetTargetJointWithDesiredLength(
  287. initJoints[(int)Kinect.JointType.HipLeft], initJoints[(int)Kinect.JointType.KneeLeft], 0.25f) + rescaledJoints[(int)Kinect.JointType.HipLeft];
  288. rescaledJoints[(int)Kinect.JointType.KneeRight] = GetTargetJointWithDesiredLength(
  289. initJoints[(int)Kinect.JointType.HipRight], initJoints[(int)Kinect.JointType.KneeRight], 0.25f) + rescaledJoints[(int)Kinect.JointType.HipRight];
  290. rescaledJoints[(int)Kinect.JointType.AnkleLeft] = GetTargetJointWithDesiredLength(
  291. initJoints[(int)Kinect.JointType.KneeLeft], initJoints[(int)Kinect.JointType.AnkleLeft], 0.3f) + rescaledJoints[(int)Kinect.JointType.KneeLeft];
  292. rescaledJoints[(int)Kinect.JointType.AnkleRight] = GetTargetJointWithDesiredLength(
  293. initJoints[(int)Kinect.JointType.KneeRight], initJoints[(int)Kinect.JointType.AnkleRight], 0.3f) + rescaledJoints[(int)Kinect.JointType.KneeRight];
  294. rescaledJoints[(int)Kinect.JointType.FootLeft] = GetTargetJointWithDesiredLength(
  295. initJoints[(int)Kinect.JointType.AnkleLeft], initJoints[(int)Kinect.JointType.FootLeft], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleLeft];
  296. rescaledJoints[(int)Kinect.JointType.FootRight] = GetTargetJointWithDesiredLength(
  297. initJoints[(int)Kinect.JointType.AnkleRight], initJoints[(int)Kinect.JointType.FootRight], 0.1f) + rescaledJoints[(int)Kinect.JointType.AnkleRight];
  298. // Upper part
  299. rescaledJoints[(int)Kinect.JointType.SpineMid] = GetTargetJointWithDesiredLength(
  300. initJoints[(int)Kinect.JointType.SpineBase], initJoints[(int)Kinect.JointType.SpineMid], 0.2f) + rescaledJoints[(int)Kinect.JointType.SpineBase];
  301. rescaledJoints[(int)Kinect.JointType.SpineShoulder] = GetTargetJointWithDesiredLength(
  302. initJoints[(int)Kinect.JointType.SpineMid], initJoints[(int)Kinect.JointType.SpineShoulder], 0.2f) + rescaledJoints[(int)Kinect.JointType.SpineMid];
  303. rescaledJoints[(int)Kinect.JointType.ShoulderLeft] = GetTargetJointWithDesiredLength(
  304. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderLeft], 0.1f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  305. rescaledJoints[(int)Kinect.JointType.ShoulderRight] = GetTargetJointWithDesiredLength(
  306. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.ShoulderRight], 0.1f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  307. rescaledJoints[(int)Kinect.JointType.ElbowLeft] = GetTargetJointWithDesiredLength(
  308. initJoints[(int)Kinect.JointType.ShoulderLeft], initJoints[(int)Kinect.JointType.ElbowLeft], 0.2f) + rescaledJoints[(int)Kinect.JointType.ShoulderLeft];
  309. rescaledJoints[(int)Kinect.JointType.ElbowRight] = GetTargetJointWithDesiredLength(
  310. initJoints[(int)Kinect.JointType.ShoulderRight], initJoints[(int)Kinect.JointType.ElbowRight], 0.2f) + rescaledJoints[(int)Kinect.JointType.ShoulderRight];
  311. rescaledJoints[(int)Kinect.JointType.WristLeft] = GetTargetJointWithDesiredLength(
  312. initJoints[(int)Kinect.JointType.ElbowLeft], initJoints[(int)Kinect.JointType.WristLeft], 0.2f) + rescaledJoints[(int)Kinect.JointType.ElbowLeft];
  313. rescaledJoints[(int)Kinect.JointType.WristRight] = GetTargetJointWithDesiredLength(
  314. initJoints[(int)Kinect.JointType.ElbowRight], initJoints[(int)Kinect.JointType.WristRight], 0.2f) + rescaledJoints[(int)Kinect.JointType.ElbowRight];
  315. rescaledJoints[(int)Kinect.JointType.Neck] = GetTargetJointWithDesiredLength(
  316. initJoints[(int)Kinect.JointType.SpineShoulder], initJoints[(int)Kinect.JointType.Neck], 0.07f) + rescaledJoints[(int)Kinect.JointType.SpineShoulder];
  317. return rescaledJoints;
  318. }
  319. private Vector3 GetTargetJointWithDesiredLength(Vector3 source, Vector3 target, float desiredLength)
  320. {
  321. Vector3 sourceToTarget = target - source;
  322. float length = sourceToTarget.magnitude;
  323. return sourceToTarget * desiredLength / length;
  324. }
  325. }