t1 / TFDContents / Assets / KinectDemos / ColliderDemo / Scripts / DepthSpriteViewer.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (5.59 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
|
4 |
public class DepthSpriteViewer : 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 |
[Tooltip("Depth image renderer.")] |
13 |
public SpriteRenderer depthImage; |
14 |
|
15 |
// width of the created box colliders |
16 |
private const float colliderWidth = 0.3f; |
17 |
|
18 |
// the KinectManager instance |
19 |
private KinectManager manager; |
20 |
|
21 |
// screen rectangle taken by the foreground image (in pixels) |
22 |
private Rect foregroundImgRect; |
23 |
|
24 |
// game objects to contain the joint colliders |
25 |
private GameObject[] jointColliders = null; |
26 |
private int numColliders = 0; |
27 |
|
28 |
// depth image resolution |
29 |
private int depthImageWidth; |
30 |
private int depthImageHeight; |
31 |
|
32 |
// depth sensor platform |
33 |
private KinectInterop.DepthSensorPlatform sensorPlatform = KinectInterop.DepthSensorPlatform.None; |
34 |
|
35 |
|
36 |
void Start () |
37 |
{ |
38 |
if (foregroundCamera == null) |
39 |
{ |
40 |
// by default use the main camera |
41 |
foregroundCamera = Camera.main; |
42 |
} |
43 |
|
44 |
manager = KinectManager.Instance; |
45 |
if(manager && manager.IsInitialized()) |
46 |
{ |
47 |
KinectInterop.SensorData sensorData = manager.GetSensorData(); |
48 |
|
49 |
if(sensorData != null && sensorData.sensorInterface != null && foregroundCamera != null) |
50 |
{ |
51 |
// sensor platform |
52 |
sensorPlatform = sensorData.sensorIntPlatform; |
53 |
|
54 |
// get depth image size |
55 |
depthImageWidth = sensorData.depthImageWidth; |
56 |
depthImageHeight = sensorData.depthImageHeight; |
57 |
|
58 |
// calculate the foreground rectangles |
59 |
Rect cameraRect = foregroundCamera.pixelRect; |
60 |
float rectHeight = cameraRect.height; |
61 |
float rectWidth = cameraRect.width; |
62 |
|
63 |
if(rectWidth > rectHeight) |
64 |
rectWidth = rectHeight * depthImageWidth / depthImageHeight; |
65 |
else |
66 |
rectHeight = rectWidth * depthImageHeight / depthImageWidth; |
67 |
|
68 |
float foregroundOfsX = (cameraRect.width - rectWidth) / 2; |
69 |
float foregroundOfsY = (cameraRect.height - rectHeight) / 2; |
70 |
foregroundImgRect = new Rect(foregroundOfsX, foregroundOfsY, rectWidth, rectHeight); |
71 |
|
72 |
// create joint colliders |
73 |
numColliders = sensorData.jointCount; |
74 |
jointColliders = new GameObject[numColliders]; |
75 |
|
76 |
for(int i = 0; i < numColliders; i++) |
77 |
{ |
78 |
string sColObjectName = ((KinectInterop.JointType)i).ToString() + "Collider"; |
79 |
jointColliders[i] = new GameObject(sColObjectName); |
80 |
jointColliders[i].transform.parent = transform; |
81 |
|
82 |
if (i == 0) |
83 |
{ |
84 |
// circle collider for body center |
85 |
CircleCollider2D collider = jointColliders[i].AddComponent<CircleCollider2D>(); |
86 |
collider.radius = colliderWidth; |
87 |
} |
88 |
else |
89 |
{ |
90 |
// box colliders for bones |
91 |
BoxCollider2D collider = jointColliders[i].AddComponent<BoxCollider2D>(); |
92 |
collider.size = new Vector2(colliderWidth, colliderWidth); |
93 |
} |
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
} |
99 |
|
100 |
void Update () |
101 |
{ |
102 |
// get the users texture |
103 |
if(manager && manager.IsInitialized() && depthImage && |
104 |
(depthImage.sprite == null || sensorPlatform == KinectInterop.DepthSensorPlatform.KinectSDKv1)) |
105 |
{ |
106 |
Texture2D texDepth = manager.GetUsersLblTex(); |
107 |
Rect rectDepth = new Rect(0, 0, texDepth.width, texDepth.height); |
108 |
Vector2 pivotSprite = new Vector2(0.5f, 0.5f); |
109 |
|
110 |
depthImage.sprite = Sprite.Create(texDepth, rectDepth, pivotSprite); |
111 |
depthImage.flipY = true; |
112 |
|
113 |
float worldScreenHeight = foregroundCamera.orthographicSize * 2f; |
114 |
float spriteHeight = depthImage.sprite.bounds.size.y; |
115 |
|
116 |
float scale = worldScreenHeight / spriteHeight; |
117 |
depthImage.transform.localScale = new Vector3(scale, scale, 1f); |
118 |
|
119 |
} |
120 |
|
121 |
if(manager && manager.IsUserDetected(playerIndex) && foregroundCamera) |
122 |
{ |
123 |
long userId = manager.GetUserIdByIndex(playerIndex); // manager.GetPrimaryUserID(); |
124 |
|
125 |
// update colliders |
126 |
for(int i = 0; i < numColliders; i++) |
127 |
{ |
128 |
bool bActive = false; |
129 |
|
130 |
if(manager.IsJointTracked(userId, i)) |
131 |
{ |
132 |
Vector3 posJoint = manager.GetJointPosDepthOverlay(userId, i, foregroundCamera, foregroundImgRect); |
133 |
posJoint.z = depthImage ? depthImage.transform.position.z : 0f; |
134 |
|
135 |
if (i == 0) |
136 |
{ |
137 |
// circle collider for body center |
138 |
jointColliders[i].transform.position = posJoint; |
139 |
|
140 |
Quaternion rotCollider = manager.GetJointOrientation(userId, i, true); |
141 |
jointColliders[i].transform.rotation = rotCollider; |
142 |
|
143 |
bActive = true; |
144 |
} |
145 |
else |
146 |
{ |
147 |
int p = (int)manager.GetParentJoint((KinectInterop.JointType)i); |
148 |
|
149 |
if (manager.IsJointTracked (userId, p)) |
150 |
{ |
151 |
// box colliders for bones |
152 |
Vector3 posParent = manager.GetJointPosDepthOverlay(userId, p, foregroundCamera, foregroundImgRect); |
153 |
posParent.z = depthImage ? depthImage.transform.position.z : 0f; |
154 |
|
155 |
Vector3 posCollider = (posJoint + posParent) / 2f; |
156 |
jointColliders[i].transform.position = posCollider; |
157 |
|
158 |
Quaternion rotCollider = Quaternion.FromToRotation (Vector3.up, (posJoint - posParent).normalized); |
159 |
jointColliders[i].transform.rotation = rotCollider; |
160 |
|
161 |
BoxCollider2D collider = jointColliders [i].GetComponent<BoxCollider2D>(); |
162 |
collider.size = new Vector2(collider.size.x, (posJoint - posParent).magnitude); |
163 |
|
164 |
bActive = true; |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
if (jointColliders[i].activeSelf != bActive) |
170 |
{ |
171 |
// change collider activity |
172 |
jointColliders[i].SetActive(bActive); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
} |
178 |
|
179 |
} |