프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / PhotoBoothController.cs @ 3

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

1 3 KTH
using UnityEngine;
2
using System.Collections;
3
using System.IO;
4
5
public class PhotoBoothController : MonoBehaviour, KinectGestures.GestureListenerInterface, InteractionListenerInterface
6
{
7
	[Tooltip("GUI-texture used to display the color camera feed on the scene background.")]
8
	public GUITexture backgroundImage;
9
10
	[Tooltip("Camera that will be used to render the background.")]
11
	public Camera backroundCamera;
12
13
	[Tooltip("Camera that will be used to overlay the 3D-objects over the background.")]
14
	public Camera foreroundCamera;
15
16
	[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
17
	public int playerIndex = 0;
18
19
	[Tooltip("Reference to the head joint-overlayer component.")]
20
	public JointOverlayer headOverlayer;
21
22
	[Tooltip("Reference to the left hand joint-overlayer component.")]
23
	public JointOverlayer leftHandOverlayer;
24
25
	[Tooltip("Reference to the chest joint-overlayer component.")]
26
	public JointOverlayer chestOverlayer;
27
28
	[Tooltip("Array of sprite transforms that will be used for head overlays on each step.")]
29
	public Transform[] headMasks;
30
31
	[Tooltip("Array of sprite transforms that will be used for left hand overlays on each step.")]
32
	public Transform[] leftHandMasks;
33
34
	[Tooltip("Array of sprite transforms that will be used for chest overlays on each step.")]
35
	public Transform[] chestMasks;
36
37
	[Tooltip("GUI-Text used to display information messages.")]
38
	public GUIText infoText;
39
40
41
	private int maskCount = 0;
42
	private int currentIndex = -1;
43
	private int prevIndex = -1;
44
45
46
	void Start ()
47
	{
48
		maskCount = 0;
49
50
		if (headMasks != null && headMasks.Length > maskCount)
51
			maskCount = headMasks.Length;
52
53
		if (leftHandMasks != null && leftHandMasks.Length > maskCount)
54
			maskCount = leftHandMasks.Length;
55
56
		if (chestMasks != null && chestMasks.Length > maskCount)
57
			maskCount = chestMasks.Length;
58
	}
59
60
	void Update ()
61
	{
62
		KinectManager manager = KinectManager.Instance;
63
64
		if (manager && manager.IsInitialized ())
65
		{
66
			if (backgroundImage && (backgroundImage.texture == null))
67
			{
68
				backgroundImage.texture = manager.GetUsersClrTex ();
69
			}
70
		}
71
72
		if (currentIndex != prevIndex)
73
		{
74
			prevIndex = currentIndex;
75
76
			if (headOverlayer && headMasks != null)
77
			{
78
				if (headOverlayer.overlayObject)
79
				{
80
					headOverlayer.overlayObject.rotation = headOverlayer.initialRotation;
81
					headOverlayer.overlayObject.gameObject.SetActive (false);
82
				}
83
84
				headOverlayer.overlayObject = currentIndex >= 0 && currentIndex < headMasks.Length ? headMasks [currentIndex] : null;
85
				headOverlayer.playerIndex = playerIndex;
86
87
				if (headOverlayer.overlayObject)
88
				{
89
					headOverlayer.overlayObject.gameObject.SetActive (true);
90
				}
91
92
				headOverlayer.Start();
93
			}
94
95
			if (leftHandOverlayer && leftHandMasks != null)
96
			{
97
				if (leftHandOverlayer.overlayObject)
98
				{
99
					leftHandOverlayer.overlayObject.rotation = leftHandOverlayer.initialRotation;
100
					leftHandOverlayer.overlayObject.gameObject.SetActive (false);
101
				}
102
103
				leftHandOverlayer.overlayObject = currentIndex >= 0 && currentIndex < leftHandMasks.Length ? leftHandMasks [currentIndex] : null;
104
				leftHandOverlayer.playerIndex = playerIndex;
105
106
				if (leftHandOverlayer.overlayObject)
107
				{
108
					leftHandOverlayer.overlayObject.gameObject.SetActive (true);
109
				}
110
111
				leftHandOverlayer.Start();
112
			}
113
114
			if (chestOverlayer && chestMasks != null)
115
			{
116
				if (chestOverlayer.overlayObject)
117
				{
118
					chestOverlayer.overlayObject.rotation = chestOverlayer.initialRotation;
119
					chestOverlayer.overlayObject.gameObject.SetActive (false);
120
				}
121
122
				chestOverlayer.overlayObject = currentIndex >= 0 && currentIndex < chestMasks.Length ? chestMasks [currentIndex] : null;
123
				chestOverlayer.playerIndex = playerIndex;
124
125
				if (chestOverlayer.overlayObject)
126
				{
127
					chestOverlayer.overlayObject.gameObject.SetActive (true);
128
				}
129
130
				chestOverlayer.Start();
131
			}
132
		}
133
	}
134
135
136
	// GestureListenerInterface
137
138
	public void UserDetected(long userId, int userIndex)
139
	{
140
		KinectManager manager = KinectManager.Instance;
141
		if(!manager || (userIndex != playerIndex))
142
			return;
143
144
		manager.DetectGesture(userId, KinectGestures.Gestures.SwipeLeft);
145
		manager.DetectGesture(userId, KinectGestures.Gestures.SwipeRight);
146
147
		currentIndex = 0;
148
149
		if (infoText)
150
		{
151
			infoText.text = "Swipe left or right to change props. Make hand grip to take photo.";
152
		}
153
	}
154
155
	public void UserLost(long userId, int userIndex)
156
	{
157
		if(userIndex != playerIndex)
158
			return;
159
160
		currentIndex = -1;
161
	}
162
163
	public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
164
	{
165
		// nothing to do here
166
	}
167
168
	public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
169
	{
170
		if(userIndex != playerIndex)
171
			return false;
172
173
		switch (gesture)
174
		{
175
		case KinectGestures.Gestures.SwipeLeft:
176
			currentIndex++;
177
			if (currentIndex >= maskCount)
178
				currentIndex = 0;
179
			break;
180
181
		case KinectGestures.Gestures.SwipeRight:
182
			currentIndex--;
183
			if (currentIndex < 0)
184
				currentIndex = maskCount - 1;
185
			break;
186
		}
187
188
		return true;
189
	}
190
191
	public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
192
	{
193
		if(userIndex != playerIndex)
194
			return false;
195
196
		return true;
197
	}
198
199
200
	// InteractionListenerInterface
201
202
	public void HandGripDetected(long userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
203
	{
204
		if (userIndex != playerIndex)
205
			return;
206
207
		if (isRightHand && handScreenPos.y >= 0.5f)
208
		{
209
			if (infoText)
210
			{
211
				infoText.text = "Hand grip detected.";
212
			}
213
214
			//StartCoroutine(CountdownAndTakePicture());
215
			PhotoShooter photoShooter = gameObject.GetComponent<PhotoShooter>();
216
			if (photoShooter && photoShooter.enabled)
217
			{
218
				photoShooter.CountdownAndMakePhoto();
219
			}
220
		}
221
	}
222
223
	public void HandReleaseDetected(long userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
224
	{
225
		if (userIndex != playerIndex)
226
			return;
227
228
		// nothing to do here
229
	}
230
231
	public bool HandClickDetected(long userId, int userIndex, bool isRightHand, Vector3 handScreenPos)
232
	{
233
		if (userIndex != playerIndex)
234
			return false;
235
236
		return true;
237
	}
238
239
240
}