프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / FaceTrackingDemo / Scripts / ModelHatController.cs @ 3

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

1
using UnityEngine;
2
using System.Collections;
3

    
4
public class ModelHatController : MonoBehaviour 
5
{
6
	[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
7
	public int playerIndex = 0;
8
	
9
	[Tooltip("Camera used to estimate the overlay positions of 3D-objects over the background.")]
10
	public Camera foregroundCamera;
11
	
12
	[Tooltip("Vertical offset of the hat above the head position (in meters).")]
13
	public float verticalOffset = 0f;
14

    
15
	[Tooltip("Scale factor for the hat model.")]
16
	[Range(0.1f, 2.0f)]
17
	public float modelScaleFactor = 1f;
18
	
19
	[Tooltip("Smooth factor used for hat-model rotation.")]
20
	public float smoothFactorRotation = 10f;
21

    
22
	[Tooltip("Smooth factor used for hat-model movement.")]
23
	public float smoothFactorMovement = 0f;
24
	
25
	private KinectManager kinectManager;
26
	private FacetrackingManager faceManager;
27
	private Quaternion initialRotation;
28

    
29

    
30
	void Start () 
31
	{
32
		initialRotation = transform.rotation;
33
		transform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
34
	}
35
	
36
	void Update () 
37
	{
38
		// get the face-tracking manager instance
39
		if(faceManager == null)
40
		{
41
			kinectManager = KinectManager.Instance;
42
			faceManager = FacetrackingManager.Instance;
43
		}
44

    
45
		// get user-id by user-index
46
		long userId = kinectManager ? kinectManager.GetUserIdByIndex(playerIndex) : 0;
47

    
48
		if(kinectManager && kinectManager.IsInitialized() && userId != 0 &&
49
			faceManager && faceManager.IsTrackingFace(userId) && foregroundCamera)
50
		{
51
			// get head position
52
			Vector3 newPosition = faceManager.GetHeadPosition(userId, true);
53
			
54
			// get head rotation
55
			Quaternion newRotation = initialRotation * faceManager.GetHeadRotation(userId, true);
56

    
57
			// rotational fix, provided by Richard Borys:
58
			// The added rotation fixes rotational error that occurs when person is not centered in the middle of the kinect
59
			Vector3 addedRotation = newPosition.z != 0f ? new Vector3(Mathf.Rad2Deg * (Mathf.Tan(newPosition.y) / newPosition.z),
60
				Mathf.Rad2Deg * (Mathf.Tan(newPosition.x) / newPosition.z), 0) : Vector3.zero;
61
			
62
			addedRotation.x = newRotation.eulerAngles.x + addedRotation.x;
63
			addedRotation.y = newRotation.eulerAngles.y + addedRotation.y;
64
			addedRotation.z = newRotation.eulerAngles.z + addedRotation.z;
65
			
66
			newRotation = Quaternion.Euler(addedRotation.x, addedRotation.y, addedRotation.z);
67
			// end of rotational fix
68

    
69
			if(smoothFactorRotation != 0f)
70
				transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, smoothFactorRotation * Time.deltaTime);
71
			else
72
				transform.rotation = newRotation;
73

    
74
			// get the background rectangle (use the portrait background, if available)
75
			Rect backgroundRect = foregroundCamera.pixelRect;
76
			PortraitBackground portraitBack = PortraitBackground.Instance;
77
			
78
			if(portraitBack && portraitBack.enabled)
79
			{
80
				backgroundRect = portraitBack.GetBackgroundRect();
81
			}
82
			
83
			// model position
84
			newPosition = kinectManager.GetJointPosColorOverlay(userId, (int)KinectInterop.JointType.Head, foregroundCamera, backgroundRect);
85
			if(newPosition == Vector3.zero)
86
			{
87
				// hide the model behind the camera
88
				newPosition.z = -10f;
89
			}
90
			
91
			if(verticalOffset != 0f)
92
			{
93
				// add the vertical offset
94
				Vector3 dirHead = new Vector3(0, verticalOffset, 0);
95
				dirHead = transform.InverseTransformDirection(dirHead);
96
				newPosition += dirHead;
97
			}
98

    
99
			// go to the new position
100
			if(smoothFactorMovement != 0f && transform.position.z >= 0f)
101
				transform.position = Vector3.Lerp(transform.position, newPosition, smoothFactorMovement * Time.deltaTime);
102
			else
103
				transform.position = newPosition;
104

    
105
			// scale the model if needed
106
			if(transform.localScale.x != modelScaleFactor)
107
			{
108
				transform.localScale = new Vector3(modelScaleFactor, modelScaleFactor, modelScaleFactor);
109
			}
110
		}
111
		else
112
		{
113
			// hide the model behind the camera
114
			if(transform.position.z >= 0f)
115
			{
116
				transform.position = new Vector3(0f, 0f, -10f);
117
			}
118
		}
119
	}
120

    
121
}