프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectScripts / Samples / SimpleBackgroundRemoval.cs @ 3

이력 | 보기 | 이력해설 | 다운로드 (2.02 KB)

1
using UnityEngine;
2
using System.Collections;
3

    
4
public class SimpleBackgroundRemoval : MonoBehaviour 
5
{
6
	[Tooltip("Whether to display the foreground texture on the screen or not.")]
7
	public bool displayForeground = true;
8

    
9
	[Tooltip("Please follow these instructions to make this component work.")]
10
	[Multiline]
11
	public string instructions = "Set 'Compute user map'-setting of KinectManager-component to 'Cut-Out Texture'.";
12

    
13

    
14
	// the foreground texture
15
	private Texture2D foregroundTex;
16
	
17
	// rectangle taken by the foreground texture (in pixels)
18
	private Rect foregroundRect;
19

    
20
	// the Kinect manager
21
	private KinectManager manager;
22
	
23

    
24
	void Start () 
25
	{
26
		manager = KinectManager.Instance;
27

    
28
		if(manager && manager.IsInitialized())
29
		{
30
			Rect cameraRect = Camera.main.pixelRect;
31
			float rectHeight = cameraRect.height;
32
			float rectWidth = cameraRect.width;
33

    
34
			KinectInterop.SensorData sensorData = manager.GetSensorData();
35

    
36
			if(sensorData != null && sensorData.sensorInterface != null)
37
			{
38
				if(rectWidth > rectHeight)
39
					rectWidth = rectHeight * sensorData.depthImageWidth / sensorData.depthImageHeight;
40
				else
41
					rectHeight = rectWidth * sensorData.depthImageHeight / sensorData.depthImageWidth;
42
				
43
				foregroundRect = new Rect((cameraRect.width - rectWidth) / 2, cameraRect.height - (cameraRect.height - rectHeight) / 2, rectWidth, -rectHeight);
44
			}
45
		}
46
	}
47
	
48
	void Update () 
49
	{
50
		if(manager && manager.IsInitialized())
51
		{
52
			foregroundTex = manager.GetUsersLblTex();
53
		}
54
	}
55

    
56
	void OnGUI()
57
	{
58
		if(displayForeground && foregroundTex)
59
		{
60
			// get the foreground rectangle (use the portrait background, if available)
61
			PortraitBackground portraitBack = PortraitBackground.Instance;
62
			if(portraitBack && portraitBack.enabled)
63
			{
64
				foregroundRect = portraitBack.GetBackgroundRect();
65

    
66
				foregroundRect.y += foregroundRect.height;  // invert y
67
				foregroundRect.height = -foregroundRect.height;
68
			}
69
			
70
			GUI.DrawTexture(foregroundRect, foregroundTex);
71
		}
72
	}
73
	
74
}