프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectScripts / Cubeman / PointmanController.cs @ 3

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

1
using UnityEngine;
2
//using Windows.Kinect;
3

    
4
using System;
5
using System.Collections;
6

    
7
public class PointmanController : MonoBehaviour 
8
{
9
	[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
10
	public int playerIndex = 0;
11

    
12
	[Tooltip("Whether the pointman is allowed to move vertically or not.")]
13
	public bool verticalMovement = true;
14

    
15
	[Tooltip("Whether the pointman is facing the player or not.")]
16
	public bool mirroredMovement = false;
17

    
18
	[Tooltip("Rate at which the pointman will move through the scene.")]
19
	public float moveRate = 1f;
20

    
21
	[Tooltip("Kinect origin position.")]
22
	public Vector3 originPosition;
23

    
24
	[Tooltip("Whether the z-movement is inverted or not.")]
25
	public bool invertedZMovement = false;
26
	
27
	[Tooltip("Camera that may be used to overlay the mesh over the color background.")]
28
	public Camera foregroundCamera;
29

    
30
	public GameObject bodyJoint;
31
	public GameObject skeletonLine;
32

    
33
	private GameObject[] bones;
34
	private GameObject[] lines;
35

    
36
	private Vector3 initialPosition;
37
	private Quaternion initialRotation;
38
	private Vector3 initialPosOffset = Vector3.zero;
39
	private Int64 initialPosUserID = 0;
40
	private bool initialPosSetY = true;
41

    
42
//	private readonly int[] ColliderJoints = { 
43
//		(int)KinectInterop.JointType.Head, 
44
//		(int)KinectInterop.JointType.HandLeft, (int)KinectInterop.JointType.HandRight, 
45
//		(int)KinectInterop.JointType.HandTipLeft, (int)KinectInterop.JointType.HandTipRight,
46
//		(int)KinectInterop.JointType.FootLeft, (int)KinectInterop.JointType.FootRight
47
//	};
48

    
49

    
50
	/// <summary>
51
	/// Gets the bone transform.
52
	/// </summary>
53
	/// <returns>The bone transform.</returns>
54
	/// <param name="index">Index.</param>
55
	public Transform GetBoneTransform(int index)
56
	{
57
		if(bones != null && index >= 0 && index < bones.Length && bones[index] != null)
58
		{
59
			return bones[index].transform;
60
		}
61

    
62
		return null;
63
	}
64

    
65
	
66
	void Start () 
67
	{
68
		//store bones in a list for easier access
69
		bones = new GameObject[KinectInterop.Constants.MaxJointCount];
70
		
71
		// array holding the skeleton lines
72
		lines = new GameObject[bones.Length];
73
		
74
		initialPosition = transform.position;
75
		initialRotation = transform.rotation;
76
		//transform.rotation = Quaternion.identity;
77
	}
78
	
79

    
80
	void Update () 
81
	{
82
		KinectManager manager = KinectManager.Instance;
83
		
84
		// get 1st player
85
		Int64 userID = manager ? manager.GetUserIdByIndex(playerIndex) : 0;
86
		
87
		if(userID <= 0)
88
		{
89
			initialPosUserID = 0;
90
			initialPosOffset = Vector3.zero;
91
			initialPosSetY = true;
92

    
93
			// reset the pointman position and rotation
94
			if(transform.position != initialPosition)
95
			{
96
				transform.position = initialPosition;
97
			}
98
			
99
			if(transform.rotation != initialRotation)
100
			{
101
				transform.rotation = initialRotation;
102
			}
103

    
104
			for(int i = 0; i < bones.Length; i++) 
105
			{
106
				if(bones[i] != null)
107
				{
108
					bones[i].gameObject.SetActive(false);
109
					
110
					bones[i].transform.localPosition = Vector3.zero;
111
					bones[i].transform.localRotation = Quaternion.identity;
112
				}
113

    
114
				if(lines[i] != null)
115
				{
116
					lines[i].gameObject.SetActive(false);
117
				}
118
			}
119

    
120
			return;
121
		}
122
		
123
		// set the position in space
124
		Vector3 posPointMan = GetJointPosition(manager, userID, (int)KinectInterop.JointType.SpineBase);
125

    
126
		Vector3 posPointManWorld = new Vector3(posPointMan.x, posPointMan.y, invertedZMovement ? -posPointMan.z : posPointMan.z) + originPosition;
127
		Vector3 posPointManHips = new Vector3(posPointMan.x, posPointMan.y, !mirroredMovement ? -posPointMan.z : posPointMan.z) + originPosition;
128

    
129
		// store the initial position
130
		if(initialPosUserID != userID)
131
		{
132
			initialPosUserID = userID;
133
			initialPosOffset = posPointManWorld;
134
		}
135
		
136
		if(!verticalMovement && !initialPosSetY)
137
		{
138
			float fFootPosY = 0f;
139
			if(manager.IsJointTracked(userID, (int)KinectInterop.JointType.FootLeft))
140
			{
141
				fFootPosY = GetJointPosition(manager, userID, (int)KinectInterop.JointType.FootLeft).y;
142
			}
143
			else if(manager.IsJointTracked(userID, (int)KinectInterop.JointType.FootRight))
144
			{
145
				fFootPosY = GetJointPosition(manager, userID, (int)KinectInterop.JointType.FootRight).y;
146
			}
147
			
148
			initialPosOffset.y = posPointManWorld.y - (fFootPosY + originPosition.y);
149
			initialPosSetY = true;
150
		}
151

    
152
		Vector3 relPosUser = (posPointManWorld - initialPosOffset);
153
		//relPosUser.z = invertedZMovement ? -relPosUser.z : relPosUser.z;
154

    
155
		transform.position = initialPosOffset + 
156
			(verticalMovement ? relPosUser * moveRate : new Vector3(relPosUser.x, 0, relPosUser.z) * moveRate);
157

    
158
		if(manager.IsJointTracked(userID, (int)KinectInterop.JointType.Head))
159
		{
160
//			float fHeadPosY = GetJointPosition(manager, userID, (int)KinectInterop.JointType.Head).y + originPosition.y;
161
//			float halfHeight = Mathf.Abs(fHeadPosY - posPointManWorld.y);
162
//
163
//			CapsuleCollider collider = GetComponent<CapsuleCollider>();
164
//			if(collider != null)
165
//			{
166
//				collider.height = 2f * halfHeight;
167
//			}
168
		}
169

    
170
		// update the local positions of the bones
171
		for(int i = 0; i < bones.Length; i++) 
172
		{
173
			if(bones[i] == null && bodyJoint != null)
174
			{
175
				bones[i] = Instantiate(bodyJoint) as GameObject;
176
				bones[i].transform.parent = transform;
177
				bones[i].name = ((KinectInterop.JointType)i).ToString();
178

    
179
//				if(Array.IndexOf(ColliderJoints, i) >= 0)
180
//				{
181
//					bones[i].GetComponent<SphereCollider>().radius = 1f;
182
//				}
183
			}
184

    
185
			if(bones[i] != null)
186
			{
187
				int joint = !mirroredMovement ? i : (int)KinectInterop.GetMirrorJoint((KinectInterop.JointType)i);
188
				if(joint < 0)
189
					continue;
190
				
191
				if(manager.IsJointTracked(userID, joint))
192
				{
193
					bones[i].gameObject.SetActive(true);
194
					
195
					Vector3 posJoint = GetJointPosition(manager, userID, joint);
196
					posJoint.z = !mirroredMovement ? -posJoint.z : posJoint.z;
197

    
198
					if (posJoint == Vector3.zero) 
199
					{
200
						bones[i].gameObject.SetActive(false);
201
						if(lines[i] != null)
202
						{
203
							lines[i].gameObject.SetActive(false);
204
						}
205

    
206
						continue;
207
					}
208

    
209
					posJoint += originPosition;
210
					posJoint -= posPointManHips;
211
					
212
					if(mirroredMovement)
213
					{
214
						posJoint.x = -posJoint.x;
215
						posJoint.z = -posJoint.z;
216
					}
217

    
218
					Quaternion rotJoint = manager.GetJointOrientation(userID, joint, !mirroredMovement);
219
					rotJoint = initialRotation * rotJoint;
220
					
221
					bones[i].transform.localPosition = posJoint;
222
					bones[i].transform.rotation = rotJoint;
223
					
224
					if(lines[i] == null && i > 0 && skeletonLine != null) 
225
					{
226
						lines[i] = Instantiate(skeletonLine) as GameObject;
227
						lines[i].transform.parent = transform;
228
						lines[i].name = ((KinectInterop.JointType)i).ToString() + "_Line";
229
					}
230

    
231
					if(lines[i] != null && i > 0)
232
					{
233
						int jParent = (int)manager.GetParentJoint((KinectInterop.JointType)joint);
234

    
235
						if (manager.IsJointTracked (userID, jParent)) 
236
						{
237
							lines[i].gameObject.SetActive (true);
238

    
239
							Vector3 posJoint2 = GetJointPosition (manager, userID, jParent);
240
							posJoint2.z = !mirroredMovement ? -posJoint2.z : posJoint2.z;
241

    
242
							if (posJoint2 == Vector3.zero) 
243
							{
244
								lines[i].gameObject.SetActive (false);
245
								continue;
246
							}
247

    
248
							posJoint2 += originPosition;
249
							posJoint2 -= posPointManHips;
250

    
251
							if (mirroredMovement) 
252
							{
253
								posJoint2.x = -posJoint2.x;
254
								posJoint2.z = -posJoint2.z;
255
							}
256

    
257
							Vector3 dirFromParent = posJoint - posJoint2;
258

    
259
							lines [i].transform.localPosition = posJoint2 + dirFromParent / 2f;
260
							lines [i].transform.up = transform.rotation * dirFromParent.normalized;
261

    
262
							Vector3 lineScale = lines [i].transform.localScale;
263
							lines [i].transform.localScale = new Vector3 (lineScale.x, dirFromParent.magnitude / 2f, lineScale.z);
264
						} 
265
						else 
266
						{
267
							lines[i].gameObject.SetActive(false);
268
						}
269
					}
270

    
271
				}
272
				else
273
				{
274
					if(bones[i] != null)
275
					{
276
						bones[i].gameObject.SetActive(false);
277
					}
278
					
279
					if(lines[i] != null)
280
					{
281
						lines[i].gameObject.SetActive(false);
282
					}
283
				}
284
			}	
285
		}
286
	}
287

    
288

    
289
	// returns the world- or camera-overlay joint position 
290
	private Vector3 GetJointPosition(KinectManager manager, long userID, int iJoint)
291
	{
292
		if (foregroundCamera) 
293
		{
294
			Rect backgroundRect = foregroundCamera.pixelRect;
295
			PortraitBackground portraitBack = PortraitBackground.Instance;
296

    
297
			if (portraitBack && portraitBack.enabled) 
298
			{
299
				backgroundRect = portraitBack.GetBackgroundRect ();
300
			}
301

    
302
			return manager.GetJointPosColorOverlay(userID, iJoint, foregroundCamera, backgroundRect);
303
		} 
304
		else
305
		{
306
			return manager.GetJointPosition(userID, iJoint);
307
		}
308
	}
309

    
310
}