BodySourceView.cs 8.5 KB

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