Tanoda
XRSupportUtil.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections.Generic;
10using UnityEngine;
11
12#if UNITY_2017_2_OR_NEWER
13using UnityEngine.XR;
14#else
15using UnityEngine.VR;
16#endif
17
18namespace Leap.Unity {
19
24 public static class XRSupportUtil {
25
26 #if UNITY_2019_2_OR_NEWER
27 private static System.Collections.Generic.List<XRNodeState> nodeStates =
28 new System.Collections.Generic.List<XRNodeState>();
29 #endif
30
31 public static bool IsXREnabled() {
32 #if UNITY_2017_2_OR_NEWER
33 return XRSettings.enabled;
34 #else
35 return VRSettings.enabled;
36 #endif
37 }
38
39 public static bool IsXRDevicePresent() {
40 #if UNITY_2020_1_OR_NEWER
41 return XRSettings.isDeviceActive;
42 #elif UNITY_2017_2_OR_NEWER
43 return XRDevice.isPresent;
44 #else
45 return VRDevice.isPresent;
46 #endif
47 }
48
49 static bool outputPresenceWarning = false;
50 public static bool IsUserPresent(bool defaultPresence = true) {
51 #if UNITY_2019_3_OR_NEWER
52 var devices = new List<InputDevice>();
53 InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
54 if (devices.Count == 0 && !outputPresenceWarning) {
55 Debug.LogWarning("No head-mounted devices found. Possibly no HMD is available to the XR system.");
56 outputPresenceWarning = true;
57 }
58 if (devices.Count != 0) {
59 var device = devices[0];
60 if (device.TryGetFeatureValue(CommonUsages.userPresence, out var userPresent)) {
61 return userPresent;
62 }
63 }
64 #elif UNITY_2017_2_OR_NEWER
65 var userPresence = XRDevice.userPresence;
66 if (userPresence == UserPresenceState.Present) {
67 return true;
68 } else if (!outputPresenceWarning && userPresence == UserPresenceState.Unsupported) {
69 Debug.LogWarning("XR UserPresenceState unsupported (XR support is probably disabled).");
70 outputPresenceWarning = true;
71 }
72 #else
73 if (!outputPresenceWarning){
74 Debug.LogWarning("XR UserPresenceState is only supported in 2017.2 and newer.");
75 outputPresenceWarning = true;
76 }
77 #endif
78 return defaultPresence;
79 }
80
81 public static Vector3 GetXRNodeCenterEyeLocalPosition() {
82 #if UNITY_2019_2_OR_NEWER
83 InputTracking.GetNodeStates(nodeStates);
84 Vector3 position;
85 foreach(XRNodeState state in nodeStates) {
86 if(state.nodeType == XRNode.CenterEye &&
87 state.TryGetPosition(out position))
88 { return position; }
89 }
90 return Vector3.zero;
91 #elif UNITY_2017_2_OR_NEWER
92 return InputTracking.GetLocalPosition(XRNode.CenterEye);
93 #else
94 return InputTracking.GetLocalPosition(VRNode.CenterEye);
95 #endif
96 }
97
98 public static Quaternion GetXRNodeCenterEyeLocalRotation() {
99 #if UNITY_2019_2_OR_NEWER
100 InputTracking.GetNodeStates(nodeStates);
101 Quaternion rotation;
102 foreach (XRNodeState state in nodeStates) {
103 if (state.nodeType == XRNode.CenterEye &&
104 state.TryGetRotation(out rotation))
105 { return rotation; }
106 }
107 return Quaternion.identity;
108 #elif UNITY_2017_2_OR_NEWER
109 return InputTracking.GetLocalRotation(XRNode.CenterEye);
110 #else
111 return InputTracking.GetLocalRotation(VRNode.CenterEye);
112 #endif
113 }
114
115 public static Vector3 GetXRNodeHeadLocalPosition() {
116 #if UNITY_2019_2_OR_NEWER
117 InputTracking.GetNodeStates(nodeStates);
118 Vector3 position;
119 foreach(XRNodeState state in nodeStates) {
120 if(state.nodeType == XRNode.Head &&
121 state.TryGetPosition(out position))
122 { return position; }
123 }
124 return Vector3.zero;
125 #elif UNITY_2017_2_OR_NEWER
126 return InputTracking.GetLocalPosition(XRNode.Head);
127 #else
128 return InputTracking.GetLocalPosition(VRNode.Head);
129 #endif
130 }
131
132 public static Quaternion GetXRNodeHeadLocalRotation() {
133 #if UNITY_2019_2_OR_NEWER
134 InputTracking.GetNodeStates(nodeStates);
135 Quaternion rotation;
136 foreach (XRNodeState state in nodeStates) {
137 if (state.nodeType == XRNode.Head &&
138 state.TryGetRotation(out rotation))
139 { return rotation; }
140 }
141 return Quaternion.identity;
142 #elif UNITY_2017_2_OR_NEWER
143 return InputTracking.GetLocalRotation(XRNode.Head);
144 #else
145 return InputTracking.GetLocalRotation(VRNode.Head);
146 #endif
147 }
148
149 public static Vector3 GetXRNodeLocalPosition(int node) {
150 #if UNITY_2019_2_OR_NEWER
151 InputTracking.GetNodeStates(nodeStates);
152 Vector3 position;
153 foreach(XRNodeState state in nodeStates) {
154 if(state.nodeType == (XRNode)node &&
155 state.TryGetPosition(out position))
156 { return position; }
157 }
158 return Vector3.zero;
159 #elif UNITY_2017_2_OR_NEWER
160 return InputTracking.GetLocalPosition((XRNode)node);
161 #else
162 return InputTracking.GetLocalPosition((VRNode)node);
163 #endif
164 }
165
166 public static Quaternion GetXRNodeLocalRotation(int node) {
167 #if UNITY_2019_2_OR_NEWER
168 InputTracking.GetNodeStates(nodeStates);
169 Quaternion rotation;
170 foreach (XRNodeState state in nodeStates) {
171 if (state.nodeType == (XRNode)node &&
172 state.TryGetRotation(out rotation))
173 { return rotation; }
174 }
175 return Quaternion.identity;
176 #elif UNITY_2017_2_OR_NEWER
177 return InputTracking.GetLocalRotation((XRNode)node);
178 #else
179 return InputTracking.GetLocalRotation((VRNode)node);
180 #endif
181 }
182
183 public static void Recenter()
184 {
185 var devices = new List<InputDevice>();
186 InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
187 if (devices.Count == 0) return;
188 var hmdDevice = devices[0];
189
190 if(hmdDevice.subsystem != null)
191 hmdDevice.subsystem.TryRecenter();
192 }
193
194 public static string GetLoadedDeviceName() {
195 #if UNITY_2017_2_OR_NEWER
196 return XRSettings.loadedDeviceName;
197 #else
198 return VRSettings.loadedDeviceName;
199 #endif
200 }
201
203 public static bool IsRoomScale() {
204 #if UNITY_2019_3_OR_NEWER
205 var devices = new List<InputDevice>();
206 InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
207 if (devices.Count == 0) return false;
208 var hmdDevice = devices[0];
209 if (hmdDevice.subsystem == null) return false;
210#if !UNITY_2020_1_OR_NEWER
211 if(hmdDevice.subsystem != null) {
212#endif
213 return hmdDevice.subsystem.GetTrackingOriginMode().HasFlag(TrackingOriginModeFlags.Floor);
214 #if !UNITY_2020_1_OR_NEWER
215 }else{
216 #pragma warning disable 0618
217 return XRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale;
218 #pragma warning restore 0618
219 }
220 #endif
221 #elif UNITY_2017_2_OR_NEWER
222 return XRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale;
223 #else
224 return VRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale;
225 #endif
226 }
227
228 #if UNITY_2020_1_OR_NEWER
229 static List<Vector3> _boundaryPoints = new List<Vector3>();
230 #endif
232 public static bool IsLargePlayspace() {
233 #if UNITY_2020_1_OR_NEWER // Oculus reports a floor centered space now...
234 var devices = new List<InputDevice>();
235 InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
236 if (devices.Count == 0) return false;
237 var hmdDevice = devices[0];
238 if (hmdDevice.subsystem == null) return false;
239 hmdDevice.subsystem.TryGetBoundaryPoints(_boundaryPoints);
240 Bounds playspaceSize = new Bounds();
241 foreach(Vector3 boundaryPoint in _boundaryPoints) { playspaceSize.Encapsulate(boundaryPoint); }
242 return playspaceSize.size.magnitude > 1f; // Playspace is greater than 1m on its shortest axis
243 #else
244 return IsRoomScale();
245 #endif
246 }
247
248 public static float GetGPUTime() {
249 float gpuTime = 0f;
250 #if UNITY_5_6_OR_NEWER
251 #if UNITY_2017_2_OR_NEWER
252 UnityEngine.XR.XRStats.TryGetGPUTimeLastFrame(out gpuTime);
253 #else
254 UnityEngine.VR.VRStats.TryGetGPUTimeLastFrame(out gpuTime);
255 #endif
256 #else
257 gpuTime = UnityEngine.VR.VRStats.gpuTimeLastFrame;
258 #endif
259 return gpuTime;
260 }
261
262 }
263
264}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19