BodySourceView.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 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.material = BoneMaterial;
  101. lr.SetWidth(0.05f, 0.05f);
  102. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  103. jointObj.name = jt.ToString();
  104. jointObj.transform.parent = body.transform;
  105. }
  106. return body;
  107. }
  108. private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
  109. {
  110. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  111. {
  112. Kinect.Joint sourceJoint = body.Joints[jt];
  113. Kinect.Joint? targetJoint = null;
  114. if(_BoneMap.ContainsKey(jt))
  115. {
  116. targetJoint = body.Joints[_BoneMap[jt]];
  117. }
  118. Transform jointObj = bodyObject.transform.Find(jt.ToString());
  119. jointObj.localPosition = GetVector3FromJoint(sourceJoint);
  120. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  121. if(targetJoint.HasValue)
  122. {
  123. lr.SetPosition(0, jointObj.localPosition);
  124. lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
  125. lr.SetColors(GetColorForState (sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
  126. }
  127. else
  128. {
  129. lr.enabled = false;
  130. }
  131. }
  132. }
  133. private static Color GetColorForState(Kinect.TrackingState state)
  134. {
  135. switch (state)
  136. {
  137. case Kinect.TrackingState.Tracked:
  138. return Color.green;
  139. case Kinect.TrackingState.Inferred:
  140. return Color.red;
  141. default:
  142. return Color.black;
  143. }
  144. }
  145. private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
  146. {
  147. return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
  148. }
  149. }