BodySourceView.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. orientation[0] = data[0].JointOrientations[Kinect.JointType.Head].Orientation;
  95. orientation[1] = data[0].JointOrientations[Kinect.JointType.Neck].Orientation;
  96. orientation[2] = data[0].JointOrientations[Kinect.JointType.SpineMid].Orientation;
  97. orientation[3] = data[0].JointOrientations[Kinect.JointType.ShoulderLeft].Orientation;
  98. orientation[4] = data[0].JointOrientations[Kinect.JointType.ShoulderRight].Orientation;
  99. orientation[5] = data[0].JointOrientations[Kinect.JointType.ElbowLeft].Orientation;
  100. orientation[6] = data[0].JointOrientations[Kinect.JointType.ElbowRight].Orientation;
  101. orientation[7] = data[0].JointOrientations[Kinect.JointType.WristLeft].Orientation;
  102. orientation[8] = data[0].JointOrientations[Kinect.JointType.WristRight].Orientation;
  103. orientation[9] = data[0].JointOrientations[Kinect.JointType.HipLeft].Orientation;
  104. orientation[10] = data[0].JointOrientations[Kinect.JointType.HipRight].Orientation;
  105. orientation[11] = data[0].JointOrientations[Kinect.JointType.KneeLeft].Orientation;
  106. orientation[12] = data[0].JointOrientations[Kinect.JointType.KneeRight].Orientation;
  107. orientation[13] = data[0].JointOrientations[Kinect.JointType.SpineBase].Orientation;
  108. orientation[14] = data[0].JointOrientations[Kinect.JointType.AnkleLeft].Orientation;
  109. orientation[15] = data[0].JointOrientations[Kinect.JointType.AnkleRight].Orientation;
  110. orientation[16] = data[0].JointOrientations[Kinect.JointType.FootLeft].Orientation;
  111. orientation[17] = data[0].JointOrientations[Kinect.JointType.FootRight].Orientation;
  112. orientation[18] = data[0].JointOrientations[Kinect.JointType.HandLeft].Orientation;
  113. orientation[19] = data[0].JointOrientations[Kinect.JointType.HandRight].Orientation;
  114. orientation[20] = data[0].JointOrientations[Kinect.JointType.ThumbLeft].Orientation;
  115. orientation[21] = data[0].JointOrientations[Kinect.JointType.ThumbRight].Orientation;
  116. for(int i = 0; i < 22; i++)
  117. {
  118. joints[i].rotation = ConvertKinectOrientationToUnity(orientation[i]);
  119. }
  120. }
  121. private GameObject CreateBodyObject(ulong id)
  122. {
  123. GameObject body = new GameObject("Body:" + id);
  124. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  125. {
  126. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  127. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  128. lr.SetVertexCount(2);
  129. lr.material = BoneMaterial;
  130. lr.SetWidth(0.05f, 0.05f);
  131. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  132. jointObj.name = jt.ToString();
  133. jointObj.transform.parent = body.transform;
  134. }
  135. return body;
  136. }
  137. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  138. {
  139. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  140. {
  141. Kinect.Joint sourceJoint = body.Joints[jt];
  142. Kinect.Joint? targetJoint = null;
  143. if(_BoneMap.ContainsKey(jt))
  144. {
  145. targetJoint = body.Joints[_BoneMap[jt]];
  146. }
  147. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  148. jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  149. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  150. if(targetJoint.HasValue)
  151. {
  152. lr.SetPosition(0, jointObj.localPosition);
  153. lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  154. lr.SetColors(GetColorForState (sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  155. }
  156. else
  157. {
  158. lr.enabled = false;
  159. }
  160. }
  161. }
  162. private static Color GetColorForState(Kinect.TrackingState state)
  163. {
  164. switch (state)
  165. {
  166. case Kinect.TrackingState.Tracked:
  167. return Color.green;
  168. case Kinect.TrackingState.Inferred:
  169. return Color.red;
  170. default:
  171. return Color.black;
  172. }
  173. }
  174. private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
  175. {
  176. return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
  177. }
  178. private Quaternion ConvertKinectOrientationToUnity(Kinect.Vector4 orientation)
  179. {
  180. Quaternion orientationUnity;
  181. orientationUnity.x = orientation.X;
  182. orientationUnity.y = orientation.Y;
  183. orientationUnity.z = orientation.Z;
  184. orientationUnity.w = orientation.W;
  185. return orientationUnity;
  186. }
  187. }