private static void ReportError(EVRInitError error) { if (error != EVRInitError.None) { if (error != EVRInitError.Init_VRClientDLLNotFound) { if (error != EVRInitError.Driver_RuntimeOutOfDate) { if (error != EVRInitError.VendorSpecific_UnableToConnectToOculusRuntime) { Debug.Log(OpenVR.GetStringForHmdError(error)); } else { Debug.Log("SteamVR Initialization Failed! Make sure device is on, Oculus runtime is installed, and OVRService_*.exe is running."); } } else { Debug.Log("SteamVR Initialization Failed! Make sure device's runtime is up to date."); } } else { Debug.Log("SteamVR drivers not found! They can be installed via Steam under Library > Tools. Visit http://steampowered.com to install Steam."); } } }
protected void SetStatus(object toString) { if (toString is EVRInitError) { FErrorOut[0] = OpenVR.GetStringForHmdError((EVRInitError)toString); } else if (toString is EVRCompositorError) { var error = (EVRCompositorError)toString; if (error == EVRCompositorError.TextureIsOnWrongDevice) { FErrorOut[0] = "Texture on wrong device. Set your graphics driver to use the same video card for vvvv as the headset is plugged into."; } else if (error == EVRCompositorError.TextureUsesUnsupportedFormat) { FErrorOut[0] = "Unsupported texture format. Make sure texture uses RGBA, is not compressed and has no mipmaps."; } else { FErrorOut[0] = error.ToString(); } } else { FErrorOut[0] = toString.ToString(); } }
public void InitOpenVR() { ExtenFunctions.SetDllDirectory(OpenVRDllPath); if (!OpenVR.IsHmdPresent()) { throw new InvalidOperationException("Could not find vr headset"); } if (!OpenVR.IsRuntimeInstalled()) { throw new InvalidOperationException("Could not find openVr runtime"); } EVRInitError hmdInitErrorCode = EVRInitError.None; OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); if (hmdInitErrorCode != EVRInitError.None) { throw new Exception("OpenVR error: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); } OpenVR.System.ResetSeatedZeroPose(); uint renderTextureWidth = 0; uint renderTextureHeight = 0; OpenVR.System.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); VRManager.Instance.InitEyeTextures((int)renderTextureWidth, (int)renderTextureHeight); }
public void Init() { Console.CancelKeyPress += CancelKeyPressed; var initError = EVRInitError.None; vrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Background); if (initError != EVRInitError.None) { var message = OpenVR.GetStringForHmdError(initError); throw new Exception($"Failed to initialize OpenVR: {message}"); } vrInput = OpenVR.Input; vrSettings = OpenVR.Settings; var appDir = AppDomain.CurrentDomain.BaseDirectory; var manifestPath = Path.Combine(appDir, "action_manifest.json"); var inputError = vrInput.SetActionManifestPath(manifestPath); if (inputError != EVRInputError.None) { var message = inputError.ToString(); throw new Exception($"Failed to set action manifest path: {message}"); } inputError = vrInput.GetActionSetHandle("/actions/main", ref inputSet); if (inputError != EVRInputError.None) { var message = inputError.ToString(); throw new Exception($"Failed to action set handle: {message}"); } inputError = vrInput.GetActionHandle("/actions/main/in/activate", ref inputActivate); if (inputError != EVRInputError.None) { var message = inputError.ToString(); throw new Exception($"Failed to get action handle for Activate: {message}"); } inputError = vrInput.GetActionHandle("/actions/main/in/reset-auto", ref inputResetAuto); if (inputError != EVRInputError.None) { var message = inputError.ToString(); throw new Exception($"Failed to get action handle for Reset (Auto): {message}"); } inputError = vrInput.GetActionHandle("/actions/main/in/reset", ref inputResetHold); if (inputError != EVRInputError.None) { var message = inputError.ToString(); throw new Exception($"Failed to get action handle for Reset (Hold): {message}"); } activateSound = new SoundPlayer(Path.Combine(appDir, "activate.wav")); resetSound = new SoundPlayer(Path.Combine(appDir, "reset.wav")); }
/// <summary> /// Reports the initialization error. /// </summary> /// <param name="error">The error.</param> public static void ReportInitError(EVRInitError error) { if (error == EVRInitError.None) { return; } //TODO: Don't log to Debug, since this is visible in editor console Debug.LogError("[VR] Init error: `" + OpenVR.GetStringForHmdError(error) + "`"); }
private bool ErrorCheck(EVRInitError error) { bool err = (error != EVRInitError.None); if (err) { Debug.Log("VR Error: " + OpenVR.GetStringForHmdError(error)); } return(err); }
void SetStatus(object toString) { if (toString is EVRInitError) { FErrorOut[0] = OpenVR.GetStringForHmdError((EVRInitError)toString); } else { FErrorOut[0] = toString.ToString(); } }
static void SetStatus(object toString) { if (toString is EVRInitError) { ErrorMessage = OpenVR.GetStringForHmdError((EVRInitError)toString); } else { ErrorMessage = toString.ToString(); } }
/// <summary> /// Tries to initialize VR - returns the VR support object, or null if loading failed. /// </summary> /// <param name="tclient">The game client.</param> /// <returns>VR, if any.</returns> public static VRSupport TryInit(GameClientWindow tclient) { if (!Available()) { return(null); } EVRInitError err = EVRInitError.None; VRSupport vrs = new VRSupport() { TheClient = tclient, VR = OpenVR.Init(ref err) }; if (err != EVRInitError.None) { SysConsole.Output(OutputType.INFO, "VR error: " + err + ": " + OpenVR.GetStringForHmdError(err)); return(null); } vrs.Start(); return(vrs); }
static void Main() { var error = EVRInitError.None; var handle = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Background); if (error != EVRInitError.None) { MessageBox.Show($"Could not connect to OpenVR server: {OpenVR.GetStringForHmdError(error)}\n\nPlease make sure an OpenVR server is running (e.g. SteamVR) and try again.", "Fatal Error"); Application.Exit(); return; } OpenVRHandle = handle; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); new BatteryStatus().Start(); Application.Run(); }
static void ReportError(EVRInitError error) { switch (error) { case EVRInitError.None: break; case EVRInitError.VendorSpecific_UnableToConnectToOculusRuntime: throw new System.Exception("SteamVR Initialization Failed! Make sure device is on, Oculus runtime is installed, and OVRService_*.exe is running."); case EVRInitError.Init_VRClientDLLNotFound: throw new System.Exception("SteamVR drivers not found! They can be installed via Steam under Library > Tools. Visit http://steampowered.com to install Steam."); case EVRInitError.Driver_RuntimeOutOfDate: throw new System.Exception("SteamVR Initialization Failed! Make sure device's runtime is up to date."); default: throw new System.Exception(OpenVR.GetStringForHmdError(error)); } }
/// <summary> /// Initialize HMD using OpenVR API calls. /// </summary> /// <returns>True on success, false otherwise. Errors logged.</returns> bool InitHMD() { bool retVal = false; // return if HMD has already been initialized if (hmdIsInitialized) { return(true); } bool is64bit = (IntPtr.Size == 8); string mypath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); log("OpenVR path set to " + Path.Combine(mypath, is64bit ? "win64" : "win32")); SetDllDirectory(Path.Combine(mypath, is64bit ? "win64" : "win32")); // check if HMD is connected on the system retVal = OpenVR.IsHmdPresent(); if (!retVal) { err("HMD not found on this system."); return(retVal); } // check if SteamVR runtime is installed. // For this plugin, MAKE SURE IT IS ALREADY RUNNING. retVal = OpenVR.IsRuntimeInstalled(); if (!retVal) { err("SteamVR runtime not found on this system."); return(retVal); } // initialize HMD EVRInitError hmdInitErrorCode = EVRInitError.None; vrSystem = OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); // return if failure retVal = (hmdInitErrorCode == EVRInitError.None); if (!retVal) { err("Failed to initialize HMD. Init returned: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); return(retVal); } else { log("OpenVR.Init passed."); } // reset "seated position" and capture initial position. this means you should hold the HMD in // the position you would like to consider "seated", before running this code. ResetInitialHmdPosition(); // initialize Compositor vrCompositor = OpenVR.Compositor; // initialize render textures (for displaying on HMD) uint renderTextureWidth = 0; uint renderTextureHeight = 0; vrSystem.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); //renderTextureWidth /= 2; //renderTextureHeight /= 2; log("Render Texture size: " + renderTextureWidth + " x " + renderTextureHeight); hmdLeftEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdLeftEyeRenderTexture.Create(); hmdRightEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdRightEyeRenderTexture.Create(); hmdLeftEyeTexture.handle = hmdLeftEyeRenderTexture.GetNativeTexturePtr(); hmdLeftEyeTexture.eColorSpace = EColorSpace.Auto; hmdRightEyeTexture.handle = hmdRightEyeRenderTexture.GetNativeTexturePtr(); hmdRightEyeTexture.eColorSpace = EColorSpace.Auto; switch (SystemInfo.graphicsDeviceType) { case UnityEngine.Rendering.GraphicsDeviceType.OpenGL2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3: hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; break; //doesnt work in unity 5.4 with current SteamVR (12/2016) case UnityEngine.Rendering.GraphicsDeviceType.Direct3D9: throw (new Exception("DirectX9 not supported")); case UnityEngine.Rendering.GraphicsDeviceType.Direct3D11: hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; break; default: throw (new Exception(SystemInfo.graphicsDeviceType.ToString() + " not supported")); } // Set rendering bounds on texture to render? // I assume min=0.0 and max=1.0 renders to the full extent of the texture hmdTextureBounds.uMin = 0.0f; hmdTextureBounds.uMax = 1.0f; hmdTextureBounds.vMin = 0.0f; hmdTextureBounds.vMax = 1.0f; // TODO: Need to understand better how to create render targets and incorporate hidden area mask mesh foreach (Camera camera in Camera.allCameras) { log("KSP Camera: " + camera.name); } // search for camera objects to render foreach (string cameraName in cameraNamesToRender) { foreach (Camera camera in Camera.allCameras) { if (cameraName.Equals(camera.name)) { float nearClipPlane = (camera.name.Equals(cameraNames[3])) ? 0.05f : camera.nearClipPlane; HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); //HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work //HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work camerasToRender.Add(new CameraProperties(camera, camera.projectionMatrix, MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projLeft), MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projRight))); break; } } } // detect controllers for (uint idx = 0; idx < OpenVR.k_unMaxTrackedDeviceCount; idx++) { if ((ctrlIndexLeft == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexLeft = idx; } else if ((ctrlIndexRight == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexRight = idx; } } bool ctrlFocusCaptured = vrSystem.CaptureInputFocus(); if (!ctrlFocusCaptured) { warn("Controller input focus was not captured"); } initTmr.Start(); return(retVal); }
public MyOpenVR() { if (true) { m_viewHMD = Matrix.Identity; m_headsetPosD = MatrixD.Identity; m_c1pos = Matrix.Identity; m_c2pos = Matrix.Identity; //IntPtr Handle = MyRender11.LoadLibrary(@"D:\KeenSWH.VR\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); //IntPtr Handle = LoadLibrary(@"C:\Program Files (x86)\Steam\SteamApps\common\SteamVR\bin\win64\openvr_api.dll"); IntPtr Handle = LoadLibrary(@"openvr_api.dll"); //IntPtr Handle = MyRender11.LoadLibrary(@"c:\Program Files (x86)\Steam\bin\openvr_api.dll");//err code 193 - not a valid Win32 application //Assembly.LoadFile(@"D:\KeenSWH\Sandbox\Sources\SpaceEngineers\bin\x64\Debug\Bin64\openvr_api.dll"); if (Handle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode)); } EVRInitError error = EVRInitError.None; IntPtr ptr = OpenVR.Init(ref error, EVRApplicationType.VRApplication_Scene); if (error == EVRInitError.None) { m_vrSystem = new CVRSystem(ptr); uint sizeX = 0, sizeY = 0; m_vrSystem.GetRecommendedRenderTargetSize(ref sizeX, ref sizeY); m_vrSystem.SetDisplayVisibility(true); ETrackedPropertyError pError = 0; m_ipd_2 = 0.5f * m_vrSystem.GetFloatTrackedDeviceProperty(0, ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref pError); SetIPD(m_ipd_2 * 2); IntPtr pointer = OpenVR.GetGenericInterface(OpenVR.IVRCompositor_Version, ref error); if (error == EVRInitError.None) { m_vrCompositor = new CVRCompositor(pointer); m_vrCompositor.CompositorBringToFront(); m_vrCompositor.ShowMirrorWindow(); m_openVR = this; } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); Debug.Fail("No compositor interface"); throw new Exception(errString); } } else { var errString = OpenVR.GetStringForHmdError(error); Log.WriteLineAndConsole(errString); //Debug.Fail("OpenVR init failed"); throw new Exception(errString); } InitOverlays(); } }
/// <summary> /// Initialize HMD using OpenVR API calls. /// </summary> /// <returns>True on success, false otherwise. Errors logged.</returns> bool InitHMD() { bool retVal = false; // return if HMD has already been initialized if (hmdIsInitialized) { return(true); } // check if HMD is connected on the system retVal = OpenVR.IsHmdPresent(); if (!retVal) { Debug.Log("[KerbalVR] HMD not found on this system."); return(retVal); } // check if SteamVR runtime is installed. // For this plugin, MAKE SURE IT IS ALREADY RUNNING. retVal = OpenVR.IsRuntimeInstalled(); if (!retVal) { Debug.Log("[KerbalVR] SteamVR runtime not found on this system."); return(retVal); } // initialize HMD EVRInitError hmdInitErrorCode = EVRInitError.None; vrSystem = OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); // return if failure retVal = (hmdInitErrorCode == EVRInitError.None); if (!retVal) { Debug.Log("[KerbalVR] Failed to initialize HMD. Init returned: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); return(retVal); } else { Debug.Log("[KerbalVR] OpenVR.Init passed."); } // reset "seated position" and capture initial position. this means you should hold the HMD in // the position you would like to consider "seated", before running this code. hmdIsInitialized = true; ResetInitialHmdPosition(); // initialize Compositor vrCompositor = OpenVR.Compositor; // initialize render textures (for displaying on HMD) uint renderTextureWidth = 0; uint renderTextureHeight = 0; vrSystem.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); //renderTextureWidth /= 2; //renderTextureHeight /= 2; Debug.Log("[KerbalVR] Render Texture size: " + renderTextureWidth + " x " + renderTextureHeight); hmdLeftEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdLeftEyeRenderTexture.antiAliasing = 1; hmdLeftEyeRenderTexture.Create(); hmdRightEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdRightEyeRenderTexture.Create(); hmdLeftEyeTexture.handle = hmdLeftEyeRenderTexture.GetNativeTexturePtr(); hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; //hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; // this doesn't seem to work hmdLeftEyeTexture.eColorSpace = EColorSpace.Auto; hmdRightEyeTexture.handle = hmdRightEyeRenderTexture.GetNativeTexturePtr(); hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_OpenGL; //hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; // this doesn't seem to work hmdRightEyeTexture.eColorSpace = EColorSpace.Auto; // Set rendering bounds on texture to render? // I assume min=0.0 and max=1.0 renders to the full extent of the texture hmdTextureBounds.uMin = 0.0f; hmdTextureBounds.uMax = 1.0f; hmdTextureBounds.vMin = 0.0f; hmdTextureBounds.vMax = 1.0f; // create the hidden area mask meshes HiddenAreaMesh_t vrHiddenAreaMesh = vrSystem.GetHiddenAreaMesh(EVREye.Eye_Left); hmdHiddenAreaMeshLeft = SteamVR_Utils.CreateHiddenAreaMesh(vrHiddenAreaMesh, hmdTextureBounds); vrHiddenAreaMesh = vrSystem.GetHiddenAreaMesh(EVREye.Eye_Right); hmdHiddenAreaMeshRight = SteamVR_Utils.CreateHiddenAreaMesh(vrHiddenAreaMesh, hmdTextureBounds); // TODO: Need to understand better how to create render targets and incorporate hidden area mask mesh // search for camera objects to render foreach (string cameraName in cameraNamesToRender) { foreach (Camera camera in Camera.allCameras) { if (cameraName.Equals(camera.name)) { float nearClipPlane = (camera.name.Equals(cameraNames[3])) ? 0.05f : camera.nearClipPlane; HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL); //HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work //HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work camerasToRender.Add(new CameraProperties(camera, camera.projectionMatrix, MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projLeft), MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projRight))); break; } } } foreach (Camera camera in Camera.allCameras) { if (cameraNames[5].Equals(camera.name)) { uiCamera = camera; } } // detect controllers for (uint idx = 0; idx < OpenVR.k_unMaxTrackedDeviceCount; idx++) { if ((ctrlIndexLeft == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexLeft = idx; } else if ((ctrlIndexRight == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller)) { ctrlIndexRight = idx; } } bool ctrlFocusCaptured = vrSystem.CaptureInputFocus(); if (!ctrlFocusCaptured) { Debug.LogWarning("[KerbalVR] Controller input focus was not captured"); } return(retVal); }
static void ReportError(EVRInitError error) { Debug.Log(OpenVR.GetStringForHmdError(error)); }
/// <summary> /// Initialize HMD using OpenVR API calls. /// </summary> /// <returns>True on success, false otherwise. Errors logged.</returns> private bool InitHMD() { bool retVal = false; // return if HMD has already been initialized if (hmdIsInitialized) { return(true); } // set the location of the OpenVR DLL SetDllDirectory(Globals.OpenVRDllPath); // check if HMD is connected on the system retVal = OpenVR.IsHmdPresent(); if (!retVal) { Utils.LogError("HMD not found on this system."); return(retVal); } // check if SteamVR runtime is installed retVal = OpenVR.IsRuntimeInstalled(); if (!retVal) { Utils.LogError("SteamVR runtime not found on this system."); return(retVal); } // initialize HMD EVRInitError hmdInitErrorCode = EVRInitError.None; OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); retVal = (hmdInitErrorCode == EVRInitError.None); if (!retVal) { Utils.LogError("Failed to initialize HMD. Init returned: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); return(retVal); } // reset "seated position" and capture initial position. this means you should hold the HMD in // the position you would like to consider "seated", before running this code. ResetInitialHmdPosition(); // get HMD render target size uint renderTextureWidth = 0; uint renderTextureHeight = 0; OpenVR.System.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); // at the moment, only Direct3D12 is working with Kerbal Space Program ETextureType textureType = ETextureType.DirectX; switch (SystemInfo.graphicsDeviceType) { case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3: textureType = ETextureType.OpenGL; break; // doesn't work case UnityEngine.Rendering.GraphicsDeviceType.Direct3D9: case UnityEngine.Rendering.GraphicsDeviceType.Direct3D11: textureType = ETextureType.DirectX; break; // doesn't work case UnityEngine.Rendering.GraphicsDeviceType.Direct3D12: textureType = ETextureType.DirectX; // do not use DirectX12 break; default: throw (new Exception(SystemInfo.graphicsDeviceType.ToString() + " not supported")); } openvrTexture = new Texture_t(); openvrTexture.eType = textureType; openvrTexture.eColorSpace = EColorSpace.Auto; Debug.Log("Graphics Type: " + SystemInfo.graphicsDeviceType); // initialize render textures (for displaying on HMD) rightEyeRenderTextures[0] = new Texture2D((int)(renderTextureWidth * 1.5), (int)(renderTextureHeight * 1.5), TextureFormat.ARGB32, false); leftEyeRenderTextures[0] = new Texture2D((int)(renderTextureWidth * 1.5), (int)(renderTextureHeight * 1.5), TextureFormat.ARGB32, false); renderTextures[0] = new RenderTexture((int)(renderTextureWidth * 1.5), (int)(renderTextureHeight * 1.5), 24, RenderTextureFormat.ARGB32); rightEyeRenderTextures[1] = new Texture2D((int)(renderTextureWidth), (int)(renderTextureHeight), TextureFormat.ARGB32, false); leftEyeRenderTextures[1] = new Texture2D((int)(renderTextureWidth), (int)(renderTextureHeight), TextureFormat.ARGB32, false); renderTextures[1] = new RenderTexture((int)(renderTextureWidth), (int)(renderTextureHeight), 24, RenderTextureFormat.ARGB32); rightEyeRenderTextures[2] = new Texture2D((int)(renderTextureWidth * .75), (int)(renderTextureHeight * .75), TextureFormat.ARGB32, false); leftEyeRenderTextures[2] = new Texture2D((int)(renderTextureWidth * .75), (int)(renderTextureHeight * .75), TextureFormat.ARGB32, false); renderTextures[2] = new RenderTexture((int)(renderTextureWidth * .75), (int)(renderTextureHeight * .75), 24, RenderTextureFormat.ARGB32); rightEyeRenderTextures[3] = new Texture2D((int)(renderTextureWidth * .5), (int)(renderTextureHeight * .5), TextureFormat.ARGB32, false); leftEyeRenderTextures[3] = new Texture2D((int)(renderTextureWidth * .5), (int)(renderTextureHeight * .5), TextureFormat.ARGB32, false); renderTextures[3] = new RenderTexture((int)(renderTextureWidth * .5), (int)(renderTextureHeight * .5), 24, RenderTextureFormat.ARGB32); rightEyeRenderTextures[4] = new Texture2D((int)(renderTextureWidth * .25), (int)(renderTextureHeight * .25), TextureFormat.ARGB32, false); leftEyeRenderTextures[4] = new Texture2D((int)(renderTextureWidth * .25), (int)(renderTextureHeight * .25), TextureFormat.ARGB32, false); renderTextures[4] = new RenderTexture((int)(renderTextureWidth * .25), (int)(renderTextureHeight * .25), 24, RenderTextureFormat.ARGB32); // set rendering bounds on texture to render hmdTextureBounds.uMin = 0.0f; hmdTextureBounds.uMax = 1.0f; hmdTextureBounds.vMin = 1.0f; // flip the vertical coordinate for some reason hmdTextureBounds.vMax = 0.0f; hmdIsInitialized = true; return(retVal); }
/// <summary> /// Initialize HMD using OpenVR API calls. /// </summary> /// <returns>True on successful initialization, false otherwise.</returns> private static void InitializeOpenVR() { // return if HMD has already been initialized if (hmdState == HmdState.Initialized) { return; } // set the location of the OpenVR DLL SetDllDirectory(Globals.OpenVRDllPath); // check if HMD is connected on the system if (!OpenVR.IsHmdPresent()) { throw new InvalidOperationException("HMD not found on this system"); } // check if SteamVR runtime is installed if (!OpenVR.IsRuntimeInstalled()) { throw new InvalidOperationException("SteamVR runtime not found on this system"); } // initialize HMD EVRInitError hmdInitErrorCode = EVRInitError.None; OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene); if (hmdInitErrorCode != EVRInitError.None) { throw new Exception("OpenVR error: " + OpenVR.GetStringForHmdError(hmdInitErrorCode)); } // reset "seated position" and capture initial position. this means you should hold the HMD in // the position you would like to consider "seated", before running this code. ResetInitialHmdPosition(); // get HMD render target size uint renderTextureWidth = 0; uint renderTextureHeight = 0; OpenVR.System.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight); // at the moment, only Direct3D12 is working with Kerbal Space Program ETextureType textureType = ETextureType.DirectX; switch (SystemInfo.graphicsDeviceType) { case UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2: case UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3: textureType = ETextureType.OpenGL; throw new InvalidOperationException(SystemInfo.graphicsDeviceType.ToString() + " does not support VR. You must use -force-d3d12"); case UnityEngine.Rendering.GraphicsDeviceType.Direct3D9: throw new InvalidOperationException(SystemInfo.graphicsDeviceType.ToString() + " does not support VR. You must use -force-d3d12"); case UnityEngine.Rendering.GraphicsDeviceType.Direct3D11: textureType = ETextureType.DirectX; break; case UnityEngine.Rendering.GraphicsDeviceType.Direct3D12: textureType = ETextureType.DirectX; break; default: throw new InvalidOperationException(SystemInfo.graphicsDeviceType.ToString() + " not supported"); } // initialize render textures (for displaying on HMD) for (int i = 0; i < 2; i++) { hmdEyeRenderTexture[i] = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32); hmdEyeRenderTexture[i].Create(); hmdEyeTexture[i].handle = hmdEyeRenderTexture[i].GetNativeTexturePtr(); hmdEyeTexture[i].eColorSpace = EColorSpace.Auto; hmdEyeTexture[i].eType = textureType; } // set rendering bounds on texture to render hmdTextureBounds.uMin = 0.0f; hmdTextureBounds.uMax = 1.0f; hmdTextureBounds.vMin = 1.0f; // flip the vertical coordinate for some reason hmdTextureBounds.vMax = 0.0f; }
public static VRInitException Make(EVRInitError errorCode) { string message = OpenVR.GetStringForHmdError(errorCode); return(new VRInitException(message, errorCode)); }