프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / VariousDemos / Scripts / UserHeadCamera.cs @ 3

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

1
using UnityEngine;
2
using System.Collections;
3

    
4
public class UserHeadCamera : MonoBehaviour 
5
{
6
	[Tooltip("Index of the player, tracked by this component. -1 means all players, 0 - the 1st player only, 1 - the 2nd player only, etc.")]
7
	public int playerIndex = 0;
8

    
9
	[Tooltip("Kinect joint used to control the camera.")]
10
	public KinectInterop.JointType playerJoint = KinectInterop.JointType.Head;
11

    
12
	[Tooltip("Whether the camera view is mirrored or not.")]
13
	public bool mirroredView = false;
14

    
15
	[Tooltip("Kinect origin position.")]
16
	public Vector3 originPosition = Vector3.zero;
17
	
18
	[Tooltip("Whether to apply the joint rotation to the camera.")]
19
	public bool applyJointRotation = false;
20

    
21
	[Tooltip("Initial camera rotation.")]
22
	public Vector3 initialRotation = Vector3.zero;
23
	
24
	[Tooltip("Whether the z-movement is inverted or not.")]
25
	public bool invertedZMovement = false;
26
	
27
	[Tooltip("Smooth factor used for the camera orientation.")]
28
	public float smoothFactor = 5f;
29
	
30

    
31
	private KinectManager kinectManager = null;
32
	private Quaternion initialHeadRot;
33

    
34

    
35
	void Start () 
36
	{
37
		kinectManager = KinectManager.Instance;
38
		initialHeadRot = (mirroredView ? Quaternion.Euler(0f, 180f, 0f) : Quaternion.identity) * Quaternion.Euler(initialRotation);
39
	}
40
	
41
	void Update () 
42
	{
43
		if(kinectManager && kinectManager.IsInitialized())
44
		{
45
			long userId = kinectManager.GetUserIdByIndex(playerIndex);
46

    
47
			if(userId != 0 && kinectManager.IsJointTracked(userId, (int)playerJoint))
48
			{
49
				Vector3 headPos = kinectManager.GetJointPosition(userId, (int)playerJoint);
50
				if(invertedZMovement)
51
				{
52
					headPos.z = -headPos.z;
53
				}
54

    
55
				headPos += originPosition;
56
				transform.position = headPos + transform.forward * 0.1f;
57

    
58
				if(applyJointRotation)
59
				{
60
					Quaternion headRot = kinectManager.GetJointOrientation(userId, (int)playerJoint, !mirroredView);
61

    
62
					Vector3 jointDir = kinectManager.GetJointDirection (userId, (int)playerJoint, mirroredView, invertedZMovement);
63
					Quaternion invPitchRot = Quaternion.FromToRotation (jointDir, Vector3.up);
64
					headRot = headRot * invPitchRot;
65

    
66
					transform.rotation = Quaternion.Slerp(transform.rotation, initialHeadRot * headRot, smoothFactor * Time.deltaTime);
67
				}
68
			}
69
		}
70
	}
71
}