t1 / TFDContents / Assets / KinectScripts / BackgroundDepthImage.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (1.71 KB)
| 1 |
using UnityEngine; |
|---|---|
| 2 |
using System.Collections; |
| 3 |
|
| 4 |
/// <summary> |
| 5 |
/// Background depth image is component that displays the depth camera image on GUI texture, usually the scene background. |
| 6 |
/// </summary> |
| 7 |
public class BackgroundDepthImage : MonoBehaviour |
| 8 |
{
|
| 9 |
[Tooltip("GUI-texture used to display the depth image.")]
|
| 10 |
public GUITexture backgroundImage; |
| 11 |
|
| 12 |
[Tooltip("Camera that will be used to display the background image.")]
|
| 13 |
public Camera backgroundCamera; |
| 14 |
|
| 15 |
|
| 16 |
void Update () |
| 17 |
{
|
| 18 |
KinectManager manager = KinectManager.Instance; |
| 19 |
|
| 20 |
if (manager && manager.IsInitialized()) |
| 21 |
{
|
| 22 |
if (backgroundImage && (backgroundImage.texture == null)) |
| 23 |
{
|
| 24 |
backgroundImage.texture = manager.GetUsersLblTex(); |
| 25 |
|
| 26 |
KinectInterop.SensorData sensorData = manager.GetSensorData(); |
| 27 |
if (sensorData != null && sensorData.sensorInterface != null && backgroundCamera != null) |
| 28 |
{
|
| 29 |
// get depth image size |
| 30 |
int depthImageWidth = sensorData.depthImageWidth; |
| 31 |
int depthImageHeight = sensorData.depthImageHeight; |
| 32 |
|
| 33 |
// calculate insets |
| 34 |
Rect cameraRect = backgroundCamera.pixelRect; |
| 35 |
float rectHeight = cameraRect.height; |
| 36 |
float rectWidth = cameraRect.width; |
| 37 |
|
| 38 |
if (rectWidth > rectHeight) |
| 39 |
rectWidth = rectHeight * depthImageWidth / depthImageHeight; |
| 40 |
else |
| 41 |
rectHeight = rectWidth * depthImageHeight / depthImageWidth; |
| 42 |
|
| 43 |
float deltaWidth = cameraRect.width - rectWidth; |
| 44 |
float deltaHeight = cameraRect.height - rectHeight; |
| 45 |
|
| 46 |
float leftX = deltaWidth / 2; |
| 47 |
float rightX = -deltaWidth; |
| 48 |
float bottomY = -deltaHeight / 2; |
| 49 |
float topY = deltaHeight; |
| 50 |
|
| 51 |
backgroundImage.pixelInset = new Rect(leftX, bottomY, rightX, topY); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |