프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / SkeletonOverlayer.cs @ 3

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

1
using UnityEngine;
2
using System.Collections;
3
//using Windows.Kinect;
4

    
5

    
6
public class SkeletonOverlayer : MonoBehaviour 
7
{
8
	[Tooltip("GUI-texture used to display the color camera feed on the scene background.")]
9
	public GUITexture backgroundImage;
10

    
11
	[Tooltip("Camera that will be used to overlay the 3D-objects over the background.")]
12
	public Camera foregroundCamera;
13
	
14
	[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
15
	public int playerIndex = 0;
16
	
17
	[Tooltip("Game object used to overlay the joints.")]
18
	public GameObject jointPrefab;
19

    
20
	[Tooltip("Line object used to overlay the bones.")]
21
	public LineRenderer linePrefab;
22
	//public float smoothFactor = 10f;
23

    
24
	//public GUIText debugText;
25
	
26
	private GameObject[] joints = null;
27
	private LineRenderer[] lines = null;
28

    
29
	private Quaternion initialRotation = Quaternion.identity;
30

    
31

    
32
	void Start()
33
	{
34
		KinectManager manager = KinectManager.Instance;
35

    
36
		if(manager && manager.IsInitialized())
37
		{
38
			int jointsCount = manager.GetJointCount();
39

    
40
			if(jointPrefab)
41
			{
42
				// array holding the skeleton joints
43
				joints = new GameObject[jointsCount];
44
				
45
				for(int i = 0; i < joints.Length; i++)
46
				{
47
					joints[i] = Instantiate(jointPrefab) as GameObject;
48
					joints[i].transform.parent = transform;
49
					joints[i].name = ((KinectInterop.JointType)i).ToString();
50
					joints[i].SetActive(false);
51
				}
52
			}
53

    
54
			// array holding the skeleton lines
55
			lines = new LineRenderer[jointsCount];
56

    
57
//			if(linePrefab)
58
//			{
59
//				for(int i = 0; i < lines.Length; i++)
60
//				{
61
//					lines[i] = Instantiate(linePrefab) as LineRenderer;
62
//					lines[i].transform.parent = transform;
63
//					lines[i].gameObject.SetActive(false);
64
//				}
65
//			}
66
		}
67

    
68
		// always mirrored
69
		initialRotation = Quaternion.Euler(new Vector3(0f, 180f, 0f));
70

    
71
		if (!foregroundCamera) 
72
		{
73
			// by default - the main camera
74
			foregroundCamera = Camera.main;
75
		}
76
	}
77
	
78
	void Update () 
79
	{
80
		KinectManager manager = KinectManager.Instance;
81
		
82
		if(manager && manager.IsInitialized() && foregroundCamera)
83
		{
84
			//backgroundImage.renderer.material.mainTexture = manager.GetUsersClrTex();
85
			if(backgroundImage && (backgroundImage.texture == null))
86
			{
87
				backgroundImage.texture = manager.GetUsersClrTex();
88
			}
89

    
90
			// get the background rectangle (use the portrait background, if available)
91
			Rect backgroundRect = foregroundCamera.pixelRect;
92
			PortraitBackground portraitBack = PortraitBackground.Instance;
93
			
94
			if(portraitBack && portraitBack.enabled)
95
			{
96
				backgroundRect = portraitBack.GetBackgroundRect();
97
			}
98

    
99
			// overlay all joints in the skeleton
100
			if(manager.IsUserDetected(playerIndex))
101
			{
102
				long userId = manager.GetUserIdByIndex(playerIndex);
103
				int jointsCount = manager.GetJointCount();
104

    
105
				for(int i = 0; i < jointsCount; i++)
106
				{
107
					int joint = i;
108

    
109
					if(manager.IsJointTracked(userId, joint))
110
					{
111
						Vector3 posJoint = manager.GetJointPosColorOverlay(userId, joint, foregroundCamera, backgroundRect);
112
						//Vector3 posJoint = manager.GetJointPosition(userId, joint);
113

    
114
						if(joints != null)
115
						{
116
							// overlay the joint
117
							if(posJoint != Vector3.zero)
118
							{
119
//								if(debugText && joint == 0)
120
//								{
121
//									debugText.text = string.Format("{0} - {1}\nRealPos: {2}", 
122
//									                                       (KinectInterop.JointType)joint, posJoint,
123
//									                                       manager.GetJointPosition(userId, joint));
124
//								}
125
								
126
								joints[i].SetActive(true);
127
								joints[i].transform.position = posJoint;
128

    
129
								Quaternion rotJoint = manager.GetJointOrientation(userId, joint, false);
130
								rotJoint = initialRotation * rotJoint;
131
								joints[i].transform.rotation = rotJoint;
132
							}
133
							else
134
							{
135
								joints[i].SetActive(false);
136
							}
137
						}
138

    
139
						if(lines[i] == null && linePrefab != null)
140
						{
141
							lines[i] = Instantiate(linePrefab) as LineRenderer;
142
							lines[i].transform.parent = transform;
143
							lines[i].gameObject.SetActive(false);
144
						}
145

    
146
						if(lines[i] != null)
147
						{
148
							// overlay the line to the parent joint
149
							int jointParent = (int)manager.GetParentJoint((KinectInterop.JointType)joint);
150
							Vector3 posParent = manager.GetJointPosColorOverlay(userId, jointParent, foregroundCamera, backgroundRect);
151

    
152
							if(posJoint != Vector3.zero && posParent != Vector3.zero)
153
							{
154
								lines[i].gameObject.SetActive(true);
155
								
156
								//lines[i].SetVertexCount(2);
157
								lines[i].SetPosition(0, posParent);
158
								lines[i].SetPosition(1, posJoint);
159
							}
160
							else
161
							{
162
								lines[i].gameObject.SetActive(false);
163
							}
164
						}
165
						
166
					}
167
					else
168
					{
169
						if(joints != null)
170
						{
171
							joints[i].SetActive(false);
172
						}
173
						
174
						if(lines[i] != null)
175
						{
176
							lines[i].gameObject.SetActive(false);
177
						}
178
					}
179
				}
180

    
181
			}
182
		}
183
	}
184

    
185
}