t1 / TFDContents / Assets / 01.Script / 01.MultyInteraction / DepthDetector.cs @ 11
이력 | 보기 | 이력해설 | 다운로드 (6.29 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using UnityEngine.UI; |
4 |
using System; |
5 |
using System.Xml; |
6 |
using System.Collections; |
7 |
using UnityEngine.Video; |
8 |
|
9 |
class Sorter : IComparer<Sprite> |
10 |
{ |
11 |
public int Compare(Sprite x, Sprite y) |
12 |
{ |
13 |
return String.Compare(x.name, y.name); |
14 |
} |
15 |
} |
16 |
|
17 |
public class DepthDetector : MonoBehaviour, KinectGestures.GestureListenerInterface |
18 |
{ |
19 |
KinectManager manager; |
20 |
[SerializeField] |
21 |
List<Sprite> sprites; |
22 |
[SerializeField] |
23 |
Image image; |
24 |
public static bool isPlaying; |
25 |
|
26 |
int oldindex; |
27 |
public float minZ; |
28 |
public float maxZ; |
29 |
|
30 |
|
31 |
|
32 |
public List<Texture2D> textureList; |
33 |
public Material InputMaterial; |
34 |
public GameObject quad; |
35 |
|
36 |
public VideoClip[] videoclips; |
37 |
public VideoPlayer videoPlayer; |
38 |
|
39 |
|
40 |
|
41 |
void Start() |
42 |
{ |
43 |
Screen.SetResolution(3240,1920,false); |
44 |
|
45 |
|
46 |
manager = KinectManager.Instance; |
47 |
sprites.Sort(new Sorter()); |
48 |
//XmlDocument doc = new XmlDocument(); |
49 |
//doc.Load("Setting.xml"); |
50 |
//var root = doc.DocumentElement; |
51 |
//minZ = float.Parse(root.GetAttribute("minZ")); |
52 |
//maxZ = float.Parse(root.GetAttribute("maxZ")); |
53 |
//manager.maxLeftRightDistance = float.Parse(root.GetAttribute("maxLeftRight")); |
54 |
|
55 |
StartCoroutine(CheckPerson()); |
56 |
StartCoroutine(PlaySetting()); |
57 |
} |
58 |
|
59 |
int nowPlayVideoNum = 0; |
60 |
|
61 |
IEnumerator PlaySetting() |
62 |
{ |
63 |
while(true) |
64 |
{ |
65 |
if (videoPlayer.isPlaying == false) |
66 |
{ |
67 |
videoPlayer.clip = videoclips[nowPlayVideoNum]; |
68 |
nowPlayVideoNum++; |
69 |
if(nowPlayVideoNum >= videoclips.Length) |
70 |
{ |
71 |
nowPlayVideoNum = 0; |
72 |
} |
73 |
videoPlayer.Play(); |
74 |
} |
75 |
|
76 |
yield return null; |
77 |
} |
78 |
|
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
public List<int> UserList = new List<int>(); |
86 |
|
87 |
public enum Type { LEFT, RIGHT } |
88 |
|
89 |
public class ItemInfo |
90 |
{ |
91 |
public Type direction; |
92 |
public GameObject obj; |
93 |
} |
94 |
|
95 |
|
96 |
public Dictionary<long, ItemInfo> UserDictionary = new Dictionary<long, ItemInfo>(); |
97 |
|
98 |
|
99 |
public GameObject VideoPanel; |
100 |
|
101 |
IEnumerator CheckPerson() |
102 |
{ |
103 |
while(true) |
104 |
{ |
105 |
if ( manager.GetUsersCount() > 0) |
106 |
{ |
107 |
VideoPanel.SetActive(false); |
108 |
} |
109 |
else |
110 |
{ |
111 |
VideoPanel.SetActive(true); |
112 |
} |
113 |
|
114 |
|
115 |
for (int i = 0; i < manager.GetUsersCount(); i++) |
116 |
{ |
117 |
var tmpID = manager.GetUserIdByIndex(i); |
118 |
|
119 |
var xValue = manager.GetUserPosition(tmpID).x; |
120 |
var yValue = manager.GetUserPosition(tmpID).y; |
121 |
var zValue = manager.GetUserPosition(tmpID).z; |
122 |
|
123 |
|
124 |
Vector3 direction = manager.GetDirectionBetweenJoints(tmpID, 0,1, false, false); |
125 |
|
126 |
print("direction : " + direction); |
127 |
|
128 |
// 인식 범위 안 |
129 |
if (zValue > 1.6f && zValue < 3.5f) |
130 |
{ |
131 |
// x 범위 안쪽 |
132 |
if (xValue >= -1f && xValue <= 1f) |
133 |
{ |
134 |
ItemInfo tmpInfo; |
135 |
|
136 |
UserDictionary.TryGetValue(tmpID, out tmpInfo); |
137 |
|
138 |
xValue = xValue + 1f; |
139 |
|
140 |
if (tmpInfo.direction == Type.LEFT) |
141 |
{ |
142 |
oldindex = (int)Mathf.Lerp(0, textureList.Count - 1, (xValue - 0) / 2f); |
143 |
} |
144 |
else |
145 |
{ |
146 |
oldindex = (int)Mathf.Lerp(textureList.Count - 1, 0, (xValue - 0) / 2f); |
147 |
} |
148 |
//oldindex = (int)Mathf.Lerp(0, textureList.Count - 1, (xValue - 0) / 2f); |
149 |
|
150 |
tmpInfo.obj.GetComponent<MeshRenderer>().material.mainTexture = textureList[oldindex]; |
151 |
|
152 |
|
153 |
|
154 |
//InputMaterial.mainTexture = textureList[oldindex]; |
155 |
|
156 |
|
157 |
//var pos = Mathf.Lerp(-64.0f, 64.0f, (xValue - 0) / 2f); |
158 |
|
159 |
//GameObject tmpObj = new GameObject(); |
160 |
|
161 |
//UserDictionary.TryGetValue(tmpID, out tmpObj); |
162 |
|
163 |
//tmpObj.transform.position = Vector3.Lerp(tmpObj.transform.position, new Vector3(pos, 0, tmpObj.transform.position.z), Time.deltaTime * 10f); |
164 |
} |
165 |
} |
166 |
} |
167 |
yield return null; |
168 |
|
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
// Update is called once per frame |
174 |
void Update() |
175 |
{ |
176 |
|
177 |
|
178 |
} |
179 |
|
180 |
public void UserDetected(long userId, int userIndex) |
181 |
{ |
182 |
GameObject tmpObj = Instantiate(quad); |
183 |
ItemInfo tmpInfo = new ItemInfo(); |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
// 오른쪽에서 진입함. |
189 |
if (manager.GetUserPosition(userId).x >= 0) |
190 |
{ |
191 |
|
192 |
|
193 |
print("1 진입"); |
194 |
|
195 |
tmpObj.transform.localScale = new Vector3(-tmpObj.transform.localScale.x, tmpObj.transform.localScale.y, tmpObj.transform.localScale.z); |
196 |
tmpInfo.obj = tmpObj; |
197 |
tmpInfo.direction = Type.RIGHT ; |
198 |
} |
199 |
|
200 |
// 왼쪽에서 진입함 |
201 |
else |
202 |
{ |
203 |
print("2 진입"); |
204 |
tmpInfo.obj = tmpObj; |
205 |
|
206 |
tmpInfo.direction = Type.LEFT; |
207 |
} |
208 |
|
209 |
UserDictionary.Add(userId, tmpInfo); |
210 |
|
211 |
|
212 |
} |
213 |
|
214 |
public void UserLost(long userId, int userIndex) |
215 |
{ |
216 |
ItemInfo tmpInfo; |
217 |
|
218 |
UserDictionary.TryGetValue(userId, out tmpInfo); |
219 |
|
220 |
tmpInfo.obj.GetComponent<Player>().Hide(); |
221 |
|
222 |
UserDictionary.Remove(userId); |
223 |
|
224 |
} |
225 |
|
226 |
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos) |
227 |
{ |
228 |
throw new NotImplementedException(); |
229 |
} |
230 |
|
231 |
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos) |
232 |
{ |
233 |
throw new NotImplementedException(); |
234 |
} |
235 |
|
236 |
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint) |
237 |
{ |
238 |
throw new NotImplementedException(); |
239 |
} |
240 |
} |