AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SpatialUnderstandingBasicCursor.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
5 using UnityEngine;
6 
7 namespace HoloToolkit.Examples.SpatialUnderstandingFeatureOverview
8 {
14  public class SpatialUnderstandingBasicCursor : MonoBehaviour
15  {
16  public struct RaycastResult
17  {
18  public bool Hit;
19  public Vector3 Position;
20  public Vector3 Normal;
21  }
22 
23  [Tooltip("Distance, in meters, to offset the cursor from the collision point.")]
24  public float DistanceFromCollision = 0.01f;
25 
26  private Quaternion cursorDefaultRotation;
27 
28  private MeshRenderer meshRenderer;
29 
30  private GazeManager gazeManager;
31 
32  protected virtual void Awake()
33  {
34  meshRenderer = gameObject.GetComponent<MeshRenderer>();
35 
36  if (meshRenderer == null)
37  {
38  Debug.LogError("This script requires that your cursor asset has a MeshRenderer component on it.");
39  return;
40  }
41 
42  // Hide the Cursor to begin with.
43  meshRenderer.enabled = false;
44 
45  // Cache the cursor default rotation so the cursor can be rotated with respect to the original rotation.
46  cursorDefaultRotation = gameObject.transform.rotation;
47  }
48 
49  protected virtual void Start()
50  {
51  gazeManager = GazeManager.Instance;
52 
53  if (gazeManager == null)
54  {
55  Debug.LogError("Must have a GazeManager somewhere in the scene.");
56  }
57 
58  if ((GazeManager.Instance.RaycastLayerMasks[0] & (1 << gameObject.layer)) != 0)
59  {
60  Debug.LogError("The cursor has a layer that is checked in the GazeManager's Raycast Layer Mask. Change the cursor layer (e.g.: to Ignore Raycast) or uncheck the layer in GazeManager: " +
61  LayerMask.LayerToName(gameObject.layer));
62  }
63  }
64 
66  {
67  var result = new RaycastResult
68  {
69  Hit = (GazeManager.Instance.HitObject == null) ? false : true,
70  Position = GazeManager.Instance.HitPosition,
71  Normal = GazeManager.Instance.GazeNormal
72  };
73 
74  return result;
75  }
76 
77  protected virtual void LateUpdate()
78  {
79  if (meshRenderer == null || gazeManager == null)
80  {
81  return;
82  }
83 
84  // Calculate the raycast result
85  RaycastResult rayResult = CalculateRayIntersect();
86 
87  // Show or hide the Cursor based on if the user's gaze hit a hologram.
88  meshRenderer.enabled = rayResult.Hit;
89 
90  // Place the cursor at the calculated position.
91  gameObject.transform.position = rayResult.Position + rayResult.Normal * DistanceFromCollision;
92 
93  // Reorient the cursor to match the hit object normal.
94  gameObject.transform.up = rayResult.Normal;
95  gameObject.transform.rotation *= cursorDefaultRotation;
96  }
97  }
98 }
The gaze manager manages everything related to a gaze ray that can interact with other objects...
Definition: GazeManager.cs:13
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26