BodySourceView.cs 12 KB

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