BodySourceView.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Kinect = Windows.Kinect;
  4. public class BodySourceView : MonoBehaviour
  5. {
  6. public Transform[] joints;
  7. public GameObject BodySourceManager;
  8. public Transform cam;
  9. public PlayerReplay playerReplay;
  10. public ModeController modeController;
  11. public GameObject body;
  12. private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
  13. private BodySourceManager _BodyManager;
  14. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  15. {
  16. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  17. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  18. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  19. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  20. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  21. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  22. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  23. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  24. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  25. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  26. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  27. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  28. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  29. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  30. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  31. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  32. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  33. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  34. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  35. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  36. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  37. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  38. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  39. { Kinect.JointType.Neck, Kinect.JointType.Head },
  40. };
  41. void Update()
  42. {
  43. if (BodySourceManager == null)
  44. {
  45. return;
  46. }
  47. _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
  48. if (_BodyManager == null)
  49. {
  50. return;
  51. }
  52. Kinect.Body[] data = _BodyManager.GetData();
  53. if (data == null)
  54. {
  55. return;
  56. }
  57. List<ulong> trackedIds = new List<ulong>();
  58. foreach (var body in data)
  59. {
  60. if (body == null)
  61. {
  62. continue;
  63. }
  64. if (body.IsTracked)
  65. {
  66. trackedIds.Add(body.TrackingId);
  67. }
  68. }
  69. List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
  70. // First delete untracked bodies
  71. foreach (ulong trackingId in knownIds)
  72. {
  73. if (!trackedIds.Contains(trackingId))
  74. {
  75. Destroy(_Bodies[trackingId]);
  76. _Bodies.Remove(trackingId);
  77. }
  78. }
  79. foreach (var body in data)
  80. {
  81. if (body == null)
  82. {
  83. continue;
  84. }
  85. if (body.IsTracked)
  86. {
  87. if (!_Bodies.ContainsKey(body.TrackingId))
  88. {
  89. _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
  90. }
  91. RefreshBodyObject(body, _Bodies[body.TrackingId]);
  92. }
  93. }
  94. }
  95. private GameObject CreateBodyObject(ulong id)
  96. {
  97. body = new GameObject("Body:" + id);
  98. body.transform.parent = gameObject.transform;
  99. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  100. {
  101. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  102. // Give WristLeft and WristRight rigidbody
  103. if (jt == Kinect.JointType.WristLeft)
  104. {
  105. jointObj.tag = "WristLeft";
  106. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  107. rb.useGravity = false;
  108. rb.isKinematic = true;
  109. }
  110. else if (jt == Kinect.JointType.WristRight)
  111. {
  112. jointObj.tag = "WristRight";
  113. Rigidbody rb = jointObj.AddComponent<Rigidbody>();
  114. rb.useGravity = false;
  115. rb.isKinematic = true;
  116. }
  117. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  118. //lr.SetVertexCount(2);
  119. lr.positionCount = 2;
  120. //lr.SetWidth(0.05f, 0.05f);
  121. lr.startWidth = 0.05f;
  122. lr.endWidth = 0.05f;
  123. Material whiteDiffuseMat = new Material(Shader.Find("Sprites/Default"));
  124. whiteDiffuseMat.color = new Color(1, 1, 1, 0.5f);
  125. lr.material = whiteDiffuseMat;
  126. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  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. Vector3[] initJoints = new Vector3[25];
  135. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  136. {
  137. Kinect.Joint sourceJoint = body.Joints[jt];
  138. initJoints[(int)jt] = GetVector3FromJoint(sourceJoint);
  139. }
  140. HelperScript.SetPlayerJoints(initJoints);
  141. Vector3[] rescaledJoints = HelperScript.RescaleJoints(initJoints);
  142. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  143. {
  144. Kinect.Joint sourceJoint = body.Joints[jt];
  145. Kinect.Joint? targetJoint = null;
  146. if (_BoneMap.ContainsKey(jt))
  147. {
  148. targetJoint = body.Joints[_BoneMap[jt]];
  149. }
  150. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  151. //jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  152. jointObj.localPosition = rescaledJoints[(int)jt];
  153. if (jt == Kinect.JointType.Head)
  154. {
  155. // Make head joint invisible
  156. jointObj.GetComponent<Renderer>().enabled = false;
  157. }
  158. if (jt == Kinect.JointType.Neck)
  159. {
  160. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective)
  161. {
  162. cam.position = new Vector3(jointObj.position.x, jointObj.position.y + 0.05f, jointObj.position.z - 0.05f);
  163. jointObj.GetComponent<Renderer>().enabled = false;
  164. }
  165. else
  166. {
  167. cam.position = new Vector3(jointObj.position.x, jointObj.position.y + 0.1f, jointObj.position.z + 0.7f);
  168. }
  169. }
  170. // Make these joints invisible
  171. if (jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  172. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  173. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  174. {
  175. jointObj.GetComponent<Renderer>().enabled = false;
  176. }
  177. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  178. if (targetJoint.HasValue && jt != Kinect.JointType.Neck && jt != Kinect.JointType.ThumbLeft && jt != Kinect.JointType.ThumbRight
  179. && jt != Kinect.JointType.HandLeft && jt != Kinect.JointType.HandRight
  180. && jt != Kinect.JointType.HandTipLeft && jt != Kinect.JointType.HandTipRight) // Do not make LineRenderer for these joints
  181. {
  182. lr.SetPosition(0, jointObj.localPosition);
  183. //lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  184. lr.SetPosition(1, rescaledJoints[(int)_BoneMap[jt]]);
  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. if (modeController.perspective == ModeController.Perspective.FirstPersonPerspective && jt == Kinect.JointType.SpineShoulder)
  195. {
  196. lr.enabled = false;
  197. }
  198. // Record joints to PlayerReplay.cs
  199. playerReplay.joints[(int)jt] = jointObj;
  200. }
  201. }
  202. private static Color GetColorForState(Kinect.TrackingState state)
  203. {
  204. switch (state)
  205. {
  206. case Kinect.TrackingState.Tracked:
  207. return Color.green;
  208. case Kinect.TrackingState.Inferred:
  209. return Color.red;
  210. default:
  211. return Color.black;
  212. }
  213. }
  214. private Vector3 GetVector3FromJoint(Kinect.Joint joint)
  215. {
  216. //return new Vector3(joint.Position.X * -10, joint.Position.Y * 10, joint.Position.Z * 10);
  217. return new Vector3(joint.Position.X * -1, joint.Position.Y, joint.Position.Z);
  218. }
  219. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  220. {
  221. Quaternion orientationUnity;
  222. orientationUnity.x = -orientation.X;
  223. orientationUnity.y = orientation.Y;
  224. orientationUnity.z = orientation.Z;
  225. orientationUnity.w = orientation.W;
  226. return orientationUnity;
  227. }
  228. }