프로젝트

일반

사용자정보

통계
| 개정판:

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

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

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

    
5

    
6
public class JointPositionView : MonoBehaviour 
7
{
8
	[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
9
	public int playerIndex = 0;
10

    
11
	[Tooltip("The Kinect joint we want to track.")]
12
	public KinectInterop.JointType trackedJoint = KinectInterop.JointType.SpineBase;
13

    
14
	[Tooltip("Whether the movement is relative to transform's initial position, or is in absolute coordinates.")]
15
	public bool relToInitialPos = false;
16
	
17
	[Tooltip("Whether the z-movement is inverted or not.")]
18
	public bool invertedZMovement = false;
19

    
20
	[Tooltip("Transform offset to the Kinect-reported position.")]
21
	public Vector3 transformOffset = Vector3.zero;
22

    
23
	[Tooltip("Whether the displayed position is in Kinect coordinates, or in world coordinates.")]
24
	public bool useKinectSpace = false;
25

    
26
	//public bool moveTransform = true;
27

    
28
	[Tooltip("Smooth factor used for the joint position smoothing.")]
29
	public float smoothFactor = 5f;
30

    
31
	[Tooltip("GUI-Text to display the current joint position.")]
32
	public GUIText debugText;
33

    
34

    
35
	private Vector3 initialPosition = Vector3.zero;
36
	private long currentUserId = 0;
37
	private Vector3 initialUserOffset = Vector3.zero;
38

    
39
	private Vector3 vPosJoint = Vector3.zero;
40

    
41

    
42
	void Start()
43
	{
44
		initialPosition = transform.position;
45
	}
46
	
47
	void Update () 
48
	{
49
		KinectManager manager = KinectManager.Instance;
50
		
51
		if(manager && manager.IsInitialized())
52
		{
53
			int iJointIndex = (int)trackedJoint;
54

    
55
			if(manager.IsUserDetected(playerIndex))
56
			{
57
				long userId = manager.GetUserIdByIndex(playerIndex);
58
				
59
				if(manager.IsJointTracked(userId, iJointIndex))
60
				{
61
					if(useKinectSpace)
62
						vPosJoint = manager.GetJointKinectPosition(userId, iJointIndex);
63
					else
64
						vPosJoint = manager.GetJointPosition(userId, iJointIndex);
65

    
66
					vPosJoint.z = invertedZMovement ? -vPosJoint.z : vPosJoint.z;
67
					vPosJoint += transformOffset;
68

    
69
					if(userId != currentUserId)
70
					{
71
						currentUserId = userId;
72
						initialUserOffset = vPosJoint;
73
					}
74

    
75
					Vector3 vPosObject = relToInitialPos ? initialPosition + (vPosJoint - initialUserOffset) : vPosJoint;
76

    
77
					if(debugText)
78
					{
79
						debugText.text = string.Format("{0} - ({1:F3}, {2:F3}, {3:F3})", trackedJoint, 
80
						                                                       vPosObject.x, vPosObject.y, vPosObject.z);
81
					}
82

    
83
					//if(moveTransform)
84
					{
85
						if(smoothFactor != 0f)
86
							transform.position = Vector3.Lerp(transform.position, vPosObject, smoothFactor * Time.deltaTime);
87
						else
88
							transform.position = vPosObject;
89
					}
90
				}
91
				
92
			}
93
			
94
		}
95
	}
96
}