t1 / TFDContents / Assets / KinectScripts / Samples / GetFacePointsDemo.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (3.13 KB)
1 |
#if !(UNITY_WSA_10_0 && NETFX_CORE) |
---|---|
2 |
using UnityEngine; |
3 |
using System.Collections; |
4 |
using System.Collections.Generic; |
5 |
using Microsoft.Kinect.Face; |
6 |
|
7 |
|
8 |
public class GetFacePointsDemo : MonoBehaviour |
9 |
{ |
10 |
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
11 |
public int playerIndex = 0; |
12 |
|
13 |
[Tooltip("Tracked face point.")] |
14 |
public HighDetailFacePoints facePoint = HighDetailFacePoints.NoseTip; |
15 |
|
16 |
[Tooltip("Transform used to show the selected face point in space.")] |
17 |
public Transform facePointTransform; |
18 |
|
19 |
[Tooltip("GUI-Text to display face-information messages.")] |
20 |
public GUIText faceInfoText; |
21 |
|
22 |
private KinectManager manager = null; |
23 |
private FacetrackingManager faceManager = null; |
24 |
//private Kinect2Interface k2interface = null; |
25 |
|
26 |
private Vector3[] faceVertices; |
27 |
private Dictionary<HighDetailFacePoints, Vector3> dictFacePoints = new Dictionary<HighDetailFacePoints, Vector3> (); |
28 |
|
29 |
|
30 |
// returns the face point coordinates or Vector3.zero if not found |
31 |
public Vector3 GetFacePoint(HighDetailFacePoints pointType) |
32 |
{ |
33 |
if(dictFacePoints != null && dictFacePoints.ContainsKey(pointType)) |
34 |
{ |
35 |
return dictFacePoints[pointType]; |
36 |
} |
37 |
|
38 |
return Vector3.zero; |
39 |
} |
40 |
|
41 |
|
42 |
void Update () |
43 |
{ |
44 |
if (!manager) |
45 |
{ |
46 |
manager = KinectManager.Instance; |
47 |
} |
48 |
|
49 |
if (!faceManager) |
50 |
{ |
51 |
faceManager = FacetrackingManager.Instance; |
52 |
} |
53 |
|
54 |
// // get reference to the Kinect2Interface |
55 |
// if(k2interface == null) |
56 |
// { |
57 |
// manager = KinectManager.Instance; |
58 |
// |
59 |
// if(manager && manager.IsInitialized()) |
60 |
// { |
61 |
// KinectInterop.SensorData sensorData = manager.GetSensorData(); |
62 |
// |
63 |
// if(sensorData != null && sensorData.sensorInterface != null) |
64 |
// { |
65 |
// k2interface = (Kinect2Interface)sensorData.sensorInterface; |
66 |
// } |
67 |
// } |
68 |
// } |
69 |
|
70 |
// get the face points |
71 |
if(manager != null && manager.IsInitialized() && faceManager && faceManager.IsFaceTrackingInitialized()) |
72 |
{ |
73 |
long userId = manager.GetUserIdByIndex(playerIndex); |
74 |
|
75 |
if (faceVertices == null) |
76 |
{ |
77 |
int iVertCount = faceManager.GetUserFaceVertexCount(userId); |
78 |
|
79 |
if (iVertCount > 0) |
80 |
{ |
81 |
faceVertices = new Vector3[iVertCount]; |
82 |
} |
83 |
} |
84 |
|
85 |
if (faceVertices != null) |
86 |
{ |
87 |
if (faceManager.GetUserFaceVertices(userId, ref faceVertices)) |
88 |
{ |
89 |
Matrix4x4 kinectToWorld = manager.GetKinectToWorldMatrix(); |
90 |
HighDetailFacePoints[] facePoints = (HighDetailFacePoints[])System.Enum.GetValues(typeof(HighDetailFacePoints)); |
91 |
|
92 |
for (int i = 0; i < facePoints.Length; i++) |
93 |
{ |
94 |
HighDetailFacePoints point = facePoints[i]; |
95 |
dictFacePoints[point] = kinectToWorld.MultiplyPoint3x4(faceVertices[(int)point]); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
} |
101 |
|
102 |
if(faceVertices != null && faceVertices[(int)facePoint] != Vector3.zero) |
103 |
{ |
104 |
Vector3 facePointPos = faceVertices [(int)facePoint]; |
105 |
|
106 |
if (facePointTransform) |
107 |
{ |
108 |
facePointTransform.position = facePointPos; |
109 |
} |
110 |
|
111 |
if(faceInfoText) |
112 |
{ |
113 |
string sStatus = string.Format("{0}: {1}", facePoint, facePointPos); |
114 |
faceInfoText.text = sStatus; |
115 |
} |
116 |
} |
117 |
|
118 |
} |
119 |
|
120 |
|
121 |
} |
122 |
#endif |