프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / KinectDemos / InteractionDemo / Scripts / GrabRotateScript.cs @ 3

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

1
using UnityEngine;
2
using System.Collections;
3

    
4
public class GrabRotateScript : MonoBehaviour, InteractionListenerInterface
5
{
6
	[Tooltip("Material used to outline the object when selected.")]
7
	public Material selectedObjectMaterial;
8

    
9
	[Tooltip("Smooth factor used for object rotations.")]
10
	public float smoothFactor = 3.0f;
11

    
12
	[Tooltip("Camera used for screen ray-casting. This is usually the main camera.")]
13
	public Camera screenCamera;
14

    
15
	[Tooltip("GUI-Text used to display information messages.")]
16
	public GUIText infoGuiText;
17

    
18
	[Tooltip("Interaction manager instance, used to detect hand interactions. If left empty, it will be the first interaction manager found in the scene.")]
19
	public InteractionManager interactionManager;
20

    
21

    
22
	//private bool isLeftHandDrag = false;
23
	private InteractionManager.HandEventType lastHandEvent = InteractionManager.HandEventType.None;
24
	private Vector3 screenNormalPos = Vector3.zero;
25

    
26
	private GameObject selectedObject;
27
	private Material savedObjectMaterial;
28

    
29

    
30
	void Start()
31
	{
32
		// by default set the main-camera to be screen-camera
33
		if (screenCamera == null) 
34
		{
35
			screenCamera = Camera.main;
36
		}
37

    
38
		// get the interaction manager instance
39
		if (interactionManager == null) 
40
		{
41
			interactionManager = InteractionManager.Instance;
42
		}
43
	}
44

    
45

    
46
	void Update() 
47
	{
48
		if(interactionManager != null && interactionManager.IsInteractionInited())
49
		{
50
			Vector3 screenPixelPos = Vector3.zero;
51

    
52
			if(selectedObject == null)
53
			{
54
				// no object is currently selected or dragged.
55
				// check if there is an underlying object to be selected
56
				if(lastHandEvent == InteractionManager.HandEventType.Grip && screenNormalPos != Vector3.zero)
57
				{
58
					// convert the normalized screen pos to pixel pos
59
					screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();
60

    
61
					screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
62
					screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));
63
					Ray ray = screenCamera ? screenCamera.ScreenPointToRay(screenPixelPos) : new Ray();
64

    
65
					// check for underlying objects
66
					RaycastHit hit;
67
					if(Physics.Raycast(ray, out hit))
68
					{
69
						if(hit.collider.gameObject == gameObject)
70
						{
71
							selectedObject = gameObject;
72
						
73
							savedObjectMaterial = selectedObject.GetComponent<Renderer>().material;
74
							selectedObject.GetComponent<Renderer>().material = selectedObjectMaterial;
75
						}
76
					}
77
				}
78
			}
79
			else
80
			{
81
				// continue dragging the object
82
				screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();
83

    
84
				float angleArounfY = screenNormalPos.x * 360f;  // horizontal rotation
85
				float angleArounfX = screenNormalPos.y * 360f;  // vertical rotation
86

    
87
				Vector3 vObjectRotation = new Vector3(-angleArounfX, -angleArounfY, 180f);
88
				Quaternion qObjectRotation = screenCamera ? screenCamera.transform.rotation * Quaternion.Euler(vObjectRotation) : Quaternion.Euler(vObjectRotation);
89

    
90
				selectedObject.transform.rotation = Quaternion.Slerp(selectedObject.transform.rotation, qObjectRotation, smoothFactor * Time.deltaTime);
91

    
92
				// check if the object (hand grip) was released
93
				bool isReleased = lastHandEvent == InteractionManager.HandEventType.Release;
94

    
95
				if(isReleased)
96
				{
97
					// restore the object's material and stop dragging the object
98
					selectedObject.GetComponent<Renderer>().material = savedObjectMaterial;
99
					selectedObject = null;
100
				}
101
			}
102

    
103
		}
104

    
105
	}
106

    
107

    
108
	void OnGUI()
109
	{
110
		if(infoGuiText != null && interactionManager != null && interactionManager.IsInteractionInited())
111
		{
112
			string sInfo = string.Empty;
113
			
114
			long userID = interactionManager.GetUserID();
115
			if(userID != 0)
116
			{
117
				if(selectedObject != null)
118
					sInfo =  selectedObject.name + " grabbed.\nYou can turn it now in all directions.";
119
				else
120
					sInfo = "Grab the object to turn it.";
121
			}
122
			else
123
			{
124
				KinectManager kinectManager = KinectManager.Instance;
125

    
126
				if(kinectManager && kinectManager.IsInitialized())
127
				{
128
					sInfo = "Waiting for Users...";
129
				}
130
				else
131
				{
132
					sInfo = "Kinect is not initialized. Check the log for details.";
133
				}
134
			}
135
			
136
			infoGuiText.text = sInfo;
137
		}
138
	}
139

    
140

    
141
	public void HandGripDetected(long userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
142
	{
143
		if (!isHandInteracting || !interactionManager)
144
			return;
145
		if (userId != interactionManager.GetUserID())
146
			return;
147

    
148
		lastHandEvent = InteractionManager.HandEventType.Grip;
149
		//isLeftHandDrag = !isRightHand;
150
		screenNormalPos = handScreenPos;
151
	}
152

    
153
	public void HandReleaseDetected(long userId, int userIndex, bool isRightHand, bool isHandInteracting, Vector3 handScreenPos)
154
	{
155
		if (!isHandInteracting || !interactionManager)
156
			return;
157
		if (userId != interactionManager.GetUserID())
158
			return;
159

    
160
		lastHandEvent = InteractionManager.HandEventType.Release;
161
		//isLeftHandDrag = !isRightHand;
162
		screenNormalPos = handScreenPos;
163
	}
164

    
165
	public bool HandClickDetected(long userId, int userIndex, bool isRightHand, Vector3 handScreenPos)
166
	{
167
		return true;
168
	}
169

    
170

    
171
}