Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created November 13, 2015 07:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonixis/a648e2834a4ddc813896 to your computer and use it in GitHub Desktop.
Save demonixis/a648e2834a4ddc813896 to your computer and use it in GitHub Desktop.
A static method used to check if an HMD is available with the OSVR SDK for Unity.
public static bool HasOSVRHMDEnabled()
{
#if UNITY_STANDALONE
var clientContext = new OSVR.ClientKit.ClientContext(GamePrefs.AppID);
// Check if the server is running
if (clientContext != null && clientContext.CheckStatus())
{
// Check if the HMD si connected
var displayConfig = clientContext.GetDisplayConfig();
if (displayConfig != null && displayConfig.CheckDisplayStartup())
{
DeviceType = VRDeviceType.OSVR;
displayConfig.Dispose();
}
clientContext.Dispose();
}
#endif
return DeviceType == VRDeviceType.OSVR;
}
@JeroMiya
Copy link

CheckDisplayStartup may return false initially. You may need to update the clientContext a few times. I typically something like while(!displayCOnfig.CheckDisplayStartup() && !TimedOut()) { clientContext.Update(); Thread.Sleep(100);} or something similar.

Also note: It's our intent to abstract HMD vs Monitor vs cave/etc... in the DisplayConfig API to enable the widest possible supported environments with roughly the same code. You may encounter one of the following configurations (not an exhaustive list):

A PC monitor: one viewer, one eye, one surface, surface size = displayInput size, no distortion.
A 3D monitor: one viewer, two eyes, two surfaces, both surfaces = displayInput size, no distortion.
Cave: one viewer per wall, two eyes per viewer, surfaces = displayInput size, no distortion.
HMD: one viewer, two eyes, one surface per eye, surface size = half of displayInput size (possibly upscaled for distortion correction), distortion according to the HMD config.

The last scenario is roughly equivalent to "Is there an HMD attached?", but I would caution filtering out the other scenarios unless absolutely necessary. The real question you're interested in is not that there is an HMD attached, but is there is a head-tracked viewer. That tells you whether to adjust the UI and controls for an HMD-like experience (i.e. draw the UI in world space, disable mouselook, etc...) vs a 2D monitor experience. Unfortunately OSVR doesn't yet have that information explicitly. If you don't care about 3D monitors though (which have two eyes but aren't head-tracked), then you can just check for the existence of a viewer with two eyes and treat it like an HMD.

@demonixis
Copy link
Author

Hum thanks a lot for these explications!

So this code must be better right?

var isHMD = displayConfig.GetNumViewers() == 1 &&
                    displayConfig.GetNumEyesForViewer(0) == 2 &&
                    displayConfig.GetNumSurfacesForViewerEye(0, 0) == 1;

@JeroMiya
Copy link

I would leave out the GetNumSurfacesForViewerEye check unless you're implementing a rendering plugin or engine backend and can only support one surface.

Also, a correction: A cave system would have one viewer, two eyes, and multiple surfaces per eye. You'll probably be ok treating a cave system like an HMD for UI/control purposes. You definitely don't want mouselook on in a cave! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment