using Godot; using System; /// /// Utility class for managing OpenXR initialization and configuration. /// public static class OpenXRManager { /// /// This method follows the approach in xr_origin_3d.gd, which only sets the viewport to use XR /// if the interface is already initialized, but doesn't try to initialize it. /// /// A node in the scene to get the viewport from. If null, tries to use the main viewport. /// True if OpenXR was successfully enabled, false otherwise. public static bool EnableOpenXR(Node node = null) { try { // Get the XR interface var xrInterface = XRServer.FindInterface("OpenXR"); xrInterface.Initialize(); // Ensure the interface is initialized. if (xrInterface != null && xrInterface.IsInitialized()) { GD.Print("OpenXR initialized successfully"); // Set the viewport to use XR if (node != null) { // In C#, we need to use reflection to set the use_xr property // since the property name might be different var viewport = node.GetViewport(); if (viewport != null) { // Try different property names that might exist try { // Method 1: Using property setter viewport.Set("use_xr", true); GD.Print("Set viewport.use_xr = true"); } catch (Exception ex) { GD.PrintErr($"Could not set viewport.use_xr: {ex.Message}"); } } } // Disable V-sync for better performance in VR DisplayServer.WindowSetVsyncMode(DisplayServer.VSyncMode.Disabled); return true; } else { GD.Print("OpenXR not initialized, please check if your headset is connected"); return false; } } catch (Exception ex) { GD.PrintErr($"Error enabling OpenXR: {ex.Message}"); return false; } } /// /// Checks if OpenXR is currently initialized. /// /// True if OpenXR is initialized, false otherwise. public static bool IsOpenXRInitialized() { try { var xrInterface = XRServer.FindInterface("OpenXR"); return xrInterface != null && xrInterface.IsInitialized(); } catch (Exception ex) { GD.PrintErr($"Error checking OpenXR status: {ex.Message}"); return false; } } }