BodySourceView.cs 8.8 KB

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