프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / VariousDemos / Scripts / PoseModelHelper.cs @ 3

이력 | 보기 | 이력해설 | 다운로드 (6.4 KB)

1 3 KTH
using UnityEngine;
2
//using Windows.Kinect;
3
4
using System;
5
using System.Collections;
6
using System.Collections.Generic;
7
using System.Runtime.InteropServices;
8
using System.IO;
9
using System.Text;
10
11
12
/// <summary>
13
/// Pose model helper matches joints to model transforms.
14
/// </summary>
15
[RequireComponent(typeof(Animator))]
16
public class PoseModelHelper : MonoBehaviour
17
{
18
	// Variable to hold all them bones. It will initialize the same size as initialRotations.
19
	private Transform[] bones;
20
21
22
	/// <summary>
23
	/// Gets the number of bone transforms (array length).
24
	/// </summary>
25
	/// <returns>The number of bone transforms.</returns>
26
	public int GetBoneTransformCount()
27
	{
28
		return bones != null ? bones.Length : 0;
29
	}
30
31
	/// <summary>
32
	/// Gets the bone transform by index.
33
	/// </summary>
34
	/// <returns>The bone transform.</returns>
35
	/// <param name="index">Index</param>
36
	public Transform GetBoneTransform(int index)
37
	{
38
		if(index >= 0 && index < bones.Length)
39
		{
40
			return bones[index];
41
		}
42
43
		return null;
44
	}
45
46
	/// <summary>
47
	/// Gets the bone index by joint type.
48
	/// </summary>
49
	/// <returns>The bone index.</returns>
50
	/// <param name="joint">Joint type</param>
51
	/// <param name="bMirrored">If set to <c>true</c> gets the mirrored joint index.</param>
52
	public int GetBoneIndexByJoint(KinectInterop.JointType joint, bool bMirrored)
53
	{
54
		int boneIndex = -1;
55
56
		if(jointMap2boneIndex.ContainsKey(joint))
57
		{
58
			boneIndex = !bMirrored ? jointMap2boneIndex[joint] : mirrorJointMap2boneIndex[joint];
59
		}
60
61
		return boneIndex;
62
	}
63
64
65
	// transform caching gives performance boost since Unity calls GetComponent<Transform>() each time you call transform
66
	private Transform _transformCache;
67
	public new Transform transform
68
	{
69
		get
70
		{
71
			if (!_transformCache)
72
			{
73
				_transformCache = base.transform;
74
			}
75
76
			return _transformCache;
77
		}
78
	}
79
80
81
	public void Awake()
82
    {
83
		// check for double start
84
		if(bones != null)
85
			return;
86
		if(!gameObject.activeInHierarchy)
87
			return;
88
89
		// inits the bones array
90
		bones = new Transform[31];
91
92
		// Map bones to the points the Kinect tracks
93
		MapBones();
94
	}
95
96
	// If the bones to be mapped have been declared, map that bone to the model.
97
	private void MapBones()
98
	{
99
		// get bone transforms from the animator component
100
		Animator animatorComponent = GetComponent<Animator>();
101
102
		for (int boneIndex = 0; boneIndex < bones.Length; boneIndex++)
103
		{
104
			if (!boneIndex2MecanimMap.ContainsKey(boneIndex))
105
				continue;
106
107
			bones[boneIndex] = animatorComponent ? animatorComponent.GetBoneTransform(boneIndex2MecanimMap[boneIndex]) : null;
108
		}
109
	}
110
111
	// dictionaries to speed up bones' processing
112
	// the author of the terrific idea for kinect-joints to mecanim-bones mapping
113
	// along with its initial implementation, including following dictionary is
114
	// Mikhail Korchun (korchoon@gmail.com). Big thanks to this guy!
115
	private readonly Dictionary<int, HumanBodyBones> boneIndex2MecanimMap = new Dictionary<int, HumanBodyBones>
116
	{
117
		{0, HumanBodyBones.Hips},
118
		{1, HumanBodyBones.Spine},
119
        {2, HumanBodyBones.Chest},
120
		{3, HumanBodyBones.Neck},
121
		{4, HumanBodyBones.Head},
122
123
		{5, HumanBodyBones.LeftUpperArm},
124
		{6, HumanBodyBones.LeftLowerArm},
125
		{7, HumanBodyBones.LeftHand},
126
		{8, HumanBodyBones.LeftIndexProximal},
127
		{9, HumanBodyBones.LeftIndexDistal},
128
		{10, HumanBodyBones.LeftThumbProximal},
129
130
		{11, HumanBodyBones.RightUpperArm},
131
		{12, HumanBodyBones.RightLowerArm},
132
		{13, HumanBodyBones.RightHand},
133
		{14, HumanBodyBones.RightIndexProximal},
134
		{15, HumanBodyBones.RightIndexDistal},
135
		{16, HumanBodyBones.RightThumbProximal},
136
137
		{17, HumanBodyBones.LeftUpperLeg},
138
		{18, HumanBodyBones.LeftLowerLeg},
139
		{19, HumanBodyBones.LeftFoot},
140
		{20, HumanBodyBones.LeftToes},
141
142
		{21, HumanBodyBones.RightUpperLeg},
143
		{22, HumanBodyBones.RightLowerLeg},
144
		{23, HumanBodyBones.RightFoot},
145
		{24, HumanBodyBones.RightToes},
146
	};
147
148
	private readonly Dictionary<KinectInterop.JointType, int> jointMap2boneIndex = new Dictionary<KinectInterop.JointType, int>
149
	{
150
		{KinectInterop.JointType.SpineBase, 0},
151
		{KinectInterop.JointType.SpineMid, 1},
152
		{KinectInterop.JointType.SpineShoulder, 2},
153
		{KinectInterop.JointType.Neck, 3},
154
		{KinectInterop.JointType.Head, 4},
155
156
		{KinectInterop.JointType.ShoulderLeft, 5},
157
		{KinectInterop.JointType.ElbowLeft, 6},
158
		{KinectInterop.JointType.WristLeft, 7},
159
		{KinectInterop.JointType.HandLeft, 8},
160
161
		{KinectInterop.JointType.HandTipLeft, 9},
162
		{KinectInterop.JointType.ThumbLeft, 10},
163
164
		{KinectInterop.JointType.ShoulderRight, 11},
165
		{KinectInterop.JointType.ElbowRight, 12},
166
		{KinectInterop.JointType.WristRight, 13},
167
		{KinectInterop.JointType.HandRight, 14},
168
169
		{KinectInterop.JointType.HandTipRight, 15},
170
		{KinectInterop.JointType.ThumbRight, 16},
171
172
		{KinectInterop.JointType.HipLeft, 17},
173
		{KinectInterop.JointType.KneeLeft, 18},
174
		{KinectInterop.JointType.AnkleLeft, 19},
175
		{KinectInterop.JointType.FootLeft, 20},
176
177
		{KinectInterop.JointType.HipRight, 21},
178
		{KinectInterop.JointType.KneeRight, 22},
179
		{KinectInterop.JointType.AnkleRight, 23},
180
		{KinectInterop.JointType.FootRight, 24},
181
	};
182
183
	private readonly Dictionary<KinectInterop.JointType, int> mirrorJointMap2boneIndex = new Dictionary<KinectInterop.JointType, int>
184
	{
185
		{KinectInterop.JointType.SpineBase, 0},
186
		{KinectInterop.JointType.SpineMid, 1},
187
		{KinectInterop.JointType.SpineShoulder, 2},
188
		{KinectInterop.JointType.Neck, 3},
189
		{KinectInterop.JointType.Head, 4},
190
191
		{KinectInterop.JointType.ShoulderRight, 5},
192
		{KinectInterop.JointType.ElbowRight, 6},
193
		{KinectInterop.JointType.WristRight, 7},
194
		{KinectInterop.JointType.HandRight, 8},
195
196
		{KinectInterop.JointType.HandTipRight, 9},
197
		{KinectInterop.JointType.ThumbRight, 10},
198
199
		{KinectInterop.JointType.ShoulderLeft, 11},
200
		{KinectInterop.JointType.ElbowLeft, 12},
201
		{KinectInterop.JointType.WristLeft, 13},
202
		{KinectInterop.JointType.HandLeft, 14},
203
204
		{KinectInterop.JointType.HandTipLeft, 15},
205
		{KinectInterop.JointType.ThumbLeft, 16},
206
207
		{KinectInterop.JointType.HipRight, 17},
208
		{KinectInterop.JointType.KneeRight, 18},
209
		{KinectInterop.JointType.AnkleRight, 19},
210
		{KinectInterop.JointType.FootRight, 20},
211
212
		{KinectInterop.JointType.HipLeft, 21},
213
		{KinectInterop.JointType.KneeLeft, 22},
214
		{KinectInterop.JointType.AnkleLeft, 23},
215
		{KinectInterop.JointType.FootLeft, 24},
216
	};
217
218
219
}