프로젝트

일반

사용자정보

통계
| 개정판:

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

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

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

    
4
using System;
5
using System.Collections;
6

    
7
public class CubemanController : 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 cubeman is allowed to move vertically or not.")]
13
	public bool verticalMovement = true;
14

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

    
18
	[Tooltip("Rate at which the cubeman will move through the scene.")]
19
	public float moveRate = 1f;
20
	
21
	//public GameObject debugText;
22
	
23
	public GameObject Hip_Center;
24
	public GameObject Spine;
25
	public GameObject Neck;
26
	public GameObject Head;
27
	public GameObject Shoulder_Left;
28
	public GameObject Elbow_Left;
29
	public GameObject Wrist_Left;
30
	public GameObject Hand_Left;
31
	public GameObject Shoulder_Right;
32
	public GameObject Elbow_Right;
33
	public GameObject Wrist_Right;
34
	public GameObject Hand_Right;
35
	public GameObject Hip_Left;
36
	public GameObject Knee_Left;
37
	public GameObject Ankle_Left;
38
	public GameObject Foot_Left;
39
	public GameObject Hip_Right;
40
	public GameObject Knee_Right;
41
	public GameObject Ankle_Right;
42
	public GameObject Foot_Right;
43
	public GameObject Spine_Shoulder;
44
    public GameObject Hand_Tip_Left;
45
    public GameObject Thumb_Left;
46
    public GameObject Hand_Tip_Right;
47
    public GameObject Thumb_Right;
48
	
49
	public LineRenderer skeletonLine;
50
	public LineRenderer debugLine;
51

    
52
	private GameObject[] bones;
53
	private LineRenderer[] lines;
54

    
55
	private LineRenderer lineTLeft;
56
	private LineRenderer lineTRight;
57
	private LineRenderer lineFLeft;
58
	private LineRenderer lineFRight;
59

    
60
	private Vector3 initialPosition;
61
	private Quaternion initialRotation;
62
	private Vector3 initialPosOffset = Vector3.zero;
63
	private Int64 initialPosUserID = 0;
64
	
65
	
66
	void Start () 
67
	{
68
		//store bones in a list for easier access
69
		bones = new GameObject[] {
70
			Hip_Center,
71
            Spine,
72
            Neck,
73
            Head,
74
            Shoulder_Left,
75
            Elbow_Left,
76
            Wrist_Left,
77
            Hand_Left,
78
            Shoulder_Right,
79
            Elbow_Right,
80
            Wrist_Right,
81
            Hand_Right,
82
            Hip_Left,
83
            Knee_Left,
84
            Ankle_Left,
85
            Foot_Left,
86
            Hip_Right,
87
            Knee_Right,
88
            Ankle_Right,
89
            Foot_Right,
90
            Spine_Shoulder,
91
            Hand_Tip_Left,
92
            Thumb_Left,
93
            Hand_Tip_Right,
94
            Thumb_Right
95
		};
96
		
97
		// array holding the skeleton lines
98
		lines = new LineRenderer[bones.Length];
99
		
100
//		if(skeletonLine)
101
//		{
102
//			for(int i = 0; i < lines.Length; i++)
103
//			{
104
//				Debug.Log ("Line: " + i + " instantiate started.");
105
//
106
//				if((i == 22 || i == 24) && debugLine)
107
//					lines[i] = Instantiate(debugLine) as LineRenderer;
108
//				else
109
//					lines[i] = Instantiate(skeletonLine) as LineRenderer;
110
//
111
//				lines[i].transform.parent = transform;
112
//			}
113
//		}
114

    
115
		initialPosition = transform.position;
116
		initialRotation = transform.rotation;
117
		//transform.rotation = Quaternion.identity;
118
	}
119
	
120

    
121
	void Update () 
