forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTapToPlace.cs
More file actions
252 lines (217 loc) · 9.66 KB
/
TapToPlace.cs
File metadata and controls
252 lines (217 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;
namespace HoloToolkit.Unity.SpatialMapping
{
/// <summary>
/// The TapToPlace class is a basic way to enable users to move objects
/// and place them on real world surfaces.
/// Put this script on the object you want to be able to move.
/// Users will be able to tap objects, gaze elsewhere, and perform the tap gesture again to place.
/// This script is used in conjunction with GazeManager, WorldAnchorManager, and SpatialMappingManager.
/// </summary>
[RequireComponent(typeof(Collider))]
[RequireComponent(typeof(Interpolator))]
public class TapToPlace : MonoBehaviour, IInputClickHandler
{
[Tooltip("Distance from camera to keep the object while placing it.")]
public float DefaultGazeDistance = 2.0f;
[Tooltip("Place parent on tap instead of current game object.")]
public bool PlaceParentOnTap;
[Tooltip("Specify the parent game object to be moved on tap, if the immediate parent is not desired.")]
public GameObject ParentGameObjectToPlace;
/// <summary>
/// Keeps track of if the user is moving the object or not.
/// Setting this to true will enable the user to move and place the object in the scene.
/// Useful when you want to place an object immediately.
/// </summary>
[Tooltip("Setting this to true will enable the user to move and place the object in the scene without needing to tap on the object. Useful when you want to place an object immediately.")]
public bool IsBeingPlaced;
[Tooltip("Setting this to true will allow this behavior to control the DrawMesh property on the spatial mapping.")]
public bool AllowMeshVisualizationControl = true;
[Tooltip("Should the center of the Collider be used instead of the gameObjects world transform.")]
public bool UseColliderCenter;
private Interpolator interpolator;
/// <summary>
/// The default ignore raycast layer built into unity.
/// </summary>
private const int IgnoreRaycastLayer = 2;
private Dictionary<GameObject, int> layerCache = new Dictionary<GameObject, int>();
private Vector3 PlacementPosOffset;
protected virtual void Start()
{
if (PlaceParentOnTap)
{
ParentGameObjectToPlace = GetParentToPlace();
PlaceParentOnTap = ParentGameObjectToPlace != null;
}
interpolator = EnsureInterpolator();
if (IsBeingPlaced)
{
StartPlacing();
}
else // If we are not starting out with actively placing the object, give it a World Anchor
{
AttachWorldAnchor();
}
}
private void OnEnable()
{
Bounds bounds = transform.GetColliderBounds();
PlacementPosOffset = transform.position - bounds.center;
}
/// <summary>
/// Returns the predefined GameObject or the immediate parent when it exists
/// </summary>
/// <returns></returns>
private GameObject GetParentToPlace()
{
if (ParentGameObjectToPlace)
{
return ParentGameObjectToPlace;
}
return gameObject.transform.parent ? gameObject.transform.parent.gameObject : null;
}
/// <summary>
/// Ensures an interpolator on either the parent or on the GameObject itself and returns it.
/// </summary>
private Interpolator EnsureInterpolator()
{
var interpolatorHolder = PlaceParentOnTap ? ParentGameObjectToPlace : gameObject;
return interpolatorHolder.EnsureComponent<Interpolator>();
}
protected virtual void Update()
{
if (!IsBeingPlaced) { return; }
Transform cameraTransform = CameraCache.Main.transform;
Vector3 placementPosition = GetPlacementPosition(cameraTransform.position, cameraTransform.forward, DefaultGazeDistance);
if (UseColliderCenter)
{
placementPosition += PlacementPosOffset;
}
// Here is where you might consider adding intelligence
// to how the object is placed. For example, consider
// placing based on the bottom of the object's
// collider so it sits properly on surfaces.
if (PlaceParentOnTap)
{
placementPosition = ParentGameObjectToPlace.transform.position + (placementPosition - gameObject.transform.position);
}
// update the placement to match the user's gaze.
interpolator.SetTargetPosition(placementPosition);
// Rotate this object to face the user.
interpolator.SetTargetRotation(Quaternion.Euler(0, cameraTransform.localEulerAngles.y, 0));
}
public virtual void OnInputClicked(InputClickedEventData eventData)
{
// On each tap gesture, toggle whether the user is in placing mode.
IsBeingPlaced = !IsBeingPlaced;
HandlePlacement();
eventData.Use();
}
private void HandlePlacement()
{
if (IsBeingPlaced)
{
StartPlacing();
}
else
{
StopPlacing();
}
}
private void StartPlacing()
{
var layerCacheTarget = PlaceParentOnTap ? ParentGameObjectToPlace : gameObject;
layerCacheTarget.SetLayerRecursively(IgnoreRaycastLayer, out layerCache);
InputManager.Instance.PushModalInputHandler(gameObject);
ToggleSpatialMesh();
RemoveWorldAnchor();
}
private void StopPlacing()
{
var layerCacheTarget = PlaceParentOnTap ? ParentGameObjectToPlace : gameObject;
layerCacheTarget.ApplyLayerCacheRecursively(layerCache);
InputManager.Instance.PopModalInputHandler();
ToggleSpatialMesh();
AttachWorldAnchor();
}
private void AttachWorldAnchor()
{
if (WorldAnchorManager.Instance != null)
{
// Add world anchor when object placement is done.
WorldAnchorManager.Instance.AttachAnchor(PlaceParentOnTap ? ParentGameObjectToPlace : gameObject);
}
}
private void RemoveWorldAnchor()
{
if (WorldAnchorManager.Instance != null)
{
//Removes existing world anchor if any exist.
WorldAnchorManager.Instance.RemoveAnchor(PlaceParentOnTap ? ParentGameObjectToPlace : gameObject);
}
}
/// <summary>
/// If the user is in placing mode, display the spatial mapping mesh.
/// </summary>
private void ToggleSpatialMesh()
{
if (SpatialMappingManager.Instance != null && AllowMeshVisualizationControl)
{
SpatialMappingManager.Instance.DrawVisualMeshes = IsBeingPlaced;
}
}
/// <summary>
/// If we're using the spatial mapping, check to see if we got a hit, else use the gaze position.
/// </summary>
/// <returns>Placement position in front of the user</returns>
private static Vector3 GetPlacementPosition(Vector3 headPosition, Vector3 gazeDirection, float defaultGazeDistance)
{
RaycastHit hitInfo;
if (SpatialMappingRaycast(headPosition, gazeDirection, out hitInfo))
{
return hitInfo.point;
}
return GetGazePlacementPosition(headPosition, gazeDirection, defaultGazeDistance);
}
/// <summary>
/// Does a raycast on the spatial mapping layer to try to find a hit.
/// </summary>
/// <param name="origin">Origin of the raycast</param>
/// <param name="direction">Direction of the raycast</param>
/// <param name="spatialMapHit">Result of the raycast when a hit occurred</param>
/// <returns>Whether it found a hit or not</returns>
private static bool SpatialMappingRaycast(Vector3 origin, Vector3 direction, out RaycastHit spatialMapHit)
{
if (SpatialMappingManager.Instance != null)
{
RaycastHit hitInfo;
if (Physics.Raycast(origin, direction, out hitInfo, 30.0f, SpatialMappingManager.Instance.LayerMask))
{
spatialMapHit = hitInfo;
return true;
}
}
spatialMapHit = new RaycastHit();
return false;
}
/// <summary>
/// Get placement position either from GazeManager hit or in front of the user as backup
/// </summary>
/// <param name="headPosition">Position of the users head</param>
/// <param name="gazeDirection">Gaze direction of the user</param>
/// <param name="defaultGazeDistance">Default placement distance in front of the user</param>
/// <returns>Placement position in front of the user</returns>
private static Vector3 GetGazePlacementPosition(Vector3 headPosition, Vector3 gazeDirection, float defaultGazeDistance)
{
if (GazeManager.Instance.HitObject != null)
{
return GazeManager.Instance.HitPosition;
}
return headPosition + gazeDirection * defaultGazeDistance;
}
}
}