OVRSharp

A project from OVRTools

High-level idiomatic C# interface for working with the OpenVR API.

With OVRSharp

Beautiful. Immaculate. Easy to follow. Wow.

Application app;

try {
  app = new Application(ApplicationType.Overlay);
} catch(OpenVRSystemException e) {
  // Errors are exceptions!
}

var overlay = new Overlay("cool_overlay", "Cool Overlay", true) {
  WidthInMeters = 3.8f
};

overlay.SetTextureFromFile(@"C:\path\to\file.png");
overlay.SetThumbnailTextureFromFile(@"C:\path\to\thumb.png");

It's so much shorter that I could probably write a few more paragraphs here to make this side as long as the other.

Without OVRSharp

Awful. Hard to read. It's a mess.

var err = EVRInitError.None;
var vrSystem = OpenVR.Init(ref err, EVRApplicationType.VRApplication_Overlay);

if (err != EVRInitError.None)
{
  // whatever error handling
}

// Create a dashboard overlay
var overlayErr = EVROverlayError.None;

ulong overlayHandle;
ulong thumbnailHandle;

overlayErr = OpenVR.Overlay.CreateDashboardOverlay("cool_overlay", "Cool Overlay", ref overlayHandle, ref thumbnailHandle);

if (overlayErr != EVROverlayError.None)
{
  // whatever error handling
}

overlayErr = OpenVR.Overlay.SetOverlayWidthInMeters(overlayHandle, 3.8f);

if (overlayErr != EVROverlayError.None)
{
  // whatever error handling
}

// Set the dashboard overlay up. First, the main overlay's texture
overlayErr = OpenVR.Overlay.SetOverlayFromFile(overlayHandle, @"C:\path\to\file.png");

if (overlayErr != EVROverlayError.None)
{
  // whatever error handling
}

// Then the thumbnail.
overlayErr = OpenVR.Overlay.SetOverlayFromFile(thumbnailHandle, @"C:\path\to\thumb.png");

if (overlayErr != EVROverlayError.None)
{
  // whatever error handling
}

Lightweight

OVRSharp can be integrated into any C# project, whether you're using Unity, Godot, or even no engine at all. With a light footprint, you can use as little or as much as you need.

Idiomatic

Instead of accessing low-level APIs, dealing with pointers, and the like, you can use the familiar .NET types and features you already know and love.

Open source

As with all our other projects, OVRSharp is open source. This means you can benefit from the hard work of the community, as well as tweak and improve the library to suit your needs.

public class MyCoolOverlay : Overlay {
  public MyCoolOverlay()
    : base("my_cool_overlay", "My Cool Overlay")
  {
    // Attach overlay to right hand
    TrackedDevice = TrackedDeviceRole.RightHand;

    // Set overlay from png
    SetTextureFromFile("cool_overlay.png");
  }
}

OVRSharp's simple API makes it easy to write overlays for OpenVR. Just extend the Overlay class, implement your logic, and you're good to go. The raw OpenVR API is hidden behind a simple interface.

// Use the DirectX implementation
var compositor = DirectXCompositor.Instance;

// Get a Bitmap of what the headset sees
var bitmap = compositor.GetMirrorImage();

Some parts of the OpenVR API require specialized knowledge of graphics APIs like OpenGL or DirectX. We've abstracted these details away, so you can focus on the core of your overlay.

public MyCoolThing(ICompositorAPI compositor) {
  // `compositor` could be using DirectX, OpenGL,
  // or something else entirely!
}

With an interface-based API, OVRSharp easily works with patterns like dependency injection, so you don't need to worry about what's happening under the hood.

// Building a VR game? You'll need this.
// And it's gotta be fast!
OpenVR.Compositor.Submit(EVREye.Left, ref tex);

If you want to run "close to the metal", you can still use the underlying OpenVR API. This is useful if you need functionality that's not yet available in the library or if performance is critical.

Get started

Ready to write your first VR overlay? Check out our getting started guide, where you'll write your first overlay in a few minutes. You can also check out the repository on GitHub, where you can find the source code for the library and get help from the community.