프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectScripts / PortraitBackground.cs @ 3

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

1 3 KTH
using UnityEngine;
2
using System.Collections;
3
4
/// <summary>
5
/// Sets the color background image in portrait mode. The aspect ratio of the game view should be set to 9:16 for Kinect-v2 or 3:4 for Kinect-v1.
6
/// </summary>
7
public class PortraitBackground : MonoBehaviour
8
{
9
	[Tooltip("Whether to use the depth-image resolution in the calculation, instead of the color-image resolution.")]
10
	private bool useDepthImageResolution = false;
11
12
	private bool isInitialized = false;
13
	private Rect backgroundRect;
14
	private Rect inScreenRect;
15
	private Rect shaderUvRect;
16
17
	private static PortraitBackground instance = null;
18
19
20
	/// <summary>
21
	/// Gets the singleton PortraitBackground instance.
22
	/// </summary>
23
	/// <value>The PortraitBackground instance.</value>
24
	public static PortraitBackground Instance
25
	{
26
		get
27
		{
28
			return instance;
29
		}
30
	}
31
32
	/// <summary>
33
	/// Determines whether the instance is initialized or not.
34
	/// </summary>
35
	/// <returns><c>true</c> if the instance is initialized; otherwise, <c>false</c>.</returns>
36
	public bool IsInitialized()
37
	{
38
		return isInitialized;
39
	}
40
41
	/// <summary>
42
	/// Gets the background rectangle in pixels. This rectangle can be provided as an argument to the GetJointPosColorOverlay()-KM function.
43
	/// </summary>
44
	/// <returns>The background rectangle, in pixels</returns>
45
	public Rect GetBackgroundRect()
46
	{
47
		return backgroundRect;
48
	}
49
50
	/// <summary>
51
	/// Gets the in-screen rectangle in pixels.
52
	/// </summary>
53
	/// <returns>The in-screen rectangle, in pixels.</returns>
54
	public Rect GetInScreenRect()
55
	{
56
		return inScreenRect;
57
	}
58
59
	/// <summary>
60
	/// Gets the shader uv rectangle. Can be used by custom shaders that need the portrait image uv-offsets and sizes.
61
	/// </summary>
62
	/// <returns>The shader uv rectangle.</returns>
63
	public Rect GetShaderUvRect()
64
	{
65
		return shaderUvRect;
66
	}
67
68
69
	////////////////////////////////////////////////////////////////////////
70
71
72
	void Awake()
73
	{
74
		instance = this;
75
	}
76
77
	void Start ()
78
	{
79
		KinectManager kinectManager = KinectManager.Instance;
80
		if(kinectManager && kinectManager.IsInitialized())
81
		{
82
			float fFactorDW = 0f;
83
			if(!useDepthImageResolution)
84
			{
85
				fFactorDW = (float)kinectManager.GetColorImageWidth() / (float)kinectManager.GetColorImageHeight() -
86
					(float)kinectManager.GetColorImageHeight() / (float)kinectManager.GetColorImageWidth();
87
			}
88
			else
89
			{
90
				fFactorDW = (float)kinectManager.GetDepthImageWidth() / (float)kinectManager.GetDepthImageHeight() -
91
					(float)kinectManager.GetDepthImageHeight() / (float)kinectManager.GetDepthImageWidth();
92
			}
93
94
			float fDeltaWidth = (float)Screen.height * fFactorDW;
95
			float dOffsetX = -fDeltaWidth / 2f;
96
97
			float fFactorSW = 0f;
98
			if(!useDepthImageResolution)
99
			{
100
				fFactorSW = (float)kinectManager.GetColorImageWidth() / (float)kinectManager.GetColorImageHeight();
101
			}
102
			else
103
			{
104
				fFactorSW = (float)kinectManager.GetDepthImageWidth() / (float)kinectManager.GetDepthImageHeight();
105
			}
106
107
			float fScreenWidth = (float)Screen.height * fFactorSW;
108
			float fAbsOffsetX = fDeltaWidth / 2f;
109
110
			GUITexture guiTexture = GetComponent<GUITexture>();
111
			if(guiTexture)
112
			{
113
				guiTexture.pixelInset = new Rect(dOffsetX, 0, fDeltaWidth, 0);
114
			}
115
116
			backgroundRect = new Rect(dOffsetX, 0, fScreenWidth, Screen.height);
117
			inScreenRect = new Rect(fAbsOffsetX, 0, fScreenWidth - fDeltaWidth, Screen.height);
118
			shaderUvRect = new Rect(fAbsOffsetX / fScreenWidth, 0, (fScreenWidth - fDeltaWidth) / fScreenWidth, 1);
119
120
			isInitialized = true;
121
		}
122
	}
123
}