OVRGazePointer.cs 9.2 KB

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