BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRGazePointer.cs

OVRGazePointer.cs

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