122
	{
123
		KinectManager manager = KinectManager.Instance;
124
		
125
		// get 1st player
126
		Int64 userID = manager ? manager.GetUserIdByIndex(playerIndex) : 0;
127
		
128
		if(userID <= 0)
129
		{
130
			initialPosUserID = 0;
131
			initialPosOffset = Vector3.zero;
132

    
133
			// reset the pointman position and rotation
134
			if(transform.position != initialPosition)
135
			{
136
				transform.position = initialPosition;
137
			}
138
			
139
			if(transform.rotation != initialRotation)
140
			{
141
				transform.rotation = initialRotation;
142
			}
143

    
144
			for(int i = 0; i < bones.Length; i++) 
145
			{
146
				bones[i].gameObject.SetActive(true);
147

    
148
				bones[i].transform.localPosition = Vector3.zero;
149
				bones[i].transform.localRotation = Quaternion.identity;
150
				
151
				if(lines[i] != null)
152
				{
153
					lines[i].gameObject.SetActive(false);
154
				}
155
			}
156

    
157
			return;
158
		}
159
		
160
		// set the position in space
161
		Vector3 posPointMan = manager.GetUserPosition(userID);
162
		Vector3 posPointManMP = new Vector3(posPointMan.x, posPointMan.y, !mirroredMovement ? -posPointMan.z : posPointMan.z);
163
		
164
		// store the initial position
165
		if(initialPosUserID != userID)
166
		{
167
			initialPosUserID = userID;
168
			//initialPosOffset = transform.position - (verticalMovement ? posPointMan * moveRate : new Vector3(posPointMan.x, 0, posPointMan.z) * moveRate);
169
			initialPosOffset = posPointMan;
170
		}
171

    
172
		Vector3 relPosUser = (posPointMan - initialPosOffset);
173
		relPosUser.z =!mirroredMovement ? -relPosUser.z : relPosUser.z;
174

    
175
		transform.position = initialPosOffset + 
176
			(verticalMovement ? relPosUser * moveRate : new Vector3(relPosUser.x, 0, relPosUser.z) * moveRate);
177
		
178
		// update the local positions of the bones
179
		for(int i = 0; i < bones.Length; i++) 
180
		{
181
			if(bones[i] != null)
182
			{
183
				int joint = !mirroredMovement ? i : (int)KinectInterop.GetMirrorJoint((KinectInterop.JointType)i);
184
				if(joint < 0)
185
					continue;
186
				
187
				if(manager.IsJointTracked(userID, joint))
188
				{
189
					bones[i].gameObject.SetActive(true);
190
					
191
					Vector3 posJoint = manager.GetJointPosition(userID, joint);
192
					posJoint.z = !mirroredMovement ? -posJoint.z : posJoint.z;
193
					
194
					Quaternion rotJoint = manager.GetJointOrientation(userID, joint, !mirroredMovement);
195
					rotJoint = initialRotation * rotJoint;
196

    
197
					posJoint -= posPointManMP;
198
					
199
					if(mirroredMovement)
200
					{
201
						posJoint.x = -posJoint.x;
202
						posJoint.z = -posJoint.z;
203
					}
204

    
205
					bones[i].transform.localPosition = posJoint;
206
					bones[i].transform.rotation = rotJoint;
207
					
208
					if(lines[i] == null && skeletonLine != null) 
209
					{
210
						lines[i] = Instantiate((i == 22 || i == 24) && debugLine ? debugLine : skeletonLine) as LineRenderer;
211
						lines[i].transform.parent = transform;
212
					}
213

    
214
					if(lines[i] != null)
215
					{
216
						lines[i].gameObject.SetActive(true);
217
						Vector3 posJoint2 = bones[i].transform.position;
218
						
219
						Vector3 dirFromParent = manager.GetJointDirection(userID, joint, false, false);
220
						dirFromParent.z = !mirroredMovement ? -dirFromParent.z : dirFromParent.z;
221
						Vector3 posParent = posJoint2 - dirFromParent;
222
						
223
						//lines[i].SetVertexCount(2);
224
						lines[i].SetPosition(0, posParent);
225
						lines[i].SetPosition(1, posJoint2);
226
					}
227

    
228
				}
229
				else
230
				{
231
					bones[i].gameObject.SetActive(false);
232
					
233
					if(lines[i] != null)
234
					{
235
						lines[i].gameObject.SetActive(false);
236
					}
237
				}
238
			}	
239
		}
240
	}
241

    
242
}