Skip to content

Commit f61436f

Browse files
Merge pull request #805 from StephenHodgson/HTK-uGUIRaycast
Updated GazeManager uGUI raycast calculations & GazeTransform initialization
2 parents 30bf79b + ceafcf5 commit f61436f

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class GazeManager : Singleton<GazeManager>
9393
public PointerEventData UnityUIPointerEvent { get; private set; }
9494

9595
/// <summary>
96-
/// Cached results of racast results.
96+
/// Cached results of raycast results.
9797
/// </summary>
9898
private List<RaycastResult> raycastResultList = new List<RaycastResult>();
9999

@@ -106,26 +106,13 @@ protected override void Awake()
106106
{
107107
RaycastLayerMasks = new LayerMask[] { Physics.DefaultRaycastLayers };
108108
}
109-
}
110109

111-
private void Start()
112-
{
113-
if (GazeTransform == null)
114-
{
115-
if (Camera.main != null)
116-
{
117-
GazeTransform = Camera.main.transform;
118-
}
119-
else
120-
{
121-
Debug.LogError("Gaze Manager was not given a GazeTransform and no main camera exists to default to.");
122-
}
123-
}
110+
FindGazeTransform();
124111
}
125112

126113
private void Update()
127114
{
128-
if (GazeTransform == null)
115+
if (!FindGazeTransform())
129116
{
130117
return;
131118
}
@@ -149,6 +136,20 @@ private void Update()
149136
}
150137
}
151138

139+
private bool FindGazeTransform()
140+
{
141+
if (GazeTransform != null) { return true; }
142+
143+
if (Camera.main != null)
144+
{
145+
GazeTransform = Camera.main.transform;
146+
return true;
147+
}
148+
149+
Debug.LogError("Gaze Manager was not given a GazeTransform and no main camera exists to default to.");
150+
return false;
151+
}
152+
152153
/// <summary>
153154
/// Updates the current gaze information, so that the gaze origin and normal are accurate.
154155
/// </summary>
@@ -225,15 +226,12 @@ private void RaycastUnityUI()
225226
// Graphics raycast
226227
raycastResultList.Clear();
227228
EventSystem.current.RaycastAll(UnityUIPointerEvent, raycastResultList);
228-
RaycastResult uiRaycastResult = FindClosestRaycastHitInLayermasks(raycastResultList, RaycastLayerMasks);
229+
RaycastResult uiRaycastResult = FindClosestRaycastHitInLayerMasks(raycastResultList, RaycastLayerMasks);
229230
UnityUIPointerEvent.pointerCurrentRaycast = uiRaycastResult;
230231

231232
// If we have a raycast result, check if we need to overwrite the 3D raycast info
232233
if (uiRaycastResult.gameObject != null)
233234
{
234-
// Add the near clip distance since this is where the raycast is from
235-
float uiRaycastDistance = uiRaycastResult.distance + Camera.main.nearClipPlane;
236-
237235
bool superseded3DObject = false;
238236
if (IsGazingAtObject)
239237
{
@@ -250,15 +248,15 @@ private void RaycastUnityUI()
250248
}
251249
else if (threeDLayerIndex == uiLayerIndex)
252250
{
253-
if (hitInfo.distance > uiRaycastDistance)
251+
if (hitInfo.distance > uiRaycastResult.distance)
254252
{
255253
superseded3DObject = true;
256254
}
257255
}
258256
}
259257
else
260258
{
261-
if (hitInfo.distance > uiRaycastDistance)
259+
if (hitInfo.distance > uiRaycastResult.distance)
262260
{
263261
superseded3DObject = true;
264262
}
@@ -269,10 +267,10 @@ private void RaycastUnityUI()
269267
if (!IsGazingAtObject || superseded3DObject)
270268
{
271269
IsGazingAtObject = true;
272-
Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(uiRaycastResult.screenPosition.x, uiRaycastResult.screenPosition.y, uiRaycastDistance));
273-
hitInfo = new RaycastHit()
270+
Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(uiRaycastResult.screenPosition.x, uiRaycastResult.screenPosition.y, uiRaycastResult.distance));
271+
hitInfo = new RaycastHit
274272
{
275-
distance = uiRaycastDistance,
273+
distance = uiRaycastResult.distance,
276274
normal = -Camera.main.transform.forward,
277275
point = worldPos
278276
};
@@ -292,7 +290,7 @@ private void RaycastUnityUI()
292290
/// <param name="candidates">List of RaycastResults from a Unity UI raycast</param>
293291
/// <param name="layerMaskList">List of layers to support</param>
294292
/// <returns>RaycastResult if hit, or an empty RaycastResult if nothing was hit</returns>
295-
private RaycastResult FindClosestRaycastHitInLayermasks(List<RaycastResult> candidates, LayerMask[] layerMaskList)
293+
private RaycastResult FindClosestRaycastHitInLayerMasks(List<RaycastResult> candidates, LayerMask[] layerMaskList)
296294
{
297295
int combinedLayerMask = 0;
298296
for (int i = 0; i < layerMaskList.Length; i++)
@@ -313,9 +311,9 @@ private RaycastResult FindClosestRaycastHitInLayermasks(List<RaycastResult> cand
313311
}
314312
}
315313

316-
return minHit ?? new RaycastResult();
314+
return minHit ?? new RaycastResult();
317315
}
318-
316+
319317
/// <summary>
320318
/// Look through the layerMaskList and find the index in that list for which the supplied layer is part of
321319
/// </summary>

0 commit comments

Comments
 (0)