14 public static bool active {
get {
return _instance != null; } }
17 private static bool _enabled =
true;
18 public static bool enabled
22 if (!UnityEngine.XR.XRSettings.enabled)
34 private static SteamVR _instance;
40 if (!Application.isPlaying)
46 if (_instance == null)
48 _instance = CreateInstance();
51 if (_instance == null)
59 public static bool usingNativeSupport
61 get {
return UnityEngine.XR.XRDevice.GetNativePtr() != System.IntPtr.Zero; }
68 var error = EVRInitError.None;
69 if (!
SteamVR.usingNativeSupport)
71 Debug.Log(
"OpenVR initialization failed. Ensure 'Virtual Reality Supported' is checked in Player Settings, and OpenVR is added to the list of Virtual Reality SDKs.");
77 OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error);
78 if (error != EVRInitError.None)
84 OpenVR.GetGenericInterface(OpenVR.IVROverlay_Version, ref error);
85 if (error != EVRInitError.None)
91 catch (System.Exception e)
100 static void ReportError(EVRInitError error)
104 case EVRInitError.None:
106 case EVRInitError.VendorSpecific_UnableToConnectToOculusRuntime:
107 Debug.Log(
"SteamVR Initialization Failed! Make sure device is on, Oculus runtime is installed, and OVRService_*.exe is running.");
109 case EVRInitError.Init_VRClientDLLNotFound:
110 Debug.Log(
"SteamVR drivers not found! They can be installed via Steam under Library > Tools. Visit http://steampowered.com to install Steam.");
112 case EVRInitError.Driver_RuntimeOutOfDate:
113 Debug.Log(
"SteamVR Initialization Failed! Make sure device's runtime is up to date.");
116 Debug.Log(OpenVR.GetStringForHmdError(error));
122 public CVRSystem hmd {
get;
private set; }
124 public CVROverlay overlay {
get;
private set; }
127 static public bool initializing {
get;
private set; }
128 static public bool calibrating {
get;
private set; }
129 static public bool outOfRange {
get;
private set; }
131 static public bool[] connected =
new bool[OpenVR.k_unMaxTrackedDeviceCount];
134 public float sceneWidth {
get;
private set; }
135 public float sceneHeight {
get;
private set; }
136 public float aspect {
get;
private set; }
137 public float fieldOfView {
get;
private set; }
138 public Vector2 tanHalfFov {
get;
private set; }
141 public ETextureType textureType;
144 public string hmd_TrackingSystemName {
get {
return GetStringProperty(ETrackedDeviceProperty.Prop_TrackingSystemName_String); } }
145 public string hmd_ModelNumber {
get {
return GetStringProperty(ETrackedDeviceProperty.Prop_ModelNumber_String); } }
146 public string hmd_SerialNumber {
get {
return GetStringProperty(ETrackedDeviceProperty.Prop_SerialNumber_String); } }
148 public float hmd_SecondsFromVsyncToPhotons {
get {
return GetFloatProperty(ETrackedDeviceProperty.Prop_SecondsFromVsyncToPhotons_Float); } }
149 public float hmd_DisplayFrequency {
get {
return GetFloatProperty(ETrackedDeviceProperty.Prop_DisplayFrequency_Float); } }
151 public string GetTrackedDeviceString(uint deviceId)
153 var error = ETrackedPropertyError.TrackedProp_Success;
154 var capacity = hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, null, 0, ref error);
157 var result =
new System.Text.StringBuilder((int)capacity);
158 hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, result, capacity, ref error);
159 return result.ToString();
164 public string GetStringProperty(ETrackedDeviceProperty prop, uint deviceId =
OpenVR.k_unTrackedDeviceIndex_Hmd)
166 var error = ETrackedPropertyError.TrackedProp_Success;
167 var capactiy = hmd.GetStringTrackedDeviceProperty(deviceId, prop, null, 0, ref error);
170 var result =
new System.Text.StringBuilder((int)capactiy);
171 hmd.GetStringTrackedDeviceProperty(deviceId, prop, result, capactiy, ref error);
172 return result.ToString();
174 return (error != ETrackedPropertyError.TrackedProp_Success) ? error.ToString() :
"<unknown>";
177 public float GetFloatProperty(ETrackedDeviceProperty prop, uint deviceId =
OpenVR.k_unTrackedDeviceIndex_Hmd)
179 var error = ETrackedPropertyError.TrackedProp_Success;
180 return hmd.GetFloatTrackedDeviceProperty(deviceId, prop, ref error);
183 #region Event callbacks
185 private void OnInitializing(
bool initializing)
187 SteamVR.initializing = initializing;
190 private void OnCalibrating(
bool calibrating)
192 SteamVR.calibrating = calibrating;
195 private void OnOutOfRange(
bool outOfRange)
197 SteamVR.outOfRange = outOfRange;
200 private void OnDeviceConnected(
int i,
bool connected)
202 SteamVR.connected[i] = connected;
211 for (
int i = 0; i < poses.Length; i++)
213 var connected = poses[i].bDeviceIsConnected;
214 if (connected !=
SteamVR.connected[i])
216 SteamVR_Events.DeviceConnected.Send(i, connected);
220 if (poses.Length >
OpenVR.k_unTrackedDeviceIndex_Hmd)
222 var result = poses[OpenVR.k_unTrackedDeviceIndex_Hmd].eTrackingResult;
224 var initializing = result == ETrackingResult.Uninitialized;
225 if (initializing !=
SteamVR.initializing)
227 SteamVR_Events.Initializing.Send(initializing);
231 result == ETrackingResult.Calibrating_InProgress ||
232 result == ETrackingResult.Calibrating_OutOfRange;
233 if (calibrating !=
SteamVR.calibrating)
235 SteamVR_Events.Calibrating.Send(calibrating);
239 result == ETrackingResult.Running_OutOfRange ||
240 result == ETrackingResult.Calibrating_OutOfRange;
241 if (outOfRange !=
SteamVR.outOfRange)
243 SteamVR_Events.OutOfRange.Send(outOfRange);
253 Debug.Log(
"Connected to " + hmd_TrackingSystemName +
":" + hmd_SerialNumber);
255 compositor = OpenVR.Compositor;
256 overlay = OpenVR.Overlay;
260 hmd.GetRecommendedRenderTargetSize(ref w, ref h);
261 sceneWidth = (float)w;
262 sceneHeight = (float)h;
264 float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;
265 hmd.GetProjectionRaw(EVREye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);
267 float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;
268 hmd.GetProjectionRaw(EVREye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);
270 tanHalfFov =
new Vector2(
271 Mathf.Max(-l_left, l_right, -r_left, r_right),
272 Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));
276 textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
277 textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
278 textureBounds[0].vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.y;
279 textureBounds[0].vMax = 0.5f - 0.5f * l_top / tanHalfFov.y;
281 textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
282 textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
283 textureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.y;
284 textureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov.y;
287 sceneWidth = sceneWidth / Mathf.Max(textureBounds[0].uMax - textureBounds[0].uMin, textureBounds[1].uMax - textureBounds[1].uMin);
288 sceneHeight = sceneHeight / Mathf.Max(textureBounds[0].vMax - textureBounds[0].vMin, textureBounds[1].vMax - textureBounds[1].vMin);
290 aspect = tanHalfFov.x / tanHalfFov.y;
291 fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;
293 eyes =
new SteamVR_Utils.RigidTransform[] {
294 new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(EVREye.Eye_Left)),
295 new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(EVREye.Eye_Right)) };
297 switch (SystemInfo.graphicsDeviceType)
300 case UnityEngine.Rendering.GraphicsDeviceType.OpenGL2:
302 case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore:
303 case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2:
304 case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3:
305 textureType = ETextureType.OpenGL;
308 case UnityEngine.Rendering.GraphicsDeviceType.Vulkan:
309 textureType = ETextureType.Vulkan;
313 textureType = ETextureType.DirectX;
317 SteamVR_Events.Initializing.Listen(OnInitializing);
318 SteamVR_Events.Calibrating.Listen(OnCalibrating);
319 SteamVR_Events.OutOfRange.Listen(OnOutOfRange);
320 SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
321 SteamVR_Events.NewPoses.Listen(OnNewPoses);
329 public void Dispose()
332 System.GC.SuppressFinalize(
this);
335 private void Dispose(
bool disposing)
337 SteamVR_Events.Initializing.Remove(OnInitializing);
338 SteamVR_Events.Calibrating.Remove(OnCalibrating);
339 SteamVR_Events.OutOfRange.Remove(OnOutOfRange);
340 SteamVR_Events.DeviceConnected.Remove(OnDeviceConnected);
341 SteamVR_Events.NewPoses.Remove(OnNewPoses);
347 public static void SafeDispose()
349 if (_instance != null)