BodySourceView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Kinect = Windows.Kinect;
  5. public class BodySourceView : MonoBehaviour
  6. {
  7. public Transform[] joints;
  8. public Material BoneMaterial;
  9. public GameObject BodySourceManager;
  10. private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
  11. private BodySourceManager _BodyManager;
  12. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  13. {
  14. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  15. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  16. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  17. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  18. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  19. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  20. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  21. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  22. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  23. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  24. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  25. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  26. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  27. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  28. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  29. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  30. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  31. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  32. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  33. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  34. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  35. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  36. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  37. { Kinect.JointType.Neck, Kinect.JointType.Head },
  38. };
  39. void Update ()
  40. {
  41. if (BodySourceManager == null)
  42. {
  43. return;
  44. }
  45. _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
  46. if (_BodyManager == null)
  47. {
  48. return;
  49. }
  50. Kinect.Body[] data = _BodyManager.GetData();
  51. if (data == null)
  52. {
  53. return;
  54. }
  55. List<ulong> trackedIds = new List<ulong>();
  56. foreach(var body in data)
  57. {
  58. if (body == null)
  59. {
  60. continue;
  61. }
  62. if(body.IsTracked)
  63. {
  64. trackedIds.Add (body.TrackingId);
  65. }
  66. }
  67. List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
  68. // First delete untracked bodies
  69. foreach(ulong trackingId in knownIds)
  70. {
  71. if(!trackedIds.Contains(trackingId))
  72. {
  73. Destroy(_Bodies[trackingId]);
  74. _Bodies.Remove(trackingId);
  75. }
  76. }
  77. foreach(var body in data)
  78. {
  79. if (body == null)
  80. {
  81. continue;
  82. }
  83. if(body.IsTracked)
  84. {
  85. if(!_Bodies.ContainsKey(body.TrackingId))
  86. {
  87. _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
  88. }
  89. RefreshBodyObject(body, _Bodies[body.TrackingId]);
  90. }
  91. }
  92. // Joint orientations
  93. Kinect.Vector4[] orientation = new Kinect.Vector4[22];
  94. //for(int i = 0; i < data.Length; i++)
  95. //{
  96. // orientation[0] = data[i].JointOrientations[Kinect.JointType.Head].Orientation;
  97. // orientation[1] = data[i].JointOrientations[Kinect.JointType.Neck].Orientation;
  98. // orientation[2] = data[i].JointOrientations[Kinect.JointType.SpineMid].Orientation;
  99. // orientation[3] = data[i].JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  100. // orientation[4] = data[i].JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  101. // orientation[5] = data[i].JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  102. // orientation[6] = data[i].JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  103. // orientation[7] = data[i].JointOrientations[Kinect.JointType.WristLeft].Orientation;
  104. // orientation[8] = data[i].JointOrientations[Kinect.JointType.WristRight].Orientation;
  105. // orientation[9] = data[i].JointOrientations[Kinect.JointType.HipLeft].Orientation;
  106. // orientation[10] = data[i].JointOrientations[Kinect.JointType.HipRight].Orientation;
  107. // orientation[11] = data[i].JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  108. // orientation[12] = data[i].JointOrientations[Kinect.JointType.KneeRight].Orientation;
  109. // orientation[13] = data[i].JointOrientations[Kinect.JointType.SpineBase].Orientation;
  110. // orientation[14] = data[i].JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  111. // orientation[15] = data[i].JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  112. // orientation[16] = data[i].JointOrientations[Kinect.JointType.FootLeft].Orientation;
  113. // orientation[17] = data[i].JointOrientations[Kinect.JointType.FootRight].Orientation;
  114. // orientation[18] = data[i].JointOrientations[Kinect.JointType.HandLeft].Orientation;
  115. // orientation[19] = data[i].JointOrientations[Kinect.JointType.HandRight].Orientation;
  116. // orientation[20] = data[i].JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  117. // orientation[21] = data[i].JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  118. // for (int j = 0; j < 22; j++)
  119. // {
  120. // joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  121. // }
  122. //}
  123. orientation[0] = data[0].JointOrientations[Kinect.JointType.Head].Orientation;
  124. orientation[1] = data[0].JointOrientations[Kinect.JointType.Neck].Orientation;
  125. orientation[2] = data[0].JointOrientations[Kinect.JointType.SpineMid].Orientation;
  126. orientation[3] = data[0].JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  127. orientation[4] = data[0].JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  128. orientation[5] = data[0].JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  129. orientation[6] = data[0].JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  130. orientation[7] = data[0].JointOrientations[Kinect.JointType.WristLeft].Orientation;
  131. orientation[8] = data[0].JointOrientations[Kinect.JointType.WristRight].Orientation;
  132. orientation[9] = data[0].JointOrientations[Kinect.JointType.HipLeft].Orientation;
  133. orientation[10] = data[0].JointOrientations[Kinect.JointType.HipRight].Orientation;
  134. orientation[11] = data[0].JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  135. orientation[12] = data[0].JointOrientations[Kinect.JointType.KneeRight].Orientation;
  136. orientation[13] = data[0].JointOrientations[Kinect.JointType.SpineBase].Orientation;
  137. orientation[14] = data[0].JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  138. orientation[15] = data[0].JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  139. orientation[16] = data[0].JointOrientations[Kinect.JointType.FootLeft].Orientation;
  140. orientation[17] = data[0].JointOrientations[Kinect.JointType.FootRight].Orientation;
  141. orientation[18] = data[0].JointOrientations[Kinect.JointType.HandLeft].Orientation;
  142. orientation[19] = data[0].JointOrientations[Kinect.JointType.HandRight].Orientation;
  143. orientation[20] = data[0].JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  144. orientation[21] = data[0].JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  145. for (int j = 0; j < 22; j++)
  146. {
  147. joints[j].rotation = ConvertKinectOrientationToUnity(orientation[j]);
  148. }
  149. //print("wrist right: " + joints[8].rotation);
  150. }
  151. private GameObject CreateBodyObject(ulong id)
  152. {
  153. GameObject body = new GameObject("Body:" + id);
  154. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  155. {
  156. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  157. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  158. lr.SetVertexCount(2);
  159. lr.material = BoneMaterial;
  160. lr.SetWidth(0.05f, 0.05f);
  161. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  162. jointObj.name = jt.ToString();
  163. jointObj.transform.parent = body.transform;
  164. }
  165. return body;
  166. }
  167. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  168. {
  169. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  170. {
  171. Kinect.Joint sourceJoint = body.Joints[jt];
  172. Kinect.Joint? targetJoint = null;
  173. if(_BoneMap.ContainsKey(jt))
  174. {
  175. targetJoint = body.Joints[_BoneMap[jt]];
  176. }
  177. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  178. jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  179. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  180. if(targetJoint.HasValue)
  181. {
  182. lr.SetPosition(0, jointObj.localPosition);
  183. lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  184. lr.SetColors(GetColorForState (sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  185. }
  186. else
  187. {
  188. lr.enabled = false;
  189. }
  190. }
  191. }
  192. private static Color GetColorForState(Kinect.TrackingState state)
  193. {
  194. switch (state)
  195. {
  196. case Kinect.TrackingState.Tracked:
  197. return Color.green;
  198. case Kinect.TrackingState.Inferred:
  199. return Color.red;
  200. default:
  201. return Color.black;
  202. }
  203. }
  204. private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
  205. {
  206. return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
  207. }
  208. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  209. {
  210. Quaternion orientationUnity;
  211. orientationUnity.x = orientation.X;
  212. orientationUnity.y = orientation.Y;
  213. orientationUnity.z = orientation.Z;
  214. orientationUnity.w = orientation.W;
  215. return orientationUnity;
  216. }
  217. }