BodySourceView.cs 13 KB

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