CharacterSkeleton.cs
13.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Kinect = Windows.Kinect;
class CharacterSkeleton
{
public const int
// JointType
JointType_SpineBase = 0,
JointType_SpineMid = 1,
JointType_Neck = 2,
JointType_Head = 3,
JointType_ShoulderLeft = 4,
JointType_ElbowLeft = 5,
JointType_WristLeft = 6,
JointType_HandLeft = 7,
JointType_ShoulderRight = 8,
JointType_ElbowRight = 9,
JointType_WristRight = 10,
JointType_HandRight = 11,
JointType_HipLeft = 12,
JointType_KneeLeft = 13,
JointType_AnkleLeft = 14,
JointType_FootLeft = 15,
JointType_HipRight = 16,
JointType_KneeRight = 17,
JointType_AnkleRight = 18,
JointType_FootRight = 19,
JointType_SpineShoulder = 20,
JointType_HandTipLeft = 21,
JointType_ThumbLeft = 22,
JointType_HandTipRight = 23,
JointType_ThumbRight = 24,
// TrackingState
TrackingState_NotTracked = 0,
TrackingState_Inferred = 1,
TrackingState_Tracked = 2,
// Number
bodyCount = 6,
jointCount = 25;
private static int[] jointSegment = new int[] {
JointType_SpineBase, JointType_SpineMid, // Spine
JointType_Neck, JointType_Head, // Neck
// left
JointType_ShoulderLeft, JointType_ElbowLeft, // LeftUpperArm
JointType_ElbowLeft, JointType_WristLeft, // LeftLowerArm
JointType_WristLeft, JointType_HandLeft, // LeftHand
JointType_HipLeft, JointType_KneeLeft, // LeftUpperLeg
JointType_KneeLeft, JointType_AnkleLeft, // LeftLowerLeg6
JointType_AnkleLeft, JointType_FootLeft, // LeftFoot
// right
JointType_ShoulderRight, JointType_ElbowRight, // RightUpperArm
JointType_ElbowRight, JointType_WristRight, // RightLowerArm
JointType_WristRight, JointType_HandRight, // RightHand
JointType_HipRight, JointType_KneeRight, // RightUpperLeg
JointType_KneeRight, JointType_AnkleRight, // RightLowerLeg
JointType_AnkleRight, JointType_FootRight, // RightFoot
};
public Vector3[] joint = new Vector3[jointCount];
public int[] jointState = new int[jointCount];
Dictionary<HumanBodyBones, Vector3> trackingSegment = null;
Dictionary<HumanBodyBones, int> trackingState = null;
private static HumanBodyBones[] humanBone = new HumanBodyBones[] {
HumanBodyBones.Hips,
HumanBodyBones.Spine,
HumanBodyBones.UpperChest,
HumanBodyBones.Neck,
HumanBodyBones.Head,
HumanBodyBones.LeftUpperArm,
HumanBodyBones.LeftLowerArm,
HumanBodyBones.LeftHand,
HumanBodyBones.LeftUpperLeg,
HumanBodyBones.LeftLowerLeg,
HumanBodyBones.LeftFoot,
HumanBodyBones.RightUpperArm,
HumanBodyBones.RightLowerArm,
HumanBodyBones.RightHand,
HumanBodyBones.RightUpperLeg,
HumanBodyBones.RightLowerLeg,
HumanBodyBones.RightFoot,
};
private static HumanBodyBones[] targetBone = new HumanBodyBones[] {
HumanBodyBones.Spine,
HumanBodyBones.Neck,
HumanBodyBones.LeftUpperArm,
HumanBodyBones.LeftLowerArm,
HumanBodyBones.LeftHand,
HumanBodyBones.LeftUpperLeg,
HumanBodyBones.LeftLowerLeg,
HumanBodyBones.LeftFoot,
HumanBodyBones.RightUpperArm,
HumanBodyBones.RightLowerArm,
HumanBodyBones.RightHand,
HumanBodyBones.RightUpperLeg,
HumanBodyBones.RightLowerLeg,
HumanBodyBones.RightFoot,
};
public GameObject humanoid;
private Dictionary<HumanBodyBones, RigBone> rigBone = null;
private bool isSavedPosition = false;
private Vector3 savedPosition;
private Quaternion savedHumanoidRotation;
public CharacterSkeleton(GameObject h)
{
humanoid = h;
rigBone = new Dictionary<HumanBodyBones, RigBone>();
foreach (HumanBodyBones bone in humanBone)
{
rigBone[bone] = new RigBone(humanoid, bone);
}
savedHumanoidRotation = humanoid.transform.rotation;
trackingSegment = new Dictionary<HumanBodyBones, Vector3>(targetBone.Length);
trackingState = new Dictionary<HumanBodyBones, int>(targetBone.Length);
}
private void swapJoint(int a, int b)
{
Vector3 tmp = joint[a]; joint[a] = joint[b]; joint[b] = tmp;
int t = jointState[a]; jointState[a] = jointState[b]; jointState[b] = t;
}
public void set(float[] jt, int[] st, int offset, bool mirrored, bool move)
{
if (isSavedPosition == false && jointState[JointType_SpineBase] != TrackingState_NotTracked)
{
isSavedPosition = true;
int j = offset * jointCount + JointType_SpineBase;
savedPosition = new Vector3(jt[j * 3], jt[j * 3 + 1], jt[j * 3 + 2]);
}
for (int i = 0; i < jointCount; i++)
{
int j = offset * jointCount + i;
if (mirrored)
{
joint[i] = new Vector3(-jt[j * 3], jt[j * 3 + 1], -jt[j * 3 + 2]);
}
else
{
joint[i] = new Vector3(jt[j * 3], jt[j * 3 + 1], savedPosition.z * 2 - jt[j * 3 + 2]);
}
jointState[i] = st[j];
}
if (mirrored)
{
swapJoint(JointType_ShoulderLeft, JointType_ShoulderRight);
swapJoint(JointType_ElbowLeft, JointType_ElbowRight);
swapJoint(JointType_WristLeft, JointType_WristRight);
swapJoint(JointType_HandLeft, JointType_HandRight);
swapJoint(JointType_HipLeft, JointType_HipRight);
swapJoint(JointType_KneeLeft, JointType_KneeRight);
swapJoint(JointType_AnkleLeft, JointType_AnkleRight);
swapJoint(JointType_FootLeft, JointType_FootRight);
swapJoint(JointType_HandTipLeft, JointType_HandTipRight);
swapJoint(JointType_ThumbLeft, JointType_ThumbRight);
}
for (int i = 0; i < targetBone.Length; i++)
{
int s = jointSegment[2 * i], e = jointSegment[2 * i + 1];
trackingSegment[targetBone[i]] = joint[e] - joint[s];
trackingState[targetBone[i]] = System.Math.Min(jointState[e], jointState[s]);
}
Vector3 waist = joint[JointType_HipRight] - joint[JointType_HipLeft];
waist = new Vector3(waist.x, 0, waist.z);
Quaternion rot = Quaternion.FromToRotation(Vector3.right, waist);
Quaternion rotInv = Quaternion.Inverse(rot);
Vector3 shoulder = joint[JointType_ShoulderRight] - joint[JointType_ShoulderLeft];
shoulder = new Vector3(shoulder.x, 0, shoulder.z);
Quaternion srot = Quaternion.FromToRotation(Vector3.right, shoulder);
Quaternion srotInv = Quaternion.Inverse(srot);
humanoid.transform.rotation = Quaternion.identity;
foreach (HumanBodyBones bone in targetBone)
{
rigBone[bone].transform.rotation = rotInv * Quaternion.FromToRotation(Vector3.up, trackingSegment[bone]);
}
rigBone[HumanBodyBones.UpperChest].offset(srot);
Quaternion bodyRot = rot;
if (mirrored)
{
bodyRot = Quaternion.AngleAxis(180, Vector3.up) * bodyRot;
}
humanoid.transform.rotation = bodyRot;
if (move == true)
{
Vector3 m = joint[JointType_SpineBase];
if (mirrored) m = new Vector3(-m.x, m.y, -m.z);
humanoid.transform.position = m;
}
}
public void trainee_set(float[] jt, int[] st, int offset, bool mirrored, bool move, Kinect.Joint sourceJoint, Transform jointObj)
{
if (isSavedPosition == false && jointState[JointType_SpineBase] != TrackingState_NotTracked)
{
isSavedPosition = true;
int j = offset * jointCount + JointType_SpineBase;
savedPosition = new Vector3(jt[j * 3], jt[j * 3 + 1], jt[j * 3 + 2]);
}
for (int i = 0; i < jointCount; i++)
{
int j = offset * jointCount + i;
if (mirrored)
{
joint[i] = new Vector3(-jt[j * 3], jt[j * 3 + 1], -jt[j * 3 + 2]);
}
else
{
joint[i] = new Vector3(jt[j * 3], jt[j * 3 + 1], savedPosition.z * 2 - jt[j * 3 + 2]);
}
jointState[i] = st[j];
}
if (mirrored)
{
swapJoint(JointType_ShoulderLeft, JointType_ShoulderRight);
swapJoint(JointType_ElbowLeft, JointType_ElbowRight);
swapJoint(JointType_WristLeft, JointType_WristRight);
swapJoint(JointType_HandLeft, JointType_HandRight);
swapJoint(JointType_HipLeft, JointType_HipRight);
swapJoint(JointType_KneeLeft, JointType_KneeRight);
swapJoint(JointType_AnkleLeft, JointType_AnkleRight);
swapJoint(JointType_FootLeft, JointType_FootRight);
swapJoint(JointType_HandTipLeft, JointType_HandTipRight);
swapJoint(JointType_ThumbLeft, JointType_ThumbRight);
}
for (int i = 0; i < targetBone.Length; i++)
{
int s = jointSegment[2 * i], e = jointSegment[2 * i + 1];
trackingSegment[targetBone[i]] = joint[e] - joint[s];
trackingState[targetBone[i]] = System.Math.Min(jointState[e], jointState[s]);
}
Vector3 waist = joint[JointType_HipRight] - joint[JointType_HipLeft];
waist = new Vector3(waist.x, 0, waist.z);
Quaternion rot = Quaternion.FromToRotation(Vector3.right, waist);
Quaternion rotInv = Quaternion.Inverse(rot);
Vector3 shoulder = joint[JointType_ShoulderRight] - joint[JointType_ShoulderLeft];
shoulder = new Vector3(shoulder.x, 0, shoulder.z);
Quaternion srot = Quaternion.FromToRotation(Vector3.right, shoulder);
Quaternion srotInv = Quaternion.Inverse(srot);
humanoid.transform.rotation = Quaternion.identity;
foreach (HumanBodyBones bone in targetBone)
{
rigBone[bone].transform.rotation = rotInv * Quaternion.FromToRotation(Vector3.up, trackingSegment[bone]);
}
rigBone[HumanBodyBones.UpperChest].offset(srot);
Quaternion bodyRot = rot;
if (mirrored)
{
bodyRot = Quaternion.AngleAxis(180, Vector3.up) * bodyRot;
}
humanoid.transform.rotation = bodyRot;
if (move == true)
{
Vector3 m = joint[JointType_SpineBase];
if (mirrored) m = new Vector3((float)0.9, m.y, 0);
//if (mirrored) m = new Vector3(-m.x, m.y, -m.z);
humanoid.transform.position = m;
}
}
public void trainer_set(float[] jt, int[] st, int offset, bool mirrored, bool move)
{
if (isSavedPosition == false && jointState[JointType_SpineBase] != TrackingState_NotTracked)
{
isSavedPosition = true;
int j = offset * jointCount + JointType_SpineBase;
savedPosition = new Vector3(jt[j * 3], jt[j * 3 + 1], jt[j * 3 + 2]);
}
for (int i = 0; i < jointCount; i++)
{
int j = offset * jointCount + i;
if (mirrored)
{
joint[i] = new Vector3(-jt[j * 3], jt[j * 3 + 1], -jt[j * 3 + 2]);
}
else
{
joint[i] = new Vector3(jt[j * 3], jt[j * 3 + 1], savedPosition.z * 2 - jt[j * 3 + 2]);
}
jointState[i] = st[j];
}
if (mirrored)
{
swapJoint(JointType_ShoulderLeft, JointType_ShoulderRight);
swapJoint(JointType_ElbowLeft, JointType_ElbowRight);
swapJoint(JointType_WristLeft, JointType_WristRight);
swapJoint(JointType_HandLeft, JointType_HandRight);
swapJoint(JointType_HipLeft, JointType_HipRight);
swapJoint(JointType_KneeLeft, JointType_KneeRight);
swapJoint(JointType_AnkleLeft, JointType_AnkleRight);
swapJoint(JointType_FootLeft, JointType_FootRight);
swapJoint(JointType_HandTipLeft, JointType_HandTipRight);
swapJoint(JointType_ThumbLeft, JointType_ThumbRight);
}
for (int i = 0; i < targetBone.Length; i++)
{
int s = jointSegment[2 * i], e = jointSegment[2 * i + 1];
trackingSegment[targetBone[i]] = joint[e] - joint[s];
trackingState[targetBone[i]] = System.Math.Min(jointState[e], jointState[s]);
}
Vector3 waist = joint[JointType_HipRight] - joint[JointType_HipLeft];
waist = new Vector3(waist.x, 0, waist.z);
Quaternion rot = Quaternion.FromToRotation(Vector3.right, waist);
Quaternion rotInv = Quaternion.Inverse(rot);
Vector3 shoulder = joint[JointType_ShoulderRight] - joint[JointType_ShoulderLeft];
shoulder = new Vector3(shoulder.x, 0, shoulder.z);
Quaternion srot = Quaternion.FromToRotation(Vector3.right, shoulder);
Quaternion srotInv = Quaternion.Inverse(srot);
humanoid.transform.rotation = Quaternion.identity;
foreach (HumanBodyBones bone in targetBone)
{
rigBone[bone].transform.rotation = rotInv * Quaternion.FromToRotation(Vector3.up, trackingSegment[bone]);
}
rigBone[HumanBodyBones.UpperChest].offset(srot);
Quaternion bodyRot = rot;
if (mirrored)
{
bodyRot = Quaternion.AngleAxis(180, Vector3.up) * bodyRot;
}
humanoid.transform.rotation = bodyRot;
if (move == true)
{
Vector3 m = joint[JointType_SpineBase];
if (mirrored) m = new Vector3((float)-0.9,m.y, (float)0.4);
humanoid.transform.position = m;
}
}
}