OVRGazePointer.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using System.Collections;
  15. using UnityEngine.EventSystems;
  16. using UnityEngine.UI;
  17. /// <summary>
  18. /// UI pointer driven by gaze input.
  19. /// </summary>
  20. public class OVRGazePointer : OVRCursor {
  21. private Transform gazeIcon; //the transform that rotates according to our movement
  22. [Tooltip("Should the pointer be hidden when not over interactive objects.")]
  23. public bool hideByDefault = true;
  24. [Tooltip("Time after leaving interactive object before pointer fades.")]
  25. public float showTimeoutPeriod = 1;
  26. [Tooltip("Time after mouse pointer becoming inactive before pointer unfades.")]
  27. public float hideTimeoutPeriod = 0.1f;
  28. [Tooltip("Keep a faint version of the pointer visible while using a mouse")]
  29. public bool dimOnHideRequest = true;
  30. [Tooltip("Angular scale of pointer")]
  31. public float depthScaleMultiplier = 0.03f;
  32. public bool matchNormalOnPhysicsColliders;
  33. /// <summary>
  34. /// The gaze ray.
  35. /// </summary>
  36. public Transform rayTransform;
  37. /// <summary>
  38. /// Is gaze pointer current visible
  39. /// </summary>
  40. public bool hidden { get; private set; }
  41. /// <summary>
  42. /// Current scale applied to pointer
  43. /// </summary>
  44. public float currentScale { get; private set; }
  45. /// <summary>
  46. /// Current depth of pointer from camera
  47. /// </summary>
  48. private float depth;
  49. private float hideUntilTime;
  50. /// <summary>
  51. /// How many times position has been set this frame. Used to detect when there are no position sets in a frame.
  52. /// </summary>
  53. private int positionSetsThisFrame = 0;
  54. /// <summary>
  55. /// Last time code requested the pointer be shown. Usually when pointer passes over interactive elements.
  56. /// </summary>
  57. private float lastShowRequestTime;
  58. /// <summary>
  59. /// Last time pointer was requested to be hidden. Usually mouse pointer activity.
  60. /// </summary>
  61. private float lastHideRequestTime;
  62. // Optionally present GUI element displaying progress when using gaze-to-select mechanics
  63. private OVRProgressIndicator progressIndicator;
  64. private static OVRGazePointer _instance;
  65. public static OVRGazePointer instance
  66. {
  67. // If there's no GazePointer already in the scene, instanciate one now.
  68. get
  69. {
  70. if (_instance == null)
  71. {
  72. Debug.Log(string.Format("Instanciating GazePointer", 0));
  73. _instance = (OVRGazePointer)GameObject.Instantiate((OVRGazePointer)Resources.Load("Prefabs/GazePointerRing", typeof(OVRGazePointer)));
  74. }
  75. return _instance;
  76. }
  77. }
  78. /// <summary>
  79. /// Used to determine alpha level of gaze cursor. Could also be used to determine cursor size, for example, as the cursor fades out.
  80. /// </summary>
  81. public float visibilityStrength
  82. {
  83. get
  84. {
  85. // It's possible there are reasons to show the cursor - such as it hovering over some UI - and reasons to hide
  86. // the cursor - such as another input method (e.g. mouse) being used. We take both of these in to account.
  87. float strengthFromShowRequest;
  88. if (hideByDefault)
  89. {
  90. // fade the cursor out with time
  91. strengthFromShowRequest = Mathf.Clamp01(1 - (Time.time - lastShowRequestTime) / showTimeoutPeriod);
  92. }
  93. else
  94. {
  95. // keep it fully visible
  96. strengthFromShowRequest = 1;
  97. }
  98. // Now consider factors requesting pointer to be hidden
  99. float strengthFromHideRequest;
  100. strengthFromHideRequest = (lastHideRequestTime + hideTimeoutPeriod > Time.time) ? (dimOnHideRequest ? 0.1f : 0) : 1;
  101. // Hide requests take priority
  102. return Mathf.Min(strengthFromShowRequest, strengthFromHideRequest);
  103. }
  104. }
  105. public float SelectionProgress
  106. {
  107. get
  108. {
  109. return progressIndicator ? progressIndicator.currentProgress : 0;
  110. }
  111. set
  112. {
  113. if (progressIndicator)
  114. progressIndicator.currentProgress = value;
  115. }
  116. }
  117. public void Awake()
  118. {
  119. currentScale = 1;
  120. // Only allow one instance at runtime.
  121. if (_instance != null && _instance != this)
  122. {
  123. enabled = false;
  124. DestroyImmediate(this);
  125. return;
  126. }
  127. _instance = this;
  128. gazeIcon = transform.Find("GazeIcon");
  129. progressIndicator = transform.GetComponent<OVRProgressIndicator>();
  130. }
  131. void Update ()
  132. {
  133. if (rayTransform == null && Camera.main != null)
  134. rayTransform = Camera.main.transform;
  135. // Move the gaze cursor to keep it in the middle of the view
  136. transform.position = rayTransform.position + rayTransform.forward * depth;
  137. // Should we show or hide the gaze cursor?
  138. if (visibilityStrength == 0 && !hidden)
  139. {
  140. Hide();
  141. }
  142. else if (visibilityStrength > 0 && hidden)
  143. {
  144. Show();
  145. }
  146. }
  147. /// <summary>
  148. /// Set position and orientation of pointer
  149. /// </summary>
  150. /// <param name="pos"></param>
  151. /// <param name="normal"></param>
  152. public override void SetCursorStartDest(Vector3 _, Vector3 pos, Vector3 normal)
  153. {
  154. transform.position = pos;
  155. if (!matchNormalOnPhysicsColliders) normal = rayTransform.forward;
  156. // Set the rotation to match the normal of the surface it's on.
  157. Quaternion newRot = transform.rotation;
  158. newRot.SetLookRotation(normal, rayTransform.up);
  159. transform.rotation = newRot;
  160. // record depth so that distance doesn't pop when pointer leaves an object
  161. depth = (rayTransform.position - pos).magnitude;
  162. //set scale based on depth
  163. currentScale = depth * depthScaleMultiplier;
  164. transform.localScale = new Vector3(currentScale, currentScale, currentScale);
  165. positionSetsThisFrame++;
  166. RequestShow();
  167. }
  168. public override void SetCursorRay(Transform ray)
  169. {
  170. // We don't do anything here, because we already set this properly by default in Update.
  171. }
  172. void LateUpdate()
  173. {
  174. // This happens after all Updates so we know that if positionSetsThisFrame is zero then nothing set the position this frame
  175. if (positionSetsThisFrame == 0)
  176. {
  177. // No geometry intersections, so gazing into space. Make the cursor face directly at the camera
  178. Quaternion newRot = transform.rotation;
  179. newRot.SetLookRotation(rayTransform.forward, rayTransform.up);
  180. transform.rotation = newRot;
  181. }
  182. Quaternion iconRotation = gazeIcon.rotation;
  183. iconRotation.SetLookRotation(transform.rotation * new Vector3(0, 0, 1));
  184. gazeIcon.rotation = iconRotation;
  185. positionSetsThisFrame = 0;
  186. }
  187. /// <summary>
  188. /// Request the pointer be hidden
  189. /// </summary>
  190. public void RequestHide()
  191. {
  192. if (!dimOnHideRequest)
  193. {
  194. Hide();
  195. }
  196. lastHideRequestTime = Time.time;
  197. }
  198. /// <summary>
  199. /// Request the pointer be shown. Hide requests take priority
  200. /// </summary>
  201. public void RequestShow()
  202. {
  203. Show();
  204. lastShowRequestTime = Time.time;
  205. }
  206. // Disable/Enable child elements when we show/hide the cursor. For performance reasons.
  207. void Hide()
  208. {
  209. foreach (Transform child in transform)
  210. {
  211. child.gameObject.SetActive(false);
  212. }
  213. if (GetComponent<Renderer>())
  214. GetComponent<Renderer>().enabled = false;
  215. hidden = true;
  216. }
  217. void Show()
  218. {
  219. foreach (Transform child in transform)
  220. {
  221. child.gameObject.SetActive(true);
  222. }
  223. if (GetComponent<Renderer>())
  224. GetComponent<Renderer>().enabled = true;
  225. hidden = false;
  226. }
  227. }