t1 / TFDContents / Assets / KinectDemos / ColliderDemo / Scripts / DepthImageViewer.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (4.84 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
|
4 |
public class DepthImageViewer : MonoBehaviour |
5 |
{ |
6 |
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
7 |
public int playerIndex = 0; |
8 |
|
9 |
[Tooltip("Camera used to estimate the overlay positions of 3D-objects over the background. By default it is the main camera.")] |
10 |
public Camera foregroundCamera; |
11 |
|
12 |
// radius of the created capsule colliders |
13 |
private const float colliderRadius = 0.3f; |
14 |
|
15 |
// the KinectManager instance |
16 |
private KinectManager manager; |
17 |
|
18 |
// the foreground texture |
19 |
private Texture2D foregroundTex; |
20 |
|
21 |
// rectangle taken by the foreground texture (in pixels) |
22 |
private Rect foregroundGuiRect; |
23 |
private Rect foregroundImgRect; |
24 |
|
25 |
// game objects to contain the joint colliders |
26 |
private GameObject[] jointColliders = null; |
27 |
private int numColliders = 0; |
28 |
|
29 |
private int depthImageWidth; |
30 |
private int depthImageHeight; |
31 |
|
32 |
|
33 |
void Start () |
34 |
{ |
35 |
if (foregroundCamera == null) |
36 |
{ |
37 |
// by default use the main camera |
38 |
foregroundCamera = Camera.main; |
39 |
} |
40 |
|
41 |
manager = KinectManager.Instance; |
42 |
if(manager && manager.IsInitialized()) |
43 |
{ |
44 |
KinectInterop.SensorData sensorData = manager.GetSensorData(); |
45 |
|
46 |
if(sensorData != null && sensorData.sensorInterface != null && foregroundCamera != null) |
47 |
{ |
48 |
// get depth image size |
49 |
depthImageWidth = sensorData.depthImageWidth; |
50 |
depthImageHeight = sensorData.depthImageHeight; |
51 |
|
52 |
// calculate the foreground rectangles |
53 |
Rect cameraRect = foregroundCamera.pixelRect; |
54 |
float rectHeight = cameraRect.height; |
55 |
float rectWidth = cameraRect.width; |
56 |
|
57 |
if(rectWidth > rectHeight) |
58 |
rectWidth = rectHeight * depthImageWidth / depthImageHeight; |
59 |
else |
60 |
rectHeight = rectWidth * depthImageHeight / depthImageWidth; |
61 |
|
62 |
float foregroundOfsX = (cameraRect.width - rectWidth) / 2; |
63 |
float foregroundOfsY = (cameraRect.height - rectHeight) / 2; |
64 |
foregroundImgRect = new Rect(foregroundOfsX, foregroundOfsY, rectWidth, rectHeight); |
65 |
foregroundGuiRect = new Rect(foregroundOfsX, cameraRect.height - foregroundOfsY, rectWidth, -rectHeight); |
66 |
|
67 |
// create joint colliders |
68 |
numColliders = sensorData.jointCount; |
69 |
jointColliders = new GameObject[numColliders]; |
70 |
|
71 |
for(int i = 0; i < numColliders; i++) |
72 |
{ |
73 |
string sColObjectName = ((KinectInterop.JointType)i).ToString() + "Collider"; |
74 |
jointColliders[i] = new GameObject(sColObjectName); |
75 |
jointColliders[i].transform.parent = transform; |
76 |
|
77 |
if (i == 0) |
78 |
{ |
79 |
// sphere collider for body center |
80 |
SphereCollider collider = jointColliders[i].AddComponent<SphereCollider>(); |
81 |
collider.radius = colliderRadius; |
82 |
} |
83 |
else |
84 |
{ |
85 |
// capsule collider for bones |
86 |
CapsuleCollider collider = jointColliders[i].AddComponent<CapsuleCollider>(); |
87 |
collider.radius = colliderRadius; |
88 |
} |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
} |
94 |
|
95 |
void Update () |
96 |
{ |
97 |
// get the users texture |
98 |
if(manager && manager.IsInitialized()) |
99 |
{ |
100 |
foregroundTex = manager.GetUsersLblTex(); |
101 |
} |
102 |
|
103 |
if(manager && manager.IsUserDetected(playerIndex) && foregroundCamera) |
104 |
{ |
105 |
long userId = manager.GetUserIdByIndex(playerIndex); // manager.GetPrimaryUserID(); |
106 |
|
107 |
// update colliders |
108 |
for(int i = 0; i < numColliders; i++) |
109 |
{ |
110 |
bool bActive = false; |
111 |
|
112 |
if(manager.IsJointTracked(userId, i)) |
113 |
{ |
114 |
Vector3 posJoint = manager.GetJointPosDepthOverlay(userId, i, foregroundCamera, foregroundImgRect); |
115 |
|
116 |
if (i == 0) |
117 |
{ |
118 |
// sphere collider for body center |
119 |
jointColliders[i].transform.position = posJoint; |
120 |
|
121 |
Quaternion rotCollider = manager.GetJointOrientation(userId, i, true); |
122 |
jointColliders[i].transform.rotation = rotCollider; |
123 |
|
124 |
bActive = true; |
125 |
} |
126 |
else |
127 |
{ |
128 |
int p = (int)manager.GetParentJoint((KinectInterop.JointType)i); |
129 |
|
130 |
if (manager.IsJointTracked (userId, p)) |
131 |
{ |
132 |
// capsule collider for bones |
133 |
Vector3 posParent = manager.GetJointPosDepthOverlay(userId, p, foregroundCamera, foregroundImgRect); |
134 |
|
135 |
Vector3 posCollider = (posJoint + posParent) / 2f; |
136 |
jointColliders[i].transform.position = posCollider; |
137 |
|
138 |
Quaternion rotCollider = Quaternion.FromToRotation (Vector3.up, (posJoint - posParent).normalized); |
139 |
jointColliders[i].transform.rotation = rotCollider; |
140 |
|
141 |
CapsuleCollider collider = jointColliders [i].GetComponent<CapsuleCollider>(); |
142 |
collider.height = (posJoint - posParent).magnitude; |
143 |
|
144 |
bActive = true; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
if (jointColliders[i].activeSelf != bActive) |
150 |
{ |
151 |
// change collider activity |
152 |
jointColliders[i].SetActive(bActive); |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
|
159 |
void OnGUI() |
160 |
{ |
161 |
if(foregroundTex) |
162 |
{ |
163 |
GUI.DrawTexture(foregroundGuiRect, foregroundTex); |
164 |
} |
165 |
} |
166 |
|
167 |
} |