-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathKUInterface.cs
392 lines (316 loc) · 11.5 KB
/
KUInterface.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/************************************************************************
* *
* KinectSDK-Unity3D C# Wrapper: *
* Attach to a GameObject *
* *
* Author: Andrew DeVine *
* University of Central Florida ISUE Lab *
* 2012 *
* (see included BSD license for licensing information) *
************************************************************************/
using UnityEngine;
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
/// <summary>
/// Kinect method access
/// </summary>
public class KUInterface : MonoBehaviour {
//public variables
/// <summary>
/// scales all joint positions by given amount. Do not set to zero.
/// </summary>
public int scaleFactor = 1000;
/// <summary>
/// set to true to track two skeletons
/// </summary>
public bool twoPlayer = false;
/// <summary>
/// set to false to optimize performance if RGB camera is not being used
/// </summary>
public bool useRGB = true;
/// <summary>
/// set to false to optimize performance if depth camera is not being used
/// </summary>
public bool useDepth = true;
/// <summary>
/// displays joint position data on screen
/// </summary>
public bool displayJointInformation = false;
/// <summary>
/// displays RGB texture image on screen
/// </summary>
public bool displayTextureImage = false;
/// <summary>
/// displays depth image on screen
/// </summary>
public bool displayDepthImage = false;
//constants
private const int IM_W = 640;
private const int IM_H = 480;
//private variables
private bool NUIisReady = false;
private float lastCameraAngleChange = -30.0f;
private byte[] seqTex;
private Color32[] cols;
private Texture2D texture;
private byte[] seqDepth;
private Color32[] dcols;
private Texture2D depthImg;
private short[][] depth;
/********************************************************************************
* USER METHODS -> Call these methods from your scripts
* ******************************************************************************/
/// <summary>
/// main joint position get function
/// </summary>
/// <param name="player">player number (1,2)</param>
/// <param name="joint">KinectWrapper.Joints enum</param>
/// <returns>position of given joint for given player</returns>
public Vector3 GetJointPos(int player, KinectWrapper.Joints joint) {
KinectWrapper.SkeletonTransform trans = new KinectWrapper.SkeletonTransform();
if (NUIisReady && (player == 1 || player == 2))
{
KinectWrapper.GetSkeletonTransform(player, (int)joint, ref trans);
return new Vector3(trans.x * scaleFactor, trans.y * scaleFactor, trans.z * scaleFactor);
}
else
{
return Vector3.zero;
}
}
/// <summary>
/// one-player overload of joint position get function
/// </summary>
/// <param name="joint">KinectWrapper.Joints enum</param>
/// <returns>position of given joint</returns>
public Vector3 GetJointPos(KinectWrapper.Joints joint) {
return GetJointPos(1, joint);
}
/// <summary>
/// gets color texture image from Kinect RGB camera
/// </summary>
/// <returns>RGB image</returns>
public Texture2D GetTextureImage() {
return this.texture;
}
/// <summary>
/// gets depth data from Kinect depth camera
/// </summary>
/// <returns>2D array of pixel depths as bytes</returns>
/// <remarks>depth[x=0][y=0] corresponds to top-left corner of image</remarks>
public short[][] GetDepthData() {
return this.depth;
}
/// <summary>
/// returns current Kinect camera angle from horizontal
/// </summary>
/// <returns>camera angle from horizontal</returns>
public float GetCameraAngle() {
float cameraAngle = 0;
KinectWrapper.GetCameraAngle(ref cameraAngle);
return cameraAngle;
}
/// <summary>
/// sets Kinect camera angle
/// </summary>
/// <param name="angle">range: -27 -> 27</param>
/// <returns>returns true if successful</returns>
/// <remarks>do not change angle more than once every 30 sec</remarks>
public bool SetCameraAngle(int angle) {
/* DO NOT CHANGE CAMERA ANGLE MORE OFTEN THAN ONCE
* EVERY 30 SECONDS, IT COULD DAMAGE THE KINECT
* SENSOR (SEE KINECT SDK DOCUMENTATION).
*/
if (Time.time - lastCameraAngleChange > 30 && NUIisReady) {
lastCameraAngleChange = Time.time;
return KinectWrapper.SetCameraAngle(angle);
} else {
return false;
}
}
/********************************************************************************
* DEVICE MANAGEMENT -> initialize, uninitialize, and update sensor
* ******************************************************************************/
//called on application start
private void Start() {
NUIisReady = false;
//initialize Kinect sensor
NUIisReady = KinectWrapper.NuiContextInit(twoPlayer);
//display messages
if (NUIisReady)
Debug.Log("Sensor Initialized.");
else
Debug.Log("Could Not Initialize Sensor.");
if (scaleFactor == 0)
Debug.Log("WARNING: KUInterface.scaleFactor is set to zero. All joint positions will be the zero vector.");
//set up image memory
seqTex = new byte[IM_W * IM_H * 4];
cols = new Color32[IM_W * IM_H];
texture = new Texture2D(IM_W, IM_H);
seqDepth = new byte[IM_W * IM_H * 2];
dcols = new Color32[IM_W * IM_H];
depthImg = new Texture2D(IM_W, IM_H);
depth = new short[IM_W][];
for (int i = 0; i < depth.Length; i++) {
depth[i] = new short[IM_H];
}
}
//called on application stop
private void OnApplicationQuit() {
NUIisReady = false;
KinectWrapper.NuiContextUnInit();
Debug.Log("Sensor Off.");
}
//called every Unity frame (frame-rate dependent)
private void Update() {
if (NUIisReady)
{
//update Kinect
KinectWrapper.NuiUpdate();
//update RGB texture
if (useRGB)
UpdateTextureImage();
//update Depth data
if (useDepth)
UpdateDepth();
}
}
//update RGB texture
private void UpdateTextureImage() {
int size = 0;
//copy pixel data from unmanaged memory
IntPtr ptr = KinectWrapper.GetTextureImage(ref size);
if (ptr != IntPtr.Zero)
{
Marshal.Copy(ptr, seqTex, 0, size);
Debug.Log(size.ToString());
//create color matrix
for (int i = 0; i < (IM_W * IM_H * 4); i += 4)
{
cols[(IM_W * IM_H) - (i / 4) - 1] = new Color32(seqTex[i + 2], seqTex[i + 1], seqTex[i], 255);
}
//set texture
texture.SetPixels32(cols);
texture.Apply();
}
}
//update Depth array and texture
private void UpdateDepth() {
int size = 0;
//copy pixel data from unmanaged memory
IntPtr ptr = KinectWrapper.GetDepthImage(ref size);
if (ptr != IntPtr.Zero)
{
Marshal.Copy(ptr, seqDepth, 0, size);
//create depth array
for (int x = 0; x < IM_W; x++)
{
for (int y = 0; y < IM_H; y++)
{
depth[IM_W - x - 1][IM_H - y - 1] = BitConverter.ToInt16(seqDepth, (x*2)+(y*IM_W*2));
}
}
if (displayDepthImage)
{
//create color matrix as bytes (loops resolution)
for (int x = 0; x < IM_W; x++)
{
for (int y = 0; y < IM_H; y++)
{
dcols[x + (y * IM_W)] = new Color32((byte)depth[x][y], (byte)depth[x][y], (byte)depth[x][y], 255);
}
}
//set texture
depthImg.SetPixels32(dcols);
depthImg.Apply();
}
}
}
//update function for GUI (if enabled)
private void OnGUI() {
if (displayJointInformation && NUIisReady) {
//display joint info in upper-left corner
if (twoPlayer) {
for (int i = 0; i < (int)KinectWrapper.Joints.COUNT; i++) {
DisplayPlayerData(1, i);
DisplayPlayerData(2, i);
}
} else {
for (int i = 0; i < (int)KinectWrapper.Joints.COUNT; i++) {
DisplayPlayerData(1, i);
}
}
}
if (displayTextureImage && useRGB && NUIisReady) {
//scaled to half-res
GUI.DrawTexture(new Rect(600, 50, IM_W / 2, IM_H / 2), texture, ScaleMode.ScaleToFit, false);
}
if (displayDepthImage && useDepth && NUIisReady) {
//scaled to half-res
GUI.DrawTexture(new Rect(600, 300, IM_W / 2, IM_H / 2), depthImg, ScaleMode.ScaleToFit, false);
}
}
//displays joint position data in GUI (if enabled)
private void DisplayPlayerData(int player, int place) {
Vector3 joint = GetJointPos(player, (KinectWrapper.Joints)place);
GUI.Label(new Rect((player - 1) * 300, place * 30, 200, 30), joint.ToString());
}
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
/// <summary>
/// KUInterface.dll wrapper
/// </summary>
public class KinectWrapper {
/// <summary>
/// defines Kinect skeleton
/// </summary>
public enum Joints {
HIP_CENTER = 0,
SPINE,
SHOULDER_CENTER,
HEAD,
SHOULDER_LEFT,
ELBOW_LEFT,
WRIST_LEFT,
HAND_LEFT,
SHOULDER_RIGHT,
ELBOW_RIGHT,
WRIST_RIGHT,
HAND_RIGHT,
HIP_LEFT,
KNEE_LEFT,
ANKLE_LEFT,
FOOT_LEFT,
HIP_RIGHT,
KNEE_RIGHT,
ANKLE_RIGHT,
FOOT_RIGHT,
COUNT
};
[StructLayout(LayoutKind.Sequential)]
public struct SkeletonTransform {
public float x, y, z, w;
}
//NUI Context Management
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern bool NuiContextInit(bool twoPlayer);
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern void NuiUpdate();
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern void NuiContextUnInit();
//Get Methods
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern void GetSkeletonTransform(int player, int joint, ref SkeletonTransform trans);
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern IntPtr GetTextureImage(ref int size);
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern IntPtr GetDepthImage(ref int size);
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern void GetCameraAngle(ref float angle);
//Set Methods
[DllImport("/Assets/Plugins/KUInterface.dll")]
public static extern bool SetCameraAngle(int angle);
}