AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CursorModifier.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 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity.InputModule
7 {
12  public class CursorModifier : MonoBehaviour, ICursorModifier
13  {
14  [Tooltip("Transform for which this cursor modifier applies its various properties.")]
15  public Transform HostTransform;
16 
17  [Tooltip("How much a cursor should be offset from the surface of the object when overlapping.")]
18  public Vector3 CursorOffset = Vector3.zero;
19 
20  [Tooltip("Direction of the cursor offset.")]
21  public Vector3 CursorNormal = Vector3.back;
22 
23  [Tooltip("Scale of the cursor when looking at this object.")]
24  public Vector3 CursorScaleOffset = Vector3.one;
25 
26  [Tooltip("Should the cursor snap to the object.")]
27  public bool SnapCursor = false;
28 
29  [Tooltip("If true, the normal from the pointing vector will be used to orient the cursor " +
30  "instead of the targeted object's normal at point of contact.")]
31  public bool UseGazeBasedNormal = false;
32 
33  [Tooltip("Should the cursor be hiding when this object is focused.")]
34  public bool HideCursorOnFocus = false;
35 
36  [Tooltip("Cursor animation parameters to set when this object is focused. Leave empty for none.")]
38 
39  private void Awake()
40  {
41  if (HostTransform == null)
42  {
43  HostTransform = transform;
44  }
45  }
46 
51  public bool GetCursorVisibility()
52  {
53  return HideCursorOnFocus;
54  }
55 
56  public Vector3 GetModifiedPosition(ICursor cursor)
57  {
58  Vector3 position;
59 
60  if (SnapCursor)
61  {
62  // Snap if the targeted object has a cursor modifier that supports snapping
63  position = HostTransform.position +
64  HostTransform.TransformVector(CursorOffset);
65  }
66  else
67  {
68  FocusDetails focusDetails = FocusManager.Instance.GetFocusDetails(cursor.Pointer);
69 
70  // Else, consider the modifiers on the cursor modifier, but don't snap
71  position = focusDetails.Point + HostTransform.TransformVector(CursorOffset);
72  }
73 
74  return position;
75  }
76 
77  public Quaternion GetModifiedRotation(ICursor cursor)
78  {
79  Quaternion rotation;
80 
81  RayStep lastStep = cursor.Pointer.Rays[cursor.Pointer.Rays.Length - 1];
82  Vector3 forward = UseGazeBasedNormal ? -lastStep.Direction : HostTransform.rotation * CursorNormal;
83 
84  // Determine the cursor forward
85  if (forward.magnitude > 0)
86  {
87  rotation = Quaternion.LookRotation(forward, Vector3.up);
88  }
89  else
90  {
91  rotation = cursor.Rotation;
92  }
93 
94  return rotation;
95  }
96 
97  public Vector3 GetModifiedScale(ICursor cursor)
98  {
99  return CursorScaleOffset;
100  }
101 
102  public void GetModifiedTransform(ICursor cursor, out Vector3 position, out Quaternion rotation, out Vector3 scale)
103  {
104  position = GetModifiedPosition(cursor);
105  rotation = GetModifiedRotation(cursor);
106  scale = GetModifiedScale(cursor);
107  }
108  }
109 }
Component that can be added to any game object with a collider to modify how a cursor reacts when on ...
Vector3 GetModifiedPosition(ICursor cursor)
Returns the cursor position after considering this modifier.
Quaternion Rotation
Rotation of the cursor.
Definition: ICursor.cs:26
Quaternion GetModifiedRotation(ICursor cursor)
Returns the cursor rotation after considering this modifier.
Vector3 GetModifiedScale(ICursor cursor)
Returns the cursor local scale after considering this modifier.
IPointingSource Pointer
The pointer this cursor is associated with.
Definition: ICursor.cs:16
FocusDetails struct contains information about which game object has the focus currently. Also contains information about the normal of that point.
Definition: FocusDetails.cs:14
Cursor Interface for handling input events and setting visibility.
Definition: ICursor.cs:11
Created a copy of the AnimatorControllerParameter because that class is not Serializable and cannot b...
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
void GetModifiedTransform(ICursor cursor, out Vector3 position, out Quaternion rotation, out Vector3 scale)
Returns the modified transform for the cursor after considering this modifier.
Focus manager is the bridge that handles different types of pointing sources like gaze cursor or poin...
Definition: FocusManager.cs:16
bool GetCursorVisibility()
Return whether or not hide the cursor
Cursor Modifier Interface that provides basic overrides for cursor behavior.