BodySourceView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // TODO: change size of line
  124. //lr.SetWidth(0.05f, 0.05f);
  125. lr.startWidth = 0.05f;
  126. lr.endWidth = 0.05f;
  127. Material whiteDiffuseMat = new Material(Shader.Find("Unlit/Texture"));
  128. lr.material = whiteDiffuseMat;
  129. // TODO: change size of cube
  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. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  139. {
  140. Kinect.Joint sourceJoint = body.Joints[jt];
  141. Kinect.Joint? targetJoint = null;
  142. if (_BoneMap.ContainsKey(jt))
  143. {
  144. targetJoint = body.Joints[_BoneMap[jt]];
  145. }
  146. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  147. jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  148. if (jt == Kinect.JointType.Head)
  149. {
  150. // Make head joint invisible
  151. jointObj.GetComponent<Renderer>().enabled = false;
  152. // Make camera follow the head
  153. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective)
  154. {
  155. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y - 1.4f, jointObj.localPosition.z - 0.3f);
  156. } else
  157. {
  158. cameraRig.localPosition = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y - 1.4f, jointObj.localPosition.z + 0.3f);
  159. }
  160. }
  161. // Make these joints invisible
  162. if (jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  163. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  164. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  165. {
  166. jointObj.GetComponent<Renderer>().enabled = false;
  167. }
  168. // WristLeft and WristRight do something if not following the motions
  169. if ((jt == Kinect.JointType.WristLeft && wristLeftLate) || (jt == Kinect.JointType.WristRight && wristRightLate))
  170. {
  171. if (modeController.feedback == ModeController.Feedback.ColorFeedback)
  172. {
  173. // Change to red color
  174. jointObj.GetComponent<Renderer>().material.color = new Color(1, 0, 0);
  175. } else if (modeController.feedback == ModeController.Feedback.HapticFeedback)
  176. {
  177. // TODO: Add haptic feedback need to test
  178. if (jt == Kinect.JointType.WristLeft)
  179. {
  180. Debug.Log("WristLeft vibrate");
  181. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.LeftHand].Execute(0, 0.01f, 10, 1);
  182. } else if (jt == Kinect.JointType.WristRight)
  183. {
  184. Debug.Log("WristRight vibrate");
  185. SteamVR_Actions.default_Haptic[SteamVR_Input_Sources.RightHand].Execute(0, 0.01f, 10, 1);
  186. }
  187. }
  188. }
  189. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  190. if (targetJoint.HasValue && jt != Kinect.JointType.Neck && jt != Kinect.JointType.ThumbLeft && jt != Kinect.JointType.ThumbRight
  191. && jt != Kinect.JointType.HandLeft && jt != Kinect.JointType.HandRight
  192. && jt != Kinect.JointType.HandTipLeft && jt != Kinect.JointType.HandTipRight) // Do not make LineRenderer for these joints
  193. {
  194. lr.SetPosition(0, jointObj.localPosition);
  195. lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  196. // Coloring the line renderer
  197. //lr.SetColors(GetColorForState(sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  198. //lr.startColor = GetColorForState(sourceJoint.TrackingState);
  199. //lr.endColor = GetColorForState(targetJoint.Value.TrackingState);
  200. }
  201. else
  202. {
  203. lr.enabled = false;
  204. }
  205. // Record joints to PlayerReplay.cs
  206. playerReplay.joints[(int)jt] = jointObj;
  207. }
  208. // Joint orientations
  209. //Kinect.Vector4[] orientation = new Kinect.Vector4[22];
  210. //orientation[0] = body.JointOrientations[Kinect.JointType.Head].Orientation;
  211. //orientation[1] = body.JointOrientations[Kinect.JointType.Neck].Orientation;
  212. //orientation[2] = body.JointOrientations[Kinect.JointType.SpineMid].Orientation;
  213. //orientation[3] = body.JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  214. //orientation[4] = body.JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  215. //orientation[5] = body.JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  216. //orientation[6] = body.JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  217. //orientation[7] = body.JointOrientations[Kinect.JointType.WristLeft].Orientation;
  218. //orientation[8] = body.JointOrientations[Kinect.JointType.WristRight].Orientation;
  219. //orientation[9] = body.JointOrientations[Kinect.JointType.HipLeft].Orientation;
  220. //orientation[10] = body.JointOrientations[Kinect.JointType.HipRight].Orientation;
  221. //orientation[11] = body.JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  222. //orientation[12] = body.JointOrientations[Kinect.JointType.KneeRight].Orientation;
  223. //orientation[13] = body.JointOrientations[Kinect.JointType.SpineBase].Orientation;
  224. //orientation[14] = body.JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  225. //orientation[15] = body.JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  226. //orientation[16] = body.JointOrientations[Kinect.JointType.FootLeft].Orientation;
  227. //orientation[17] = body.JointOrientations[Kinect.JointType.FootRight].Orientation;
  228. //orientation[18] = body.JointOrientations[Kinect.JointType.HandLeft].Orientation;
  229. //orientation[19] = body.JointOrientations[Kinect.JointType.HandRight].Orientation;
  230. //orientation[20] = body.JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  231. //orientation[21] = body.JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  232. //for (int j = 0; j < 22; j++)
  233. //{
  234. // //joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  235. //}
  236. }
  237. private static Color GetColorForState(Kinect.TrackingState state)
  238. {
  239. switch (state)
  240. {
  241. case Kinect.TrackingState.Tracked:
  242. return Color.green;
  243. case Kinect.TrackingState.Inferred:
  244. return Color.red;
  245. default:
  246. return Color.black;
  247. }
  248. }
  249. private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
  250. {
  251. //return new Vector3(joint.Position.X * -10, joint.Position.Y * 10, joint.Position.Z * 10);
  252. // TODO: change offset to be more realistic
  253. if (joint.JointType == Kinect.JointType.ShoulderLeft)
  254. {
  255. return new Vector3(joint.Position.X * -1 + 0.1f, joint.Position.Y * 1, joint.Position.Z * 1);
  256. } else if (joint.JointType == Kinect.JointType.ShoulderRight)
  257. {
  258. return new Vector3(joint.Position.X * -1 - 0.1f, joint.Position.Y * 1, joint.Position.Z * 1);
  259. }
  260. return new Vector3(joint.Position.X * -1, joint.Position.Y * 1, joint.Position.Z * 1);
  261. }
  262. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  263. {
  264. Quaternion orientationUnity;
  265. orientationUnity.x = -orientation.X;
  266. orientationUnity.y = orientation.Y;
  267. orientationUnity.z = orientation.Z;
  268. orientationUnity.w = orientation.W;
  269. return orientationUnity;
  270. }
  271. }