BodyView.cs
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Kinect = Windows.Kinect;
public class BodyView : MonoBehaviour
{
public Material BoneMaterial;
public GameObject BodyManager;
public GameObject Camera;
private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
private BodyManager _BodyManager;
// map out all the bones by the two joints that they will be connected to
private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
{
//Left leg
{ Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
{ Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
{ Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
{ Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
//Right leg
{ Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
{ Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
{ Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
{ Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
//Left arm
{ Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft }, //Need this for HandSates
{ Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
{ Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
{ Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
{ Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
{ Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
//Right arm
{ Kinect.JointType.HandTipRight, Kinect.JointType.HandRight }, //Needthis for Hand State
{ Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
{ Kinect.JointType.HandRight, Kinect.JointType.WristRight },
{ Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
{ Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
{ Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
//Spine and head
{ Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
{ Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
{ Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
{ Kinect.JointType.Neck, Kinect.JointType.Head },
};
// Update is called on each frames
void Update()
{
//we need to store the keys of the tracked bodies in order to generate them
//We check if the KinectManager has data to work with
if (BodyManager == null)
{
return;
}
_BodyManager = BodyManager.GetComponent<BodyManager>();
if (_BodyManager == null)
{
return;
}
//We store the data of the bodies detected
Kinect.Body[] data = _BodyManager.GetData();
if (data == null)
{
return;
}
/////////////////////////////////////////////////////////////////////////////////
//List of the tracked bodies (ulong is a an unsigned integer)
List<ulong> trackedIds = new List<ulong>();
foreach (var body in data)
{
if (body == null)
{
continue;
}
if (body.IsTracked)
{
trackedIds.Add(body.TrackingId); //We add the ID of all the tracked body from the the current frame in the tracked body list
if (!_Bodies.ContainsKey(body.TrackingId))
{
//if the body isn't already in the _Bodies dictionnary, we create a new body object and add it to the dictionnary
_Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId, body);
}
//otherwise the body exists already and we have to refresh it
RefreshBodyObject(body, _Bodies[body.TrackingId]);
}
}
List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
// First delete untracked bodies
foreach (ulong trackingId in knownIds)
{
if (!trackedIds.Contains(trackingId)) //we check every Ids in knownIds and check if the body are still tracked. If it isn't the case we destroy the body
{ //by updating the _Bodies dictionnary
Destroy(_Bodies[trackingId]);
_Bodies.Remove(trackingId);
}
}
}
private GameObject CreateBodyObject(ulong id, Kinect.Body bodyKinect)
{
GameObject body = new GameObject("Body:" + id); //We create a new body object named by the id
//Joint hierarchy flows from the center of the body to the extremities. It will go on each joint
//The value of SpineBase = 0 and the value of the ThumbRight = 24, the maximum
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Sphere); //for each joint we create a gameObject with a sphere
jointObj.GetComponent<Collider>().isTrigger = true;
Kinect.Joint? targetJoint = null;
if (_BoneMap.ContainsKey(jt))
{
targetJoint = bodyKinect.Joints[_BoneMap[jt]]; //parent of the analyzed joint
//{ Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft }, AnkleLeft joint will be the targetJoint of FootLeft joint
}
if (targetJoint != null && jt != Kinect.JointType.Neck) //we add a cylinder only to the joints that have a parent
{
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.GetComponent<Collider>().isTrigger = true;
cylinder.name = jt.ToString() + " bone";
cylinder.transform.parent = jointObj.transform; //we attach the cylinder to the jointObj parent
}
if (jt == Kinect.JointType.Head)
{
jointObj.transform.localScale = new Vector3(0f, 0f, 0f);
}
else
{
jointObj.transform.localScale = new Vector3(1f, 1f, 1f);
}
jointObj.name = jt.ToString();
jointObj.transform.parent = body.transform; //we attach the joint to the body parent
}
return body;
}
private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
{
for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
{
Kinect.Joint sourceJoint = body.Joints[jt]; //we load the data of the kinect joint
Transform jointObj = bodyObject.transform.FindChild(jt.ToString()); //we store the position of the current jt
jointObj.localPosition = GetVector3FromJoint(sourceJoint); //set the local position of each joint, we "scale" the distance of everything by 10
//otherwise we would be represented as a heap of spheres
//we have to check if the current joint has another joint after
if (_BoneMap.ContainsKey(jt) && jt != Kinect.JointType.Neck)
{
Kinect.Joint targetJoint = body.Joints[_BoneMap[jt]]; //parent of the analyzed joint
//{ Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft }, AnkleLeft joint will be the targetJoint of FootLeft joint
Transform targetJointObj = bodyObject.transform.FindChild(body.Joints[_BoneMap[jt]].JointType.ToString());
Transform bone = bodyObject.transform.FindChild(jt.ToString()).FindChild(jt.ToString() + " bone");
//move the cylinder between the sourceJoint and the targetJoint
bone.position = new Vector3((targetJointObj.position.x - jointObj.position.x) / 2 + jointObj.position.x , (targetJointObj.position.y - jointObj.position.y) / 2 + jointObj.position.y, (targetJointObj.position.z - jointObj.position.z) / 2 + jointObj.position.z);
float distance = Vector3.Distance(targetJointObj.position, jointObj.position);
jointObj.LookAt(targetJointObj); //position the direction of the bones to point at their joint target
Vector3 rot = jointObj.rotation.eulerAngles;
rot = new Vector3(rot.x + 90, rot.y, rot.z); //we have to add 90 degrees to the x axis of the bones
jointObj.rotation = Quaternion.Euler(rot);
/*
float AngleX = 0f;
if (targetJointObj.position.y > jointObj.position.y && targetJointObj.position.z > jointObj.position.z)
{
AngleX = (Mathf.Atan(Mathf.Abs((float)targetJointObj.position.z - (float)jointObj.position.z) / Mathf.Abs((float)targetJointObj.position.y - (float)jointObj.position.y))) * 180 / Mathf.PI - 180;
}else if (jointObj.position.z <= targetJointObj.position.z && jointObj.position.y > targetJointObj.position.y)
{
AngleX = (Mathf.Atan(Mathf.Abs((float)targetJointObj.position.z - (float)jointObj.position.z) / Mathf.Abs((float)targetJointObj.position.y - (float)jointObj.position.y))) * 180 / Mathf.PI * -1;
}else if (jointObj.position.z > targetJointObj.position.z && jointObj.position.y > targetJointObj.position.y)
{
AngleX = (Mathf.Atan(Mathf.Abs((float)targetJointObj.position.z - (float)jointObj.position.z) / Mathf.Abs((float)targetJointObj.position.y - (float)jointObj.position.y))) * 180 / Mathf.PI;
}else if (jointObj.position.z > targetJointObj.position.z && jointObj.position.y < targetJointObj.position.y)
{
AngleX = (Mathf.Atan(Mathf.Abs((float)targetJointObj.position.z - (float)jointObj.position.z) / Mathf.Abs((float)targetJointObj.position.y - (float)jointObj.position.y))) * 180 / Mathf.PI * -1 - 180;
}
//Angle Z has a problem when the sourceJoint is under the targetJoint vertically
float AngleZ = 0f;
if (jointObj.position.x < targetJointObj.position.x && jointObj.position.y < targetJointObj.position.y)
{
AngleZ = ((Mathf.Atan(Mathf.Abs((float)(targetJointObj.position.x) - (float)(jointObj.position.x)) / Mathf.Abs((float)(targetJointObj.position.y) - (float)(jointObj.position.y)))) * 180 / Mathf.PI) * -1 - 180;
}else if (jointObj.position.x >= targetJointObj.position.x && jointObj.position.y < targetJointObj.position.y)
{
AngleZ = (Mathf.Atan(Mathf.Abs((float)(targetJointObj.position.x) - (float)(jointObj.position.x)) / Mathf.Abs((float)(targetJointObj.position.y) - (float)(jointObj.position.y)))) * 180 / Mathf.PI - 180;
}else if (jointObj.position.x < targetJointObj.position.x && jointObj.position.y >= targetJointObj.position.y)
{
AngleZ = (Mathf.Atan(Mathf.Abs((float)(targetJointObj.position.x) - (float)(jointObj.position.x)) / Mathf.Abs((float)(targetJointObj.position.y) - (float)(jointObj.position.y)))) * 180 / Mathf.PI;
}else
{
AngleZ = ((Mathf.Atan(Mathf.Abs((float)(jointObj.position.x) - (float)(targetJointObj.position.x)) / Mathf.Abs((float)(jointObj.position.y) - (float)(targetJointObj.position.y)))) * 180 / Mathf.PI) * -1;
}
Vector3 temp = new Vector3(AngleX, 0f, AngleZ);
bone.rotation = Quaternion.Euler(temp);
*/
bone.localScale = new Vector3(0.3f, distance * 0.45f, 0.3f);
}
//move the main camera on the head
if (jt == Kinect.JointType.Head)
{
Camera.transform.position = new Vector3(jointObj.localPosition.x, jointObj.localPosition.y, jointObj.localPosition.z);
}
}
}
private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
{
return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
}
private static Color GetColorForState(Kinect.TrackingState state)
{
switch (state)
{
case Kinect.TrackingState.Tracked:
return Color.green;
case Kinect.TrackingState.Inferred:
return Color.red;
default:
return Color.black;
}
}
}